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
12GNU_DIAG_OFF("maybe-uninitialized")
13
14#include <boost/python/class.hpp>
15#include <boost/python/init.hpp>
16#ifdef _MSC_VER
17#pragma warning(disable : 4267) // Kill warning from line 176 of indexing suite
18#endif
19#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
20#include <boost/python/tuple.hpp>
21
22#include <set>
23#include <vector>
24
25using boost::python::arg;
26
27namespace Mantid {
28namespace PythonInterface {
34template <typename ElementType> inline std::string toString(const ElementType &value) {
35 std::ostringstream os;
36 os << value;
37 return os.str();
38}
39
47template <> inline std::string toString(const std::string &value) {
48 std::ostringstream os;
49 os << "'" << value << "'";
50 return os.str();
51}
52
54template <typename SequenceType, typename ElementType> std::string toString(const SequenceType &values) {
55 auto iend = values.end();
56 std::ostringstream os;
57 for (auto itr = values.begin(); itr != iend;) {
58 os << toString(*itr);
59 if (++itr != iend) {
60 os << ",";
61 }
62 }
63 return os.str();
64}
65
69template <typename ElementType, bool NoIndexingProxy = false> struct std_vector_exporter {
71 using w_t = std::vector<ElementType>;
72
73 static std::string to_string(const w_t &values) {
74 if (values.empty())
75 return "[]";
76 std::string retval("[");
77 retval += toString<w_t, ElementType>(values);
78 retval += "]";
79 return retval;
80 }
81
83 static void wrap(std::string const &python_name) {
84 boost::python::class_<w_t, std::shared_ptr<w_t>>(python_name.c_str())
85 .def(boost::python::init<w_t const &>())
86 .def(boost::python::vector_indexing_suite<w_t, NoIndexingProxy>())
87 .def("__str__", &std_vector_exporter::to_string);
88 }
89};
90
91//-------------------------------------------------------------------------
92// std::set
93//-------------------------------------------------------------------------
94
96// Found this at
97// http://cctbx.svn.sourceforge.net/viewvc/cctbx/trunk/scitbx/stl/set_wrapper.h?view=log
98template <typename ElementType> struct std_set_exporter {
99 using w_t = std::set<ElementType>;
100 using e_t = ElementType;
101
102 static void insert_element(w_t &self, e_t const &x) { self.insert(x); }
103
104 static void insert_set(w_t &self, w_t const &other) { self.insert(other.begin(), other.end()); }
105
106 static bool contains(w_t const &self, e_t const &x) { return self.find(x) != self.end(); }
107
108 static e_t getitem(w_t const &self, std::size_t i) {
109 if (i >= self.size()) {
110 PyErr_SetString(PyExc_IndexError, "Index out of range");
111 boost::python::throw_error_already_set();
112 }
113 return *std::next(self.begin(), i);
114 }
115
116 static boost::python::tuple getinitargs(w_t const &self) {
117 return boost::python::make_tuple(boost::python::tuple(self));
118 }
119
120 static std::string to_string(const w_t &values) {
121 if (values.empty())
122 return "set()";
123 std::string retval("set(");
124 retval += toString<w_t, ElementType>(values);
125 retval += ")";
126 return retval;
127 }
128
129 static void wrap(std::string const &python_name) {
130 boost::python::class_<w_t, std::shared_ptr<w_t>>(python_name.c_str())
131 .def(boost::python::init<w_t const &>())
132 // special methods
133 .def("__str__", &std_set_exporter::to_string, arg("self"))
134 .def("__len__", &w_t::size, arg("self"))
135 .def("__contains__", contains, (arg("self"), arg("element")))
136 .def("__getitem__", getitem, (arg("self"), arg("index")))
137 .def("__getinitargs__", getinitargs, arg("self"))
138 // Standard methods
139 .def("size", &w_t::size, arg("self"))
140 .def("insert", insert_element, (arg("self"), arg("element")))
141 .def("append", insert_element, (arg("self"), arg("element")))
142 .def("insert", insert_set, (arg("self"), arg("set")))
143 .def("extend", insert_set, (arg("self"), arg("set")))
144 .def("erase", (std::size_t (w_t::*)(e_t const &))&w_t::erase, (arg("self"), arg("index")))
145 .def("clear", &w_t::clear, arg("self"))
146 .enable_pickling()
147
148 ;
149 }
150};
151} // namespace PythonInterface
152} // namespace Mantid
double value
The value of the point.
Definition FitMW.cpp:51
#define GNU_DIAG_OFF(x)
This is a collection of macros for turning compiler warnings off in a controlled manner.
std::string toString(const T &value)
Convert values to strings.
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.