Safir SDK Core
Loading...
Searching...
No Matches
ObjectContainer.h
Go to the documentation of this file.
1/******************************************************************************
2*
3* Copyright Saab AB, 2006-2013 (http://safirsdkcore.com)
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#ifndef __DOTS_OBJECT_CONTAINER_H__
25#define __DOTS_OBJECT_CONTAINER_H__
26
31#include <typeinfo>
32
33namespace Safir
34{
35namespace Dob
36{
37namespace Typesystem
38{
45 {
46 public:
49
50
53
64 virtual void SetPtr(const ObjectPtr & ptr) = 0;
65
73 bool IsChangedHere() const {return m_bIsChanged;}
74
82 void SetChangedHere(const bool changed) {m_bIsChanged = changed;}
83
106 virtual ContainerBase & GetMember(const int member, const int index) = 0;
107
122 virtual const ContainerBase & GetMember(const int member, const int index) const = 0;
123
137 virtual const ObjectPtr GetObjectPointer() const = 0;
138
151 virtual void SetObjectPointer(const ObjectPtr ptr) = 0;
152
159 virtual void ResetObjectPointer() = 0;
160
161
163 protected:
165
174 };
175
188 template <class T>
190 {
191 public:
193 typedef std::shared_ptr<T> T_Ptr;
195
202
212 ObjectContainerBase(other) //make sure we use copy constructor of parent
213 {
214 if (!other.IsNull())
215 {
216 m_pObject = std::dynamic_pointer_cast<T>(other.m_pObject->Clone());
217 if (m_pObject == NULL)
218 {
219 throw IncompatibleTypesException(L"The types are not compatible!",__WFILE__,__LINE__);
220 }
221 }
222 }
223
232 {
233 ObjectContainerBase::operator=(other);//make sure we use copy assignment of parent
234 if (other.IsNull())
235 {
236 m_pObject.reset();
237 }
238 else
239 {
240 m_pObject = std::dynamic_pointer_cast<T>(other.m_pObject->Clone());
241 if (m_pObject == NULL)
242 {
243 throw IncompatibleTypesException(L"The types are not compatible!",__WFILE__,__LINE__);
244 }
245 }
246 return *this;
247 }
248
258 void SetPtr(const T_Ptr & ptr)
259 {
260 m_bIsChanged = true;
261 m_pObject = ptr;
262 }
263
264 void SetPtr(const ObjectPtr & ptr) override
265 {
266 m_bIsChanged = true;
267 m_pObject = std::dynamic_pointer_cast<T>(ptr);
268 if (m_pObject == NULL)
269 {
270 throw IncompatibleTypesException(L"The types are not compatible!",__WFILE__,__LINE__);
271 }
272 }
273
283 const T_Ptr & GetPtr() const
284 {if (IsNull()) throw NullException(L"Object is null",__WFILE__,__LINE__); return m_pObject;}
285
295 T * operator->() const
296 { if (IsNull()) throw NullException(L"Object is null",__WFILE__,__LINE__); return m_pObject.operator->(); }
297
298
299 void SetChanged(const bool changed) override {m_bIsChanged = changed; if (!IsNull()) m_pObject->SetChanged(changed);}
300
301 bool IsChanged() const override {return m_bIsChanged || (!IsNull() && m_pObject->IsChanged());}
302
303 bool IsNull() const override {return m_pObject == NULL;}
304
305 void SetNull() override
306 {
307 m_bIsChanged = true;
308 m_pObject.reset();
309 }
310
311 void Copy(const ContainerBase & that) override
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
332 //Reflection part (Don't use unless you really know what you're doing!!)
333 ContainerBase & GetMember(const int member, const int index) override
334 {if (IsNull()) throw NullException(L"Object is null",__WFILE__,__LINE__); return m_pObject->GetMember(member,index);}
335 const ContainerBase & GetMember(const int member, const int index) const override
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 const ObjectPtr GetObjectPointer() const override {return std::static_pointer_cast<Object>(m_pObject);}
356 void SetObjectPointer(const ObjectPtr ptr) override
357 {
358#ifndef NDEBUG
359 m_pObject = std::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 = std::static_pointer_cast<T>(ptr);
366#endif
367 }
368 void ResetObjectPointer() override {m_pObject.reset();}
369 private:
370 T_Ptr m_pObject;
371 };
372
379 template <>
381 {
382 public:
384 typedef std::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 void SetPtr(const ObjectPtr & ptr) override
433 {
434 m_bIsChanged = true;
435 m_pObject = ptr;
436 }
437
447 const T_Ptr & GetPtr() const
448 {
449 if (IsNull())
450 {
451 throw NullException(L"Object is null",__WFILE__,__LINE__);
452 }
453 return m_pObject;
454 }
455
466 {
467 if (IsNull())
468 {
469 throw NullException(L"Object is null",__WFILE__,__LINE__);
470 }
471 return m_pObject.operator->();
472 }
473
474 void SetChanged(const bool changed) override
475 {
476 m_bIsChanged = changed;
477 if (!IsNull())
478 {
479 m_pObject->SetChanged(changed);
480 }
481 }
482
483 bool IsChanged() const override {return m_bIsChanged || (!IsNull() && m_pObject->IsChanged());}
484
485 bool IsNull() const override {return m_pObject == NULL;}
486
487 void SetNull() override
488 {
489 m_bIsChanged = true;
490 m_pObject.reset();
491 }
492
493 void Copy(const ContainerBase & that) override
494 {
495 if (this != &that)
496 {
497 if (typeid(*this) != typeid(that))
498 {
499 throw SoftwareViolationException(L"Invalid call to Copy, containers are not of same type",__WFILE__,__LINE__);
500 }
501 const ObjectContainerImpl<Object> & castedThat = static_cast<const ObjectContainerImpl<Object> &>(that);
502 m_bIsChanged = castedThat.m_bIsChanged;
503 if (that.IsNull())
504 {
505 m_pObject.reset();
506 }
507 else
508 {
509 SetObjectPointer(castedThat.m_pObject->Clone());
510 }
511 }
512 }
513
514 //Reflection part (Don't use unless you really know what you're doing!!)
515 ContainerBase & GetMember(const int member, const int index) override
516 {if (IsNull()) throw NullException(L"Object is null",__WFILE__,__LINE__); return m_pObject->GetMember(member,index);}
517 const ContainerBase & GetMember(const int member, const int index) const override
518 {if (IsNull()) throw NullException(L"Object is null",__WFILE__,__LINE__); return m_pObject->GetMember(member,index);}
519
520 private:
521 const ObjectPtr GetObjectPointer() const override {return std::static_pointer_cast<Object>(m_pObject);}
522 void SetObjectPointer(const ObjectPtr ptr) override
523 {
524 m_pObject = ptr;
525 }
526 void ResetObjectPointer() override {m_pObject.reset();}
527
528
529 T_Ptr m_pObject;
530 };
531
541}
542}
543}
544#endif
545
#define __WFILE__
Definition Exceptions.h:31
This namespace contains all the functionality and definitions of the SAFIR SDK.
Definition Backdoor.h:31
DotsC_Int32 Int32
32 bit integer type.
Definition Defs.h:66
Safir::Dob::Typesystem::ObjectContainerImpl< Object > ObjectContainer
Container for DOB Objects.
Definition ObjectContainer.h:540
std::shared_ptr< Object > ObjectPtr
A smart pointer to an Object.
Definition Object.h:44
Base class for all Containers.
Definition ContainerBase.h:44
bool m_bIsChanged
The variable containing the change flag.
Definition ContainerBase.h:127
virtual bool IsNull() const =0
Is the container set to null?
ContainerBase & operator=(const ContainerBase &other)
Copy assignment operator.
Definition ContainerBase.h:121
This exception is thrown if a class cannot be cast to the expected type.
Definition Exceptions.h:297
Meant to be used when something goes very wrong.
Definition Exceptions.h:364
Thrown when an application attempts to get the value of a member that is null.
Definition Exceptions.h:396
The base class for all DOB objects.
Definition Object.h:55
Base class for all object containers.
Definition ObjectContainer.h:45
void SetChangedHere(const bool changed)
Set the change flag in the container.
Definition ObjectContainer.h:82
virtual ContainerBase & GetMember(const int member, const int index)=0
Get a reference to a member container from an object.
virtual void ResetObjectPointer()=0
Reset (ie set to null) the contained pointer.
friend class Safir::Dob::Typesystem::Internal::BlobOperations
Definition ObjectContainer.h:164
virtual const ObjectPtr GetObjectPointer() const =0
Get a smart pointer to the contained object.
bool IsChangedHere() const
Is the change flag in the container set?
Definition ObjectContainer.h:73
virtual const ContainerBase & GetMember(const int member, const int index) const =0
Get a const reference to a member container from an object.
ObjectContainerBase()
Default constructor.
Definition ObjectContainer.h:48
virtual void SetPtr(const ObjectPtr &ptr)=0
Set the smart pointer in the container.
ObjectContainerBase(const ObjectContainerBase &)=default
Copy constructor.
ObjectContainerBase & operator=(const ObjectContainerBase &other)
Copy assignment operator.
Definition ObjectContainer.h:172
virtual void SetObjectPointer(const ObjectPtr ptr)=0
Set the smart pointer in the container.
Template class for all containers of automatically generated DOB objects.
Definition ObjectContainer.h:190
void SetObjectPointer(const ObjectPtr ptr) override
Set the smart pointer in the container.
Definition ObjectContainer.h:356
Int32 CalculateBlobSize() const
Calculate the size of the blob-serialized form of the contained object.
Definition ObjectContainer.h:351
void ResetObjectPointer() override
Reset (ie set to null) the contained pointer.
Definition ObjectContainer.h:368
void SetPtr(const ObjectPtr &ptr) override
Set the smart pointer in the container.
Definition ObjectContainer.h:264
void SetChanged(const bool changed) override
Set the containers change flag.
Definition ObjectContainer.h:299
ContainerBase & GetMember(const int member, const int index) override
Get a reference to a member container from an object.
Definition ObjectContainer.h:333
T_Ptr ContainedType
Definition ObjectContainer.h:194
bool IsNull() const override
Is the container set to null?
Definition ObjectContainer.h:303
const ObjectPtr GetObjectPointer() const override
Get a smart pointer to the contained object.
Definition ObjectContainer.h:355
ObjectContainerImpl & operator=(const ObjectContainerImpl &other)
Copy assignment operator.
Definition ObjectContainer.h:231
const T_Ptr & GetPtr() const
Get the smart pointer from the container.
Definition ObjectContainer.h:283
const ContainerBase & GetMember(const int member, const int index) const override
Get a const reference to a member container from an object.
Definition ObjectContainer.h:335
T * operator->() const
Dereference the smart pointer in the container.
Definition ObjectContainer.h:295
ObjectContainerImpl(const ObjectContainerImpl &other)
Copy constructor.
Definition ObjectContainer.h:211
void Copy(const ContainerBase &that) override
Virtual assignment.
Definition ObjectContainer.h:311
bool IsChanged() const override
Is the change flag set on the container?
Definition ObjectContainer.h:301
void SetNull() override
Set the container to null.
Definition ObjectContainer.h:305
ObjectContainerImpl()
Default constructor.
Definition ObjectContainer.h:201
std::shared_ptr< T > T_Ptr
Typedef for the contained smart pointer.
Definition ObjectContainer.h:193
void SetPtr(const T_Ptr &ptr)
Set the smart pointer in the container.
Definition ObjectContainer.h:258
ObjectContainerImpl & operator=(const ObjectContainerImpl &other)
Copy assignment operator.
Definition ObjectContainer.h:417
const T_Ptr & GetPtr() const
Get the smart pointer from the container.
Definition ObjectContainer.h:447
Object * operator->() const
Dereference the smart pointer in the container.
Definition ObjectContainer.h:465
void SetChanged(const bool changed) override
Set the containers change flag.
Definition ObjectContainer.h:474
ContainerBase & GetMember(const int member, const int index) override
Get a reference to a member container from an object.
Definition ObjectContainer.h:515
void SetPtr(const ObjectPtr &ptr) override
Set the smart pointer in the container.
Definition ObjectContainer.h:432
ObjectContainerImpl()
Default constructor.
Definition ObjectContainer.h:392
T_Ptr ContainedType
Definition ObjectContainer.h:385
bool IsChanged() const override
Is the change flag set on the container?
Definition ObjectContainer.h:483
bool IsNull() const override
Is the container set to null?
Definition ObjectContainer.h:485
void Copy(const ContainerBase &that) override
Virtual assignment.
Definition ObjectContainer.h:493
std::shared_ptr< Object > T_Ptr
Typedef for the contained smart pointer.
Definition ObjectContainer.h:384
ObjectContainerImpl(const ObjectContainerImpl &other)
Copy constructor.
Definition ObjectContainer.h:402
void SetNull() override
Set the container to null.
Definition ObjectContainer.h:487
const ContainerBase & GetMember(const int member, const int index) const override
Get a const reference to a member container from an object.
Definition ObjectContainer.h:517