SoDaRadio-5.0.3-master:8901fb5
PropTree.hxx
Go to the documentation of this file.
1 /*
2  Copyright (c) 2015, Matthew H. Reilly (kb1vc)
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are
7  met:
8 
9  Redistributions of source code must retain the above copyright
10  notice, this list of conditions and the following disclaimer.
11  Redistributions in binary form must reproduce the above copyright
12  notice, this list of conditions and the following disclaimer in
13  the documentation and/or other materials provided with the
14  distribution.
15 
16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 
29 #ifndef PROP_TREE_HDR
30 #define PROP_TREE_HDR
31 
32 #include <string>
33 #include <uhd/usrp/multi_usrp.hpp>
34 #include <uhd/property_tree.hpp>
35 #include <boost/format.hpp>
36 #include <iostream>
37 #include <boost/property_tree/exceptions.hpp>
38 
39 namespace SoDa {
44  class PropTree {
45  public:
54  PropTree(const uhd::usrp::multi_usrp::sptr usrp, const std::string & requester, int mb_index = 0) {
55  client_name = requester;
56  try {
57  mb0_name = usrp->get_device()->get_tree()->list("/mboards").at(mb_index);
58  }
59  catch (std::runtime_error e) {
60  complain((boost::format("Exception looking for motherboard %d") % mb_index).str(), e, false);
61  }
62 
63  // the object is to create a tree rooted at the first motherboard.
64  fqpn = "/mboards/" + mb0_name;
65  try {
66  tree = usrp->get_device()->get_tree()->subtree(fqpn);
67  }
68  catch (std::runtime_error e) {
69  complain("Exception while creating initial motherboard property tree", fqpn, e, false);
70  }
71  }
72 
80  PropTree(PropTree * ptree, const std::string & path) {
81  init(ptree, path);
82  }
83 
91  PropTree(PropTree & ptree, const std::string & path) {
92  init(&ptree, path);
93  }
94 
100  bool hasProperty(const std::string & propname) {
101  bool ret = false;
102  try {
103  std::vector<std::string> pslis = tree->list("");
104  ret = std::find(pslis.begin(), pslis.end(), propname) != pslis.end();
105  }
106  catch (std::runtime_error e) {
107  complain("Exception while looking for property", fqpn + "/" + propname, e, false);
108  }
109  return ret;
110  }
111 
112  std::vector<std::string> getPropNames(const std::string & path = std::string(""))
113  {
114  return tree->list(path);
115  }
116 
117  std::string getStringProp(const std::string & propname, const std::string defval = std::string("None"))
118  {
119  return getProperty<std::string>(propname, defval);
120  }
121 
122  int getIntProp(const std::string & propname, const int defval = 0)
123  {
124  return getProperty<int>(propname, defval);
125  }
126 
127  int getDoubleProp(const std::string & propname, const double defval = 0.0)
128  {
129  return getProperty<double>(propname, defval);
130  }
131 
132  bool getBoolProp(const std::string & propname, const bool defval = false)
133  {
134  return getProperty<bool>(propname, defval);
135  }
136 
137  void setStringProp(const std::string & propname, const std::string val = std::string("None"))
138  {
139  setProperty<std::string>(propname, val);
140  }
141 
142  void setIntProp(const std::string & propname, const int val = 0)
143  {
144  setProperty<int>(propname, val);
145  }
146 
147  void setDoubleProp(const std::string & propname, const double val = 0.0)
148  {
149  setProperty<double>(propname, val);
150  }
151 
152  void setBoolProp(const std::string & propname, const bool val = false)
153  {
154  setProperty<bool>(propname, val);
155  }
156 
157  template <typename T> T getProperty(const std::string & propname, const T defval) {
158  T ret = defval;
159  std::string path = fqpn + "/" + propname;
160  try {
161  ret = tree->access<T>(propname).get();
162  }
163  catch (std::runtime_error e) {
164  complain("getProperty Unknown exception -- unknown property", path, e);
165  }
166  return ret;
167  }
168 
169 
170  template <typename T> T getProperty(const std::string & propname) {
171  T ret;
172  std::string path = fqpn + "/" + propname;
173  try {
174  ret = tree->access<T>(propname).get();
175  }
176  catch (std::runtime_error e) {
177  complain("getProperty Unknown exception -- unknown property", path, e, false);
178  }
179  return ret;
180  }
181 
182 
183  template <typename T> void setProperty(const std::string & propname, const T val) {
184  std::string path = fqpn + "/" + propname;
185  try {
186  tree->access<T>(propname).set(val);
187  }
188  catch (std::runtime_error e) {
189  complain("setProperty Unknown exception -- unknown property", path, e);
190  }
191  }
192 
193 
194  private:
195  void init(PropTree * ptree, const std::string & path) {
196  client_name = ptree->client_name;
197  try {
198  tree = ptree->tree->subtree(path);
199  }
200  catch (std::runtime_error e) {
201  complain("Exception while creating a subtree", path, e, false);
202  }
203 
204  fqpn = ptree->fqpn + "/" + path;
205  }
206 
207 
208  void complain(const std::string & explain_string,
209  std::runtime_error & e,
210  bool continue_p = true) {
211  std::cerr << boost::format("PropTree encountered %s\n\tRequester: %s\n\tException: [%s]\n\t%s\n")
212  % explain_string % client_name % e.what() % (continue_p ? "Thread will continue" : "Thread will terminate");
213  if(!continue_p) exit(-1);
214  }
215 
216  void complain(const std::string & explain_string,
217  const std::string & path,
218  std::runtime_error & e,
219  bool continue_p = true) {
220  std::cerr << boost::format("PropTree encountered %s\n\twhile looking for path [%s]\n\tRequester: %s\n\tException: [%s]\n\t%s\n")
221  % explain_string % path % client_name % e.what() % (continue_p ? "Thread will continue" : "Thread will terminate");
222 
223  if(!continue_p) exit(-1);
224  }
225 
226  std::string client_name;
227  std::string fqpn;
228  uhd::property_tree::sptr tree;
229  std::string mb0_name;
230  };
231 }
232 #endif
void setBoolProp(const std::string &propname, const bool val=false)
Definition: PropTree.hxx:152
void init(PropTree *ptree, const std::string &path)
Definition: PropTree.hxx:195
T getProperty(const std::string &propname, const T defval)
Definition: PropTree.hxx:157
std::string mb0_name
Definition: PropTree.hxx:229
PropTree class encapsulates the USRP property tree functions to allow better trap and error recovery ...
Definition: PropTree.hxx:44
T getProperty(const std::string &propname)
Definition: PropTree.hxx:170
void setStringProp(const std::string &propname, const std::string val=std::string("None"))
Definition: PropTree.hxx:137
PropTree(const uhd::usrp::multi_usrp::sptr usrp, const std::string &requester, int mb_index=0)
Constructor – build a property tree widget rooted at the first motherboard in this multi-usrp...
Definition: PropTree.hxx:54
void complain(const std::string &explain_string, std::runtime_error &e, bool continue_p=true)
Definition: PropTree.hxx:208
bool getBoolProp(const std::string &propname, const bool defval=false)
Definition: PropTree.hxx:132
PropTree(PropTree &ptree, const std::string &path)
Constructor – build a property tree widget that is a subtree of the mboard property tree...
Definition: PropTree.hxx:91
std::string getStringProp(const std::string &propname, const std::string defval=std::string("None"))
Definition: PropTree.hxx:117
void complain(const std::string &explain_string, const std::string &path, std::runtime_error &e, bool continue_p=true)
Definition: PropTree.hxx:216
std::string fqpn
Definition: PropTree.hxx:227
uhd::property_tree::sptr tree
Definition: PropTree.hxx:228
std::vector< std::string > getPropNames(const std::string &path=std::string(""))
Definition: PropTree.hxx:112
void setProperty(const std::string &propname, const T val)
Definition: PropTree.hxx:183
void setDoubleProp(const std::string &propname, const double val=0.0)
Definition: PropTree.hxx:147
std::string client_name
the name of the runtime object/thread that created this tree.
Definition: PropTree.hxx:226
void setIntProp(const std::string &propname, const int val=0)
Definition: PropTree.hxx:142
int getIntProp(const std::string &propname, const int defval=0)
Definition: PropTree.hxx:122
PropTree(PropTree *ptree, const std::string &path)
Constructor – build a property tree widget that is a subtree of the mboard property tree...
Definition: PropTree.hxx:80
bool hasProperty(const std::string &propname)
does the property tree have this property name as a child?
Definition: PropTree.hxx:100
int getDoubleProp(const std::string &propname, const double defval=0.0)
Definition: PropTree.hxx:127