CFugue
EventedProperty.h
1 #ifndef __EVENTEDPROPERTY_H__221ADEB0_5C31_4bfe_872F_73B16AB08F75
2 #define __EVENTEDPROPERTY_H__221ADEB0_5C31_4bfe_872F_73B16AB08F75
3 
4 #include "EventHandler.h"
5 
6 template<typename DATATYPE>
8 {
9 protected:
10  DATATYPE m_OldValue;
11  DATATYPE m_NewValue;
12 public:
13  template<typename OLDVALTYPE, typename NEWVALTYPE>
14  CPropertyChangedEventArgs(const OLDVALTYPE& oldVal, const NEWVALTYPE& NewVal)
15  : m_OldValue(oldVal), m_NewValue(NewVal)
16  {
17  }
18  inline const DATATYPE& OldValue() const { return m_OldValue; }
19  inline const DATATYPE& NewValue() const { return m_NewValue; }
20 };
21 
22 template<typename DATATYPE>
24 {
25  bool m_bCancel;
26 public:
27  template<typename OLDVALTYPE, typename NEWVALTYPE>
28  CPropertyChangingEventArgs(const OLDVALTYPE& oldVal, const NEWVALTYPE& NewVal)
29  : CPropertyChangedEventArgs<DATATYPE>(oldVal, NewVal), m_bCancel(false)
30  {
31  }
32  inline bool Cancel() const { return m_bCancel; }
33  inline void SetCancel() { m_bCancel = true; }
34 };
35 
36 template<typename DATATYPE>
38 {
39  DATATYPE m_Data;
40 public:
42 
45 
46  PropertyChangingEvent ValueChanging;
47  PropertyChangedEvent ValueChanged;
48 
49  CEventedProperty() {}
50 
51  template<typename T>
52  CEventedProperty(const T& InitVal): m_Data(InitVal) {}
53 
54  inline operator DATATYPE() const { return m_Data; }
55 
56  template<typename T>
57  inline CEventedProperty& operator =(const T& other)
58  {
59  CPropertyChangingEventArgs<DATATYPE> evArgs(m_Data, other);
60  RaiseEvent(&ValueChanging, &evArgs);
61 
62  if(evArgs.Cancel() == false)
63  {
64  CPropertyChangedEventArgs<DATATYPE> ChangedEvArgs(m_Data,other); //Not to Loose Old Data we first create the EvArgs obj
65  m_Data = other;
66  RaiseEvent(&ValueChanged, &ChangedEvArgs);
67  }
68 
69  return *this;
70  }
71 };
72 
73 #endif
void RaiseEvent(CEventT< TEventSource, TEventHandlerArgs > *pEvent, TEventHandlerArgs *pArgs)
Definition: EventHandler.h:320

CFugue, the C++ Music Programming Library © Copyright 2009 Cenacle Research India Private Limited Gopalakrishna Palem