TModelObserverCompWrap.h
Go to the documentation of this file.
1 #ifndef ibase_TModelObserverCompWrap_included
2 #define ibase_TModelObserverCompWrap_included
3 
4 
5 // Qt includes
6 #include <QtCore/QDebug>
7 
8 // ACF includes
9 #include <imod/IModel.h>
11 #include <ilog/TLoggerCompWrap.h>
12 
13 
14 namespace ibase
15 {
16 
17 
25 template <class ObserverComponent>
26 class TModelObserverCompBaseWrap: public ObserverComponent
27 {
28 public:
29  typedef ObserverComponent BaseClass;
30 
31  I_BEGIN_BASE_COMPONENT(TModelObserverCompBaseWrap);
32  I_REGISTER_INTERFACE(imod::IObserver);
33  I_ASSIGN(m_defaultModelCompPtr, "DefaultModel", "Data model to be connected to the observer", false, "DefaultModel");
34  I_TASSIGN_TO(m_defaultObjectCompPtr, m_defaultModelCompPtr, false);
35  I_END_COMPONENT;
36 
37 protected:
38  // reimplemented (icomp::CComponentBase)
39  virtual void OnComponentCreated();
40  virtual void OnComponentDestroyed();
41 
42 private:
43  I_REF(imod::IModel, m_defaultModelCompPtr);
44  I_TREF(typename ObserverComponent::ModelType, m_defaultObjectCompPtr);
45 };
46 
47 
48 // protected methods
49 
50 // reimplemented (icomp::CComponentBase)
51 
52 template <class ObserverComponent>
54 {
55  BaseClass::OnComponentCreated();
56 
57  I_IF_DEBUG(
58  if (m_defaultModelCompPtr.IsValid() && !m_defaultObjectCompPtr.IsValid()){
59  const icomp::IComponentContext* contextPtr = BaseClass::GetComponentContext();
60  QByteArray observerComponentId = "(unidentified)";
61  if (contextPtr != NULL){
62  const icomp::CComponentContext* extContextPtr = dynamic_cast<const icomp::CComponentContext*>(contextPtr);
63  if (extContextPtr != NULL){
64  observerComponentId = extContextPtr->GetCompleteContextId();
65  }
66  else{
67  observerComponentId = contextPtr->GetContextId();
68  }
69  }
70 
71  QByteArray modelComponentId = "(unidentified)";
72 
73  icomp::IComponent* modelComponentPtr = dynamic_cast<icomp::IComponent*>(m_defaultModelCompPtr.GetPtr());
74  if (modelComponentPtr != NULL){
75  const icomp::IComponentContext* modelContextPtr = modelComponentPtr->GetComponentContext();
76  if (modelContextPtr != NULL){
77  const icomp::CComponentContext* extModelContextPtr = dynamic_cast<const icomp::CComponentContext*>(modelContextPtr);
78  if (extModelContextPtr != NULL){
79  modelComponentId = extModelContextPtr->GetCompleteContextId();
80  }
81  else{
82  modelComponentId = modelContextPtr->GetContextId();
83  }
84  }
85  }
86 
87  QString exptectedObjectInterface = typeid(typename ObserverComponent::ModelType).name();
88 
89  qDebug() << "Component " << observerComponentId << " Component model " << modelComponentId << " doesn't implement observed interface " << exptectedObjectInterface;
90  }
91  )
92 
93  if (m_defaultModelCompPtr.IsValid() && m_defaultObjectCompPtr.IsValid()){
94  m_defaultModelCompPtr->AttachObserver(this);
95  }
96 }
97 
98 
99 template <class ObserverComponent>
101 {
102  if (m_defaultModelCompPtr.IsValid() && m_defaultModelCompPtr->IsAttached(this)){
103  m_defaultModelCompPtr->DetachObserver(this);
104  }
105 
106  BaseClass::OnComponentDestroyed();
107 }
108 
109 
114 template <class BaseComponent>
116 {
117 public:
119 
120  I_BEGIN_COMPONENT(TModelObserverCompWrap);
121  I_END_COMPONENT;
122 };
123 
124 
125 } // namespace ibase
126 
127 
128 #endif // !ibase_TModelObserverCompWrap_included
129 
130 
Generic basic implementation of component wrapper for model observer classes.
#define I_IF_DEBUG(instructions)
Definition: istd.h:49
virtual const IComponentContext * GetComponentContext() const =0
Get access to component context describing all application-specified component information loaded fro...
Main component interface.
Definition: IComponent.h:31
Final implementation of the component-based observer wrapper.
Provide session context of component.
virtual const QByteArray & GetContextId() const =0
Get ID of this component in the context tree.
#define NULL
Definition: istd.h:64
TModelObserverCompBaseWrap< BaseComponent > BaseClass
QByteArray GetCompleteContextId() const
Common interface for all classes implementing the Observer functionality in the Model/Observer design...
Definition: IObserver.h:24
Common interface for model objects, that supports Model/Observer design pattern.
Definition: IModel.h:23
Base implementation of component session context.

© 2007-2017 Witold Gantzke and Kirill Lepskiy