Pol  Revision:f37d500
cfgelem.h
Go to the documentation of this file.
1 
7 #ifndef CLIB_CFGELEM_H
8 #define CLIB_CFGELEM_H
9 #include "compilerspecifics.h"
10 #include "maputil.h"
11 
12 #include <map>
13 #include <vector>
14 
15 namespace Pol
16 {
17 namespace Clib
18 {
20 {
21 public:
23  ConfigProperty( const char* name, const char* value );
24  ConfigProperty( const std::string& name, const std::string& value );
25  ConfigProperty( std::string* pname, std::string* pvalue );
26 
28 
29 protected:
30  std::string name_;
31  std::string value_;
32  friend class ConfigElem;
33  friend class VectorConfigElem;
34 };
35 
36 class ConfigSource;
37 
39 {
40 public:
41  virtual ~ConfigElemBase(){};
42  bool type_is( const char* name ) const;
43  const char* type() const;
44  const char* rest() const;
45  virtual size_t estimateSize() const;
46 
47 protected:
49 
50 protected:
51  std::string type_;
52  std::string rest_;
53 
55 };
56 
57 class ConfigElem : public ConfigElemBase
58 {
59 public:
60  ConfigElem();
61  virtual ~ConfigElem();
62  virtual size_t estimateSize() const POL_OVERRIDE;
63  friend class ConfigFile;
64 
65  bool has_prop( const char* propname ) const;
66 
67  std::string remove_string( const char* propname );
68  std::string remove_string( const char* propname, const char* dflt );
69 
70  unsigned short remove_ushort( const char* propname );
71  unsigned short remove_ushort( const char* propname, unsigned short dflt );
72 
73  int remove_int( const char* propname );
74  int remove_int( const char* propname, int dflt );
75 
76  unsigned remove_unsigned( const char* propname );
77  unsigned remove_unsigned( const char* propname, int dflt );
78 
79  unsigned int remove_ulong( const char* propname );
80  unsigned int remove_ulong( const char* propname, unsigned int dflt );
81 
82  bool remove_bool( const char* propname );
83  bool remove_bool( const char* propname, bool dflt );
84 
85  float remove_float( const char* propname, float dflt );
86  double remove_double( const char* propname, double dflt );
87 
88  void clear_prop( const char* propname );
89 
90  bool remove_first_prop( std::string* propname, std::string* value );
91  bool remove_prop( const char* propname, std::string* value );
92  bool remove_prop( const char* propname, unsigned int* plong );
93  bool remove_prop( const char* propname, unsigned short* pushort );
94 
95  bool read_prop( const char* propname, std::string* value ) const;
96 
97  // get_prop calls: don't remove, and throw if not found.
98  void get_prop( const char* propname, unsigned int* plong ) const;
99 
100  std::string read_string( const char* propname ) const;
101  std::string read_string( const char* propname, const char* dflt ) const;
102 
103 
104  void add_prop( const char* propname, const char* str );
105  void add_prop( const char* propname, unsigned int lval );
106  void add_prop( const char* propname, unsigned short sval );
107  void add_prop( const char* propname, short sval );
108 
109  POL_NORETURN void throw_error( const std::string& errmsg ) const;
110  void warn( const std::string& errmsg ) const;
111  void warn_with_line( const std::string& errmsg ) const;
112 
113  void set_rest( const char* newrest );
114  void set_type( const char* newtype );
115  void set_source( const ConfigElem& elem );
116  void set_source( const ConfigSource* source );
117 
118 protected:
119  POL_NORETURN void prop_not_found( const char* propname ) const;
120  typedef std::multimap<std::string, std::string, ci_cmp_pred> Props;
121  Props properties;
122 };
123 
125 {
126 public:
128  virtual ~VectorConfigElem();
129 
130  friend class ConfigFile;
131  bool type_is( const char* name ) const;
132  const char* type() const;
133  const char* rest() const;
134 
135  bool has_prop( const char* propname ) const;
136 
137  std::string remove_string( const char* propname );
138  std::string remove_string( const char* propname, const char* dflt );
139 
140  unsigned short remove_ushort( const char* propname );
141  unsigned short remove_ushort( const char* propname, unsigned short dflt );
142 
143  int remove_int( const char* propname );
144  int remove_int( const char* propname, int dflt );
145 
146  unsigned int remove_ulong( const char* propname );
147  unsigned int remove_ulong( const char* propname, unsigned int dflt );
148 
149  bool remove_bool( const char* propname );
150  bool remove_bool( const char* propname, bool dflt );
151 
152  float remove_float( const char* propname, float dflt );
153  double remove_double( const char* propname, double dflt );
154 
155  void clear_prop( const char* propname );
156 
157  bool remove_first_prop( std::string* propname, std::string* value );
158  bool remove_prop( const char* propname, std::string* value );
159  bool remove_prop( const char* propname, unsigned int* plong );
160  bool remove_prop( const char* propname, unsigned short* pushort );
161 
162  bool read_prop( const char* propname, std::string* value ) const;
163  std::string read_string( const char* propname ) const;
164  std::string read_string( const char* propname, const char* dflt ) const;
165 
166 
167  void add_prop( const char* propname, const char* str );
168  void add_prop( const char* propname, unsigned int lval );
169  void add_prop( const char* propname, unsigned short sval );
170 
171  POL_NORETURN void throw_error( const std::string& errmsg ) const;
172  void warn( const std::string& errmsg ) const;
173 
174  void set_rest( const char* newrest );
175  void set_source( const ConfigElem& elem );
176 
177 protected:
178  POL_NORETURN void prop_not_found( const char* propname ) const;
179  std::string type_;
180  std::string rest_;
181 
182  typedef std::vector<ConfigProperty*> Props;
183  Props properties;
184 
186 };
187 }
188 }
189 #endif
#define POL_OVERRIDE
friend class VectorConfigElem
Definition: cfgelem.h:33
friend class ConfigElem
Definition: cfgelem.h:32
std::vector< ConfigProperty * > Props
Definition: cfgelem.h:182
virtual ~ConfigElemBase()
Definition: cfgelem.h:41
#define POL_NORETURN
std::string name
Definition: osmod.cpp:943
std::multimap< std::string, std::string, ci_cmp_pred > Props
Definition: cfgelem.h:120
std::string value_
Definition: cfgelem.h:31
Definition: berror.cpp:12
const ConfigSource * _source
Definition: cfgelem.h:54
ConfigSource * source_
Definition: cfgelem.h:185