StructuredMap.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * @file Config.h
  3. * @brief configuration mgt
  4. * @author Erik Rodner
  5. * @date 2006-03-12
  6. */
  7. #ifndef STRUCTUREDINCLUDE
  8. #define STRUCTUREDINCLUDE
  9. #include <iostream>
  10. #include <map>
  11. #include <string>
  12. #include <algorithm>
  13. #include <functional>
  14. #include <cctype>
  15. #include <set>
  16. #include <math.h>
  17. #include "core/basics/Persistent.h"
  18. namespace NICE {
  19. template<class ValueType>
  20. /** This class provides a structured map. */
  21. class StructuredMap : public NICE::Persistent
  22. {
  23. protected:
  24. typedef typename std::map<std::string, ValueType> MapValueType;
  25. typedef typename std::map<std::string, ValueType>::const_iterator MapValueTypeCIterator;
  26. MapValueType conf;
  27. public:
  28. bool keyExists ( const std::string & block, const std::string & key ) const
  29. {
  30. std::string newkey = block + "::" + key;
  31. MapValueTypeCIterator map_i = conf.find(newkey);
  32. return ( map_i != conf.end() );
  33. }
  34. bool find ( const std::string & block, const std::string & key, ValueType & value ) const
  35. {
  36. std::string newkey = block + "::" + key;
  37. MapValueTypeCIterator map_i = conf.find(newkey);
  38. if ( map_i == conf.end() ) return false;
  39. value = map_i->second;
  40. return true;
  41. }
  42. void store ( const std::string & block, const std::string & key, const ValueType & value )
  43. {
  44. std::string newkey = block + "::" + key;
  45. conf[newkey] = value;
  46. }
  47. void getAll ( const std::string & block, std::map<std::string, ValueType> & list ) const
  48. {
  49. // FIXME: optimize this !
  50. for ( MapValueTypeCIterator map_i = conf.begin(); map_i != conf.end() ; map_i++ )
  51. {
  52. std::string key = map_i->first;
  53. ValueType val = map_i->second;
  54. if ( key.find (block + "::") == 0 )
  55. {
  56. size_t dpos = key.find_first_of(':', 0);
  57. size_t dend = key.find_first_not_of(':', dpos);
  58. list[key.substr(dend)] = val;
  59. }
  60. }
  61. }
  62. void getAllBlocks ( std::set<std::string> & blocks ) const
  63. {
  64. for ( MapValueTypeCIterator map_i = conf.begin(); map_i != conf.end() ; map_i++ )
  65. {
  66. std::string key = map_i->first;
  67. size_t dpos = key.find_first_of(':', 0);
  68. std::string block = key.substr(0,dpos);
  69. if ( blocks.find(block) == blocks.end() )
  70. blocks.insert(block);
  71. }
  72. }
  73. size_t size () const
  74. {
  75. return conf.size();
  76. }
  77. void restore (std::istream & is, int format = 0)
  78. {
  79. fthrow ( Exception, "StructuredMap: restore function of structured map not implemented\n" );
  80. }
  81. void clear()
  82. {
  83. conf.clear();
  84. }
  85. void store ( std::ostream & os, int format = 0 ) const
  86. {
  87. std::string lastblock = "";
  88. for ( MapValueTypeCIterator i = conf.begin();
  89. i != conf.end();
  90. i++ )
  91. {
  92. std::string k = i->first;
  93. size_t dpos = k.find_first_of(':', 0);
  94. size_t dend = k.find_first_not_of(':', dpos);
  95. std::string block = k.substr(0,dpos);
  96. std::string key = k.substr(dend);
  97. ValueType value = i->second;
  98. if ( block.compare(lastblock) != 0 ) {
  99. os << std::endl << "[" << block << "]" << std::endl;
  100. }
  101. os << key << " = " << value << std::endl;
  102. lastblock = block;
  103. }
  104. }
  105. void copyFrom ( const StructuredMap & map )
  106. {
  107. conf.clear();
  108. conf = map.conf;
  109. }
  110. };
  111. } // namespace
  112. #endif