src/Sim/Foundation/Event/EventBase.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 #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 // Take statistics of events.
00042 //#define ONIKIRI_EVENT_STAT
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         // Reference count operations
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         // Event related methods
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     // ƒCƒxƒ“ƒg‚ÌŠî’êƒNƒ‰ƒX
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         // EventPtr T::Construct() is defined in PooledIntrusivePtrObject.
00199 
00200     };
00201 
00202 
00203 
00204 }; // namespace Onikiri
00205 
00206 #endif // SIM_EVENT_EVENTBASE_H
00207 

Onikiri2¤ËÂФ·¤ÆTue Jun 18 14:34:23 2013¤ËÀ¸À®¤µ¤ì¤Þ¤·¤¿¡£  doxygen 1.4.7