rogue
Loading...
Searching...
No Matches
Bsp.cpp
Go to the documentation of this file.
1
18
19#include <boost/make_shared.hpp>
20#include <memory>
21#include <string>
22
23#include "rogue/GeneralError.h"
24
25namespace bp = boost::python;
26namespace ria = rogue::interfaces::api;
27
28// Class factory which returns a pointer to a Bsp (BspPtr)
29ria::BspPtr ria::Bsp::create(bp::object obj) {
30 ria::BspPtr r = std::make_shared<ria::Bsp>(obj);
31 return (r);
32}
33
34// Class factory which returns a pointer to a Root (BspPtr)
35ria::BspPtr ria::Bsp::create(std::string modName, std::string rootClass) {
36 ria::BspPtr r = std::make_shared<ria::Bsp>(modName, rootClass);
37 return (r);
38}
39
40ria::Bsp::Bsp(boost::python::object obj) {
41 this->_obj = obj;
42 this->_name = std::string(bp::extract<char*>(this->_obj.attr("name")));
43 this->_isRoot = false;
44}
45
46ria::Bsp::Bsp(std::string modName, std::string rootClass) {
47 Py_Initialize();
48
49 bp::object mod = bp::import(modName.c_str());
50 this->_obj = mod.attr(rootClass.c_str())();
51 this->_name = std::string(bp::extract<char*>(this->_obj.attr("name")));
52 this->_isRoot = true;
53 this->_obj.attr("start")();
54
55 while (bp::extract<bool>(this->_obj.attr("running")) == false) usleep(10);
56}
57
58ria::Bsp::~Bsp() {
59 if (this->_isRoot) {
60 this->_obj.attr("stop")();
61 }
62}
63
64void ria::Bsp::addVarListener(void (*func)(std::string, std::string), void (*done)()) {
65 if (this->_isRoot)
66 this->_obj.attr("_addVarListenerCpp")(func, done);
67 else
68 throw(rogue::GeneralError::create("Bsp::addVarListener",
69 "Attempt to execute addVarListener on non Root node %s",
70 this->_name.c_str()));
71}
72
74std::string ria::Bsp::getAttribute(std::string attribute) {
75 try {
76 return (std::string(bp::extract<char*>(this->_obj.attr(attribute.c_str()))));
77 } catch (...) {
78 // Boost.Python leaves the interpreter error indicator set on error_already_set.
79 if (PyErr_Occurred()) PyErr_Clear();
80 throw(rogue::GeneralError::create("Bsp::getAttribute",
81 "Invalid attribute %s for node %s",
82 attribute.c_str(),
83 this->_name.c_str()));
84 }
85}
86
88rogue::interfaces::api::Bsp ria::Bsp::operator[](std::string name) {
89 try {
90 bp::object obj = this->_obj.attr("node")(name);
91 return (ria::Bsp(obj));
92 } catch (...) {
93 // Boost.Python leaves the interpreter error indicator set on error_already_set.
94 if (PyErr_Occurred()) PyErr_Clear();
95 throw(rogue::GeneralError::create("Bsp::[]",
96 "Invalid child node %s for node %s",
97 name.c_str(),
98 this->_name.c_str()));
99 }
100}
101
103std::shared_ptr<rogue::interfaces::api::Bsp> ria::Bsp::getNode(std::string name) {
104 try {
105 bp::object obj = this->_obj.attr("getNode")(name);
106 return (ria::Bsp::create(obj));
107 } catch (...) {
108 // Boost.Python leaves the interpreter error indicator set on error_already_set.
109 if (PyErr_Occurred()) PyErr_Clear();
110 throw(rogue::GeneralError::create("Bsp::node",
111 "Invalid child node %s for node %s",
112 name.c_str(),
113 this->_name.c_str()));
114 }
115}
116
118std::string ria::Bsp::operator()(std::string arg) {
119 try {
120 return (std::string(bp::extract<char*>(this->_obj.attr("callDisp")(arg))));
121 } catch (...) {
122 // Boost.Python leaves the interpreter error indicator set on error_already_set.
123 if (PyErr_Occurred()) PyErr_Clear();
124 throw(rogue::GeneralError::create("Bsp::()", "Error executing node %s", this->_name.c_str()));
125 }
126}
127
129std::string ria::Bsp::operator()() {
130 try {
131 return (std::string(bp::extract<char*>(this->_obj.attr("callDisp")())));
132 } catch (...) {
133 // Boost.Python leaves the interpreter error indicator set on error_already_set.
134 if (PyErr_Occurred()) PyErr_Clear();
135 throw(rogue::GeneralError::create("Bsp::()", "Error executing node %s", this->_name.c_str()));
136 }
137}
138
140std::string ria::Bsp::execute(std::string arg) {
141 try {
142 return (std::string(bp::extract<char*>(this->_obj.attr("callDisp")(arg))));
143 } catch (...) {
144 // Boost.Python leaves the interpreter error indicator set on error_already_set.
145 if (PyErr_Occurred()) PyErr_Clear();
146 throw(rogue::GeneralError::create("Bsp::execute", "Error executing node %s", this->_name.c_str()));
147 }
148}
149
151std::string ria::Bsp::execute() {
152 try {
153 return (std::string(bp::extract<char*>(this->_obj.attr("callDisp")())));
154 } catch (...) {
155 // Boost.Python leaves the interpreter error indicator set on error_already_set.
156 if (PyErr_Occurred()) PyErr_Clear();
157 throw(rogue::GeneralError::create("Bsp::execute", "Error executing node %s", this->_name.c_str()));
158 }
159}
160
162void ria::Bsp::set(std::string value) {
163 try {
164 this->_obj.attr("setDisp")(value, false, -1);
165 } catch (...) {
166 // Boost.Python leaves the interpreter error indicator set on error_already_set.
167 if (PyErr_Occurred()) PyErr_Clear();
168 throw(rogue::GeneralError::create("Bsp::set",
169 "Error setting value %s on node %s",
170 value.c_str(),
171 this->_name.c_str()));
172 }
173}
174
176void ria::Bsp::setWrite(std::string value) {
177 try {
178 this->_obj.attr("setDisp")(value, true, -1);
179 } catch (...) {
180 // Boost.Python leaves the interpreter error indicator set on error_already_set.
181 if (PyErr_Occurred()) PyErr_Clear();
182 throw(rogue::GeneralError::create("Bsp::set",
183 "Error setting value %s on node %s",
184 value.c_str(),
185 this->_name.c_str()));
186 }
187}
188
190std::string ria::Bsp::get() {
191 try {
192 return (std::string(bp::extract<char*>(this->_obj.attr("getDisp")(false, -1))));
193 } catch (...) {
194 // Boost.Python leaves the interpreter error indicator set on error_already_set.
195 if (PyErr_Occurred()) PyErr_Clear();
196 throw(rogue::GeneralError::create("Bsp::set", "Error getting value on node %s", this->_name.c_str()));
197 }
198}
199
201std::string ria::Bsp::readGet() {
202 try {
203 return (std::string(bp::extract<char*>(this->_obj.attr("getDisp")(true, -1))));
204 } catch (...) {
205 // Boost.Python leaves the interpreter error indicator set on error_already_set.
206 if (PyErr_Occurred()) PyErr_Clear();
207 throw(rogue::GeneralError::create("Bsp::set", "Error getting value on node %s", this->_name.c_str()));
208 }
209}
static GeneralError create(std::string src, const char *fmt,...)
Creates a formatted error instance.
C++ convenience wrapper around a PyRogue node object.
Definition Bsp.h:44
std::shared_ptr< rogue::interfaces::api::Bsp > BspPtr
Shared pointer alias for Bsp.
Definition Bsp.h:205