GlsAnimation Library  1.0.6
Provides classes and functions to support animating GL Studio objects programmatically or with script files
gls_animation_observer.h
Go to the documentation of this file.
1 
41 #ifndef INCLUDED_GLS_ANIMATION_OBSERVER_H
42 #define INCLUDED_GLS_ANIMATION_OBSERVER_H
43 
44 #include "gls_keyframe.h"
45 
46 namespace disti
47 {
51  {
52  public:
53  typedef stdortr1::shared_ptr<GlsAnimationObserverBase> Ptr;
54  typedef Detail::ID ID;
55 
58 
61 
64  virtual void OnStarted( ID ) {}
65 
67  virtual void OnUpdated( ID ) {}
68 
73  virtual void OnCompleted( ID ) {}
74 
76  virtual void OnCanceled( ID ) {}
77 
78  private:
80  void operator=( const GlsAnimationObserverBase& ); // = delete
81  };
82 
94  {
95  public:
102  template<class OnStartedCallback, class OnUpdatedCallback, class OnCompletedCallback, class OnCanceledCallback>
104  const OnStartedCallback& onStarted,
105  const OnUpdatedCallback& onUpdated,
106  const OnCompletedCallback& onCompleted,
107  const OnCanceledCallback& onCanceled )
108  : _onStarted( MakeCallback( onStarted ) )
109  , _onUpdated( MakeCallback( onUpdated ) )
110  , _onCompleted( MakeCallback( onCompleted ) )
111  , _onCanceled( MakeCallback( onCanceled ) )
112  {}
113 
115  virtual void OnStarted( ID id ) // override
116  {
117  if( _onStarted )
118  {
119  (*_onStarted)( id );
120  }
121  }
122 
124  virtual void OnUpdated( ID id ) // override
125  {
126  if( _onUpdated )
127  {
128  (*_onUpdated)( id );
129  }
130  }
131 
133  virtual void OnCompleted( ID id ) // override
134  {
135  if( _onCompleted )
136  {
137  (*_onCompleted)( id );
138  }
139  }
140 
142  virtual void OnCanceled( ID id ) // override
143  {
144  if( _onCanceled )
145  {
146  (*_onCanceled)( id );
147  }
148  }
149  private:
150  // Callback types to enable delayed type declaration.
151  // Compare the bit about shared_ptr's deleter in http://www.artima.com/cppsource/top_cpp_aha_moments.html
152  struct CallbackBase
153  {
154  typedef stdortr1::shared_ptr<CallbackBase> Ptr;
155  virtual ~CallbackBase() {}
156  virtual void operator()( ID ) = 0;
157  };
158 
159  template<class Fn>
160  struct Callback : CallbackBase
161  {
162  explicit Callback( const Fn& fn ) : _fn( fn ) {}
163  virtual void operator()( ID id ) // override
164  {
165  _fn( id );
166  }
167  private:
168  const Fn _fn;
169  };
170 
171  // Classes to do compile-time checking of whether an object is valid.
172  // See http://en.wikibooks.org/wiki/More_C++_Idioms/Member_Detector
173  template<typename T, typename RESULT=bool>
174  class HasPolicyBool
175  {
176  template <typename U, RESULT (U::*)()> struct Check;
177  template <typename U> static char func(Check<U, &U::operator bool> *);
178  template <typename U> static int func(...);
179  public:
180  typedef HasPolicyBool type;
181  enum { value = sizeof(func<T>(0)) == sizeof(char) };
182  };
183 
184  template<typename T, typename RESULT=void*>
185  class HasPolicyVoid
186  {
187  template <typename U, RESULT (U::*)()> struct Check;
188  template <typename U> static char func(Check<U, &U::operator void*> *);
189  template <typename U> static int func(...);
190  public:
191  typedef HasPolicyVoid type;
192  enum { value = sizeof(func<T>(0)) == sizeof(char) };
193  };
194 
195  // Check for validity of the object so that we don't create a callback object if it's invalid
196  template<class Fn> static bool IsValid( Fn fn, typename Detail::enable_if< HasPolicyBool<Fn>::value && HasPolicyVoid<Fn>::value >::type* = 0 ) { return static_cast<bool>(fn); } // Check for things that can be tested by conversion to bool or void*
197  template<class Fn> static bool IsValid( Fn fn, typename Detail::enable_if< !HasPolicyBool<Fn>::value && HasPolicyVoid<Fn>::value >::type* = 0 ) { return static_cast<void*>(fn) != NULL; } // Check for things that can be tested by conversion to bool or void*
198  template<class Fn> static bool IsValid( Fn fn, typename Detail::enable_if< HasPolicyBool<Fn>::value && !HasPolicyVoid<Fn>::value >::type* = 0 ) { return static_cast<bool>(fn); } // Check for things that can be tested by conversion to bool or void*
199  template<class Fn> static bool IsValid( Fn fn, typename Detail::enable_if< !HasPolicyBool<Fn>::value && !HasPolicyVoid<Fn>::value >::type* = 0 ) { return true; } // We can't tell if it's valid (e.g., an arbitrary functor), so just assume it is.
200  static bool IsValid( void(*fn)(ID) ) { return fn != 0; } // Handle the case of stand-alone function pointers
201 
202  // Create valid callbacks, or return a null pointer if incoming object is invalid
203  // (within our ability to check it).
204  template<class Fn>
205  static CallbackBase::Ptr MakeCallback( const Fn& fn )
206  {
207  return CallbackBase::Ptr( IsValid(fn) ? new Callback<Fn>( fn ) : 0 );
208  }
209 
210  // Member data
211  const CallbackBase::Ptr _onStarted;
212  const CallbackBase::Ptr _onUpdated;
213  const CallbackBase::Ptr _onCompleted;
214  const CallbackBase::Ptr _onCanceled;
215  };
216 
233  template<class Fn>
234  GlsAnimationObserverBase::Ptr CreateOnStartedCallback( const Fn& fn )
235  {
236  void(*dummy)( GlsAnimationObserverBase::ID ) = 0;
237  return GlsAnimationObserverBase::Ptr( new GlsAnimationObserver( fn, dummy, dummy, dummy ) );
238  }
239 
243  template<class Fn>
244  GlsAnimationObserverBase::Ptr CreateOnUpdatedCallback( const Fn& fn )
245  {
246  void(*dummy)( GlsAnimationObserverBase::ID ) = 0;
247  return GlsAnimationObserverBase::Ptr( new GlsAnimationObserver( dummy, fn, dummy, dummy ) );
248  }
249 
253  template<class Fn>
254  GlsAnimationObserverBase::Ptr CreateOnCompletedCallback( const Fn& fn )
255  {
256  void(*dummy)( GlsAnimationObserverBase::ID ) = 0;
257  return GlsAnimationObserverBase::Ptr( new GlsAnimationObserver( dummy, dummy, fn, dummy ) );
258  }
259 
263  template<class Fn>
264  GlsAnimationObserverBase::Ptr CreateOnCanceledCallback( const Fn& fn )
265  {
266  void(*dummy)( GlsAnimationObserverBase::ID ) = 0;
267  return GlsAnimationObserverBase::Ptr( new GlsAnimationObserver( dummy, dummy, dummy, fn ) );
268  }
269 }
270 
271 #endif
virtual void OnStarted(ID id)
Inherited from GlsAnimationObserverBase.
Definition: gls_animation_observer.h:115
GlsAnimationObserverBase()
Constructor.
Definition: gls_animation_observer.h:57
virtual ~GlsAnimationObserverBase()
Destructor.
Definition: gls_animation_observer.h:60
virtual void OnCompleted(ID)
Definition: gls_animation_observer.h:73
virtual void OnCanceled(ID)
Called when the animation is canceled.
Definition: gls_animation_observer.h:76
Definition: gls_animation_observer.h:50
virtual void OnUpdated(ID id)
Inherited from GlsAnimationObserverBase.
Definition: gls_animation_observer.h:124
virtual void OnStarted(ID)
Definition: gls_animation_observer.h:64
virtual void OnUpdated(ID)
Called when the animation updates (i.e., SetKey() is called with a new value)
Definition: gls_animation_observer.h:67
The GL Studio keyframe animation classes.
Detail::ID ID
Alias for easier reading.
Definition: gls_animation_observer.h:54
GlsAnimationObserver(const OnStartedCallback &onStarted, const OnUpdatedCallback &onUpdated, const OnCompletedCallback &onCompleted, const OnCanceledCallback &onCanceled)
Definition: gls_animation_observer.h:103
virtual void OnCanceled(ID id)
Inherited from GlsAnimationObserverBase.
Definition: gls_animation_observer.h:142
virtual void OnCompleted(ID id)
Inherited from GlsAnimationObserverBase.
Definition: gls_animation_observer.h:133
stdortr1::shared_ptr< GlsAnimationObserverBase > Ptr
Alias for easier reading.
Definition: gls_animation_observer.h:53
Definition: gls_animation_observer.h:93
Definition: gls_animation.cpp:64