src/Samples/SampleHookModule.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 // This is a sample class to demonstrate how to use hook mechanisms.
00034 // You can activate this class by defining USE_SAMPLE_HOOK_MODULE.
00035 //
00036 
00037 #ifndef SAMPLES_SAMPLE_HOOK_MODULE_H
00038 #define SAMPLES_SAMPLE_HOOK_MODULE_H
00039 
00040 // USE_SAMPLE_HOOK_MODULE is referred in 'User/UserResourceMap.h' and 'User/UserInit.h'.
00041 //#define USE_SAMPLE_HOOK_MODULE 1
00042 
00043 
00044 #include "Sim/Foundation/Resource/ResourceNode.h"
00045 
00046 #include "Sim/Pipeline/Fetcher/Fetcher.h"
00047 #include "Sim/Pipeline/Renamer/Renamer.h"
00048 #include "Sim/Pipeline/Dispatcher/Dispatcher.h"
00049 #include "Sim/Pipeline/Scheduler/Scheduler.h"
00050 #include "Sim/Pipeline/Scheduler/FinishEvent.h"
00051 #include "Sim/Pipeline/Retirer/Retirer.h"
00052 #include "Sim/InorderList/InorderList.h"
00053 #include "Sim/System/SimulationSystem/SimulationSystem.h"
00054 
00055 #include "Sim/Op/Op.h"
00056 
00057 // All Onikiri classes are placed in a 'Onikiri' namespace.
00058 namespace Onikiri
00059 {
00060     // All modules must inherit PhysicalResourceNode.
00061     class SampleHookModule : public PhysicalResourceNode
00062     {
00063     public:
00064 
00065         void OnOpFetch( Fetcher::FetchHookParam* param )
00066         {
00067             if( param->op.IsNull() ){
00068                 g_env.Print( "op is not fetched yet.\n" );
00069             }
00070             else{
00071                 g_env.Print( "op(%s) is fetched.\n", param->op->ToString(0).c_str() );
00072             }
00073         }
00074 
00075         void OnOpRename( HookParameter<Renamer>* param )
00076         {
00077             g_env.Print( "op(%s) is renamed.\n", param->GetOp()->ToString(0).c_str() );
00078         }
00079 
00080         void OnOpDispatch( HookParameter<Dispatcher, Dispatcher::DispatchHookParam>* param )
00081         {
00082             g_env.Print( "op(%s) is dispatched.\n", param->GetOp()->ToString(0).c_str() );
00083         }
00084 
00085         void OnOpIssue( HookParameter<Scheduler>* param )
00086         {
00087             g_env.Print( "op(%s) is issued.\n", param->GetOp()->ToString(0).c_str() );
00088         }
00089 
00090         void OnOpExecutionFinish( OpIterator op, OpFinishEvent::FinishHookParam* param )
00091         {
00092             if( op.IsAlive() ){
00093                 g_env.Print( "op(%s) is executed.\n", op->ToString(0).c_str() );
00094             }
00095             else{
00096                 g_env.Print( "op is flushed.\n" );
00097             }
00098         }
00099 
00100         void OnOpRetire( HookParameter<Retirer>* param )
00101         {
00102             g_env.Print( "op(%s) is retired.\n", param->GetOp()->ToString(0).c_str() );
00103         }
00104 
00105         void OnOpFlushed( HookParameter<InorderList>* param )
00106         {
00107             g_env.Print( "op(%s) is flushed.\n", param->GetOp()->ToString(0).c_str() );
00108         }
00109 
00110         void OnOpRescheduled( OpIterator op, Scheduler::RescheduleHookParam* param )
00111         {
00112             g_env.Print( "op(%s) is re-scheduled.\n", op->ToString(0).c_str() );
00113         }
00114 
00115         void OnCycleBegin()
00116         {
00117             g_env.Print( "--- Cycle Begin\n" );
00118         }
00119 
00120         void OnCycleEvaluate()
00121         {
00122             g_env.Print( "On a cycle evaluation phase.\n" );
00123         }
00124 
00125         void OnCycleProcess()
00126         {
00127             g_env.Print( "On a cycle process phase.\n" );
00128         }
00129 
00130         SampleHookModule(){};
00131         virtual ~SampleHookModule(){};
00132 
00133         // Initialize and Finalize must be implemented.
00134         virtual void Initialize( InitPhase phase )
00135         {
00136             if( phase == INIT_PRE_CONNECTION ){
00137                 // After constructing and before object connection.
00138                 // LoadParam() must be called in this phase or later.
00139                 LoadParam();
00140             }
00141             else if ( phase == INIT_POST_CONNECTION ){
00142                 // After connection
00143 
00144                 // Register the handlers to the hooks.
00145 
00146                 // To pipeline stages.
00147                 Fetcher::s_fetchHook.Register( 
00148                     this, &SampleHookModule::OnOpFetch, 0, HookType::HOOK_AFTER 
00149                 );
00150                 Renamer::s_renameUpdateHook.Register( 
00151                     this, &SampleHookModule::OnOpRename, 0, HookType::HOOK_AFTER 
00152                 );
00153                 Dispatcher::s_dispatchUpdateHook.Register( 
00154                     this, &SampleHookModule::OnOpDispatch, 0, HookType::HOOK_AFTER 
00155                 );
00156                 Scheduler::s_issueHook.Register(
00157                     this, &SampleHookModule::OnOpIssue, 0, HookType::HOOK_AFTER
00158                 );
00159                 OpFinishEvent::s_finishHook.Register(
00160                     this, &SampleHookModule::OnOpExecutionFinish, 0, HookType::HOOK_AFTER 
00161                 );
00162                 
00163                 // A hook handler for retirement needs to be set with HOOK_BEFORE,
00164                 // because 'op' cannot be used after retirement (HOOK_AFTER).
00165                 Retirer::s_retireHook.Register(
00166                     this, &SampleHookModule::OnOpRetire, 0, HookType::HOOK_BEFORE
00167                 );
00168 
00169 
00170                 // Flush/Re-schedule
00171                 // 'OnOpFlushed()' needs to be set with HOOK_BEFORE for the similar
00172                 // reason in retirement.
00173                 InorderList::s_opFlushHook.Register(
00174                     this, &SampleHookModule::OnOpFlushed, 0, HookType::HOOK_BEFORE
00175                 );
00176                 Scheduler::s_rescheduleHook.Register(
00177                     this, &SampleHookModule::OnOpRescheduled, 0, HookType::HOOK_AFTER
00178                 );
00179 
00180 
00181                 // Cycle hook handlers
00182                 SimulationSystem::s_cycleBeginHook.Register(
00183                     this, &SampleHookModule::OnCycleBegin, 0, HookType::HOOK_AFTER
00184                 );
00185                 SimulationSystem::s_cycleEvaluateHook.Register(
00186                     this, &SampleHookModule::OnCycleEvaluate, 0, HookType::HOOK_AFTER
00187                 );
00188                 SimulationSystem::s_cycleUpdateHook.Register(
00189                     this, &SampleHookModule::OnCycleProcess, 0, HookType::HOOK_AFTER
00190                 );
00191 
00192             }
00193         }
00194 
00195         virtual void Finalize()
00196         {
00197             // 'ReleaseParam()' must be called in 'Finalize()'.
00198             ReleaseParam();
00199         };
00200 
00201         // Parameter & Resource Map
00202         // These contents in this map are bound to the below XML.
00203         // This binding is defined in 'User/UserResourceMap.h' and 'User/UserInit.h'.
00204         BEGIN_PARAM_MAP("")
00205             BEGIN_PARAM_PATH( GetParamPath() )
00206             END_PARAM_PATH()
00207             BEGIN_PARAM_PATH( GetResultPath() )
00208             END_PARAM_PATH()
00209         END_PARAM_MAP()
00210 
00211         BEGIN_RESOURCE_MAP()
00212         END_RESOURCE_MAP()
00213     };
00214 
00215 #ifdef USE_SAMPLE_HOOK_MODULE
00216     // Default parameters and connections for SampleHookModule.
00217     // This XML overwrite XML in DefaultParam.xml partially.
00218     static const char g_sampleHookModuleParam[] = "     \n\
00219                                                     \n\
00220     <?xml version='1.0' encoding='UTF-8'?>          \n\
00221     <Session>                                       \n\
00222       <Simulator>                                   \n\
00223         <Configurations>                            \n\
00224           <DefaultConfiguration>                    \n\
00225                                                     \n\
00226             <Structure>                             \n\
00227               <Copy>                                \n\
00228                 <SampleHookModule                   \n\
00229                   Count = 'CoreCount'               \n\
00230                   Name  = 'sampleHookModule'        \n\
00231                 />                                  \n\
00232               </Copy>                               \n\
00233             </Structure>                            \n\
00234                                                     \n\
00235             <Parameter>                             \n\
00236               <SampleHookModule                     \n\
00237                 Name  = 'sampleHookModule'          \n\
00238               />                                    \n\
00239             </Parameter>                            \n\
00240           </DefaultConfiguration>                   \n\
00241         </Configurations>                           \n\
00242       </Simulator>                                  \n\
00243     </Session>                                      \n\
00244     ";
00245 
00246 #endif  // #ifdef USE_SAMPLE_HOOK_MODULE
00247 
00248 }
00249 
00250 #endif  // #ifndef SAMPLES_SAMPLE_HOOK_MODULE_H

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