14#include <boost/python/class.hpp>
15#include <boost/python/extract.hpp>
16#include <boost/python/list.hpp>
17#include <boost/python/str.hpp>
23namespace PythonInterface {
31 using PythonType = boost::python::class_<SvcType, boost::noncopyable>;
32 using WeakPtr = std::weak_ptr<typename SvcPtrType::element_type>;
56 "Adds the given object to the service with the given name. If "
57 "the name/object exists it will raise an error.")
59 "Adds the given object to the service with the given name. "
60 "The the name exists the object is replaced.")
61 .def(
"doesExist", &SvcType::doesExist, (arg(
"self"), arg(
"name")),
62 "Returns True if the object is found in the service.")
64 "Retrieve the named object. Raises an exception if the name "
68 .def(
"size", &SvcType::size, arg(
"self"),
"Returns the number of objects within the service")
70 "Return the list of names currently known to the ADS")
73 .def(
"__len__", &SvcType::size, arg(
"self"))
76 .def(
"__contains__", &SvcType::doesExist, (arg(
"self"), arg(
"name")))
88 static void addItem(SvcType &self,
const std::string &name,
const boost::python::object &item) {
100 static void addOrReplaceItem(SvcType &self,
const std::string &name,
const boost::python::object &item) {
110 static void removeItem(SvcType &self,
const std::string &name) {
131 boost::python::extract<WeakPtr &> extractWeak(pyvalue);
132 if (extractWeak.check()) {
133 return extractWeak().lock();
135 boost::python::extract<SvcPtrType &> extractRefShared(pyvalue);
136 if (extractRefShared.check()) {
137 return extractRefShared();
139 throw std::invalid_argument(
"Cannot extract pointer from Python object argument. Incorrect type");
158 item = self.retrieve(name);
161 std::string err =
"'" + name +
"' does not exist.";
162 PyErr_SetString(PyExc_KeyError, err.c_str());
163 throw boost::python::error_already_set();
177 boost::python::list names;
180 for (
auto itr = keys.begin(); itr != keys.end(); ++itr) {
183 assert(names.attr(
"__len__")() == keys.size());
Exception for when an item is not found in a collection.
Defines a structure for releasing the Python GIL using the RAII pattern.
Helper class which provides the Collimation Length for SANS instruments.
A helper struct to export templated DataService<> types to Python.
boost::python::class_< SvcType, boost::noncopyable > PythonType
static void addOrReplaceItem(SvcType &self, const std::string &name, const boost::python::object &item)
Add or replace an item into the service, if it exists then an error is raised.
static PythonType define(const char *pythonClassName)
Define the necessary boost.python framework to expor the templated DataService type Note: This does n...
static void removeItem(SvcType &self, const std::string &name)
Remove an item from the service.
static boost::python::list getObjectNamesAsList(SvcType const *const self, const std::string &contain)
Return a Python list of object names from the ADS as this is far easier to work with than a set.
std::weak_ptr< typename SvcPtrType::element_type > WeakPtr
static void clearItems(SvcType &self)
Remove an item from the service.
static WeakPtr retrieveOrKeyError(SvcType &self, const std::string &name)
Retrieves a shared_ptr from the ADS and raises a Python KeyError if it does not exist.
static SvcPtrType extractCppValue(const boost::python::object &pyvalue)
Extract a SvcPtrType C++ value from the Python object.
static void addItem(SvcType &self, const std::string &name, const boost::python::object &item)
Add an item into the service, if it exists then an error is raised.