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
91
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
286
295 const T_Ptr GetPtrOrNull() const {return IsNull() ? T_Ptr() : m_pObject;}
296
306 T * operator->() const
307 { if (IsNull()) throw NullException(L"Object is null",__WFILE__,__LINE__); return m_pObject.operator->(); }
308
309
310 void SetChanged(const bool changed) override {m_bIsChanged = changed; if (!IsNull()) m_pObject->SetChanged(changed);}
311
312 bool IsChanged() const override {return m_bIsChanged || (!IsNull() && m_pObject->IsChanged());}
313
314 bool IsNull() const override {return m_pObject == NULL;}
315
316 bool HasVal() const override {return m_pObject != NULL;}
317
318 void SetNull() override
319 {
320 m_bIsChanged = true;
321 m_pObject.reset();
322 }
323
324 void Copy(const ContainerBase & that) override
325 {
326 if (this != &that)
327 {
328 if (typeid(*this) != typeid(that))
329 {
330 throw SoftwareViolationException(L"Invalid call to Copy, containers are not of same type",__WFILE__,__LINE__);
331 }
332 const ObjectContainerImpl<T> & castedThat = static_cast<const ObjectContainerImpl<T> &>(that);
333 m_bIsChanged = castedThat.m_bIsChanged;
334 if (that.IsNull())
335 {
336 m_pObject.reset();
337 }
338 else
339 {
340 SetObjectPointer(castedThat.m_pObject->Clone());
341 }
342 }
343 }
344
345 //Reflection part (Don't use unless you really know what you're doing!!)
346 ContainerBase & GetMember(const int member, const int index) override
347 {if (IsNull()) throw NullException(L"Object is null",__WFILE__,__LINE__); return m_pObject->GetMember(member,index);}
348 const ContainerBase & GetMember(const int member, const int index) const override
349 {if (IsNull()) throw NullException(L"Object is null",__WFILE__,__LINE__); return m_pObject->GetMember(member,index);}
350
351
358
364 Int32 CalculateBlobSize() const {if (IsNull()) return 0; else return m_pObject->CalculateBlobSize();}
365
367
368 const ObjectPtr GetObjectPointer() const override {return std::static_pointer_cast<Object>(m_pObject);}
369 void SetObjectPointer(const ObjectPtr ptr) override
370 {
371#ifndef NDEBUG
372 m_pObject = std::dynamic_pointer_cast<T>(ptr);
373 if(m_pObject == NULL)
374 {
375 throw SoftwareViolationException(L"Failed to cast object pointer to expected type",__WFILE__,__LINE__);
376 }
377#else
378 m_pObject = std::static_pointer_cast<T>(ptr);
379#endif
380 }
381 void ResetObjectPointer() override {m_pObject.reset();}
382 private:
383 T_Ptr m_pObject;
384 };
385
392 template <>
393 class ObjectContainerImpl<Object> : public ObjectContainerBase
394 {
395 public:
397 typedef std::shared_ptr<Object> T_Ptr;
398 typedef T_Ptr ContainedType;
399
405 ObjectContainerImpl():ObjectContainerBase() {}
406
407
415 ObjectContainerImpl(const ObjectContainerImpl & other):
416 ObjectContainerBase(other) //make sure we use copy constructor of parent
417 {
418 if (!other.IsNull())
419 {
420 m_pObject = other.m_pObject->Clone();
421 }
422 }
423
430 ObjectContainerImpl & operator=(const ObjectContainerImpl & other)
431 {
432 ObjectContainerBase::operator =(other);//make sure we use copy assignment of parent
433 if (other.IsNull())
434 {
435 m_pObject.reset();
436 }
437 else
438 {
439 m_pObject = other.m_pObject->Clone();
440 }
441 return *this;
442 }
443
444
445 void SetPtr(const ObjectPtr & ptr) override
446 {
447 m_bIsChanged = true;
448 m_pObject = ptr;
449 }
450
460 const T_Ptr & GetPtr() const
461 {
462 if (IsNull())
463 {
464 throw NullException(L"Object is null",__WFILE__,__LINE__);
465 }
466 return m_pObject;
467 }
468
477 const T_Ptr GetPtrOrNull() const {return IsNull() ? T_Ptr() : m_pObject;}
478
488 Object * operator->() const
489 {
490 if (IsNull())
491 {
492 throw NullException(L"Object is null",__WFILE__,__LINE__);
493 }
494 return m_pObject.operator->();
495 }
496
497 void SetChanged(const bool changed) override
498 {
499 m_bIsChanged = changed;
500 if (!IsNull())
501 {
502 m_pObject->SetChanged(changed);
503 }
504 }
505
506 bool IsChanged() const override {return m_bIsChanged || (!IsNull() && m_pObject->IsChanged());}
507
508 bool IsNull() const override {return m_pObject == NULL;}
509
510 bool HasVal() const override {return m_pObject != NULL;}
511
512 void SetNull() override
513 {
514 m_bIsChanged = true;
515 m_pObject.reset();
516 }
517
518 void Copy(const ContainerBase & that) override
519 {
520 if (this != &that)
521 {
522 if (typeid(*this) != typeid(that))
523 {
524 throw SoftwareViolationException(L"Invalid call to Copy, containers are not of same type",__WFILE__,__LINE__);
525 }
526 const ObjectContainerImpl<Object> & castedThat = static_cast<const ObjectContainerImpl<Object> &>(that);
527 m_bIsChanged = castedThat.m_bIsChanged;
528 if (that.IsNull())
529 {
530 m_pObject.reset();
531 }
532 else
533 {
534 SetObjectPointer(castedThat.m_pObject->Clone());
535 }
536 }
537 }
538
539 //Reflection part (Don't use unless you really know what you're doing!!)
540 ContainerBase & GetMember(const int member, const int index) override
541 {if (IsNull()) throw NullException(L"Object is null",__WFILE__,__LINE__); return m_pObject->GetMember(member,index);}
542 const ContainerBase & GetMember(const int member, const int index) const override
543 {if (IsNull()) throw NullException(L"Object is null",__WFILE__,__LINE__); return m_pObject->GetMember(member,index);}
544
545 private:
546 const ObjectPtr GetObjectPointer() const override {return std::static_pointer_cast<Object>(m_pObject);}
547 void SetObjectPointer(const ObjectPtr ptr) override
548 {
549 m_pObject = ptr;
550 }
551 void ResetObjectPointer() override {m_pObject.reset();}
552
553
554 T_Ptr m_pObject;
555 };
556
566}
567}
568}
569#endif
570
#define __WFILE__
Definition Exceptions.h:31
This namespace contains all the functionality and definitions of the SAFIR SDK.
Definition Backdoor.h:31
This namespace contains all functionality of the DOB (Components DOSE and DOTS).
Definition Connection.h:38
This namespace contains the DOB Typesystem functionality and definitions.
Definition ArrayContainer.h:37
DotsC_Int32 Int32
32 bit integer type.
Definition Defs.h:66
Safir::Dob::Typesystem::ObjectContainerImpl< Object > ObjectContainer
Container for DOB Objects.
Definition ObjectContainer.h:565
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:134
constexpr ContainerBase()
Default Constructor.
Definition ContainerBase.h:51
virtual bool IsNull() const =0
Is the container set to null?
ContainerBase & operator=(const ContainerBase &other)
Copy assignment operator.
Definition ContainerBase.h:128
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
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.
void SetObjectPointer(const ObjectPtr ptr) override
Definition ObjectContainer.h:369
Int32 CalculateBlobSize() const
Calculate the size of the blob-serialized form of the contained object.
Definition ObjectContainer.h:364
void ResetObjectPointer() override
Reset (ie set to null) the contained pointer.
Definition ObjectContainer.h:381
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:310
const T_Ptr GetPtrOrNull() const
Get the smart pointer from the container or a nullptr if container is null.
Definition ObjectContainer.h:295
ContainerBase & GetMember(const int member, const int index) override
Get a reference to a member container from an object.
Definition ObjectContainer.h:346
T_Ptr ContainedType
Definition ObjectContainer.h:194
bool IsNull() const override
Is the container set to null?
Definition ObjectContainer.h:314
const ObjectPtr GetObjectPointer() const override
Get a smart pointer to the contained object.
Definition ObjectContainer.h:368
bool HasVal() const override
Does the container have a value?
Definition ObjectContainer.h:316
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:348
T * operator->() const
Dereference the smart pointer in the container.
Definition ObjectContainer.h:306
ObjectContainerImpl(const ObjectContainerImpl &other)
Copy constructor.
Definition ObjectContainer.h:211
void Copy(const ContainerBase &that) override
Virtual assignment.
Definition ObjectContainer.h:324
bool IsChanged() const override
Is the change flag set on the container?
Definition ObjectContainer.h:312
void SetNull() override
Set the container to null.
Definition ObjectContainer.h:318
ObjectContainerImpl()
Default constructor.
Definition ObjectContainer.h:201
std::shared_ptr< U > T_Ptr
Definition ObjectContainer.h:193
void SetPtr(const T_Ptr &ptr)
Set the smart pointer in the container.
Definition ObjectContainer.h:258