Xpace
config.h
Go to the documentation of this file.
1 
2 
3 /*************************************************************
4  **
5  ** @file base/config.h
6  **
7  ** Copyright (C) 2012 Xpace, LLC. All rights reserved
8  **
9  ** www.xpace.net
10  **
11  **************************************************************/
12 
13 
14 #ifndef XPACE_CONFIG_H
15 #define XPACE_CONFIG_H
16 
17 #include <functional>
18 
19 #ifndef _WIN32
20 #include <stdint.h>
21 #endif
22 
23 #include "base/types.h"
24 #include "base/exception.h"
25 
26 namespace Xpace
27 {
28  /// Each high-level Xpace object has a Configuration
30  {
31  public:
32  /// create a configuration from a fragment of XML
33  /// @param str the XML fragment
34  /// @param tag search the Configuration for a sub-configuration with this tag (cf. findTag)
35  /// @throw XML_No_Value if tag is not found
36  /// @throw XML_Error
38  (const String& str = String(),
39  const String& tag = String());
40 
41  #if defined XPACE_FILE_H || defined DOCUMENTATION
42  /// create a configuration from a configuration File
43  /// @param file the configuration File
44  /// @param tag search the Configuration for a sub-configuration with this tag (cf. findTag)
45  /// @throw File_Cant_Read
46  /// @throw XML_Error
47  /// @throw XML_No_Value if tag is not found
49  (File* file,
50  const String& tag = String());
51  #endif
52 
53  /// @param tag search the Configuration for a sub-configuration with this tag (cf. findTag)
55  (const Configuration&,
56  const String& tag = String());
57  Configuration& operator=
58  (const Configuration&);
59 
60  ~Configuration();
61 
62  /// @return true if empty
63  bool operator!
64  ()
65  const;
66 
67  /// @return the XML representation of the entire Configuration
68  String toString
69  ()
70  const;
71 
72  #if defined QDOM_H || defined DOCUMENTATION
73  /// Convert a QDomElement to a Configuration
74  /// @param elt the QDomElement to be converted
75  /// @param doc the QDomDocument the element's XML document (if none, create a document)
76  /// @param deep if true, convert children as well
78  (const QDomElement& elt,
79  const QDomDocument& doc = QDomDocument(),
80  bool deep = false);
81 
82  /// Convert a Configuration to a QDomElement
83  /// @return the QDomElement
84  operator const QDomElement&
85  ()
86  const;
87 
88  /// Get the QDomDocument containing this Config
89  /// @return the QDomDocument
90  const QDomDocument& getDomDocument
91  ()
92  const;
93  #endif
94 
95  /// @return this Configuration's XML tag
96  String getTag
97  ()
98  const;
99 
100  /// Copy a sub-configuration
101  /// @param tag search the Configuration for a sub-configuration with this tag
102  /// @param name search for a sub-configuration with this attribute or child
103  /// @param value search for a sub-configuration with an attribute/child called name with this value
104  /// @param deep iff true, return all descendants
105  /// @return a copy of the sub-configuration
106  Configuration findTag
107  (const String& tag,
108  const String& name = String(),
109  const String& value = String(),
110  bool deep = true)
111  const;
112 
113  /// get a node's value or an attribute
114  /// search for attribute first, then child value
115 
116  /// @param tag the tag of the child/attribute whose value we want; separate >1 tags with '|'
117  /// if tag is empty, return this node's value
118  /// @return the (String) value
119  String getValue
120  (const String& tag = String())
121  const;
122 
123  /// @param tag the tag of the child/attribute whose value we want; separate >1 tags with '|'
124  /// if tag is empty, return this node's value
125  /// @param def the default value if not found
126  /// @param ok fillin true if found
127  /// @return the integer value
128  int64 getValueInt
129  (const String& tag = String(),
130  int64 def = 0,
131  bool* ok = 0)
132  const;
133 
134  /// @param tag the tag of the child/attribute whose value we want; separate >1 tags with '|'
135  /// if tag is empty, return this node's value
136  /// @return the (boolean) value
137  bool getValueBool
138  (const String& tag = String())
139  const;
140 
141  /// get named values, starting at root
142  /// @param name depth-first search for this tag
143  /// @return the values associated with this tag, delimited by '.'
144  String getValuePath
145  (const String& tag)
146  const;
147 
148  // find children only, not attributes
149 
150  /// @param tag the Child's tag
151  /// @return true iff the Child exists
152  bool hasChild
153  (const String& tag)
154  const;
155 
156  /// @param add a this as a child
157  void addChild
158  (const Configuration& child);
159 
160  /// @param tag the child tag whose values we want
161  /// @return the (String) values
162  std::vector<String> getChild
163  (const String& tag)
164  const;
165 
166  /// @param tag the child tag whose values we want
167  /// @param def the default value if not found
168  /// @param ok fillin true if found
169  /// @return the integer values
170  std::vector<int64> getChildInt
171  (const String& tag,
172  int64 def,
173  bool* ok)
174  const;
175 
176  /// @param tag the child tags whose values we want
177  /// @return the (boolean) values
178  std::vector<bool> getChildBool
179  (const String& tag)
180  const;
181 
182  /// set attribute values
183 
184  /// set this Configuation's XML tag
185  /// @param tag the new tag
186  void setTag
187  (const String& tag);
188 
189  /// @param name set this attribute
190  /// @param value to this (String) value
191  void setValue
192  (const String& name,
193  const String& value);
194 
195  /// @param name set this attribute
196  /// @param value to this (integer) value
197  void setValue
198  (const String& name,
199  int64 value);
200 
201  /// @param tag set this attribute
202  /// @param value to this (boolean) value
203  void setValue
204  (const String& name,
205  bool value);
206 
207  /// Callback for traversing Configuration nodes
208  /// @oaram depth the node's depth
209  /// @param pos the node's position (relative to parent)
210  /// @param tag the node's XML tag
211  /// @param config the node's (sub-)Configuration
212  /// @return true to continue, false to break
213  typedef std::function<bool(uint depth,
214  uint pos,
215  const String& tag,
216  const String& config)> nodeCallback;
217 
218  /// Traverse (pre-order) a Configuration
219  /// @param callback the callback function
220  /// @param deep iff true, put each node's children in config sent to callback
221  /// @param tag if not empty, match this tag before calling back
222  /// @param maxDepth how many levels to descend (relative to tag, if any)
223  /// @return true iff the callback always returned true
224  bool traverse
225  (nodeCallback callback,
226  bool deep = false,
227  const String& tag = String(),
228  uint maxDepth = ~0)
229  const;
230 
231  #if defined XPACE_FILE_H || defined DOCUMENTATION
232  /// serialize the Configuration
233  /// @param f to this file
234  /// @return true if successfully serialized
235  bool write
236  (File* f)
237  const;
238  #endif
239 
240  //protected:
241  typedef std::function<bool(uint depth,
242  const String& tag,
243  intptr_t elt)> elt_callback;
244  protected:
245  bool elt_traverse
246  (elt_callback,
247  const String& tag);
248 
249  private:
250  intptr_t config[2];
251  };
252 
253  /// base class for a configurable object
254  /// inherit from this to make your object configurable
256  {
257  public :
258  /// @return this object's read-only Configuration
259  virtual const Configuration& getConfig
260  ()
261  const
262  = 0;
263  };
264 
265  /// Configuration errors
266 
267  /// XML parse error
268  class XML_Error : public Exception
269  {
270  public :
271  /// @param error an English description of the error
272  /// @param source the source XML
273  /// @line the line number on which the error occurred
274  /// @col the column in which the error occurred
275  XML_Error
276  (const String error,
277  const String source,
278  uint line,
279  uint col);
280  };
281 
282  /// Can't find a named value
283  class XML_No_Value : public Exception
284  {
285  public :
286  /// @param name the name of the value
287  /// @param source the source XML
288  /// @param the exception cauing the failure, if any
290  (const String name,
291  const String source,
292  const Exception reason = Exception());
293  };
294 
295  /// Found a value, but it's bad (e.g., out of range, meaningless)
296  class XML_Bad_Value : public Exception
297  {
298  public :
299  /// @param name the name of the value
300  /// @param value the bad value
301  /// @param source the source XML
303  (const String name,
304  const String value,
305  const String source);
306  };
307 
308  // A simple, standard implementation
309  class ConfigImpl : public Configurable
310  {
311  public:
312  ConfigImpl
313  (const Configuration& c,
314  const String& tag = String());
315 
316  const Configuration& getConfig
317  ()
318  const override;
319 
320  protected:
321  void setConfig
322  (const Configuration&);
323 
324  private:
325  Configuration config;
326  };
327 }
328 
329 #endif
330 
int bool
Definition: types_c.h:66
A low-level data holder.
Definition: types_c.h:82
Found a value, but it&#39;s bad (e.g., out of range, meaningless)
Definition: config.h:296
unsigned int uint
Definition: types.h:75
Each high-level Xpace object has a Configuration.
Definition: config.h:29
A string, Unicode UTF-16 and reference-counted.
Definition: types.h:269
Can&#39;t find a named value.
Definition: config.h:283
std::function< bool(uint depth, uint pos, const String &tag, const String &config)> nodeCallback
Callback for traversing Configuration nodes depth the node&#39;s depth.
Definition: config.h:216
uint col
Definition: table_c.h:69
Configuration errors.
Definition: config.h:268
const Xpace_Char16 * name
Sink callbacks for table data.
Definition: table_c.h:141
uint bool * ok
Definition: table_c.h:117
base class for a configurable object inherit from this to make your object configurable ...
Definition: config.h:255
uint int64 value
Definition: table_c.h:159
long long int64
Definition: types.h:86
Xpace project main namespace
Definition: datetime.h:18
std::function< bool(uint depth, const String &tag, intptr_t elt)> elt_callback
Definition: config.h:243

current as of Wed Jun 10 2026 12:00:05