Safir SDK Core
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ObjectContainer.h
Go to the documentation of this file.
1 /******************************************************************************
2 *
3 * Copyright Saab AB, 2006-2013 (http://safir.sourceforge.net)
4 *
5 * Created by: Lars Hagström / stlrha
6 *
7 *******************************************************************************
8 *
9 * This file is part of Safir SDK Core.
10 *
11 * Safir SDK Core is free software: you can redistribute it and/or modify
12 * it under the terms of version 3 of the GNU General Public License as
13 * published by the Free Software Foundation.
14 *
15 * Safir SDK Core is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with Safir SDK Core. If not, see <http://www.gnu.org/licenses/>.
22 *
23 ******************************************************************************/
24 
25 #ifndef __DOTS_OBJECT_CONTAINER_H__
26 #define __DOTS_OBJECT_CONTAINER_H__
27 
28 
33 #include <typeinfo>
34 
35 namespace Safir
36 {
37 namespace Dob
38 {
39 namespace Typesystem
40 {
47  {
48  public:
51 
62  virtual void SetPtr(const ObjectPtr & ptr) = 0;
63 
71  bool IsChangedHere() const {return m_bIsChanged;}
72 
80  void SetChangedHere(const bool changed) {m_bIsChanged = changed;}
81 
104  virtual ContainerBase & GetMember(const int member, const int index) = 0;
105 
120  virtual const ContainerBase & GetMember(const int member, const int index) const = 0;
121 
135  virtual const ObjectPtr GetObjectPointer() const = 0;
136 
149  virtual void SetObjectPointer(const ObjectPtr ptr) = 0;
150 
157  virtual void ResetObjectPointer() = 0;
158 
159 
161  protected:
169  {ContainerBase::operator =(other); return *this;}
170  };
171 
184  template <class T>
186  {
187  public:
189  typedef boost::shared_ptr<T> T_Ptr;
190 
197 
207  ObjectContainerBase(other) //make sure we use copy constructor of parent
208  {
209  if (!other.IsNull())
210  {
211  m_pObject = boost::dynamic_pointer_cast<T>(other.m_pObject->Clone());
212  if (m_pObject == NULL)
213  {
214  throw IncompatibleTypesException(L"The types are not compatible!",__WFILE__,__LINE__);
215  }
216  }
217  }
218 
227  {
228  ObjectContainerBase::operator=(other);//make sure we use copy assignment of parent
229  if (other.IsNull())
230  {
231  m_pObject.reset();
232  }
233  else
234  {
235  m_pObject = boost::dynamic_pointer_cast<T>(other.m_pObject->Clone());
236  if (m_pObject == NULL)
237  {
238  throw IncompatibleTypesException(L"The types are not compatible!",__WFILE__,__LINE__);
239  }
240  }
241  return *this;
242  }
243 
253  void SetPtr(const T_Ptr & ptr)
254  {
255  m_bIsChanged = true;
256  m_pObject = ptr;
257  }
258 
259  //implementation of pure virtual method.
260  virtual void SetPtr(const ObjectPtr & ptr)
261  {
262  m_bIsChanged = true;
263  m_pObject = boost::dynamic_pointer_cast<T>(ptr);
264  if (m_pObject == NULL)
265  {
266  throw IncompatibleTypesException(L"The types are not compatible!",__WFILE__,__LINE__);
267  }
268  }
269 
279  const T_Ptr & GetPtr() const {if (IsNull()) throw NullException(L"Object is null",__WFILE__,__LINE__); return m_pObject;}
280 
290  T * const operator->() const
291  { if (IsNull()) throw NullException(L"Object is null",__WFILE__,__LINE__); return m_pObject.operator->(); }
292 
293 
294  //Override of inherited method. Parent comment describes this behaviour too..
295  virtual void SetChanged(const bool changed) {m_bIsChanged = changed; if (!IsNull()) m_pObject->SetChanged(changed);}
296 
297  //Override of inherited method. Parent comment describes this behaviour too..
298  virtual bool IsChanged() const {return m_bIsChanged || (!IsNull() && m_pObject->IsChanged());}
299 
300  //override of pure virtual method
301  virtual bool IsNull() const {return m_pObject == NULL;}
302 
303  //override of pure virtual method
304  virtual void SetNull()
305  {
306  m_bIsChanged = true;
307  m_pObject.reset();
308  }
309 
310  //implementation of pure virtual in ContainerBase.
311  virtual void Copy(const ContainerBase & that)
312  {
313  if (this != &that)
314  {
315  if (typeid(*this) != typeid(that))
316  {
317  throw SoftwareViolationException(L"Invalid call to Copy, containers are not of same type",__WFILE__,__LINE__);
318  }
319  const ObjectContainerImpl<T> & castedThat = static_cast<const ObjectContainerImpl<T> &>(that);
320  m_bIsChanged = castedThat.m_bIsChanged;
321  if (that.IsNull())
322  {
323  m_pObject.reset();
324  }
325  else
326  {
327  SetObjectPointer(castedThat.m_pObject->Clone());
328  }
329  }
330  }
331  //Reflection part (Don't use unless you really know what you're doing!!)
332  //Comments are in ObjectContainerBase.
333  virtual ContainerBase & GetMember(const int member, const int index)
334  {if (IsNull()) throw NullException(L"Object is null",__WFILE__,__LINE__); return m_pObject->GetMember(member,index);}
335  virtual const ContainerBase & GetMember(const int member, const int index) const
336  {if (IsNull()) throw NullException(L"Object is null",__WFILE__,__LINE__); return m_pObject->GetMember(member,index);}
337 
338 
351  Int32 CalculateBlobSize() const {if (IsNull()) return 0; else return m_pObject->CalculateBlobSize();}
352 
355  virtual const ObjectPtr GetObjectPointer() const {return boost::static_pointer_cast<Object>(m_pObject);}
356  virtual void SetObjectPointer(const ObjectPtr ptr)
357  {
358 #ifndef NDEBUG
359  m_pObject = boost::dynamic_pointer_cast<T>(ptr);
360  if(m_pObject == NULL)
361  {
362  throw SoftwareViolationException(L"Failed to cast object pointer to expected type",__WFILE__,__LINE__);
363  }
364 #else
365  m_pObject = boost::static_pointer_cast<T>(ptr);
366 #endif
367  }
368  virtual void ResetObjectPointer(){m_pObject.reset();}
369  private:
370 
371  T_Ptr m_pObject;
372  };
373 
380  template <>
382  {
383  public:
385  typedef boost::shared_ptr<Object> T_Ptr;
386 
393 
394 
403  ObjectContainerBase(other) //make sure we use copy constructor of parent
404  {
405  if (!other.IsNull())
406  {
407  m_pObject = other.m_pObject->Clone();
408  }
409  }
410 
418  {
419  ObjectContainerBase::operator =(other);//make sure we use copy assignment of parent
420  if (other.IsNull())
421  {
422  m_pObject.reset();
423  }
424  else
425  {
426  m_pObject = other.m_pObject->Clone();
427  }
428  return *this;
429  }
430 
431 
432  //implementation of pure virtual method.
433  virtual void SetPtr(const ObjectPtr & ptr)
434  {
435  m_bIsChanged = true;
436  m_pObject = ptr;
437  }
438 
448  const T_Ptr & GetPtr() const {if (IsNull()) throw NullException(L"Object is null",__WFILE__,__LINE__); return m_pObject;}
449 
459  Object * const operator->() const
460  {
461  if (IsNull()) throw NullException(L"Object is null",__WFILE__,__LINE__); return m_pObject.operator->();
462  }
463 
464  //Override of inherited method. Parent comment describes this behaviour too..
465  virtual void SetChanged(const bool changed) {m_bIsChanged = changed; if (!IsNull()) m_pObject->SetChanged(changed);}
466 
467  //Override of inherited method. Parent comment describes this behaviour too..
468  virtual bool IsChanged() const {return m_bIsChanged || (!IsNull() && m_pObject->IsChanged());}
469 
470  //override of pure virtual method
471  virtual bool IsNull() const {return m_pObject == NULL;}
472 
473  //override of pure virtual method
474  virtual void SetNull()
475  {
476  m_bIsChanged = true;
477  m_pObject.reset();
478  }
479 
480  //implementation of pure virtual in ContainerBase.
481  virtual void Copy(const ContainerBase & that)
482  {
483  if (this != &that)
484  {
485  if (typeid(*this) != typeid(that))
486  {
487  throw SoftwareViolationException(L"Invalid call to Copy, containers are not of same type",__WFILE__,__LINE__);
488  }
489  const ObjectContainerImpl<Object> & castedThat = static_cast<const ObjectContainerImpl<Object> &>(that);
490  m_bIsChanged = castedThat.m_bIsChanged;
491  if (that.IsNull())
492  {
493  m_pObject.reset();
494  }
495  else
496  {
497  SetObjectPointer(castedThat.m_pObject->Clone());
498  }
499  }
500  }
501 
502  //Reflection part (Don't use unless you really know what you're doing!!)
503  //Comments are in ObjectContainerBase.
504  virtual ContainerBase & GetMember(const int member, const int index)
505  {if (IsNull()) throw NullException(L"Object is null",__WFILE__,__LINE__); return m_pObject->GetMember(member,index);}
506  virtual const ContainerBase & GetMember(const int member, const int index) const
507  {if (IsNull()) throw NullException(L"Object is null",__WFILE__,__LINE__); return m_pObject->GetMember(member,index);}
508 
521  Int32 CalculateBlobSize() const {if (IsNull()) return 0; else return m_pObject->CalculateBlobSize();}
522 
524  private:
525  virtual const ObjectPtr GetObjectPointer() const {return boost::static_pointer_cast<Object>(m_pObject);}
526  virtual void SetObjectPointer(const ObjectPtr ptr)
527  {
528  m_pObject = ptr;
529  }
530  virtual void ResetObjectPointer(){m_pObject.reset();}
531 
532 
533  T_Ptr m_pObject;
534  };
535 
545 }
546 }
547 }
548 #endif
549 
virtual bool IsChanged() const
Is the change flag set on the container? The change flag gets updated every time the contained value ...
Definition: ObjectContainer.h:468
ObjectContainerImpl()
Default constructor.
Definition: ObjectContainer.h:196
The base class for all DOB objects.
Definition: Object.h:54
virtual bool IsNull() const
Is the container set to null?
Definition: ObjectContainer.h:471
virtual void SetNull()
Set the container to null.
Definition: ObjectContainer.h:304
Int32 CalculateBlobSize() const
Calculate the size of the blob-serialized form of the contained object.
Definition: ObjectContainer.h:521
virtual const ObjectPtr GetObjectPointer() const =0
Get a smart pointer to the contained object.
This is a specialization of the ObjectContainerImpl template for the case where the template argument...
Definition: ObjectContainer.h:381
const T_Ptr & GetPtr() const
Get the smart pointer from the container.
Definition: ObjectContainer.h:448
boost::shared_ptr< T > T_Ptr
Typedef for the contained smart pointer.
Definition: ObjectContainer.h:189
virtual void Copy(const ContainerBase &that)
Virtual assignment.
Definition: ObjectContainer.h:481
void SetPtr(const T_Ptr &ptr)
Set the smart pointer in the container.
Definition: ObjectContainer.h:253
virtual const ContainerBase & GetMember(const int member, const int index) const
Get a const reference to a member container from an object.
Definition: ObjectContainer.h:506
Base class for all object containers.
Definition: ObjectContainer.h:46
virtual ContainerBase & GetMember(const int member, const int index)
Get a reference to a member container from an object.
Definition: ObjectContainer.h:504
virtual void SetChanged(const bool changed)
Set the containers change flag.
Definition: ObjectContainer.h:295
virtual const ContainerBase & GetMember(const int member, const int index) const
Get a const reference to a member container from an object.
Definition: ObjectContainer.h:335
Meant to be used when something goes very wrong.
Definition: Exceptions.h:360
T *const operator->() const
Dereference the smart pointer in the container.
Definition: ObjectContainer.h:290
virtual const ObjectPtr GetObjectPointer() const
Get a smart pointer to the contained object.
Definition: ObjectContainer.h:355
Int32 CalculateBlobSize() const
Calculate the size of the blob-serialized form of the contained object.
Definition: ObjectContainer.h:351
ObjectContainerImpl & operator=(const ObjectContainerImpl &other)
Copy assignment operator.
Definition: ObjectContainer.h:417
virtual void SetPtr(const ObjectPtr &ptr)
Set the smart pointer in the container.
Definition: ObjectContainer.h:260
ContainerBase & operator=(const ContainerBase &other)
Copy assignment operator.
Definition: ContainerBase.h:117
virtual bool IsNull() const
Is the container set to null?
Definition: ObjectContainer.h:301
Template class for all containers of automatically generated DOB objects.
Definition: ObjectContainer.h:185
virtual bool IsChanged() const
Is the change flag set on the container? The change flag gets updated every time the contained value ...
Definition: ObjectContainer.h:298
virtual void ResetObjectPointer()=0
Reset (ie set to null) the contained pointer.
virtual void SetPtr(const ObjectPtr &ptr)
Set the smart pointer in the container.
Definition: ObjectContainer.h:433
virtual void SetObjectPointer(const ObjectPtr ptr)=0
Set the smart pointer in the container.
Object *const operator->() const
Dereference the smart pointer in the container.
Definition: ObjectContainer.h:459
ObjectContainerBase()
Default constructor.
Definition: ObjectContainer.h:50
Safir::Dob::Typesystem::ObjectContainerImpl< Object > ObjectContainer
Container for DOB Objects.
Definition: ObjectContainer.h:544
#define __WFILE__
Definition: Exceptions.h:31
ObjectContainerImpl(const ObjectContainerImpl &other)
Copy constructor.
Definition: ObjectContainer.h:206
boost::shared_ptr< Object > ObjectPtr
A smart pointer to an Object.
Definition: Object.h:41
ObjectContainerImpl()
Default constructor.
Definition: ObjectContainer.h:392
virtual bool IsNull() const =0
Is the container set to null?
ObjectContainerBase & operator=(const ObjectContainerBase &other)
Copy assignment operator.
Definition: ObjectContainer.h:168
virtual void Copy(const ContainerBase &that)
Virtual assignment.
Definition: ObjectContainer.h:311
bool IsChangedHere() const
Is the change flag in the container set?
Definition: ObjectContainer.h:71
Base class for all Containers.
Definition: ContainerBase.h:41
Thrown when an application attempts to get the value of a member that is null.
Definition: Exceptions.h:394
boost::shared_ptr< Object > T_Ptr
Typedef for the contained smart pointer.
Definition: ObjectContainer.h:385
virtual void SetChanged(const bool changed)
Set the containers change flag.
Definition: ObjectContainer.h:465
ObjectContainerImpl & operator=(const ObjectContainerImpl &other)
Copy assignment operator.
Definition: ObjectContainer.h:226
ObjectContainerImpl(const ObjectContainerImpl &other)
Copy constructor.
Definition: ObjectContainer.h:402
virtual ContainerBase & GetMember(const int member, const int index)=0
Get a reference to a member container from an object.
virtual void SetObjectPointer(const ObjectPtr ptr)
Set the smart pointer in the container.
Definition: ObjectContainer.h:356
virtual void SetNull()
Set the container to null.
Definition: ObjectContainer.h:474
bool m_bIsChanged
The variable containing the change flag.
Definition: ContainerBase.h:123
virtual void ResetObjectPointer()
Reset (ie set to null) the contained pointer.
Definition: ObjectContainer.h:368
virtual void SetPtr(const ObjectPtr &ptr)=0
Set the smart pointer in the container.
const T_Ptr & GetPtr() const
Get the smart pointer from the container.
Definition: ObjectContainer.h:279
DotsC_Int32 Int32
32 bit integer type.
Definition: Defs.h:67
virtual ContainerBase & GetMember(const int member, const int index)
Get a reference to a member container from an object.
Definition: ObjectContainer.h:333
This exception is thrown if a class cannot be cast to the expected type.
Definition: Exceptions.h:289
void SetChangedHere(const bool changed)
Set the change flag in the container.
Definition: ObjectContainer.h:80