TParamsPtr.h
Go to the documentation of this file.
1 #ifndef iprm_TParamsPtr_included
2 #define iprm_TParamsPtr_included
3 
4 
5 // Qt includes
6 #include <QtCore/QtGlobal>
7 #include <QtCore/QStringList>
8 
9 // ACF includes
10 #include <istd/TPointerBase.h>
11 #include <iattr/TAttribute.h>
12 #include <icomp/TAttributeMember.h>
13 #include <icomp/TReferenceMember.h>
14 #include <iprm/IParamsSet.h>
15 
16 
17 namespace iprm
18 {
19 
20 
24 template <class ParameterInterace>
25 class TParamsPtr: public istd::TPointerBase<const ParameterInterace>
26 {
27 public:
29 
30  TParamsPtr(const ParameterInterace* ptr = NULL);
31 
38  TParamsPtr(const IParamsSet* parameterSetPtr, const QByteArray& parameterId, bool isObligatory = true);
39 
47  TParamsPtr( const IParamsSet* parameterSetPtr,
48  const icomp::TAttributeMember<iattr::CIdAttribute>& parameterIdAttribute,
50  bool isObligatory = true);
51 
58  void Init(const IParamsSet* parameterSetPtr, const QByteArray& parameterId, bool isObligatory = true);
59 
67  void Init( const IParamsSet* parameterSetPtr,
68  const icomp::TAttributeMember<iattr::CIdAttribute>& parameterIdAttribute,
70  bool isObligatory = true);
71 };
72 
73 
74 // public methods
75 
76 template <class ParameterInterace>
77 TParamsPtr<ParameterInterace>::TParamsPtr(const ParameterInterace* ptr)
78 : BaseClass(ptr)
79 {
80 }
81 
82 
83 template <class ParameterInterace>
84 TParamsPtr<ParameterInterace>::TParamsPtr(const IParamsSet* parameterSetPtr, const QByteArray& parameterId, bool isObligatory)
85 {
86  Init(parameterSetPtr, parameterId, isObligatory);
87 }
88 
89 
90 template <class ParameterInterace>
92  const IParamsSet* parameterSetPtr,
93  const icomp::TAttributeMember<iattr::CIdAttribute>& parameterIdAttribute,
95  bool isObligatory)
96 {
97  Init(parameterSetPtr, parameterIdAttribute, defaultRef, isObligatory);
98 }
99 
100 
101 template <class ParameterInterace>
102 void TParamsPtr<ParameterInterace>::Init(const IParamsSet* parameterSetPtr, const QByteArray& parameterId, bool isObligatory)
103 {
104  Q_UNUSED(isObligatory);
105 
106  if ((parameterSetPtr != NULL) && !parameterId.isEmpty()){
107  const iser::ISerializable* parameterPtr = parameterSetPtr->GetParameter(parameterId);
108 
109  BaseClass::SetPtr(dynamic_cast<const ParameterInterace*>(parameterPtr));
110 
111 #if QT_VERSION >= 0x040800
112  I_IF_DEBUG(
113  if (!BaseClass::IsValid() && isObligatory){
114  iprm::IParamsSet::Ids existingParamIds = parameterSetPtr->GetParamIds();
115  QStringList existingIds;
116  for (iprm::IParamsSet::Ids::ConstIterator index = existingParamIds.constBegin(); index != existingParamIds.constEnd(); index++){
117  existingIds.push_back(*index);
118  }
119 
120  QString idList = existingIds.join(", ");
121 
122  if (parameterPtr == NULL){
123  qDebug("Parameter %s was not found in the parameter set. Following parameter IDs are registered: %s", qPrintable(parameterId), qPrintable(idList));
124  }
125  else{
126  qDebug("Parameter %s was found in the parameter set, but it doesn't implement the required interface: %s", qPrintable(parameterId), qPrintable(typeid(ParameterInterace).name()));
127  }
128  }
129  )
130 #endif
131  }
132  else{
133  BaseClass::Reset();
134  }
135 }
136 
137 
138 template <class ParameterInterace>
140  const IParamsSet* parameterSetPtr,
141  const icomp::TAttributeMember<iattr::CIdAttribute>& parameterIdAttribute,
143  bool isObligatory)
144 {
145  Q_UNUSED(isObligatory);
146 
147  BaseClass::Reset();
148 
149  if (parameterIdAttribute.IsValid()){
150  if (parameterSetPtr != NULL){
151  const iser::ISerializable* paramPtr = parameterSetPtr->GetParameter(*parameterIdAttribute);
152  const ParameterInterace* paramImplPtr = dynamic_cast<const ParameterInterace*>(paramPtr);
153  BaseClass::SetPtr(paramImplPtr);
154 
155 #if QT_VERSION >= 0x050000
156 #ifndef QT_NO_DEBUG
157  if ((paramImplPtr == NULL) && (paramPtr != NULL)){
158  qDebug("Parameter %s in parameter set is not compatible, should be %s", qPrintable(*parameterIdAttribute), qPrintable(istd::CClassInfo::GetName<ParameterInterace>()));
159  }
160 #endif
161 #endif
162  }
163 #if QT_VERSION >= 0x050000
164 #ifndef QT_NO_DEBUG
165  else if (isObligatory){
166  qDebug("Parameter set unavailable, cannot extract obligatory parameter %s", qPrintable(*parameterIdAttribute));
167  }
168 #endif
169 #endif
170  }
171 
172  if (!BaseClass::IsValid() && defaultRef.IsValid()){
173  BaseClass::SetPtr(defaultRef.GetPtr());
174  }
175 
176 #if QT_VERSION >= 0x040800
177 #ifndef QT_NO_DEBUG
178  if (!BaseClass::IsValid() && isObligatory){
179  QString debugMessage;
180 
181  if (parameterSetPtr == NULL){
182  debugMessage = QString("There is no parameter set and no default parameter is active");
183  }
184  else if (parameterIdAttribute.IsValid()){
185  iprm::IParamsSet::Ids existingParamIds = parameterSetPtr->GetParamIds();
186  QStringList existingIds;
187  for (iprm::IParamsSet::Ids::ConstIterator index = existingParamIds.constBegin(); index != existingParamIds.constEnd(); index++){
188  existingIds.push_back(*index);
189  }
190 
191  QString idList = existingIds.join(", ");
192 
193  debugMessage = QString("Parameter %1 was not found in the parameter set and no default parameter is active. Following parameter IDs are registered: %2").arg(QString(*parameterIdAttribute)).arg(idList);
194  }
195  else{
196  debugMessage = QString("Parameter was not specified and no default parameter is active");
197  }
198 
199  qDebug(qPrintable(debugMessage));
200  }
201 #endif
202 #endif
203 }
204 
205 
206 } // namespace iprm
207 
208 
209 #endif // !iprm_TParamsPtr_included
210 
211 
Help pointer wrapper for management of a parameter from the parameter set.
Definition: TParamsPtr.h:25
#define I_IF_DEBUG(instructions)
Definition: istd.h:49
Implementation of pointer wrapper.
Definition: TPointerBase.h:49
QSet< QByteArray > Ids
Definition: IParamsSet.h:26
istd::TPointerBase< const ParameterInterace > BaseClass
Definition: TParamsPtr.h:28
virtual Ids GetParamIds(bool editableOnly=false) const =0
Get list of used parameter IDs in the parameter set.
Common class for all classes which objects can be archived or restored from archive.
Definition: ISerializable.h:23
Pointer to referenced component object.
#define NULL
Definition: istd.h:64
Pointer to component attribute.
virtual const iser::ISerializable * GetParameter(const QByteArray &id) const =0
Get any parameter.
Interface * GetPtr() const
Direct cccess to internal pointer.
Set of general parameters.
Definition: IParamsSet.h:23
bool IsValid() const
Check if this attribute is valid.
void Init(const IParamsSet *parameterSetPtr, const QByteArray &parameterId, bool isObligatory=true)
Initialize the pointer with the given parameter set and parameter ID.
Definition: TParamsPtr.h:102
bool IsValid() const
Check if this reference can be resolved.
TParamsPtr(const ParameterInterace *ptr=NULL)
Definition: TParamsPtr.h:77

© 2007-2017 Witold Gantzke and Kirill Lepskiy