Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

configuration.hpp 46KB

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