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 #ifndef SIM_FOUNDATION_EVENT_EVENTBASE_H
00033 #define SIM_FOUNDATION_EVENT_EVENTBASE_H
00034
00035 #include "Sim/Op/OpArray/OpArray.h"
00036 #include "Sim/ResourcePriority.h"
00037
00038 namespace Onikiri
00039 {
00040
00041
00042
00043
00044 #ifdef ONIKIRI_EVENT_STAT
00045 class EventStatistics
00046 {
00047 public:
00048 template <typename Event>
00049 void Add( const Event& event )
00050 {
00051 m_typeMap[ typeid( event ).name() ]++;
00052 }
00053
00054 ~EventStatistics()
00055 {
00056 for( unordered_map< const char*, int >::iterator i = m_typeMap.begin(); i != m_typeMap.end(); ++i ){
00057 printf( "%s:\t%d\n", i->first, i->second );
00058 }
00059 }
00060 protected:
00061 unordered_map< const char*, int > m_typeMap;
00062 };
00063 #endif
00064
00065 class EventBaseImplement
00066 {
00067 public:
00068 EventBaseImplement() :
00069 m_refCount(0),
00070 m_priority(RP_DEFAULT_EVENT),
00071 m_canceled(false),
00072 m_updated(false)
00073 {
00074 }
00075
00076 virtual ~EventBaseImplement(){};
00077
00078
00079
00080
00081
00082 INLINE void AddRef()
00083 {
00084 m_refCount++;
00085 };
00086
00087 INLINE void Release()
00088 {
00089 --m_refCount;
00090 if( m_refCount == 0 ){
00091 Destruct();
00092 }
00093 }
00094
00095
00096
00097
00098
00099 bool IsCanceled() const
00100 {
00101 return m_canceled;
00102 }
00103
00104 bool IsUpdated() const
00105 {
00106 return m_updated;
00107 }
00108
00109 void SetUpdated()
00110 {
00111 m_updated = true;
00112 }
00113
00114 void TriggerEvaluate()
00115 {
00116 if( !IsCanceled() ){
00117 Evaluate();
00118 }
00119 }
00120
00121 void TriggerUpdate()
00122 {
00123 ASSERT( !IsUpdated() );
00124 if( !IsCanceled() ){
00125 Update();
00126 }
00127 SetUpdated();
00128 }
00129
00130 void Cancel()
00131 {
00132 m_canceled = true;
00133 }
00134
00135 int GetPriority() const
00136 {
00137 return m_priority;
00138 }
00139
00140 void SetPriority( int priority )
00141 {
00142 m_priority = priority;
00143 }
00144
00145 virtual void Destruct() = 0;
00146 virtual void Update() = 0;
00147 virtual void Evaluate(){};
00148
00149 protected:
00150 int m_refCount;
00151 int m_priority;
00152 bool m_canceled;
00153 bool m_updated;
00154
00155 #ifdef ONIKIRI_EVENT_STAT
00156 static EventStatistics* GetStat()
00157 {
00158 static EventStatistics stat;
00159 return &stat;
00160 }
00161 #endif
00162 };
00163
00164 INLINE static void intrusive_ptr_add_ref( EventBaseImplement* ptr )
00165 {
00166 ptr->AddRef();
00167 }
00168
00169 INLINE static void intrusive_ptr_release( EventBaseImplement* ptr )
00170 {
00171 ptr->Release();
00172 }
00173
00174 typedef boost::intrusive_ptr<EventBaseImplement> EventPtr;
00175
00176
00177
00178
00179 template < typename T >
00180 class EventBase :
00181 public EventBaseImplement,
00182 public PooledIntrusivePtrObject< T, EventBaseImplement >
00183 {
00184 public:
00185
00186 #ifdef ONIKIRI_EVENT_STAT
00187 EventBase()
00188 {
00189 GetStat()->Add( *this );
00190 }
00191 #endif
00192
00193 virtual void Destruct()
00194 {
00195 PooledIntrusivePtrObject< T, EventBaseImplement >::Destruct( static_cast<T*>(this) );
00196 };
00197
00198
00199
00200 };
00201
00202
00203
00204 };
00205
00206 #endif // SIM_EVENT_EVENTBASE_H
00207