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 throw(rogue::GeneralError::create("Bsp::getAttribute",
79 "Invalid attribute %s for node %s",
80 attribute.c_str(),
81 this->_name.c_str()));
82 }
83}
84
86rogue::interfaces::api::Bsp ria::Bsp::operator[](std::string name) {
87 try {
88 bp::object obj = this->_obj.attr("node")(name);
89 return (ria::Bsp(obj));
90 } catch (...) {
91 throw(rogue::GeneralError::create("Bsp::[]",
92 "Invalid child node %s for node %s",
93 name.c_str(),
94 this->_name.c_str()));
95 }
96}
97
99std::shared_ptr<rogue::interfaces::api::Bsp> ria::Bsp::getNode(std::string name) {
100 try {
101 bp::object obj = this->_obj.attr("getNode")(name);
102 return (ria::Bsp::create(obj));
103 } catch (...) {
104 throw(rogue::GeneralError::create("Bsp::node",
105 "Invalid child node %s for node %s",
106 name.c_str(),
107 this->_name.c_str()));
108 }
109}
110
112std::string ria::Bsp::operator()(std::string arg) {
113 try {
114 return (std::string(bp::extract<char*>(this->_obj.attr("callDisp")(arg))));
115 } catch (...) {
116 throw(rogue::GeneralError::create("Bsp::()", "Error executing node %s", this->_name.c_str()));
117 }
118}
119
121std::string ria::Bsp::operator()() {
122 try {
123 return (std::string(bp::extract<char*>(this->_obj.attr("callDisp")())));
124 } catch (...) {
125 throw(rogue::GeneralError::create("Bsp::()", "Error executing node %s", this->_name.c_str()));
126 }
127}
128
130std::string ria::Bsp::execute(std::string arg) {
131 try {
132 return (std::string(bp::extract<char*>(this->_obj.attr("callDisp")(arg))));
133 } catch (...) {
134 throw(rogue::GeneralError::create("Bsp::execute", "Error executing node %s", this->_name.c_str()));
135 }
136}
137
139std::string ria::Bsp::execute() {
140 try {
141 return (std::string(bp::extract<char*>(this->_obj.attr("callDisp")())));
142 } catch (...) {
143 throw(rogue::GeneralError::create("Bsp::execute", "Error executing node %s", this->_name.c_str()));
144 }
145}
146
148void ria::Bsp::set(std::string value) {
149 try {
150 this->_obj.attr("setDisp")(value, false, -1);
151 } catch (...) {
152 throw(rogue::GeneralError::create("Bsp::set",
153 "Error setting value %s on node %s",
154 value.c_str(),
155 this->_name.c_str()));
156 }
157}
158
160void ria::Bsp::setWrite(std::string value) {
161 try {
162 this->_obj.attr("setDisp")(value, true, -1);
163 } catch (...) {
164 throw(rogue::GeneralError::create("Bsp::set",
165 "Error setting value %s on node %s",
166 value.c_str(),
167 this->_name.c_str()));
168 }
169}
170
172std::string ria::Bsp::get() {
173 try {
174 return (std::string(bp::extract<char*>(this->_obj.attr("getDisp")(false, -1))));
175 } catch (...) {
176 throw(rogue::GeneralError::create("Bsp::set", "Error getting value on node %s", this->_name.c_str()));
177 }
178}
179
181std::string ria::Bsp::readGet() {
182 try {
183 return (std::string(bp::extract<char*>(this->_obj.attr("getDisp")(true, -1))));
184 } catch (...) {
185 throw(rogue::GeneralError::create("Bsp::set", "Error getting value on node %s", this->_name.c_str()));
186 }
187}
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