TSingleModelObserverBase.h
Go to the documentation of this file.
1 #ifndef imod_TSingleModelObserverBase_included
2 #define imod_TSingleModelObserverBase_included
3 
4 
5 // STL includes
6 #include <typeinfo>
7 
8 // Qt includes
9 #include <QtCore/QString>
10 
11 // ACF includes
13 #include <imod/IModel.h>
14 
15 
16 namespace imod
17 {
18 
19 
25 template <class ModelInterface>
27 {
28 public:
30  typedef ModelInterface ModelType;
31 
33 
37  ModelInterface* GetObservedObject() const;
42  ModelInterface* GetObjectPtr() const;
43 
47  bool AttachOrSetObject(ModelInterface* objectPtr);
48 
49  // reimplemented (imod::IObserver)
50  virtual bool OnModelAttached(imod::IModel* modelPtr, istd::IChangeable::ChangeSet& changeMask);
51  virtual bool OnModelDetached(imod::IModel* modelPtr);
52 
53 protected:
54  virtual ModelInterface* CastFromModel(imod::IModel* modelPtr) const;
55 
56 private:
57  ModelInterface* m_observedObjectPtr;
58 };
59 
60 
61 // public methods
62 
63 template <class ModelInterface>
65 {
66  m_observedObjectPtr = NULL;
67 }
68 
69 
70 template <class ModelInterface>
72 {
73  return m_observedObjectPtr;
74 }
75 
76 
77 template <class ModelInterface>
79 {
80  return m_observedObjectPtr;
81 }
82 
83 
84 template <class ModelInterface>
86 {
87  EnsureModelDetached();
88 
89  imod::IModel* modelPtr = dynamic_cast<imod::IModel*>(objectPtr);
90 
91  bool retVal = false;
92  if ((modelPtr != NULL) && modelPtr->AttachObserver(this)){
93  retVal = true;
94  }
95 
96  m_observedObjectPtr = objectPtr;
97 
98  return retVal;
99 }
100 
101 
102 // reimplemented (imod::IObserver)
103 
104 template <class ModelInterface>
106 {
107  m_observedObjectPtr = CastFromModel(modelPtr);
108 
109  I_IF_DEBUG(
110  if (m_observedObjectPtr == NULL){
111  QString exptectedObjectInterface = typeid(ModelInterface).name();
112 
113  qDebug("Data model interface is not supported by this observer. Expected interface is: %s", qPrintable(exptectedObjectInterface));
114  }
115  )
116 
117  if ((m_observedObjectPtr != NULL) && BaseClass::OnModelAttached(modelPtr, changeMask)){
118  return true;
119  }
120 
121  BaseClass::EnsureModelDetached();
122 
123  return false;
124 }
125 
126 
127 template <class ModelInterface>
129 {
130  if (BaseClass::OnModelDetached(modelPtr)){
131  // If model was correctly attached m_observedObjectPtr cannot be NULL. OnDetach returns true only if model was correctly attached.
132  Q_ASSERT(m_observedObjectPtr != NULL);
133 
134  m_observedObjectPtr = NULL;
135 
136  return true;
137  }
138 
139  return false;
140 }
141 
142 
143 // protected methods
144 
145 template <class ModelInterface>
147 {
148  return dynamic_cast<ModelInterface*>(modelPtr);
149 }
150 
151 
152 } // namespace imod
153 
154 
155 #endif // !imod_TSingleModelObserverBase_included
156 
157 
#define I_IF_DEBUG(instructions)
Definition: istd.h:49
Basic implementation for a single model observer.
ModelInterface * GetObservedObject() const
Get instance of observed object.
virtual bool OnModelAttached(imod::IModel *modelPtr, istd::IChangeable::ChangeSet &changeMask)
This call back function will be called, if an observable object is about to be attached.
Basic implementation for a single model observer with binding to concrete data object interface...
ModelInterface * GetObjectPtr() const
Get instance of observed object.
virtual bool OnModelDetached(imod::IModel *modelPtr)
This call back function will be called, if an observable object is about to be detached.
#define NULL
Definition: istd.h:64
bool AttachOrSetObject(ModelInterface *objectPtr)
Attach to model of object or set object pointer, if no connection is possible.
virtual bool AttachObserver(IObserver *observerPtr)=0
Attaches model object to observer observerPtr.
Common interface for model objects, that supports Model/Observer design pattern.
Definition: IModel.h:23
Set of change flags (its IDs).
Definition: IChangeable.h:30
virtual ModelInterface * CastFromModel(imod::IModel *modelPtr) const

© 2007-2017 Witold Gantzke and Kirill Lepskiy