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
00026
00027
00028 #ifndef CConfigFileBase_H
00029 #define CConfigFileBase_H
00030
00031 #include <mrpt/utils/utils_defs.h>
00032
00033 #include <mrpt/math/CMatrixTemplate.h>
00034
00035
00036
00037
00038 namespace mrpt
00039 {
00040 namespace utils
00041 {
00042
00043
00044
00045 class MRPTDLLIMPEXP CConfigFileBase
00046 {
00047 protected:
00048
00049
00050 virtual void writeString(const std::string §ion,const std::string &name, const std::string &str) = 0;
00051
00052
00053
00054
00055 virtual std::string readString(
00056 const std::string §ion,
00057 const std::string &name,
00058 const std::string &defaultStr,
00059 bool failIfNotFound = false) const = 0;
00060
00061 public:
00062
00063
00064 virtual ~CConfigFileBase()
00065 {
00066 }
00067
00068
00069
00070 virtual void getAllSections( vector_string §ions ) const = 0 ;
00071
00072
00073 bool sectionExists( const std::string §ion_name) const;
00074
00075
00076
00077 void write(const std::string §ion, const std::string &name, double value);
00078
00079
00080
00081 void write(const std::string §ion, const std::string &name, float value);
00082
00083
00084
00085 void write(const std::string §ion, const std::string &name, int value);
00086
00087
00088
00089 void write(const std::string §ion, const std::string &name, unsigned int value);
00090
00091
00092
00093 void write(const std::string §ion, const std::string &name, const std::string &value);
00094
00095
00096
00097 void write(const std::string §ion, const std::string &name, const std::vector<int> &value);
00098
00099
00100
00101 void write(const std::string §ion, const std::string &name, const std::vector<unsigned int> &value);
00102
00103
00104
00105 void write(const std::string §ion, const std::string &name, const std::vector<float> &value);
00106
00107
00108
00109 void write(const std::string §ion, const std::string &name, const std::vector<double> &value);
00110
00111
00112
00113 void write(const std::string §ion, const std::string &name, const std::vector<bool> &value);
00114
00115
00116
00117
00118 double read_double(const std::string §ion, const std::string &name, double defaultValue, bool failIfNotFound = false) const;
00119
00120
00121
00122
00123 float read_float(const std::string §ion, const std::string &name, float defaultValue, bool failIfNotFound = false) const;
00124
00125
00126
00127
00128 bool read_bool(const std::string §ion, const std::string &name, bool defaultValue, bool failIfNotFound = false) const;
00129
00130
00131
00132
00133 int read_int(const std::string §ion, const std::string &name, int defaultValue, bool failIfNotFound = false) const;
00134
00135
00136
00137
00138 uint64_t read_uint64_t(const std::string §ion, const std::string &name, uint64_t defaultValue, bool failIfNotFound ) const;
00139
00140
00141
00142
00143 std::string read_string(const std::string §ion, const std::string &name, const std::string &defaultValue, bool failIfNotFound = false) const;
00144
00145
00146
00147
00148 std::string read_string_first_word(const std::string §ion, const std::string &name, const std::string &defaultValue, bool failIfNotFound = false) const;
00149
00150
00151
00152
00153 void read_vector(
00154 const std::string §ion,
00155 const std::string &name,
00156 const std::vector<uint32_t> &defaultValue,
00157 std::vector<uint32_t> &outValues,
00158 bool failIfNotFound = false) const;
00159
00160
00161
00162
00163 void read_vector(
00164 const std::string §ion,
00165 const std::string &name,
00166 const std::vector<int32_t> &defaultValue,
00167 std::vector<int32_t> &outValues,
00168 bool failIfNotFound = false) const;
00169
00170
00171
00172
00173 void read_vector(
00174 const std::string §ion,
00175 const std::string &name,
00176 const std::vector<uint64_t> &defaultValue,
00177 std::vector<uint64_t> &outValues,
00178 bool failIfNotFound = false) const;
00179
00180
00181
00182
00183 void read_vector(
00184 const std::string §ion,
00185 const std::string &name,
00186 const std::vector<int64_t> &defaultValue,
00187 std::vector<int64_t> &outValues,
00188 bool failIfNotFound = false) const;
00189
00190
00191
00192
00193
00194 void read_vector(
00195 const std::string §ion,
00196 const std::string &name,
00197 const std::vector<float> &defaultValue,
00198 std::vector<float> &outValues,
00199 bool failIfNotFound = false) const;
00200
00201
00202
00203
00204 void read_vector(
00205 const std::string §ion,
00206 const std::string &name,
00207 const std::vector<double> &defaultValue,
00208 std::vector<double> &outValues,
00209 bool failIfNotFound = false ) const;
00210
00211
00212
00213
00214 void read_vector(
00215 const std::string §ion,
00216 const std::string &name,
00217 const std::vector<bool> &defaultValue,
00218 std::vector<bool> &outValues,
00219 bool failIfNotFound = false ) const;
00220
00221
00222
00223
00224
00225 template <class T>
00226 void read_matrix(
00227 const std::string §ion,
00228 const std::string &name,
00229 mrpt::math::CMatrixTemplate<T> &outMatrix,
00230 const mrpt::math::CMatrixTemplate<T> &defaultMatrix = mrpt::math::CMatrixTemplate<T>(0,0),
00231 bool failIfNotFound = false ) const;
00232
00233
00234 };
00235
00236
00237
00238
00239 #define MRPT_LOAD_CONFIG_VAR(variableName,variableType,configFileObject,sectionNameStr) \
00240 { variableName = configFileObject.read_##variableType(sectionNameStr,#variableName,variableName); }
00241
00242
00243
00244 #define MRPT_LOAD_CONFIG_VAR_DEGREES(variableName,configFileObject,sectionNameStr) \
00245 { variableName = DEG2RAD( configFileObject.read_float(sectionNameStr,#variableName, RAD2DEG(variableName)) ); }
00246
00247 #define MRPT_LOAD_CONFIG_VAR_CAST(variableName,variableType,variableTypeCast,configFileObject,sectionNameStr) \
00248 { variableName = static_cast<variableTypeCast>(configFileObject.read_##variableType(sectionNameStr,#variableName,variableName)); }
00249
00250
00251 #define MRPT_LOAD_HERE_CONFIG_VAR(variableName,variableType,targetVariable,configFileObject,sectionNameStr) \
00252 targetVariable = configFileObject.read_##variableType(sectionNameStr,#variableName,targetVariable,false);
00253
00254 #define MRPT_LOAD_HERE_CONFIG_VAR_NO_DEFAULT(variableName,variableType,targetVariable,configFileObject,sectionNameStr) \
00255 { try { \
00256 targetVariable = configFileObject.read_##variableType(sectionNameStr,#variableName,targetVariable,true); \
00257 } catch (std::exception &) \
00258 { \
00259 THROW_EXCEPTION( format( "Value for '%s' not found in config file", static_cast<const char*>(#variableName ) )); \
00260 } }\
00261
00262
00263 #define MRPT_LOAD_CONFIG_VAR_NO_DEFAULT(variableName,variableType,configFileObject,sectionNameStr) \
00264 { try { \
00265 variableName = configFileObject.read_##variableType(sectionNameStr,#variableName,variableName,true); \
00266 } catch (std::exception &) \
00267 { \
00268 THROW_EXCEPTION( format( "Value for '%s' not found in config file", static_cast<const char*>(#variableName ) )); \
00269 } }\
00270
00271 #define MRPT_LOAD_CONFIG_VAR_CAST_NO_DEFAULT(variableName,variableType,variableTypeCast,configFileObject,sectionNameStr) \
00272 { try { \
00273 variableName = static_cast<variableTypeCast>(configFileObject.read_##variableType(sectionNameStr,#variableName,variableName,true)); \
00274 } catch (std::exception &) \
00275 { \
00276 THROW_EXCEPTION( format( "Value for '%s' not found in config file", static_cast<const char*>(#variableName ) )); \
00277 } }\
00278
00279
00280 #define MRPT_LOAD_HERE_CONFIG_VAR_CAST(variableName,variableType,variableTypeCast,targetVariable,configFileObject,sectionNameStr) \
00281 targetVariable = static_cast<variableTypeCast>(configFileObject.read_##variableType(sectionNameStr,#variableName,targetVariable));
00282
00283 #define MRPT_LOAD_HERE_CONFIG_VAR_CAST_NO_DEFAULT(variableName,variableType,variableTypeCast,targetVariable,configFileObject,sectionNameStr) \
00284 { try { \
00285 targetVariable = static_cast<variableTypeCast>(configFileObject.read_##variableType(sectionNameStr,#variableName,targetVariable,true)); \
00286 } catch (std::exception &) \
00287 { \
00288 THROW_EXCEPTION( format( "Value for '%s' not found in config file", static_cast<const char*>(#variableName ) )); \
00289 } }\
00290
00291
00292 #define MRPT_SAVE_CONFIG_VAR(variableName,configFileObject,sectionNameStr) \
00293 { configFileObject.write(sectionNameStr,#variableName,variableName); }
00294
00295 #define MRPT_SAVE_CONFIG_VAR_DEGREES(variableName,configFileObject,sectionNameStr) \
00296 { configFileObject.write(sectionNameStr,#variableName, RAD2DEG(variableName)); }
00297
00298
00299 }
00300 }
00301 #endif