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
00029
00030
00031
00032
00033
00034
00035
00036 #ifndef __ONIKIRI_PARAM_EXCHANGE_H
00037 #define __ONIKIRI_PARAM_EXCHANGE_H
00038
00039 #include "Env/Param/ParamDB.h"
00040 #include "Env/Env.h"
00041
00042 namespace Onikiri
00043 {
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113 #define BEGIN_PARAM_MAP( relativePath ) \
00114 void ProcessParamMap(bool save){ \
00115 const ParamXMLPath& basePath = GetRootPath() + relativePath; \
00116
00117 #define BEGIN_PARAM_MAP_INDEX(relativePath, index) \
00118 void ProcessParamMap(bool save){ \
00119 const ParamXMLPath& basePath = \
00120 GetRootPath() + MakeIndexedPath( relativePath , index ); \
00121
00122 #define END_PARAM_MAP() \
00123 basePath.begin(); \
00124 } \
00125
00126 #define BEGIN_PARAM_PATH( relativePath ) \
00127 { \
00128 const ParamXMLPath& tmpPath = basePath; \
00129 const ParamXMLPath& basePath = tmpPath + relativePath; \
00130
00131 #define BEGIN_PARAM_PATH_INDEX(relativePath, index) \
00132 { \
00133 const ParamXMLPath& tmpPath = basePath; \
00134 const ParamXMLPath& basePath( \
00135 tmpPath + MakeIndexedPath( relativePath , index ) ); \
00136
00137 #define END_PARAM_PATH() \
00138 basePath.begin(); \
00139 } \
00140
00141 #define PARAM_ENTRY(relativePath, param) \
00142 { \
00143 ParamEntry((basePath), (relativePath), &(param), save); \
00144 } \
00145
00146 #define RESULT_ENTRY(relativePath, param) \
00147 if(save){ \
00148 ResultEntry((basePath), (relativePath), (param)); \
00149 } \
00150
00151 #define RESULT_RATE_ENTRY(relativePath, numerator, denominator) \
00152 if(save){ \
00153 ResultRateEntry((basePath), (relativePath), (numerator), (denominator)); \
00154 } \
00155
00156 #define RESULT_RATE_SUM_ENTRY(relativePath, numerator, denominator1, denominator2) \
00157 if(save){ \
00158 ResultRateSumEntry((basePath), (relativePath), (numerator), (denominator1), (denominator2) ); \
00159 } \
00160
00161
00162 #define CHAIN_PARAM_MAP(relativePath, param) \
00163 { \
00164 ChainParamMap((basePath), (relativePath), ¶m, save); \
00165 }
00166
00167
00168 #define CHAIN_BASE_PARAM_MAP(baseType) \
00169 { \
00170 baseType::ProcessParamMap(save); \
00171 }
00172
00173
00174
00175 #define BEGIN_PARAM_BINDING( relativePath, param, Type ) \
00176 { \
00177 Type& refParam = param; \
00178 const std::string& refRelativePath = relativePath; \
00179 typedef ParamDB::Binding<Type> Binding; \
00180 static const Binding table[] = \
00181 { \
00182
00183 #define PARAM_BINDING_ENTRY( str, value ) \
00184 { (str), (value) },
00185
00186 #define END_PARAM_BINDING() \
00187 }; \
00188 ParamBindingEntry( basePath, refRelativePath, &refParam, table, sizeof(table)/sizeof(Binding), save ); \
00189 }
00190
00191
00192 class ParamExchangeBase
00193 {
00194 protected:
00195 ParamXMLPath m_rootPath;
00196
00197 public:
00198
00199 void SetRootPath(const ParamXMLPath& root)
00200 {
00201 m_rootPath = root;
00202 }
00203
00204 const ParamXMLPath& GetRootPath()
00205 {
00206 return m_rootPath;
00207 }
00208
00209 ParamXMLPath MakeIndexedPath(const String& base, int index)
00210 {
00211 return String(
00212 base +
00213 "[" + boost::lexical_cast<String>(index) + "]/");
00214 }
00215
00216 template <typename ValueT>
00217 void ParamEntry(
00218 const ParamXMLPath& basePath,
00219 const ParamXMLPath& relativePath,
00220 ValueT* val, bool save)
00221 {
00222 if(save){
00223 g_paramDB.Set( basePath + relativePath, *val );
00224 }
00225 else{
00226 g_paramDB.Get( basePath + relativePath, val );
00227 }
00228 }
00229
00230 template < typename ValueT >
00231 void ParamBindingEntry(
00232 const ParamXMLPath& basePath,
00233 const ParamXMLPath& relativePath,
00234 ValueT* val,
00235 const ParamDB::Binding<ValueT>* bindings,
00236 int bindingsSize,
00237 bool save)
00238 {
00239 if(save){
00240 g_paramDB.Set(basePath + relativePath, *val, bindings, bindingsSize );
00241 }
00242 else{
00243 g_paramDB.Get(basePath + relativePath, val, bindings, bindingsSize );
00244 }
00245 }
00246
00247 template <typename ValueT>
00248 void ResultEntry(
00249 const ParamXMLPath& basePath,
00250 const ParamXMLPath& relativePath,
00251 const ValueT& val)
00252 {
00253 g_paramDB.Set( basePath + relativePath, val );
00254 }
00255
00256 template <typename ValueT>
00257 void ResultRateEntry(
00258 const ParamXMLPath& basePath,
00259 const ParamXMLPath& relativePath,
00260 const ValueT& numerator, const ValueT& denominator)
00261 {
00262 if( denominator != (ValueT)0 ){
00263 g_paramDB.Set( basePath + relativePath, (double)numerator / (double)denominator );
00264 }
00265 else{
00266 g_paramDB.Set( basePath + relativePath, 0 );
00267 }
00268 }
00269
00270 template <typename ValueT>
00271 void ResultRateEntry(
00272 const ParamXMLPath& basePath,
00273 const ParamXMLPath& relativePath,
00274 const std::vector<ValueT>& n, const std::vector<ValueT>& d)
00275 {
00276 std::vector<double> rate;
00277 size_t size = d.size();
00278 rate.resize( size );
00279 for( size_t i = 0; i < size; i++){
00280 if( d[i] != (ValueT)0 ){
00281 rate[i] = (double)n[i] / (double)d[i];
00282 }
00283 else{
00284 rate[i] = 0;
00285 }
00286 }
00287 g_paramDB.Set( basePath + relativePath, rate );
00288 }
00289
00290 template <typename ValueT>
00291 void ResultRateSumEntry(
00292 const ParamXMLPath& basePath,
00293 const ParamXMLPath& relativePath,
00294 const ValueT& numerator,
00295 const ValueT& denominator1,
00296 const ValueT& denominator2 )
00297 {
00298 ResultRateEntry( basePath, relativePath, numerator, denominator1 + denominator2 );
00299 }
00300
00301 template <typename ValueT>
00302 void ResultRateSumEntry(
00303 const ParamXMLPath& basePath,
00304 const ParamXMLPath& relativePath,
00305 const std::vector<ValueT>& n,
00306 const std::vector<ValueT>& d1,
00307 const std::vector<ValueT>& d2 )
00308 {
00309 std::vector<ValueT> d;
00310 size_t size = std::min( d1.size(), d2.size() );
00311 d.resize( size );
00312 for( size_t i = 0; i < size; i++){
00313 d[i] = d1[i] + d2[i];
00314 }
00315 ResultRateEntry( basePath, relativePath, n, d );
00316 }
00317
00318 template <typename T>
00319 void ChainParamMap(
00320 const ParamXMLPath& basePath,
00321 const ParamXMLPath& relativePath,
00322 T* param,
00323 bool save
00324 ){
00325 param->SetRootPath( (basePath) + (relativePath) );
00326 param->ProcessParamMap( save );
00327 }
00328
00329 template <typename T>
00330 void ChainParamMap(
00331 const ParamXMLPath& basePath,
00332 const ParamXMLPath& relativePath,
00333 std::vector<T>* param,
00334 bool save
00335 ){
00336 size_t count = g_paramDB.GetElementCount( basePath + relativePath );
00337 if(param->size() < count)
00338 param->resize(count);
00339
00340 for(size_t i = 0; i < param->size(); i++){
00341 (*param)[i].SetRootPath(
00342 MakeIndexedPath((basePath + relativePath).ToString(), (int)i) );
00343 (*param)[i].ProcessParamMap(save);
00344 }
00345
00346 }
00347
00348 virtual void ProcessParamMap(bool save) = 0;
00349
00350 ParamExchangeBase()
00351 {
00352 };
00353
00354 virtual ~ParamExchangeBase()
00355 {
00356 };
00357 };
00358
00359 class ParamExchange : public ParamExchangeBase
00360 {
00361 private:
00362 bool m_released;
00363 public:
00364 ParamExchange();
00365 virtual ~ParamExchange();
00366
00367 virtual void LoadParam();
00368 virtual void ReleaseParam();
00369 virtual bool IsParameterReleased();
00370 };
00371
00372 class ParamExchangeChild : public ParamExchangeBase
00373 {
00374 public:
00375 const ParamXMLPath& GetChildRootPath();
00376 void SetRootPath(const ParamXMLPath& root);
00377 };
00378
00379 }
00380
00381 #endif
00382
00383