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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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. #pragma once
  105. #include <string>
  106. #include <vector>
  107. #include <sstream>
  108. #include <fstream>
  109. #include <cstring>
  110. #include <cstdlib>
  111. #include <list>
  112. namespace codedweller {
  113. class ConfigurationElement; // Elements exist
  114. class ConfigurationAttribute; // Attributes exist
  115. class ConfigurationData; // Data exists
  116. class ConfigurationTranslator; // Translators exist
  117. class ConfigurationMnemonic; // Mnemonics exist
  118. class Configurator; // Configurators exist
  119. //// Configuration Element /////////////////////////////////////////////////////
  120. //
  121. // Elements make up the core of a configuration. That is, a configuration is a
  122. // tree of elements. Elements translate directly to well formed xml elements in
  123. // a configuration file or string.
  124. class ConfigurationElement {
  125. private:
  126. std::string myName; // Elements have a name.
  127. // External important things I remember but don't touch...
  128. ConfigurationElement* myParent; // They may have a parrent.
  129. std::list<Configurator*> myStartConfigurators; // Call these when we start Interpret()
  130. std::list<Configurator*> myEndConfigurators; // Call these when we finish Interpret()
  131. // Internal / subordinate things I own and kill...
  132. std::list<ConfigurationAttribute*> myAttributes; // They may have a list of attributes.
  133. std::list<ConfigurationElement*> myElements; // They may have a list of sub-elements.
  134. std::list<ConfigurationMnemonic*> myMnemonics; // They may have a list of mnemonics.
  135. std::list<ConfigurationTranslator*> myTranslators; // They may have a list of translators.
  136. // During Interpret() operations we keep track of where we are seen...
  137. int myLine; // Last line number I was seen on.
  138. int myIndex; // Last char position I was seen on.
  139. int myLength; // Last segment length.
  140. bool myCleanFlag; // Keep track of initialization.
  141. bool myInitOnInterpretFlag; // Initialize() at each Interpret()?
  142. void runStartConfigurators(ConfigurationData& D); // Does what it says ;-)
  143. void runEndConfigurators(ConfigurationData& D); // Does what it says ;-)
  144. public:
  145. ConfigurationElement(const char* Name); // Must be constructed with a name
  146. ConfigurationElement(const std::string Name); // either c string or c++ string.
  147. ConfigurationElement(const char* Name, ConfigurationElement& Parent); // Sub-elements are constructed with a
  148. ConfigurationElement(const std::string Name, ConfigurationElement& Parent); // parrent.
  149. // Upon desctruction an element will delete all subordinate objects:
  150. // * All sub element objects.
  151. // * All attribute objects.
  152. // * All mnemonic objects.
  153. // * All translator objects.
  154. // It is important to use new when passing one of these objects to an
  155. // element or attribute to prevent problems with the delete operation.
  156. // NORMALLY these things would be created using factory methods on the
  157. // element and attribute objects themselves - so be careful.
  158. // It will not delete Configurators - they must
  159. // be deleted elsewhere because they may have been
  160. // re-used and this element wouldn't know about it ;-)
  161. ~ConfigurationElement(); // The descrutor clears and deletes all!
  162. // Elements can be probed for some simple, useful things.
  163. std::string Name(); // Get the name of this element.
  164. ConfigurationElement& Parent(); // Get the parent of this element.
  165. ConfigurationElement& Parent(ConfigurationElement& newParent); // Set the parent of this element.
  166. // Note - if there is no parent (an element is the root) then it will
  167. // return a reference to itself when Parent() is called.
  168. int Line(); // Get the last line number.
  169. int Index(); // Get the last data position.
  170. int Length(); // Get the last length.
  171. // Elements can contain either data or sub-elements.
  172. ConfigurationElement& Element(const char* Name); // Add a new sub element by c string name.
  173. ConfigurationElement& Element(const std::string Name); // Add a new sub element by c++ string name.
  174. //// Mapping element factory methods for convenience.
  175. //// Root-Node elements are _usually_ empty and without attributes in xml
  176. //// so we don't make any of that type of convenience constructor here.
  177. // char* versions
  178. ConfigurationElement& Element( // Mapping factory for convenience,
  179. const char* Name, // requires a name, of course,
  180. ConfigurationTranslator& newTranslator); // Add a Translator to this element.
  181. ConfigurationElement& Element( // Mapping factory for convenience,
  182. const char* Name, // requires a name, of course,
  183. std::string& x, std::string init = std::string("")); // Map to a string.
  184. ConfigurationElement& Element( // Mapping factory for convenience,
  185. const char* Name, // requires a name, of course,
  186. int& x, int init = 0, int radix = 0); // Map to an int.
  187. ConfigurationElement& Element( // Mapping factory for convenience,
  188. const char* Name, // requires a name, of course,
  189. double& x, double init = 0.0); // Map to a double.
  190. ConfigurationElement& Element( // Mapping factory for convenience,
  191. const char* Name, // requires a name, of course,
  192. bool& x, bool init = false); // Map to a boolean.
  193. // string versions
  194. ConfigurationElement& Element( // Mapping factory for convenience,
  195. const std::string Name, // requires a name, of course,
  196. ConfigurationTranslator& newTranslator); // Add a Translator to this element.
  197. ConfigurationElement& Element( // Mapping factory for convenience,
  198. const std::string Name, // requires a name, of course,
  199. std::string& x, std::string init = std::string("")); // Map to a string.
  200. ConfigurationElement& Element( // Mapping factory for convenience,
  201. const std::string Name, // requires a name, of course,
  202. int& x, int init = 0, int radix = 0); // Map to an int.
  203. ConfigurationElement& Element( // Mapping factory for convenience,
  204. const std::string Name, // requires a name, of course,
  205. double& x, double init = 0.0); // Map to a double.
  206. ConfigurationElement& Element( // Mapping factory for convenience,
  207. const std::string Name, // requires a name, of course,
  208. bool& x, bool init = false); // Map to a boolean.
  209. // End methods for heading back up the tree at the end of an element.
  210. class EndNameDoesNotMatch {}; // Throw when End(name) doesn't match.
  211. ConfigurationElement& End(); // Return this element's parent.
  212. ConfigurationElement& End(const char* Name); // Check the name and return the parent
  213. ConfigurationElement& End(const std::string Name); // if the name is correct - or throw!
  214. // Elements can have attributes.
  215. ConfigurationAttribute& Attribute(const char* Name); // Add an attribute using a cstring.
  216. ConfigurationAttribute& Attribute(const std::string Name); // Add an attribute using a c++ string.
  217. //// Mapping Attribute factory methods for convenience.
  218. // char* versions
  219. ConfigurationAttribute& Attribute( // Mapping factory for convenience,
  220. const char* Name, // requires a name, of course,
  221. ConfigurationTranslator& newTranslator); // Add a Translator to this element.
  222. ConfigurationAttribute& Attribute( // Mapping factory for convenience,
  223. const char* Name, // requires a name, of course,
  224. std::string& x, std::string init = std::string("")); // Map to a string.
  225. ConfigurationAttribute& Attribute( // Mapping factory for convenience,
  226. const char* Name, // requires a name, of course,
  227. int& x, int init = 0, int radix = 0); // Map to an int.
  228. ConfigurationAttribute& Attribute( // Mapping factory for convenience,
  229. const char* Name, // requires a name, of course,
  230. double& x, double init = 0.0); // Map to a double.
  231. ConfigurationAttribute& Attribute( // Mapping factory for convenience,
  232. const char* Name, // requires a name, of course,
  233. bool& x, bool init = false); // Map to a boolean.
  234. // string versions
  235. ConfigurationAttribute& Attribute( // Mapping factory for convenience,
  236. const std::string Name, // requires a name, of course,
  237. ConfigurationTranslator& newTranslator); // Add a Translator to this element.
  238. ConfigurationAttribute& Attribute( // Mapping factory for convenience,
  239. const std::string Name, // requires a name, of course,
  240. std::string& x, std::string init = std::string("")); // Map to a string.
  241. ConfigurationAttribute& Attribute( // Mapping factory for convenience,
  242. const std::string Name, // requires a name, of course,
  243. int& x, int init = 0, int radix = 0); // Map to an int.
  244. ConfigurationAttribute& Attribute( // Mapping factory for convenience,
  245. const std::string Name, // requires a name, of course,
  246. double& x, double init = 0.0); // Map to a double.
  247. ConfigurationAttribute& Attribute( // Mapping factory for convenience,
  248. const std::string Name, // requires a name, of course,
  249. bool& x, bool init = false); // Map to a boolean.
  250. // Elements can Initialize() at each Interpret() call.
  251. ConfigurationElement& setInitOnInterpret(); // Set the init on interpret flag.
  252. // Elements can call external functions to aid in special operations
  253. // such as building lists.
  254. ConfigurationElement& atStartCall(Configurator& Functor); // Add an atStart call-back to this element.
  255. ConfigurationElement& atEndCall(Configurator& Functor); // Add an atEnd call-back to this element.
  256. // Extracting data from the element's contents is done with
  257. // translators. A good set of primatives are built in, but the user
  258. // can also make their own. If an Element is mapped to more than
  259. // one then they are all called once the element's contents are
  260. // collected. A translator takes the data provided by the element,
  261. // converts it into the expected type, and sets one or more variables
  262. // to the converted value. Usually - just one variable.
  263. ConfigurationElement& mapTo(ConfigurationTranslator& newTranslator); // Add a Translator to this element.
  264. ConfigurationElement& mapTo(std::string& x, std::string init = std::string("")); // Map to a string.
  265. ConfigurationElement& mapTo(int& x, int init = 0, int radix = 0); // Map to an int.
  266. ConfigurationElement& mapTo(double& x, double init = 0.0); // Map to a double.
  267. ConfigurationElement& mapTo(bool& x, bool init = false); // Map to a boolean.
  268. // An Element's contents may use some special mnemonics to make a
  269. // configuration easier to understand and less error prone. When the
  270. // contents match a mnemnoic then the translation of the mnemonic is
  271. // passed to the Translators instead of the raw contents.
  272. ConfigurationElement& Mnemonic(const char* name, const char* value); // Add a mnemonic using c strings.
  273. ConfigurationElement& Mnemonic(const char* name, const std::string value); // Add a mnemonic using c & c++ strings.
  274. ConfigurationElement& Mnemonic(const std::string name, const char* value); // Add a mnemonic using c++ & c strings.
  275. ConfigurationElement& Mnemonic(const std::string name, const std::string value); // Add a mnemonic using c++ strings.
  276. // The way data gets into an element tree is that it is Interpret()ed
  277. // recursively. The data is loaded into a ConfigurationData object which
  278. // is passed to the top Element. That element interpretes the data, moves
  279. // the interpretation pointers, and passes the data on to it's subordinate
  280. // elements in turn. They do the same recursively. When the last sub -
  281. // element has had it's way with the data, the interpretation process is
  282. // complete. The ConfigurationData object will contain the original data
  283. // and a log of anything that happened during the interpretation process.
  284. //
  285. // Each time an element is asked to Interpret() data, it calls any atStart
  286. // configurators, translates any attributes, then either translates it's
  287. // contents or passes the data to it's children, then calls any atEnd
  288. // configurators.
  289. //
  290. // To ensure that the correct default values are used the Initialize() is
  291. // always called on all internal attributes and elements before any data is
  292. // interpreted. To prevent this from being inefficient, a boolean flag is
  293. // kept in each element to keep track of whether it is clean and if it is
  294. // then the call to Initialize will simply return (skipping subordinate
  295. // elements along the way).
  296. //
  297. // Interpret returns true if this object found itself at the current
  298. // Data.Index and false if not. This helps keep the recursive parsing
  299. // code simpler ;-)
  300. void initialize(); // Reset all translators to defaults.
  301. void notifyDirty(); // Set dirty (if translators change).
  302. bool interpret(ConfigurationData& Data); // (re) Interpret this data.
  303. };
  304. //// Configuration Attribute ///////////////////////////////////////////////////
  305. //
  306. // Attributes translate directly to well formed xml attributes (within the
  307. // start tag of an element).
  308. class ConfigurationAttribute {
  309. private:
  310. std::string myName; // Elements have a name.
  311. ConfigurationElement& myParent; // They may have a parrent.
  312. std::list<ConfigurationMnemonic*> myMnemonics; // They may have a list of mnemonics.
  313. std::list<ConfigurationTranslator*> myTranslators; // They may have a list of translators.
  314. int myLine; // Last line number I was seen on.
  315. int myIndex; // Last char position I was seen on.
  316. int myLength; // Last segment length.
  317. public:
  318. ConfigurationAttribute(const char* Name, ConfigurationElement& Parent); // Sub-elements are constructed with a
  319. ConfigurationAttribute(const std::string Name, ConfigurationElement& Parent); // parrent.
  320. // Attributes delete their Mnemonics and Translators when they go.
  321. // See Elements for similar warnings about objects provided to
  322. // this object... you must use new to be safe, or better yet - stick to
  323. // the built in factory methods ;-)
  324. ~ConfigurationAttribute(); // Crush, Kill, Destroy!
  325. // Attributes can be probed for some simple, useful things.
  326. std::string Name(); // Get the name of this attribute.
  327. ConfigurationElement& Parent(); // Get the parent of this attribute.
  328. int Line(); // Get the last line number.
  329. int Index(); // Get the last data position.
  330. int Length(); // Get the last length.
  331. void notifyDirty(); // Attributes use this when they change.
  332. // For convenience in building configurations, an Attribute offers
  333. // some call-through methods to it's parrent Element. This allows for
  334. // clear, concise .method() coding that mimics an outline of the
  335. // configuration structure.
  336. //// For switching back to the parent element and adding new sub-elements.
  337. ConfigurationElement& Element(const char* Name); // Add a new sub element by c string name.
  338. ConfigurationElement& Element(const std::string Name); // Add a new sub element by c++ string name.
  339. //// Mapping element factory methods for convenience.
  340. //// Root-Node elements are _usually_ empty and without attributes in xml
  341. //// so we don't make any of that type of convenience constructor here.
  342. // char* versions
  343. ConfigurationElement& Element( // Mapping factory for convenience,
  344. const char* Name, // requires a name, of course,
  345. ConfigurationTranslator& newTranslator); // Add a Translator to this element.
  346. ConfigurationElement& Element( // Mapping factory for convenience,
  347. const char* Name, // requires a name, of course,
  348. std::string& x, std::string init = std::string("")); // Map to a string.
  349. ConfigurationElement& Element( // Mapping factory for convenience,
  350. const char* Name, // requires a name, of course,
  351. int& x, int init = 0, int radix = 0); // Map to an int.
  352. ConfigurationElement& Element( // Mapping factory for convenience,
  353. const char* Name, // requires a name, of course,
  354. double& x, double init = 0.0); // Map to a double.
  355. ConfigurationElement& Element( // Mapping factory for convenience,
  356. const char* Name, // requires a name, of course,
  357. bool& x, bool init = false); // Map to a boolean.
  358. // string versions
  359. ConfigurationElement& Element( // Mapping factory for convenience,
  360. const std::string Name, // requires a name, of course,
  361. ConfigurationTranslator& newTranslator); // Add a Translator to this element.
  362. ConfigurationElement& Element( // Mapping factory for convenience,
  363. const std::string Name, // requires a name, of course,
  364. std::string& x, std::string init = std::string("")); // Map to a string.
  365. ConfigurationElement& Element( // Mapping factory for convenience,
  366. const std::string Name, // requires a name, of course,
  367. int& x, int init = 0, int radix = 0); // Map to an int.
  368. ConfigurationElement& Element( // Mapping factory for convenience,
  369. const std::string Name, // requires a name, of course,
  370. double& x, double init = 0.0); // Map to a double.
  371. ConfigurationElement& Element( // Mapping factory for convenience,
  372. const std::string Name, // requires a name, of course,
  373. bool& x, bool init = false); // Map to a boolean.
  374. // End methods for heading back up the tree at the end of an element.
  375. ConfigurationElement& End(); // Return this element's parent.
  376. ConfigurationElement& End(const char* Name); // Check the name and return the parent
  377. ConfigurationElement& End(const std::string Name); // if the name is correct - or throw!
  378. //// For adding new attributes to the parent element.
  379. ConfigurationAttribute& Attribute(const char* Name); // Add an attribute using a cstring.
  380. ConfigurationAttribute& Attribute(const std::string Name); // Add an attribute using a c++ string.
  381. //// Mapping Attribute factory methods for convenience.
  382. // char* versions
  383. ConfigurationAttribute& Attribute( // Mapping factory for convenience,
  384. const char* Name, // requires a name, of course,
  385. ConfigurationTranslator& newTranslator); // Add a Translator to this element.
  386. ConfigurationAttribute& Attribute( // Mapping factory for convenience,
  387. const char* Name, // requires a name, of course,
  388. std::string& x, std::string init = std::string("")); // Map to a string.
  389. ConfigurationAttribute& Attribute( // Mapping factory for convenience,
  390. const char* Name, // requires a name, of course,
  391. int& x, int init = 0, int radix = 0); // Map to an int.
  392. ConfigurationAttribute& Attribute( // Mapping factory for convenience,
  393. const char* Name, // requires a name, of course,
  394. double& x, double init = 0.0); // Map to a double.
  395. ConfigurationAttribute& Attribute( // Mapping factory for convenience,
  396. const char* Name, // requires a name, of course,
  397. bool& x, bool init = false); // Map to a boolean.
  398. // string versions
  399. ConfigurationAttribute& Attribute( // Mapping factory for convenience,
  400. const std::string Name, // requires a name, of course,
  401. ConfigurationTranslator& newTranslator); // Add a Translator to this element.
  402. ConfigurationAttribute& Attribute( // Mapping factory for convenience,
  403. const std::string Name, // requires a name, of course,
  404. std::string& x, std::string init = std::string("")); // Map to a string.
  405. ConfigurationAttribute& Attribute( // Mapping factory for convenience,
  406. const std::string Name, // requires a name, of course,
  407. int& x, int init = 0, int radix = 0); // Map to an int.
  408. ConfigurationAttribute& Attribute( // Mapping factory for convenience,
  409. const std::string Name, // requires a name, of course,
  410. double& x, double init = 0.0); // Map to a double.
  411. ConfigurationAttribute& Attribute( // Mapping factory for convenience,
  412. const std::string Name, // requires a name, of course,
  413. bool& x, bool init = false); // Map to a boolean.
  414. //// Set Init On Interprete for the parent element.
  415. ConfigurationElement& setInitOnInterpret(); // Set the init on interpret flag.
  416. //// For adding configurators to the parent element.
  417. ConfigurationElement& atStartCall(Configurator& Functor); // Add an atStart call-back to this element.
  418. ConfigurationElement& atEndCall(Configurator& Functor); // Add an atEnd call-back to this element.
  419. // Of course, the most useful thing about attributes is that they can
  420. // be mapped to variables using translators. The same as those that
  421. // apply to the parent element's contents. Here they are for use on this
  422. // attribute.
  423. ConfigurationAttribute& mapTo(ConfigurationTranslator& newTranslator); // Add a Translator to this attribute.
  424. ConfigurationAttribute& mapTo(std::string& x, std::string init = std::string("")); // Map to a string.
  425. ConfigurationAttribute& mapTo(int& x, int init, int radix = 0); // Map to an int.
  426. ConfigurationAttribute& mapTo(double& x, double init = 0.0); // Map to a double.
  427. ConfigurationAttribute& mapTo(bool& x, bool init = false); // Map to a boolean.
  428. // Attributes can have mnemonics just like elements.
  429. ConfigurationAttribute& Mnemonic(const char* name, const char* value); // Add a mnemonic using a c string.
  430. ConfigurationAttribute& Mnemonic(const char* name, const std::string value); // Add a mnemonic using c & c++ strings.
  431. ConfigurationAttribute& Mnemonic(const std::string name, const char* value); // Add a mnemonic using c++ & c strings.
  432. ConfigurationAttribute& Mnemonic(const std::string name, const std::string value); // Add a mnemonic using a c++ string.
  433. // Attributes participate in the Interprete() task just like elements.
  434. void initialize(); // Reset all translators to defaults.
  435. bool interpret(ConfigurationData& Data); // (re) Interpret this data.
  436. };
  437. //// Configuration Data ////////////////////////////////////////////////////////
  438. //
  439. // A ConfigurationData object holds on to the configuration source data and
  440. // provideds a place to log any information about how the configuration was
  441. // interpreted. It also creates and destroys a handy char[] to contain the
  442. // data. To make this beastie easier to handle, we use the Named Constructor
  443. // Idiom and hide the true constructor in the private section.
  444. class ConfigurationData { // Configuration Data Source
  445. private:
  446. char* myDataBuffer; // The actual data buffer.
  447. int myBufferSize; // Size of the current buffer.
  448. int myIndex; // The current interpretation index.
  449. int myLine; // Current line number.
  450. public:
  451. ConfigurationData(const char* FileName); // Constructor from c string file name.
  452. ConfigurationData(const std::string FileName); // Constructor from c++ string file name.
  453. ConfigurationData(const char* Data, int Length); // Raw constructor from text buffer.
  454. ~ConfigurationData(); // Destroys the internal buffer etc.
  455. char Data(int Index); // Returns char from Data[Index]
  456. int Index(); // Reads the current Index.
  457. int Index(int i); // Changes the current Index.
  458. int Line(); // Reads the current Line number.
  459. int addNewLines(int Count); // Increments the Line number.
  460. std::stringstream Log; // Convenient Interpret log.
  461. };
  462. //// Configuration Translator //////////////////////////////////////////////////
  463. //
  464. // A Translator converts the contents provided to it in string form into some
  465. // other data type. The object here is a prototype for that, followed by a
  466. // collection of the basic translators used for built-in mapTo()s.
  467. class ConfigurationTranslator { // Translators exist
  468. public:
  469. virtual ~ConfigurationTranslator(){}; // Stop No Virt Dtor warnings.
  470. virtual void translate(const char* Value) = 0; // Pure virtual translator.
  471. virtual void initialize() = 0; // Pure virtual initializer.
  472. };
  473. class StringTranslator : public ConfigurationTranslator {
  474. private:
  475. std::string& myVariable; // Variable to map.
  476. std::string myInitializer; // Initial/Default value.
  477. public:
  478. StringTranslator( // Construct this with
  479. std::string& Variable, // the variable to map,
  480. std::string Inititializer); // and the default value.
  481. void translate(const char* Value); // Provide a translation method.
  482. void initialize(); // Provide an initialization method.
  483. };
  484. class IntegerTranslator : public ConfigurationTranslator {
  485. private:
  486. int& myVariable; // Variable to map.
  487. int myInitializer; // Initial/Default value.
  488. int myRadix; // Radix for strtol()
  489. public:
  490. IntegerTranslator( // Construct this with
  491. int& Variable, // the variable to map,
  492. int Inititializer, // and the default value.
  493. int Radix); // For this one we also need a Radix.
  494. void translate(const char* Value); // Provide a translation method.
  495. void initialize(); // Provide an initialization method.
  496. };
  497. class DoubleTranslator : public ConfigurationTranslator {
  498. private:
  499. double& myVariable; // Variable to map.
  500. double myInitializer; // Initial/Default value.
  501. public:
  502. DoubleTranslator( // Construct this with
  503. double& Variable, // the variable to map,
  504. double Inititializer); // and the default value.
  505. void translate(const char* Value); // Provide a translation method.
  506. void initialize(); // Provide an initialization method.
  507. };
  508. class BoolTranslator : public ConfigurationTranslator {
  509. private:
  510. bool& myVariable; // Variable to map.
  511. bool myInitializer; // Initial/Default value.
  512. public:
  513. BoolTranslator( // Construct this with
  514. bool& Variable, // the variable to map,
  515. bool Inititializer); // and the default value.
  516. void translate(const char* Value); // Provide a translation method.
  517. void initialize(); // Provide an initialization method.
  518. };
  519. //// Configuration Mnemonic ////////////////////////////////////////////////////
  520. //
  521. // A Mnemonic allows the actual contents of an element or attribute to be
  522. // exchanged for a different "named" value to help eliminate "magic numbers"
  523. // and "secret codes" from configurations. One way this might be used is to
  524. // map an enumeration to the appropriate integer values, or things like YES and
  525. // NO to boolean true and false (respectively) when turning on/off program
  526. // options.
  527. class ConfigurationMnemonic { // Mnemonics
  528. private:
  529. std::string myName; // What is the Mnemonic?
  530. std::string myValue; // What is the translation?
  531. public:
  532. ConfigurationMnemonic(std::string Name, std::string Value); // To make one, provide both parts.
  533. bool test(std::string Name); // Test to see if this Mnemonic matches.
  534. std::string Value(); // If it does then we will need it's value.
  535. };
  536. //// Configurator //////////////////////////////////////////////////////////////
  537. //
  538. // A configurator is a "functor" or "closure" or "callback" that can be used to
  539. // support sophisticated interpretation options. The most basic and necessary
  540. // of these is support for list building. Consider an object created to contain
  541. // a list of records where each record might be represented as a collection of
  542. // attributes and elements. The object would have data elements mapped to the
  543. // attributes and elements in the configuration and then control elements which
  544. // are functors for initializing the list and storing new entries as they are
  545. // completed. The object here is a pure virtual prototype.
  546. class Configurator { // Configurators exist
  547. public:
  548. virtual void operator()(ConfigurationElement& E, ConfigurationData& D) = 0; // Pure virtual configurator.
  549. virtual ~Configurator() {} // Virtual dtor keeps warnings away.
  550. };
  551. //// Utilities /////////////////////////////////////////////////////////////////
  552. // SetTrueOnComplete Configurator //////////////////////////////////////////////
  553. class ConfiguratorSetTrueOnComplete : public Configurator { // Configurator set's a boolean true.
  554. private:
  555. bool* myBoolean; // The boolean to set.
  556. public:
  557. ConfiguratorSetTrueOnComplete(); // Must init to NULL for safety.
  558. void setup(bool& Target); // Link to the target boolean.
  559. void operator()(ConfigurationElement& E, ConfigurationData& D); // Handle the operation.
  560. };
  561. } // End namespace codedweller