src/Env/Param/ParamExchange.h

説明を見る。
00001 // 
00002 // Copyright (c) 2005-2008 Kenichi Watanabe.
00003 // Copyright (c) 2005-2008 Yasuhiro Watari.
00004 // Copyright (c) 2005-2008 Hironori Ichibayashi.
00005 // Copyright (c) 2008-2009 Kazuo Horio.
00006 // Copyright (c) 2009-2013 Naruki Kurata.
00007 // Copyright (c) 2005-2013 Ryota Shioya.
00008 // Copyright (c) 2005-2013 Masahiro Goshima.
00009 // 
00010 // This software is provided 'as-is', without any express or implied
00011 // warranty. In no event will the authors be held liable for any damages
00012 // arising from the use of this software.
00013 // 
00014 // Permission is granted to anyone to use this software for any purpose,
00015 // including commercial applications, and to alter it and redistribute it
00016 // freely, subject to the following restrictions:
00017 // 
00018 // 1. The origin of this software must not be misrepresented; you must not
00019 // claim that you wrote the original software. If you use this software
00020 // in a product, an acknowledgment in the product documentation would be
00021 // appreciated but is not required.
00022 // 
00023 // 2. Altered source versions must be plainly marked as such, and must not be
00024 // misrepresented as being the original software.
00025 // 
00026 // 3. This notice may not be removed or altered from any source
00027 // distribution.
00028 // 
00029 // 
00030 
00031 
00032 //
00033 // Parameter data exchange
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 Typical usage scenario
00049 
00050 <?xml version='1.0' encoding='UTF-8'?>
00051 <Session>
00052     <Simulator>
00053         <Fetcher
00054             Width = "4"
00055         />
00056         <Core
00057             Param = "1"
00058         />
00059         <Core
00060             Param = "2"
00061         />
00062     </Simulator>
00063 </Session>
00064 ----
00065 
00066 class ParamTest : public ParamExchange
00067 {
00068     int m_paramFetcher;
00069     int m_paramCore;
00070     int m_coreIndex;
00071     enum Policy 
00072     {
00073         POLICY_REFETCH,         
00074         POLICY_REISSUE_ALL,     
00075         POLICY_REISSUE_SELECTIVE,   
00076     };
00077     Policy m_policy;
00078 
00079 public:
00080     ParamTest() 
00081     {   
00082         m_coreIndex = 0;
00083         LoadParam();    
00084     }
00085     ~ParamTest()
00086     {
00087         ReleaseParam(); 
00088     }
00089 
00090     BEGIN_PARAM_MAP("/Session/Simulator/")
00091         
00092         BEGIN_PARAM_PATH("Fetcher/")
00093             
00094             PARAM_ENTRY("@Width", m_paramFetcher)
00095         
00096             BEGIN_PARAM_BINDING(  "@Policy", m_policy, Policy )
00097                 PARAM_BINDING_ENTRY( "Refetch",     POLICY_REFETCH )
00098                 PARAM_BINDING_ENTRY( "ReissueAll",  POLICY_REISSUE_ALL )
00099                 PARAM_BINDING_ENTRY( "ReissueSelective", POLICY_REISSUE_SELECTIVE )
00100             END_PARAM_BINDING()
00101 
00102         END_PARAM_PATH()
00103 
00104         BEGIN_PARAM_PATH_INDEX("Core", m_coreIndex)
00105             PARAM_ENTRY("@Param", m_paramCore)
00106         END_PARAM_PATH()
00107 
00108     END_PARAM_MAP()
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(); /* For avoiding warning when the param map is empty and basePath is not touched. */ \
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(); /* For avoiding warning when the param map is empty and basePath is not touched. */ \
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     // For chaining to a object with a sub parameter path.
00162     #define CHAIN_PARAM_MAP(relativePath, param) \
00163         { \
00164             ChainParamMap((basePath), (relativePath), &param, save); \
00165         }
00166 
00167     // For chaining to a base class.
00168     #define CHAIN_BASE_PARAM_MAP(baseType) \
00169         { \
00170             baseType::ProcessParamMap(save); \
00171         }
00172 
00173 
00174     // For data binding
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     }; // class ParamExchange
00371 
00372     class ParamExchangeChild : public ParamExchangeBase
00373     {
00374     public:
00375         const ParamXMLPath& GetChildRootPath();
00376         void SetRootPath(const ParamXMLPath& root);
00377     };
00378 
00379 }   // namespace Onikiri
00380 
00381 #endif
00382 
00383 

Onikiri2に対してTue Jun 18 14:34:20 2013に生成されました。  doxygen 1.4.7