00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef IXE_conf_H
00026 #define IXE_conf_H
00027
00028
00029 #include "platform.h"
00030
00031
00032 #include <iostream>
00033 #include <map>
00034 #include <string>
00035
00036
00037 #include "text/less.h"
00038
00039 namespace IXE {
00040
00054 class Configuration
00055 {
00056
00057 public:
00058
00059
00060
00061
00062
00063
00064 class Item;
00065 typedef std::map<char const*, Item*> Map;
00066
00072 static void load(char const* file_name);
00073
00078 static void load(std::istream& is, Map* parameters = 0,
00079 char const* = "</desr>");
00080
00081 static Map& variables();
00082
00086 static void reset();
00087
00088 static std::ostream& error(std::ostream& o = std::cerr) {
00089 return msg(o, "error");
00090 }
00091 static std::ostream& warning(std::ostream& o = std::cerr) {
00092 return msg(o, "warning");
00093 }
00094
00095 private:
00096 static int current_config_file_line_no;
00097
00101 static void insert(Item* var);
00102
00113 static void parseLine(char const*& line, int& line_no,
00114 Map* vars = 0);
00115
00125 static std::ostream& msg(std::ostream&, char const* label);
00126
00127 public:
00128
00138 friend class Item;
00139 class Item
00140 {
00141 public:
00142 char const* name() const { return name_; }
00143
00144 virtual void serialize(std::ostream& os) {}
00145
00146 protected:
00147 Item(char const* name) : name_(name) {
00148 Configuration::insert(this);
00149 }
00150
00151 virtual ~Item() { };
00152
00153 virtual void parseValue(char const*& line) = 0;
00154
00155
00156
00157 virtual void reset() = 0;
00158
00159
00160
00161
00162 friend class Configuration;
00163
00164 private:
00165
00166 char const* const name_;
00167 };
00168 };
00169
00175 template <class T>
00176 class Var : public Configuration::Item
00177 {
00178 public:
00179 typedef T value_type;
00180
00181 Var(char const* name) : Item(name) { }
00182
00183 inline operator T() const { return this->value; }
00184 inline operator T*() const { return &this->value; }
00185
00186 T& operator *() { return this->value; }
00187 T* operator ->() { return &this->value; }
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00208 void serialize(std::ostream& os) {
00209 os << name() << '\t' << value << std::endl;
00210 }
00211
00212 T value;
00213
00214 protected:
00215 Var(char const* name, T value) :
00216 Item(name), value(value) { }
00217
00218 };
00219
00225 template <class T> class VarDefault : public Var<T>
00226 {
00227 VarDefault() { }
00228
00229 public:
00230
00231 T Default() { return default_value; }
00232
00233 protected:
00234 VarDefault(char const *name, T default_value) :
00235 Var<T>(name, default_value), default_value(default_value) { }
00236
00237 virtual void reset() { Var<T>::value = default_value; }
00238
00239 private:
00240 T default_value;
00241 };
00242
00243
00244 template <class T> class conf : public Configuration::Item { };
00245
00255 char const* next_token_line(char const*& start, const char* sep,
00256 char esc = '\\');
00257 }
00258
00259 #endif