4#include <structmember.h>
9#include <boost/python.hpp>
10#include <boost/noncopyable.hpp>
11#include <boost/mpl/front.hpp>
47namespace PyOpalObjectNS {
50template <
class C>
class PyOpalObject;
51template <
class C>
struct PyOpalObjectGetProperty;
52template <
class C>
struct PyOpalObjectSetProperty;
136 inline boost::python::class_<PyC>
make_class(
const char* className);
140 template <
class PYCLASS>
145 template <
class PYCLASS>
150 template <
class PYCLASS>
155 template <
class PYCLASS>
161 template <
class ValueType>
167 template <
class ValueType>
209 ElementBase* element = objectPtr->getElement()->removeWrappers();
211 if (component == NULL) {
213 "Failed to deduce Component from ElementBase.");
220 bool outOfBounds = component->
apply(
R, P, t, E, B);
221 return boost::python::make_tuple(outOfBounds,
232struct PyOpalObjectGetProperty : boost::python::default_call_policies {
249 template <
class ArgumentPackage>
250 PyObject*
postcall(ArgumentPackage
const&, PyObject* result);
280 template <
class ArgumentPackage>
281 PyObject*
postcall(ArgumentPackage
const& args, PyObject* result);
298 objectPtr->execute();
305 if (objectPtr == NULL) {
307 "Trying to register something that was not a Opal Object");
316 std::shared_ptr<OpalElement> elementPtr =
317 std::dynamic_pointer_cast<OpalElement, C>(pyobject.
getOpalShared());
318 if (elementPtr.get() == NULL) {
320 "Wrapped object was not an OpalElement");
323 boost::python::object pyelement(element);
339 "Object was not initialised");
341 Attribute* attribute = object_m->findAttribute(opalName);
342 if (attribute == NULL) {
344 "Failed to parse attribute "+opalName);
354 pyvalue = PyFloat_FromDouble(value);
357 pyvalue = PyLong_FromDouble(value);
360 pyvalue = PyUnicode_FromString(value.c_str());
370 pyvalue = PyList_New(value.size());
371 for (
size_t i = 0; i < value.size(); ++i) {
372 PyList_SetItem(pyvalue, i, PyFloat_FromDouble(value[i]));
387 "Element was not initialised");
389 Attribute* attribute = object_m->findAttribute(opalName);
390 if (attribute == NULL) {
392 "Failed to parse attribute "+opalName);
395 double value = PyFloat_AsDouble(pyvalue);
398 double value = PyLong_AsDouble(pyvalue);
401 std::string value = PyUnicode_AsUTF8(pyvalue);
404 bool value = PyObject_IsTrue(pyvalue);
407 Py_ssize_t listSize = PyList_Size(pyvalue);
408 std::vector<double> value(listSize);
409 for (Py_ssize_t i = 0; i < listSize; ++i) {
410 double value_i = PyFloat_AsDouble(PyList_GetItem(pyvalue, i));
427 if (attribute == NULL) {
440 typedef boost::python::class_<PyOpalObject<C> > PyClass;
441 boost::python::docstring_options docop(
true,
true,
false);
442 PyClass pyclass = PyClass(className);
443 addAttributes(pyclass);
449template <
class PYCLASS>
455template <
class PYCLASS>
461template <
class PYCLASS>
469template <
class PYCLASS>
474 std::string docString = getDocString(*iter);
475 std::string pyname = iter->pyName_m.c_str();
476 if (iter->type_m ==
DOUBLE) {
477 pyclass.add_property(pyname.c_str(),
478 boost::python::make_function(&PyC::dummyGet<double>, getProp),
479 boost::python::make_function(&PyC::dummySet<double>, setProp),
482 }
else if (iter->type_m ==
INT) {
483 pyclass.add_property(pyname.c_str(),
484 boost::python::make_function(&PyC::dummyGet<int>, getProp),
485 boost::python::make_function(&PyC::dummySet<int>, setProp),
488 }
else if (iter->type_m ==
STRING) {
489 pyclass.add_property(pyname.c_str(),
490 boost::python::make_function(&PyC::dummyGet<std::string>, getProp),
491 boost::python::make_function(&PyC::dummySet<std::string>, setProp),
494 }
else if (iter->type_m ==
BOOL) {
495 pyclass.add_property(pyname.c_str(),
496 boost::python::make_function(&PyC::dummyGet<bool>, getProp),
497 boost::python::make_function(&PyC::dummySet<bool>, setProp),
501 pyclass.add_property(pyname.c_str(),
502 boost::python::make_function(&PyC::dummyGet<boost::python::list>, getProp),
503 boost::python::make_function(&PyC::dummySet<boost::python::list>, setProp),
510 throw OpalException(
"PyOpalObject<C>::addAttributes",
"Type not implemented");
519template <
class ArgumentPackage>
522 result = object_m->getAttribute(type_m, opalName_m);
532template <
class ArgumentPackage>
536 if (!PyArg_ParseTuple(args,
"OO", &pyObject, &value)) {
540 object_m->setAttribute(type_m, opalName_m, value);
boost::python::object getFieldValue(PyOpalObjectNS::PyOpalObject< C > &pyobject, double x, double y, double z, double t)
std::map< AttributeType, std::string > attributeName
double getReal(const Attribute &attr)
Return real value.
void setRealArray(Attribute &attr, const std::vector< double > &value)
Set array value.
void setBool(Attribute &attr, bool val)
Set logical value.
void setString(Attribute &attr, const std::string &val)
Set string value.
bool getBool(const Attribute &attr)
Return logical value.
void setReal(Attribute &attr, double val)
Set real value.
std::vector< double > getRealArray(const Attribute &attr)
Get array value.
std::string getString(const Attribute &attr)
Get string value.
std::string::iterator iterator
boost::function< boost::tuple< double, bool >(arguments_t)> type
void addRegister(PYCLASS &pyclass)
void dummySet(ValueType test)
static void registerObject(PyOpalObject< C > &pyobject)
static std::vector< AttributeDef > attributes
void addAttributes(PYCLASS &pyclass)
void addGetOpalElement(PYCLASS &pyclass)
static std::string classDocstring
ValueType dummyGet() const
std::string getDocString(AttributeDef &def)
PyOpalObject(std::shared_ptr< C > object)
static boost::python::object getPyOpalElement(PyOpalObject< C > &pyobject)
std::shared_ptr< C > getOpalShared()
PyObject * getAttribute(AttributeType type, std::string opalName) const
boost::python::class_< PyC > make_class(const char *className)
static void execute(PyOpalObject< C > &pyobject)
void addExecute(PYCLASS &pyclass)
static bool converterRegistered
std::shared_ptr< C > object_m
void setAttribute(AttributeType type, std::string opalName, PyObject *value)
static void setObject(const PyOpalObject< C > *object)
static const PyOpalObject< C > * object_m
~PyOpalObjectGetProperty()
PyObject * postcall(ArgumentPackage const &, PyObject *result)
PyOpalObjectGetProperty(AttributeType type, std::string opalName)
static void setObject(PyOpalObject< C > *object)
~PyOpalObjectSetProperty()
PyObject * postcall(ArgumentPackage const &args, PyObject *result)
static PyOpalObject< C > * object_m
PyOpalObjectSetProperty(AttributeType type, std::string opalName)
A representation of an Object attribute.
const std::string & getHelp() const
Return the help string.
The base class for all OPAL objects.
static OpalData * getInstance()
void define(Object *newObject)
Define a new object.
Interface for a single beam element.
virtual bool apply(const size_t &i, const double &t, Vector_t &E, Vector_t &B)
The base class for all OPAL exceptions.