OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
PyLine.h
Go to the documentation of this file.
1#ifndef PyOpal_PyLine_h
2
3
4#include "Lines/Sequence.h"
5#include "Lines/Line.h"
8
9namespace PyOpal {
10
11// to get the inheritance from PyElement I need to make PyLine_ templated; but
12// really I only ever want to associate a PyLine_ with a TBeamline (it is a list
13// of elements in a beamline). So I typedef a PyLine class which is the
14// specialisation to PyLine_<TBeamline>.
15template <class C>
16class PyLine_; // forwards declaration of the PyLine_
18
34template <class C>
35class PyLine_ : public PyOpalObjectNS::PyOpalObject<C> {
36public:
43 boost::python::object getElement(int i);
44
54 void setElement(int i, boost::python::object element);
55
61 void append(boost::python::object element);
62
64 int getLength() const {return line.size();}
65
70 boost::python::class_<PyLine> make_class(const char* className);
71
73
74private:
76 std::vector<boost::python::object> line;
77};
78
79template <>
80void PyLine_<TBeamline<FlaggedElmPtr> >::registerObject() {
81 TBeamline<FlaggedElmPtr>* wrapped = getOpalShared().get();
82 std::cerr << "RegisterObject Line 1 " << wrapped << std::endl;
83 Line* line = new Line();
84 line->setElement(wrapped);
85 Object* objectPtr = dynamic_cast<Object*>(line);
86 std::cerr << "RegisterObject Line 2 " << objectPtr << std::endl;
87 if (objectPtr == NULL) {
88 throw OpalException("PyLine_<TBeamline<FlaggedElmPtr> >::register",
89 "Trying to register something that was not a Opal Object");
90 }
91 OpalData::getInstance()->define(objectPtr);
92}
93
94template<>
95boost::python::class_<PyLine> PyLine_<TBeamline<FlaggedElmPtr> >::make_class(const char* className) {
96 boost::python::docstring_options docop(true, true, false); // user_def, py_sig, cpp_sig
97 auto pyclass = boost::python::class_<PyLine>(className);
98 return pyclass;
99}
100
101template<>
102boost::python::object PyLine_<TBeamline<FlaggedElmPtr> >::getElement(int i) {
103 try {
104 return line.at(i);
105 } catch (std::exception& exc) {
106 throw OpalException("PyLine::getElement", "Out of range");
107 }
108}
109
110template<>
111void PyLine_<TBeamline<FlaggedElmPtr> >::setElement(int i, boost::python::object pyelement) {
112 // TBeamline is implemented as a double linked list??
113 std::cerr << "PYLINE setElement " << i << std::endl;
114 typedef TBeamline<FlaggedElmPtr> BL;
115 try {
116 line.at(i) = pyelement;
117 } catch (std::exception& exc) {
118 throw OpalException("PyLine::setElement", "Failed to set element");
119 }
120 boost::python::object pyopalelement = pyelement.attr("get_opal_element")();
122 boost::python::extract<PyOpal::PyOpalObjectNS::PyOpalObject<OpalElement>& >(pyopalelement);
123 int index = 0;
124 for (BL::iterator it = object_m->begin(); it != object_m->end(); ++it) {
125 if (index == i) {
126 // 4 layers of nested inheritance template wrapper darkness
127 std::shared_ptr<OpalElement> opalElementShared = cpyelement.getOpalShared();
128 OpalElement* opalElement = opalElementShared.get();
129 if (!opalElement) {
130 throw OpalException("PyLine::setElement", "Failed to extract element");
131 }
132 opalElement->update();
133 ElementBase* elmbase = opalElement->getElement()->removeWrappers();
134 std::cerr << " PYLINE setElement loop " << elmbase << std::endl;
135 if (!elmbase) {
136 throw OpalException("PyLine::setElement", "Failed to cast element");
137 }
138 ElmPtr elmptr(elmbase);
139 FlaggedElmPtr felmptr(elmptr);
140 object_m->insert(it, felmptr);
141 return;
142 }
143 ++index;
144 }
145 throw OpalException("PyLine::getElement", "Out of range");
146}
147
148template <>
149void PyLine_<TBeamline<FlaggedElmPtr> >::append(boost::python::object pyelement) {
150 int i = line.size();
151 // first we extend the size of the python object list by 1 with dummy variables
152 line.push_back(boost::python::object());
153 FlaggedElmPtr felmptr(ElmPtr(NULL));
154 object_m->push_back(felmptr);
155 // then we set the python object using setElement method
156 setElement(i, pyelement);
157}
158
159}
160#endif // PyOpal_PyLine_h
161
162/*
163OPAL Beamline insanity:
164
165There exists:
1661. Beamline is a beamline
1672. TBeamline which is another sort of beamline that inherits from Beamline (but has no reason to exist)
1683. BeamSequence which does nothing
1694. Line which has some bindings to the UI
1705. FlaggedBeamline which has a flag to indicate the Beamline is reflected (WTF?)
1716. Sequence which is a list of Sequence elements whatever they are
172
173Probably some other stuff. It's insane! Should be two classes
1741. Classic Beamline
1752. UI bindings
176Done.
177
178*/
179
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
PyLine_< TBeamline< FlaggedElmPtr > > PyLine
Definition: PyLine.h:16
std::string::iterator iterator
Definition: MSLang.h:16
void registerObject()
int getLength() const
Definition: PyLine.h:64
void append(boost::python::object element)
void setElement(int i, boost::python::object element)
std::vector< boost::python::object > line
Definition: PyLine.h:76
boost::python::class_< PyLine > make_class(const char *className)
boost::python::object getElement(int i)
std::shared_ptr< C > getOpalShared()
Definition: PyOpalObject.h:190
ElementBase * getElement() const
Return the embedded CLASSIC element.
Definition: Element.h:120
void setElement(ElementBase *)
Assign new CLASSIC element.
Definition: Element.h:125
The base class for all OPAL objects.
Definition: Object.h:48
static OpalData * getInstance()
Definition: OpalData.cpp:196
void define(Object *newObject)
Define a new object.
Definition: OpalData.cpp:489
A section of a beam line.
Definition: ElmPtr.h:32
A section of a beam line.
Definition: FlaggedElmPtr.h:36
virtual void update()
Update the embedded CLASSIC element.
Definition: Line.h:32
The base class for all OPAL exceptions.
Definition: OpalException.h:28