MachineIntelligenceCore:Toolchain
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
PropertyTree.cpp
Go to the documentation of this file.
1 
24 
26 
27 #include <boost/property_tree/json_parser.hpp>
28 #include <boost/foreach.hpp>
29 
30 namespace mic {
31 namespace configuration {
32 
33 
34 PropertyTree::PropertyTree(std::string node_name_) : node_name(node_name_) {
35  // Register this property tree.
36  PARAM_SERVER->registerPropertyTree(this);
37 }
38 
40  // TODO Auto-generated destructor stub
41 }
42 
43 std::string PropertyTree::getNodeName() const {
44  return node_name;
45 }
46 
47 
49  // Register the property. TODO: add checking whether another property with that name existed earlier...
50  properties[prop.name()] = &prop;
51 }
52 
54  // Check if there are any properties.
55  if (properties.empty()){
56  LOG(LDEBUG) << "Registered properties in object \""<< node_name << "\": empty";
57  return;
58  }//: if
59 
60  LOG(LDEBUG) << "Registered properties in object \""<< node_name << "\":";
61  BOOST_FOREACH(PropertyPair prop, properties) {
62  LOG(LDEBUG) << "\t" << prop.first;
63  }//: foreach
64 }
65 
67  // Check if there are any properties.
68  if (properties.empty()){
69  LOG(LINFO) << "Object \""<< node_name << "\": no properties";
70  return;
71  }//: if
72 
73  LOG(LINFO) << "Object \""<< node_name << "\":";
74  BOOST_FOREACH(PropertyPair prop, properties) {
75  LOG(LINFO) << "\t \"" << prop.first << "\" = " << prop.second->getValue();
76  }//: foreach
77 }
78 
79 PropertyInterface * PropertyTree::getProperty(const std::string& name) {
80  if (properties.count(name) > 0) {
81  return properties[name];
82  } else {
83  return NULL;
84  }
85 }
86 
87 
88 void PropertyTree::loadPropertiesFromConfigNode(boost::property_tree::ptree const& pt_) {
89  LOG(LTRACE) << "PropertyTree::loadPropertiesFromConfigNode";
90 
91  std::string name;
92  std::string value;
93  std::string key;
94 
95  // Iterate through all properties in config file.
96  using boost::property_tree::ptree;
97  for (ptree::const_iterator it = pt_.begin(); it != pt_.end(); ++it) {
98 
99  // Read name and value.
100  name = it->first;
101  value = it->second.get_value<std::string>();
102 
103  LOG(LDEBUG) << "Property: " << name << "=" << value;
104 
105  // Find adequte object property.
107  if (!prop) {
108  LOG(LWARNING) << "Object \"" << node_name << "\" has no property named \"" << name << "\", which is defined in configuration file.";
109  continue;
110  }
111 
112  // Set value.
113  prop->setValue(value);
114  LOG(LINFO) << "Object \"" << node_name << "\": property \"" << prop->name() << "\" value set to " << prop->getValue();
115  }
116 
117 }
118 
119 /*
120 BOOST_FOREACH(boost::property_tree::ptree::value_type &v, pt.get_child("particles.electron"))
121  {
122 // assert(v.first.empty()); // array elements have no names
123  std::cout << v.first.data() << std::endl;
124  std::cout << v.second.data() << std::endl;
125  // etc
126  }
127  */
128 
129 } /* namespace configuration */
130 } /* namespace mic */
Contains declaration of the PropertyTree class.
#define LDEBUG
Definition: LoggerAux.hpp:53
Contains declaration of parameter server singleton along with some auxiliary typedefs.
std::string node_name
Name of the node in configuration file.
#define LINFO
Definition: LoggerAux.hpp:55
PropertyTree(std::string node_name_)
#define LTRACE
Definition: LoggerAux.hpp:52
std::map< std::string, mic::configuration::PropertyInterface * > properties
Map of all registered properties.
#define LWARNING
Definition: LoggerAux.hpp:57
void loadPropertiesFromConfigNode(boost::property_tree::ptree const &pt_)
PropertyInterface * getProperty(const std::string &name)
Basic interface property - used during registration etc.
Definition: Property.hpp:90
std::string getNodeName() const
virtual std::string getValue()=0
std::pair< std::string, PropertyInterface * > PropertyPair
Type representing a pair consisting of name-property.
Definition: Property.hpp:251
#define PARAM_SERVER
Macro returning parameter server instance.
virtual void setValue(const std::string &str)=0
#define LOG(level)
Macro for message printing.
Definition: Log.hpp:39
void registerProperty(PropertyInterface &prop)
const std::string & name() const
Definition: Property.hpp:103