CYCLUS
Loading...
Searching...
No Matches
infile_tree.h
Go to the documentation of this file.
1#ifndef CYCLUS_SRC_INFILE_TREE_H_
2#define CYCLUS_SRC_INFILE_TREE_H_
3
4#include <string>
5#include <vector>
6#include <set>
7
8#include <boost/algorithm/string.hpp>
9#include <boost/lexical_cast.hpp>
10
11#include "xml_parser.h"
12
13namespace xmlpp {
14class Node;
15}
16
17namespace cyclus {
18
19/// @class InfileTree
20///
21/// A class for extracting information from a given XML parser
23 public:
24 /// constructor given a parser
25 /// @param parser the xml parser
26 InfileTree(XMLParser& parser);
27
28 virtual ~InfileTree();
29
30 /// @return the number of elements in the current query state
31 virtual int NElements();
32
33 /// investigates the current status and returns a string representing
34 /// the name of a given index
35 /// @param index the index of the queried element
36 virtual std::string GetElementName(int index = 0);
37
38 /// investigates the current status and returns the number of elements
39 /// matching a query
40 /// @param query the query
41 /// @return the number of elements matching the query
42 virtual int NMatches(std::string query);
43
44 /// investigates the current status and returns a string representing
45 /// the content of a query at a given index
46 /// @param query the query
47 /// @param index the index of the queried element
48 virtual std::string GetString(std::string query, int index = 0);
49
50 /// populates a child infile based on a query and index
51 /// @param query the query
52 /// @param index the index of the queried element
53 /// @return a initialized infile based on the query and index
54 InfileTree* SubTree(std::string query, int index = 0);
55
56 protected:
57 /// constructor given a node
58 /// @param node the node to set as the current node
59 InfileTree(xmlpp::Node* node);
60
61 /// every derived infile must return a new instance initialized
62 /// by a query.
63 /// @param query the query
64 /// @param index the index of the queried element
65 /// @return a infile initialized via the snippet
66 virtual InfileTree* GetEngineFromQuery(std::string query, int index);
67
68 /// sets the current node to a given node
69 /// @param node the new current node
70 void SetCurrentNode(xmlpp::Node* node);
71
72 private:
73 std::set<InfileTree*> spawned_children_;
74 xmlpp::Node* current_node_;
75};
76
77/// @brief a query method for required parameters
78/// @param tree the infile tree to use
79/// @param query the query to be made
80/// @param index the index of the queried element
81/// @return either return the optional value if it exists or return the default
82/// value
83template <typename T>
84inline T Query(InfileTree* tree, std::string query, int index = 0) {
85 return boost::lexical_cast<T>(tree->GetString(query, index).c_str());
86}
87
88template <>
89inline std::string Query(InfileTree* tree, std::string query, int index) {
90 return tree->GetString(query, index);
91}
92
93template <> inline int Query(InfileTree* tree, std::string query, int index) {
94 std::string s = tree->GetString(query, index);
95 boost::trim(s);
96 return boost::lexical_cast<int>(s.c_str());
97}
98
99template <> inline float Query(InfileTree* tree, std::string query, int index) {
100 std::string s = tree->GetString(query, index);
101 boost::trim(s);
102 return boost::lexical_cast<float>(s.c_str());
103}
104
105template <>
106inline double Query(InfileTree* tree, std::string query, int index) {
107 std::string s = tree->GetString(query, index);
108 boost::trim(s);
109 return boost::lexical_cast<double>(s.c_str());
110}
111
112/// @brief a query method for optional parameters
113/// @param tree the infile tree to use
114/// @param query the query to be made
115/// @param default_val the default value to use
116/// @return either return the optional value if it exists or return the default
117/// value
118template <typename T>
119inline T OptionalQuery(InfileTree* tree, std::string query, T default_val) {
120 T val;
121 tree->NMatches(query) == 1
122 ? val = boost::lexical_cast<T>(tree->GetString(query).c_str())
123 : val = default_val;
124 return val;
125}
126
127template <>
128inline int OptionalQuery(InfileTree* tree, std::string query, int default_val) {
129 int val = default_val;
130 if (tree->NMatches(query) == 1) {
131 std::string s = tree->GetString(query);
132 boost::trim(s);
133 val = boost::lexical_cast<int>(s.c_str());
134 }
135 return val;
136}
137
138template <>
139inline bool OptionalQuery(InfileTree* tree, std::string query,
140 bool default_val) {
141 if (tree->NMatches(query) == 0) {
142 return default_val;
143 }
144
145 std::string s = tree->GetString(query);
146 boost::trim(s);
147 std::transform(s.begin(), s.end(), s.begin(), ::tolower);
148 return s == "true" || s == "t" || s == "1";
149}
150
151template <>
152inline float OptionalQuery(InfileTree* tree, std::string query,
153 float default_val) {
154 float val = default_val;
155 if (tree->NMatches(query) == 1) {
156 std::string s = tree->GetString(query);
157 boost::trim(s);
158 val = boost::lexical_cast<float>(s.c_str());
159 }
160 return val;
161}
162
163template <>
164inline double OptionalQuery(InfileTree* tree, std::string query,
165 double default_val) {
166 double val = default_val;
167 if (tree->NMatches(query) == 1) {
168 std::string s = tree->GetString(query);
169 boost::trim(s);
170 val = boost::lexical_cast<double>(s.c_str());
171 }
172 return val;
173}
174
175} // namespace cyclus
176
177#endif // CYCLUS_SRC_INFILE_TREE_H_
A class for extracting information from a given XML parser.
Definition infile_tree.h:22
virtual int NElements()
virtual InfileTree * GetEngineFromQuery(std::string query, int index)
every derived infile must return a new instance initialized by a query.
virtual ~InfileTree()
InfileTree(XMLParser &parser)
constructor given a parser
virtual int NMatches(std::string query)
investigates the current status and returns the number of elements matching a query
virtual std::string GetElementName(int index=0)
investigates the current status and returns a string representing the name of a given index
InfileTree * SubTree(std::string query, int index=0)
populates a child infile based on a query and index
void SetCurrentNode(xmlpp::Node *node)
sets the current node to a given node
virtual std::string GetString(std::string query, int index=0)
investigates the current status and returns a string representing the content of a query at a given i...
A helper class to hold xml file data and provide automatic validation.
Definition xml_parser.h:15
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or....
Definition agent.cc:14
T Query(InfileTree *tree, std::string query, int index=0)
a query method for required parameters
Definition infile_tree.h:84
T OptionalQuery(InfileTree *tree, std::string query, T default_val)
a query method for optional parameters