You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

configuration.hpp 45KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. // configuration.hpp
  2. //
  3. // (C) 2006 - 2009 MicroNeil Research Corporation.
  4. // See http://www.codedweller.com for details.
  5. //
  6. // This program is free software; you can redistribute it and/or modify it
  7. // under the terms of the GNU General Public License as published by the
  8. // Free Software Foundation; either version 2 of the License, or (at your
  9. // option) any later version.
  10. //
  11. // This program is distributed in the hope that it will be useful, but WITHOUT
  12. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. // more details.
  15. //
  16. // You should have received a copy of the GNU General Public License along with
  17. // this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  18. // Place, Suite 330, Boston, MA 02111-1307 USA
  19. //
  20. // What about this =============================================================
  21. // The configuration module provides a platform for reading configuration files
  22. // (or string data) containing well-formed xml and mapping that data to program
  23. // variables.
  24. // The idea is to provide the ability for an object or application to provide
  25. // a modular "configuration" object that models a hierarchical collection of
  26. // "settings" that can be represented easily in code and in xml.
  27. //
  28. // The following is an example model of a configuration in code and that same
  29. // configuration fully populated in xml.
  30. //
  31. // The code might look like this...
  32. //
  33. // int IntValue, DefaultInt = 3;
  34. // double DblValue, DefaultDbl = 3.14159;
  35. // bool BooleanValue, DefaultBool = false;
  36. // string StringValue, DefaultString = "NoStringHere";
  37. //
  38. // SpecialConfigurator : public ConfigurationHandler { // Create a special handler to build a list
  39. // ...
  40. // public:
  41. //
  42. // ConfigurationHandler& Startup(ConfigurationElement& E) { // This function returns a handy handler to
  43. // return MyStartupConfigurationHandler; // (re) initialize this handler ;-)
  44. // }
  45. //
  46. // void Operator()() { // Each time the configurator is called
  47. // ...
  48. // }
  49. //
  50. // int Attribute1; // these items are interpreted and added
  51. // double Attribute2; // to the list. A ConfigurationHandler COULD
  52. // string Attribute3; // do something entirely different though ;-)
  53. // string Contents;
  54. // ...
  55. // } Special;
  56. //
  57. // ConfigurationElement SampleConfig("SampleConfiguration"); // Define a sample config (doc element)
  58. // SampleConfig // Populate the SampleConfig
  59. // .atStartCall(Special.Startup())
  60. // .Element("Integer", IntValue, DefaultInt).End() // Link an element to an int w/ default.
  61. // .Element("Double", DblValue, DefaultDbl).End("Double") // Link an element to a dbl w/ default.
  62. // .Element("String", StringValue, DefaultString).End("String") // Link an element to a string w/ default.
  63. // .Element("ComplexElements") // Create a sub element.
  64. // .Element("Complex1") // Sub element Complex1 has attributes.
  65. // .Attribute("IntAtt", IntValue, DefaultInt) // Complex1 has an integer attribute.
  66. // .Attribute("DblAtt", DblValue, DefaultDbl) // Complex1 has a dbl attribute.
  67. // .Element("IntAtt", IntValue).End() // IntAtt can also be set by a sub element.
  68. // .Element("DblAtt", DblValue).End() // DblAtt can also be set by a sub element.
  69. // .End() // That's it for Complex1.
  70. // .Element("Complex2") // Create the Complex2 sub element.
  71. // .Attribute("C2I", IntValue, DefaultInt) // C2I attribute.
  72. // .Attribute("C2D", DblValue) // C2D attribute - no default.
  73. // .Attribute("C2S", StringValue, DefultString) // C2S attribute - string w/ default.
  74. // .End("Complex2") // End of element throws if doesn't match.
  75. // .Element("Complex3", Special.Contents) // Element 3 using a special configurator.
  76. // .Attribute("A1", Special.Attribute1) // Set A1 and A2 and A3 and when the
  77. // .Attribute("A2", Special.Attribute2) // element has been completed, Special()
  78. // .Attribute("A3", Special.Attribute3) // will be called to record the entries.
  79. // .atEndCall(Special) // Here's where we register the handler.
  80. // .End() // Closing Complex3 to be ice.
  81. // .End() // Closing ComplexElements to be nice.
  82. // .End(); // Closing SampleConfiguration to be nice.
  83. //
  84. // The XML might look like this...
  85. //
  86. // <SampleConfiguration>
  87. // <Integer>10</Integer>
  88. // <Double>2.4</Double>
  89. // <String>This is a sample string</String>
  90. // <ComplexElements>
  91. // <Complex1 IntAtt="4" DblAtt="2.1324">
  92. // <IntAtt>24</IntAtt> <!-- changed IntAtt -->
  93. // </Complex1>
  94. // <Complex2 C2I='3' C2D='5.14' C2S='Some "string" we like' />
  95. // <Complex3> stuff in here </Complex3>
  96. // <Complex3> Another instance </Complex3>
  97. // <Complex3> Each one gets passed to Special() on activation </Complex3>
  98. // <Complex3> This way, Special() can build a list or some other </Complex3>
  99. // <Complex3> interesting thing with all of these. </Complex3>
  100. // <ComplexElements>
  101. // </SampleConfiguration>
  102. //
  103. // Include This Header Once Only ===============================================
  104. #ifndef configuration_included
  105. #define configuration_included
  106. #include <string>
  107. #include <vector>
  108. #include <sstream>
  109. #include <fstream>
  110. #include <cstring>
  111. #include <cstdlib>
  112. #include <list>
  113. using namespace std;
  114. class ConfigurationElement; // Elements exist
  115. class ConfigurationAttribute; // Attributes exist
  116. class ConfigurationData; // Data exists
  117. class ConfigurationTranslator; // Translators exist
  118. class ConfigurationMnemonic; // Mnemonics exist
  119. class Configurator; // Configurators exist
  120. //// Configuration Element /////////////////////////////////////////////////////
  121. //
  122. // Elements make up the core of a configuration. That is, a configuration is a
  123. // tree of elements. Elements translate directly to well formed xml elements in
  124. // a configuration file or string.
  125. class ConfigurationElement {
  126. private:
  127. string myName; // Elements have a name.
  128. // External important things I remember but don't touch...
  129. ConfigurationElement* myParent; // They may have a parrent.
  130. list<Configurator*> myStartConfigurators; // Call these when we start Interpret()
  131. list<Configurator*> myEndConfigurators; // Call these when we finish Interpret()
  132. // Internal / subordinate things I own and kill...
  133. list<ConfigurationAttribute*> myAttributes; // They may have a list of attributes.
  134. list<ConfigurationElement*> myElements; // They may have a list of sub-elements.
  135. list<ConfigurationMnemonic*> myMnemonics; // They may have a list of mnemonics.
  136. list<ConfigurationTranslator*> myTranslators; // They may have a list of translators.
  137. // During Interpret() operations we keep track of where we are seen...
  138. int myLine; // Last line number I was seen on.
  139. int myIndex; // Last char position I was seen on.
  140. int myLength; // Last segment length.
  141. bool myCleanFlag; // Keep track of initialization.
  142. bool myInitOnInterpretFlag; // Initialize() at each Interpret()?
  143. void runStartConfigurators(ConfigurationData& D); // Does what it says ;-)
  144. void runEndConfigurators(ConfigurationData& D); // Does what it says ;-)
  145. public:
  146. ConfigurationElement(const char* Name); // Must be constructed with a name
  147. ConfigurationElement(const string Name); // either c string or c++ string.
  148. ConfigurationElement(const char* Name, ConfigurationElement& Parent); // Sub-elements are constructed with a
  149. ConfigurationElement(const string Name, ConfigurationElement& Parent); // parrent.
  150. // Upon desctruction an element will delete all subordinate objects:
  151. // * All sub element objects.
  152. // * All attribute objects.
  153. // * All mnemonic objects.
  154. // * All translator objects.
  155. // It is important to use new when passing one of these objects to an
  156. // element or attribute to prevent problems with the delete operation.
  157. // NORMALLY these things would be created using factory methods on the
  158. // element and attribute objects themselves - so be careful.
  159. // It will not delete Configurators - they must
  160. // be deleted elsewhere because they may have been
  161. // re-used and this element wouldn't know about it ;-)
  162. ~ConfigurationElement(); // The descrutor clears and deletes all!
  163. // Elements can be probed for some simple, useful things.
  164. string Name(); // Get the name of this element.
  165. ConfigurationElement& Parent(); // Get the parent of this element.
  166. ConfigurationElement& Parent(ConfigurationElement& newParent); // Set the parent of this element.
  167. // Note - if there is no parent (an element is the root) then it will
  168. // return a reference to itself when Parent() is called.
  169. int Line(); // Get the last line number.
  170. int Index(); // Get the last data position.
  171. int Length(); // Get the last length.
  172. // Elements can contain either data or sub-elements.
  173. ConfigurationElement& Element(const char* Name); // Add a new sub element by c string name.
  174. ConfigurationElement& Element(const string Name); // Add a new sub element by c++ string name.
  175. //// Mapping element factory methods for convenience.
  176. //// Root-Node elements are _usually_ empty and without attributes in xml
  177. //// so we don't make any of that type of convenience constructor here.
  178. // char* versions
  179. ConfigurationElement& Element( // Mapping factory for convenience,
  180. const char* Name, // requires a name, of course,
  181. ConfigurationTranslator& newTranslator); // Add a Translator to this element.
  182. ConfigurationElement& Element( // Mapping factory for convenience,
  183. const char* Name, // requires a name, of course,
  184. string& x, string init = string("")); // Map to a string.
  185. ConfigurationElement& Element( // Mapping factory for convenience,
  186. const char* Name, // requires a name, of course,
  187. int& x, int init = 0, int radix = 0); // Map to an int.
  188. ConfigurationElement& Element( // Mapping factory for convenience,
  189. const char* Name, // requires a name, of course,
  190. double& x, double init = 0.0); // Map to a double.
  191. ConfigurationElement& Element( // Mapping factory for convenience,
  192. const char* Name, // requires a name, of course,
  193. bool& x, bool init = false); // Map to a boolean.
  194. // string versions
  195. ConfigurationElement& Element( // Mapping factory for convenience,
  196. const string Name, // requires a name, of course,
  197. ConfigurationTranslator& newTranslator); // Add a Translator to this element.
  198. ConfigurationElement& Element( // Mapping factory for convenience,
  199. const string Name, // requires a name, of course,
  200. string& x, string init = string("")); // Map to a string.
  201. ConfigurationElement& Element( // Mapping factory for convenience,
  202. const string Name, // requires a name, of course,
  203. int& x, int init = 0, int radix = 0); // Map to an int.
  204. ConfigurationElement& Element( // Mapping factory for convenience,
  205. const string Name, // requires a name, of course,
  206. double& x, double init = 0.0); // Map to a double.
  207. ConfigurationElement& Element( // Mapping factory for convenience,
  208. const string Name, // requires a name, of course,
  209. bool& x, bool init = false); // Map to a boolean.
  210. // End methods for heading back up the tree at the end of an element.
  211. class EndNameDoesNotMatch {}; // Throw when End(name) doesn't match.
  212. ConfigurationElement& End(); // Return this element's parent.
  213. ConfigurationElement& End(const char* Name); // Check the name and return the parent
  214. ConfigurationElement& End(const string Name); // if the name is correct - or throw!
  215. // Elements can have attributes.
  216. ConfigurationAttribute& Attribute(const char* Name); // Add an attribute using a cstring.
  217. ConfigurationAttribute& Attribute(const string Name); // Add an attribute using a c++ string.
  218. //// Mapping Attribute factory methods for convenience.
  219. // char* versions
  220. ConfigurationAttribute& Attribute( // Mapping factory for convenience,
  221. const char* Name, // requires a name, of course,
  222. ConfigurationTranslator& newTranslator); // Add a Translator to this element.
  223. ConfigurationAttribute& Attribute( // Mapping factory for convenience,
  224. const char* Name, // requires a name, of course,
  225. string& x, string init = string("")); // Map to a string.
  226. ConfigurationAttribute& Attribute( // Mapping factory for convenience,
  227. const char* Name, // requires a name, of course,
  228. int& x, int init = 0, int radix = 0); // Map to an int.
  229. ConfigurationAttribute& Attribute( // Mapping factory for convenience,
  230. const char* Name, // requires a name, of course,
  231. double& x, double init = 0.0); // Map to a double.
  232. ConfigurationAttribute& Attribute( // Mapping factory for convenience,
  233. const char* Name, // requires a name, of course,
  234. bool& x, bool init = false); // Map to a boolean.
  235. // string versions
  236. ConfigurationAttribute& Attribute( // Mapping factory for convenience,
  237. const string Name, // requires a name, of course,
  238. ConfigurationTranslator& newTranslator); // Add a Translator to this element.
  239. ConfigurationAttribute& Attribute( // Mapping factory for convenience,
  240. const string Name, // requires a name, of course,
  241. string& x, string init = string("")); // Map to a string.
  242. ConfigurationAttribute& Attribute( // Mapping factory for convenience,
  243. const string Name, // requires a name, of course,
  244. int& x, int init = 0, int radix = 0); // Map to an int.
  245. ConfigurationAttribute& Attribute( // Mapping factory for convenience,
  246. const string Name, // requires a name, of course,
  247. double& x, double init = 0.0); // Map to a double.
  248. ConfigurationAttribute& Attribute( // Mapping factory for convenience,
  249. const string Name, // requires a name, of course,
  250. bool& x, bool init = false); // Map to a boolean.
  251. // Elements can Initialize() at each Interpret() call.
  252. ConfigurationElement& setInitOnInterpret(); // Set the init on interpret flag.
  253. // Elements can call external functions to aid in special operations
  254. // such as building lists.
  255. ConfigurationElement& atStartCall(Configurator& Functor); // Add an atStart call-back to this element.
  256. ConfigurationElement& atEndCall(Configurator& Functor); // Add an atEnd call-back to this element.
  257. // Extracting data from the element's contents is done with
  258. // translators. A good set of primatives are built in, but the user
  259. // can also make their own. If an Element is mapped to more than
  260. // one then they are all called once the element's contents are
  261. // collected. A translator takes the data provided by the element,
  262. // converts it into the expected type, and sets one or more variables
  263. // to the converted value. Usually - just one variable.
  264. ConfigurationElement& mapTo(ConfigurationTranslator& newTranslator); // Add a Translator to this element.
  265. ConfigurationElement& mapTo(string& x, string init = string("")); // Map to a string.
  266. ConfigurationElement& mapTo(int& x, int init = 0, int radix = 0); // Map to an int.
  267. ConfigurationElement& mapTo(double& x, double init = 0.0); // Map to a double.
  268. ConfigurationElement& mapTo(bool& x, bool init = false); // Map to a boolean.
  269. // An Element's contents may use some special mnemonics to make a
  270. // configuration easier to understand and less error prone. When the
  271. // contents match a mnemnoic then the translation of the mnemonic is
  272. // passed to the Translators instead of the raw contents.
  273. ConfigurationElement& Mnemonic(const char* name, const char* value); // Add a mnemonic using c strings.
  274. ConfigurationElement& Mnemonic(const char* name, const string value); // Add a mnemonic using c & c++ strings.
  275. ConfigurationElement& Mnemonic(const string name, const char* value); // Add a mnemonic using c++ & c strings.
  276. ConfigurationElement& Mnemonic(const string name, const string value); // Add a mnemonic using c++ strings.
  277. // The way data gets into an element tree is that it is Interpret()ed
  278. // recursively. The data is loaded into a ConfigurationData object which
  279. // is passed to the top Element. That element interpretes the data, moves
  280. // the interpretation pointers, and passes the data on to it's subordinate
  281. // elements in turn. They do the same recursively. When the last sub -
  282. // element has had it's way with the data, the interpretation process is
  283. // complete. The ConfigurationData object will contain the original data
  284. // and a log of anything that happened during the interpretation process.
  285. //
  286. // Each time an element is asked to Interpret() data, it calls any atStart
  287. // configurators, translates any attributes, then either translates it's
  288. // contents or passes the data to it's children, then calls any atEnd
  289. // configurators.
  290. //
  291. // To ensure that the correct default values are used the Initialize() is
  292. // always called on all internal attributes and elements before any data is
  293. // interpreted. To prevent this from being inefficient, a boolean flag is
  294. // kept in each element to keep track of whether it is clean and if it is
  295. // then the call to Initialize will simply return (skipping subordinate
  296. // elements along the way).
  297. //
  298. // Interpret returns true if this object found itself at the current
  299. // Data.Index and false if not. This helps keep the recursive parsing
  300. // code simpler ;-)
  301. void initialize(); // Reset all translators to defaults.
  302. void notifyDirty(); // Set dirty (if translators change).
  303. bool interpret(ConfigurationData& Data); // (re) Interpret this data.
  304. };
  305. //// Configuration Attribute ///////////////////////////////////////////////////
  306. //
  307. // Attributes translate directly to well formed xml attributes (within the
  308. // start tag of an element).
  309. class ConfigurationAttribute {
  310. private:
  311. string myName; // Elements have a name.
  312. ConfigurationElement& myParent; // They may have a parrent.
  313. list<ConfigurationMnemonic*> myMnemonics; // They may have a list of mnemonics.
  314. list<ConfigurationTranslator*> myTranslators; // They may have a list of translators.
  315. int myLine; // Last line number I was seen on.
  316. int myIndex; // Last char position I was seen on.
  317. int myLength; // Last segment length.
  318. public:
  319. ConfigurationAttribute(const char* Name, ConfigurationElement& Parent); // Sub-elements are constructed with a
  320. ConfigurationAttribute(const string Name, ConfigurationElement& Parent); // parrent.
  321. // Attributes delete their Mnemonics and Translators when they go.
  322. // See Elements for similar warnings about objects provided to
  323. // this object... you must use new to be safe, or better yet - stick to
  324. // the built in factory methods ;-)
  325. ~ConfigurationAttribute(); // Crush, Kill, Destroy!
  326. // Attributes can be probed for some simple, useful things.
  327. string Name(); // Get the name of this attribute.
  328. ConfigurationElement& Parent(); // Get the parent of this attribute.
  329. int Line(); // Get the last line number.
  330. int Index(); // Get the last data position.
  331. int Length(); // Get the last length.
  332. void notifyDirty(); // Attributes use this when they change.
  333. // For convenience in building configurations, an Attribute offers
  334. // some call-through methods to it's parrent Element. This allows for
  335. // clear, concise .method() coding that mimics an outline of the
  336. // configuration structure.
  337. //// For switching back to the parent element and adding new sub-elements.
  338. ConfigurationElement& Element(const char* Name); // Add a new sub element by c string name.
  339. ConfigurationElement& Element(const string Name); // Add a new sub element by c++ string name.
  340. //// Mapping element factory methods for convenience.
  341. //// Root-Node elements are _usually_ empty and without attributes in xml
  342. //// so we don't make any of that type of convenience constructor here.
  343. // char* versions
  344. ConfigurationElement& Element( // Mapping factory for convenience,
  345. const char* Name, // requires a name, of course,
  346. ConfigurationTranslator& newTranslator); // Add a Translator to this element.
  347. ConfigurationElement& Element( // Mapping factory for convenience,
  348. const char* Name, // requires a name, of course,
  349. string& x, string init = string("")); // Map to a string.
  350. ConfigurationElement& Element( // Mapping factory for convenience,
  351. const char* Name, // requires a name, of course,
  352. int& x, int init = 0, int radix = 0); // Map to an int.
  353. ConfigurationElement& Element( // Mapping factory for convenience,
  354. const char* Name, // requires a name, of course,
  355. double& x, double init = 0.0); // Map to a double.
  356. ConfigurationElement& Element( // Mapping factory for convenience,
  357. const char* Name, // requires a name, of course,
  358. bool& x, bool init = false); // Map to a boolean.
  359. // string versions
  360. ConfigurationElement& Element( // Mapping factory for convenience,
  361. const string Name, // requires a name, of course,
  362. ConfigurationTranslator& newTranslator); // Add a Translator to this element.
  363. ConfigurationElement& Element( // Mapping factory for convenience,
  364. const string Name, // requires a name, of course,
  365. string& x, string init = string("")); // Map to a string.
  366. ConfigurationElement& Element( // Mapping factory for convenience,
  367. const string Name, // requires a name, of course,
  368. int& x, int init = 0, int radix = 0); // Map to an int.
  369. ConfigurationElement& Element( // Mapping factory for convenience,
  370. const string Name, // requires a name, of course,
  371. double& x, double init = 0.0); // Map to a double.
  372. ConfigurationElement& Element( // Mapping factory for convenience,
  373. const string Name, // requires a name, of course,
  374. bool& x, bool init = false); // Map to a boolean.
  375. // End methods for heading back up the tree at the end of an element.
  376. ConfigurationElement& End(); // Return this element's parent.
  377. ConfigurationElement& End(const char* Name); // Check the name and return the parent
  378. ConfigurationElement& End(const string Name); // if the name is correct - or throw!
  379. //// For adding new attributes to the parent element.
  380. ConfigurationAttribute& Attribute(const char* Name); // Add an attribute using a cstring.
  381. ConfigurationAttribute& Attribute(const string Name); // Add an attribute using a c++ string.
  382. //// Mapping Attribute factory methods for convenience.
  383. // char* versions
  384. ConfigurationAttribute& Attribute( // Mapping factory for convenience,
  385. const char* Name, // requires a name, of course,
  386. ConfigurationTranslator& newTranslator); // Add a Translator to this element.
  387. ConfigurationAttribute& Attribute( // Mapping factory for convenience,
  388. const char* Name, // requires a name, of course,
  389. string& x, string init = string("")); // Map to a string.
  390. ConfigurationAttribute& Attribute( // Mapping factory for convenience,
  391. const char* Name, // requires a name, of course,
  392. int& x, int init = 0, int radix = 0); // Map to an int.
  393. ConfigurationAttribute& Attribute( // Mapping factory for convenience,
  394. const char* Name, // requires a name, of course,
  395. double& x, double init = 0.0); // Map to a double.
  396. ConfigurationAttribute& Attribute( // Mapping factory for convenience,
  397. const char* Name, // requires a name, of course,
  398. bool& x, bool init = false); // Map to a boolean.
  399. // string versions
  400. ConfigurationAttribute& Attribute( // Mapping factory for convenience,
  401. const string Name, // requires a name, of course,
  402. ConfigurationTranslator& newTranslator); // Add a Translator to this element.
  403. ConfigurationAttribute& Attribute( // Mapping factory for convenience,
  404. const string Name, // requires a name, of course,
  405. string& x, string init = string("")); // Map to a string.
  406. ConfigurationAttribute& Attribute( // Mapping factory for convenience,
  407. const string Name, // requires a name, of course,
  408. int& x, int init = 0, int radix = 0); // Map to an int.
  409. ConfigurationAttribute& Attribute( // Mapping factory for convenience,
  410. const string Name, // requires a name, of course,
  411. double& x, double init = 0.0); // Map to a double.
  412. ConfigurationAttribute& Attribute( // Mapping factory for convenience,
  413. const string Name, // requires a name, of course,
  414. bool& x, bool init = false); // Map to a boolean.
  415. //// Set Init On Interprete for the parent element.
  416. ConfigurationElement& setInitOnInterpret(); // Set the init on interpret flag.
  417. //// For adding configurators to the parent element.
  418. ConfigurationElement& atStartCall(Configurator& Functor); // Add an atStart call-back to this element.
  419. ConfigurationElement& atEndCall(Configurator& Functor); // Add an atEnd call-back to this element.
  420. // Of course, the most useful thing about attributes is that they can
  421. // be mapped to variables using translators. The same as those that
  422. // apply to the parent element's contents. Here they are for use on this
  423. // attribute.
  424. ConfigurationAttribute& mapTo(ConfigurationTranslator& newTranslator); // Add a Translator to this attribute.
  425. ConfigurationAttribute& mapTo(string& x, string init = string("")); // Map to a string.
  426. ConfigurationAttribute& mapTo(int& x, int init, int radix = 0); // Map to an int.
  427. ConfigurationAttribute& mapTo(double& x, double init = 0.0); // Map to a double.
  428. ConfigurationAttribute& mapTo(bool& x, bool init = false); // Map to a boolean.
  429. // Attributes can have mnemonics just like elements.
  430. ConfigurationAttribute& Mnemonic(const char* name, const char* value); // Add a mnemonic using a c string.
  431. ConfigurationAttribute& Mnemonic(const char* name, const string value); // Add a mnemonic using c & c++ strings.
  432. ConfigurationAttribute& Mnemonic(const string name, const char* value); // Add a mnemonic using c++ & c strings.
  433. ConfigurationAttribute& Mnemonic(const string name, const string value); // Add a mnemonic using a c++ string.
  434. // Attributes participate in the Interprete() task just like elements.
  435. void initialize(); // Reset all translators to defaults.
  436. bool interpret(ConfigurationData& Data); // (re) Interpret this data.
  437. };
  438. //// Configuration Data ////////////////////////////////////////////////////////
  439. //
  440. // A ConfigurationData object holds on to the configuration source data and
  441. // provideds a place to log any information about how the configuration was
  442. // interpreted. It also creates and destroys a handy char[] to contain the
  443. // data. To make this beastie easier to handle, we use the Named Constructor
  444. // Idiom and hide the true constructor in the private section.
  445. class ConfigurationData { // Configuration Data Source
  446. private:
  447. char* myDataBuffer; // The actual data buffer.
  448. int myBufferSize; // Size of the current buffer.
  449. int myIndex; // The current interpretation index.
  450. int myLine; // Current line number.
  451. public:
  452. ConfigurationData(const char* FileName); // Constructor from c string file name.
  453. ConfigurationData(const string FileName); // Constructor from c++ string file name.
  454. ConfigurationData(const char* Data, int Length); // Raw constructor from text buffer.
  455. ~ConfigurationData(); // Destroys the internal buffer etc.
  456. char Data(int Index); // Returns char from Data[Index]
  457. int Index(); // Reads the current Index.
  458. int Index(int i); // Changes the current Index.
  459. int Line(); // Reads the current Line number.
  460. int addNewLines(int Count); // Increments the Line number.
  461. stringstream Log; // Convenient Interpret log.
  462. };
  463. //// Configuration Translator //////////////////////////////////////////////////
  464. //
  465. // A Translator converts the contents provided to it in string form into some
  466. // other data type. The object here is a prototype for that, followed by a
  467. // collection of the basic translators used for built-in mapTo()s.
  468. class ConfigurationTranslator { // Translators exist
  469. public:
  470. virtual ~ConfigurationTranslator(){}; // Stop No Virt Dtor warnings.
  471. virtual void translate(const char* Value) = 0; // Pure virtual translator.
  472. virtual void initialize() = 0; // Pure virtual initializer.
  473. };
  474. class StringTranslator : public ConfigurationTranslator {
  475. private:
  476. string& myVariable; // Variable to map.
  477. string myInitializer; // Initial/Default value.
  478. public:
  479. StringTranslator( // Construct this with
  480. string& Variable, // the variable to map,
  481. string Inititializer); // and the default value.
  482. void translate(const char* Value); // Provide a translation method.
  483. void initialize(); // Provide an initialization method.
  484. };
  485. class IntegerTranslator : public ConfigurationTranslator {
  486. private:
  487. int& myVariable; // Variable to map.
  488. int myInitializer; // Initial/Default value.
  489. int myRadix; // Radix for strtol()
  490. public:
  491. IntegerTranslator( // Construct this with
  492. int& Variable, // the variable to map,
  493. int Inititializer, // and the default value.
  494. int Radix); // For this one we also need a Radix.
  495. void translate(const char* Value); // Provide a translation method.
  496. void initialize(); // Provide an initialization method.
  497. };
  498. class DoubleTranslator : public ConfigurationTranslator {
  499. private:
  500. double& myVariable; // Variable to map.
  501. double myInitializer; // Initial/Default value.
  502. public:
  503. DoubleTranslator( // Construct this with
  504. double& Variable, // the variable to map,
  505. double Inititializer); // and the default value.
  506. void translate(const char* Value); // Provide a translation method.
  507. void initialize(); // Provide an initialization method.
  508. };
  509. class BoolTranslator : public ConfigurationTranslator {
  510. private:
  511. bool& myVariable; // Variable to map.
  512. bool myInitializer; // Initial/Default value.
  513. public:
  514. BoolTranslator( // Construct this with
  515. bool& Variable, // the variable to map,
  516. bool Inititializer); // and the default value.
  517. void translate(const char* Value); // Provide a translation method.
  518. void initialize(); // Provide an initialization method.
  519. };
  520. //// Configuration Mnemonic ////////////////////////////////////////////////////
  521. //
  522. // A Mnemonic allows the actual contents of an element or attribute to be
  523. // exchanged for a different "named" value to help eliminate "magic numbers"
  524. // and "secret codes" from configurations. One way this might be used is to
  525. // map an enumeration to the appropriate integer values, or things like YES and
  526. // NO to boolean true and false (respectively) when turning on/off program
  527. // options.
  528. class ConfigurationMnemonic { // Mnemonics
  529. private:
  530. string myName; // What is the Mnemonic?
  531. string myValue; // What is the translation?
  532. public:
  533. ConfigurationMnemonic(string Name, string Value); // To make one, provide both parts.
  534. bool test(string Name); // Test to see if this Mnemonic matches.
  535. string Value(); // If it does then we will need it's value.
  536. };
  537. //// Configurator //////////////////////////////////////////////////////////////
  538. //
  539. // A configurator is a "functor" or "closure" or "callback" that can be used to
  540. // support sophisticated interpretation options. The most basic and necessary
  541. // of these is support for list building. Consider an object created to contain
  542. // a list of records where each record might be represented as a collection of
  543. // attributes and elements. The object would have data elements mapped to the
  544. // attributes and elements in the configuration and then control elements which
  545. // are functors for initializing the list and storing new entries as they are
  546. // completed. The object here is a pure virtual prototype.
  547. class Configurator { // Configurators exist
  548. public:
  549. virtual void operator()(ConfigurationElement& E, ConfigurationData& D) = 0; // Pure virtual configurator.
  550. virtual ~Configurator() {} // Virtual dtor keeps warnings away.
  551. };
  552. //// Include our inline methods ////////////////////////////////////////////////
  553. #include "configuration.inline.hpp"
  554. //// Utilities /////////////////////////////////////////////////////////////////
  555. // SetTrueOnComplete Configurator //////////////////////////////////////////////
  556. class ConfiguratorSetTrueOnComplete : public Configurator { // Configurator set's a boolean true.
  557. private:
  558. bool* myBoolean; // The boolean to set.
  559. public:
  560. ConfiguratorSetTrueOnComplete(); // Must init to NULL for safety.
  561. void setup(bool& Target); // Link to the target boolean.
  562. void operator()(ConfigurationElement& E, ConfigurationData& D); // Handle the operation.
  563. };
  564. #endif
  565. // End Of Include Only Once