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 {
14 class 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
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 <>
94inline int Query(InfileTree* tree, std::string query, int index) {
95 std::string s = tree->GetString(query, index);
96 boost::trim(s);
97 return boost::lexical_cast<int>(s.c_str());
98}
99
100template <>
101inline float Query(InfileTree* tree, std::string query, int index) {
102 std::string s = tree->GetString(query, index);
103 boost::trim(s);
104 return boost::lexical_cast<float>(s.c_str());
105}
106
107template <>
108inline double Query(InfileTree* tree, std::string query, int index) {
109 std::string s = tree->GetString(query, index);
110 boost::trim(s);
111 return boost::lexical_cast<double>(s.c_str());
112}
113
114/// @brief a query method for optional parameters
115/// @param tree the infile tree to use
116/// @param query the query to be made
117/// @param default_val the default value to use
118/// @return either return the optional value if it exists or return the default
119/// value
120template <typename T>
121inline T OptionalQuery(InfileTree* tree, std::string query, T default_val) {
122 T val;
123 tree->NMatches(query) == 1 ?
124 val = boost::lexical_cast<T>(tree->GetString(query).c_str()) :
125 val = default_val;
126 return val;
127}
128
129template <>
130inline int OptionalQuery(InfileTree* tree, std::string query,
131 int default_val) {
132 int val = default_val;
133 if (tree->NMatches(query) == 1) {
134 std::string s = tree->GetString(query);
135 boost::trim(s);
136 val = boost::lexical_cast<int>(s.c_str());
137 }
138 return val;
139}
140
141template <>
142inline bool OptionalQuery(InfileTree* tree, std::string query,
143 bool default_val) {
144 if (tree->NMatches(query) == 0) {
145 return default_val;
146 }
147
148 std::string s = tree->GetString(query);
149 boost::trim(s);
150 std::transform(s.begin(), s.end(), s.begin(), ::tolower);
151 return s == "true" || s == "t" || s == "1";
152}
153
154template <>
155inline float OptionalQuery(InfileTree* tree, std::string query,
156 float default_val) {
157 float val = default_val;
158 if (tree->NMatches(query) == 1) {
159 std::string s = tree->GetString(query);
160 boost::trim(s);
161 val = boost::lexical_cast<float>(s.c_str());
162 }
163 return val;
164}
165
166template <>
167inline double OptionalQuery(InfileTree* tree, std::string query,
168 double default_val) {
169 double val = default_val;
170 if (tree->NMatches(query) == 1) {
171 std::string s = tree->GetString(query);
172 boost::trim(s);
173 val = boost::lexical_cast<double>(s.c_str());
174 }
175 return val;
176}
177
178} // namespace cyclus
179
180#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