OSDN Git Service

fix cmake/Macros.cmake
[moflib/moflib.git] / moflib-1.0 / src / mof / stream / Cache.hpp
1 #pragma once
2 #include "mof/stream/Manipulator.hpp"
3 #include <mof/Timer.hpp>
4
5 namespace mof{
6         void state_changed();
7         unsigned long get_global_ts();
8
9     template< typename T >
10         class Cache : public Manipulator< T >
11     {
12                         
13         public:
14         typedef typename std::shared_ptr<Cache> Handler;
15
16         virtual ~Cache( ){}
17
18                 virtual T value( FrameNumber frame ) const
19         {
20                         if (local_ts_ < mof::get_global_ts() || m_lastFrame != frame) {
21                                 // キャッシュが無効になっている可能性
22                                 local_ts_ = mof::Timer::getSystemTime();
23                 m_lastFrame = frame;
24                 m_cached = m_pBody->value(frame);
25                         }
26                         return m_cached;
27                 }
28
29                 
30     private:
31         typename Manipulator<T>::Handler m_pBody;
32         mutable FrameNumber m_lastFrame;
33         mutable T m_cached;
34                 mutable unsigned long local_ts_;
35
36                 Cache(const typename Manipulator<T>::Handler& pBody)
37                 : m_pBody(pBody) , m_lastFrame(0), local_ts_(0)
38                 {
39                 }
40
41         template<typename T > friend
42             typename std::shared_ptr< Cache<T> > makeCacheHandler
43         (
44                 const typename Manipulator<T>::Handler& pBody 
45         );
46
47         };
48
49
50
51         template< typename T >
52         std::shared_ptr< Cache< T > > makeCacheHandler
53     (
54                 const typename Manipulator< T >::Handler& pBody 
55     )
56     {
57                 return typename Cache< T >::Handler(
58                         new Cache< T >( pBody )
59                 );
60         }
61
62
63 } //namespace mof
64