Mantid
Loading...
Searching...
No Matches
StlExportDefinitions.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2011 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7#pragma once
11#include <boost/python/class.hpp>
12#include <boost/python/init.hpp>
13#ifdef _MSC_VER
14#pragma warning(disable : 4267) // Kill warning from line 176 of indexing suite
15#endif
16#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
17#include <boost/python/tuple.hpp>
18
19#include <set>
20#include <vector>
21
22using boost::python::arg;
23
24namespace Mantid {
25namespace PythonInterface {
31template <typename ElementType> inline std::string toString(const ElementType &value) {
32 std::ostringstream os;
33 os << value;
34 return os.str();
35}
36
44template <> inline std::string toString(const std::string &value) {
45 std::ostringstream os;
46 os << "'" << value << "'";
47 return os.str();
48}
49
51template <typename SequenceType, typename ElementType> std::string toString(const SequenceType &values) {
52 auto iend = values.end();
53 std::ostringstream os;
54 for (auto itr = values.begin(); itr != iend;) {
55 os << toString(*itr);
56 if (++itr != iend) {
57 os << ",";
58 }
59 }
60 return os.str();
61}
62
66template <typename ElementType, bool NoIndexingProxy = false> struct std_vector_exporter {
68 using w_t = std::vector<ElementType>;
69
70 static std::string to_string(const w_t &values) {
71 if (values.empty())
72 return "[]";
73 std::string retval("[");
74 retval += toString<w_t, ElementType>(values);
75 retval += "]";
76 return retval;
77 }
78
80 static void wrap(std::string const &python_name) {
81 boost::python::class_<w_t, std::shared_ptr<w_t>>(python_name.c_str())
82 .def(boost::python::init<w_t const &>())
83 .def(boost::python::vector_indexing_suite<w_t, NoIndexingProxy>())
84 .def("__str__", &std_vector_exporter::to_string);
85 }
86};
87
88//-------------------------------------------------------------------------
89// std::set
90//-------------------------------------------------------------------------
91
93// Found this at
94// http://cctbx.svn.sourceforge.net/viewvc/cctbx/trunk/scitbx/stl/set_wrapper.h?view=log
95template <typename ElementType> struct std_set_exporter {
96 using w_t = std::set<ElementType>;
97 using e_t = ElementType;
98
99 static void insert_element(w_t &self, e_t const &x) { self.insert(x); }
100
101 static void insert_set(w_t &self, w_t const &other) { self.insert(other.begin(), other.end()); }
102
103 static bool contains(w_t const &self, e_t const &x) { return self.find(x) != self.end(); }
104
105 static e_t getitem(w_t const &self, std::size_t i) {
106 if (i >= self.size()) {
107 PyErr_SetString(PyExc_IndexError, "Index out of range");
108 boost::python::throw_error_already_set();
109 }
110 return *std::next(self.begin(), i);
111 }
112
113 static boost::python::tuple getinitargs(w_t const &self) {
114 return boost::python::make_tuple(boost::python::tuple(self));
115 }
116
117 static std::string to_string(const w_t &values) {
118 if (values.empty())
119 return "set()";
120 std::string retval("set(");
121 retval += toString<w_t, ElementType>(values);
122 retval += ")";
123 return retval;
124 }
125
126 static void wrap(std::string const &python_name) {
127 boost::python::class_<w_t, std::shared_ptr<w_t>>(python_name.c_str())
128 .def(boost::python::init<w_t const &>())
129 // special methods
130 .def("__str__", &std_set_exporter::to_string, arg("self"))
131 .def("__len__", &w_t::size, arg("self"))
132 .def("__contains__", contains, (arg("self"), arg("element")))
133 .def("__getitem__", getitem, (arg("self"), arg("index")))
134 .def("__getinitargs__", getinitargs, arg("self"))
135 // Standard methods
136 .def("size", &w_t::size, arg("self"))
137 .def("insert", insert_element, (arg("self"), arg("element")))
138 .def("append", insert_element, (arg("self"), arg("element")))
139 .def("insert", insert_set, (arg("self"), arg("set")))
140 .def("extend", insert_set, (arg("self"), arg("set")))
141 .def("erase", (std::size_t(w_t::*)(e_t const &)) & w_t::erase, (arg("self"), arg("index")))
142 .def("clear", &w_t::clear, arg("self"))
143 .enable_pickling()
144
145 ;
146 }
147};
148} // namespace PythonInterface
149} // namespace Mantid
double value
The value of the point.
Definition: FitMW.cpp:51
Helper class which provides the Collimation Length for SANS instruments.
static e_t getitem(w_t const &self, std::size_t i)
static boost::python::tuple getinitargs(w_t const &self)
static void insert_element(w_t &self, e_t const &x)
static void wrap(std::string const &python_name)
static void insert_set(w_t &self, w_t const &other)
static std::string to_string(const w_t &values)
static bool contains(w_t const &self, e_t const &x)
A struct to help export std::vector types.
static std::string to_string(const w_t &values)
static void wrap(std::string const &python_name)
a python wrapper
std::vector< ElementType > w_t
A typedef of a vector of template ElementTypes.