Config.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /**
  2. * @file Config.h
  3. * @brief configuration mgt
  4. * @author Erik Rodner
  5. * @date 2006-03-12
  6. */
  7. #ifndef CONFIGINCLUDE
  8. #define CONFIGINCLUDE
  9. #include <iostream>
  10. #include <map>
  11. #include <math.h>
  12. #include <string>
  13. #include <vector>
  14. #include "StructuredMap.h"
  15. #include "core/basics/Persistent.h"
  16. namespace NICE {
  17. /** @brief This class provides general configuration management. */
  18. class Config : public NICE::Persistent
  19. {
  20. public:
  21. enum {
  22. CONFIG_OVERWRITE_VALUES = 0,
  23. CONFIG_DO_NOT_OVERWRITE_VALUES
  24. };
  25. protected:
  26. /** storing all config double values */
  27. StructuredMap<double> confD;
  28. /** storing all config integer values */
  29. StructuredMap<int> confI;
  30. /** storing all config boolean valzes */
  31. StructuredMap<bool> confB;
  32. /** storing all config strings */
  33. StructuredMap<std::string> confS;
  34. void addArgBoolean ( const std::string & section, const std::string & key );
  35. /** add a setting */
  36. void addKeyValuePair ( const std::string & section,
  37. const std::string & key,
  38. const std::string & value );
  39. /** storing help text for a section/key pair */
  40. std::map<std::string, std::string> helpTexts;
  41. /** storing all aux. options (for use with argv, argc) */
  42. std::vector<std::string> moreOptions;
  43. /** read until end of file fore restore (default: yes)*/
  44. bool ioUntilEndOfFile;
  45. public:
  46. /** simplest constructor, create an empty config */
  47. Config ();
  48. /** simple constructor
  49. @param configfn config file name */
  50. Config( const std::string & configfn );
  51. /** read config data from arguments
  52. @param argc number of arguments
  53. @param argv arguments */
  54. Config ( int argc,
  55. char **argv );
  56. /** copy constructor */
  57. Config ( const Config & conf );
  58. /** simple destructor */
  59. virtual ~Config();
  60. /** get string value or abort the whole
  61. program when no value is given
  62. @param key name of the variable
  63. */
  64. std::string gS(const std::string & key) const;
  65. /** get string value or abort the whole program
  66. when no value is given
  67. @param block name of the block
  68. @param key name of the variable
  69. */
  70. std::string gS(const std::string & block, const std::string & key) const;
  71. /** get string value, if non-existant its standard
  72. value is used
  73. @param block name of the block
  74. @param key name of the variable
  75. @param defv default value of the variable
  76. */
  77. virtual std::string gS(const std::string & block, const std::string & key, const std::string & defv) const;
  78. /** */
  79. virtual double gD(const std::string & key) const;
  80. virtual double gD(const std::string & block, const std::string & key) const;
  81. virtual double gD(const std::string & block, const std::string & key, const double defv) const;
  82. virtual int gI(const std::string & key) const;
  83. virtual int gI(const std::string & block, const std::string & key) const;
  84. virtual int gI(const std::string & block, const std::string & key, const int defv) const;
  85. virtual bool gB(const std::string & key) const;
  86. virtual bool gB(const std::string & block, const std::string & key) const;
  87. virtual bool gB(const std::string & block, const std::string & key, const bool defv) const;
  88. /** } */
  89. void getAllS ( const std::string & block, std::map< std::string, std::string > & list ) const;
  90. void getAllD ( const std::string & block, std::map< std::string, double> & list ) const;
  91. void getAllI ( const std::string & block, std::map< std::string, int > & list ) const;
  92. void getAllB ( const std::string & block, std::map< std::string, bool > & list ) const;
  93. void getAllBlocks ( std::set< std::string > & list ) const;
  94. /** set values */
  95. void sD(const std::string & block, const std::string & key, const double defv);
  96. void sI(const std::string & block, const std::string & key, const int defv);
  97. void sB(const std::string & block, const std::string & key, const bool defv);
  98. void sS(const std::string & block, const std::string & key, const std::string & defv);
  99. /** read values from arguments
  100. @param argc number of arguments
  101. @param argv list c-strings as provided by main-functions
  102. */
  103. void readFromArguments ( int argc, char **argv );
  104. /** check whether a key exists
  105. @param block block of the variable
  106. @param key name of the variable
  107. */
  108. bool keyExists ( const std::string & block, const std::string & key ) const;
  109. /** add a help text */
  110. void addHelp ( const std::string & block, const std::string & key,
  111. const std::string & helpText );
  112. /** get help text */
  113. std::string help ( const std::string & block, const std::string & key ) const;
  114. /** write all values of the config */
  115. void store (std::ostream & os, int format = 0) const;
  116. /** read config values from a stream
  117. @param is input stream
  118. @param format can be CONFIG_DO_NOT_OVERWRITE_VALUES or CONFIG_OVERWRITE_VALUES
  119. */
  120. void restore (std::istream & is, int format = 0);
  121. /** clear all values */
  122. void clear ();
  123. /** get more options (command line arguments not covered by config values */
  124. const std::vector<std::string> & getMoreOptions() { return moreOptions; };
  125. /** set the flag wether we restore a config and read until end of file or until a precise statement*/
  126. void setIoUntilEndOfFile(const bool & _ioUntilEndOfFile) { ioUntilEndOfFile = _ioUntilEndOfFile;};
  127. };
  128. } // namespace
  129. #endif