1 //===-- MIUtilVariant.h -----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
15 #include "MIDataTypes.h"
17 //++ ============================================================================
18 // Details: MI common code utility class. The class implements behaviour of a
19 // variant object which holds any data object of type T. A copy of the
20 // data object specified is made and stored in *this wrapper. When the
21 // *this object is destroyed the data object hold within calls its
22 // destructor should it have one.
24 // Authors: Illya Rudkin 18/06/2014.
31 /* ctor */ CMIUtilVariant(void);
32 /* ctor */ CMIUtilVariant(const CMIUtilVariant &vrOther);
33 /* ctor */ CMIUtilVariant(CMIUtilVariant &vrOther);
34 /* ctor */ CMIUtilVariant(CMIUtilVariant &&vrwOther);
35 /* dtor */ ~CMIUtilVariant(void);
37 template <typename T> void Set(const T &vArg);
38 template <typename T> T *Get(void) const;
40 CMIUtilVariant &operator=(const CMIUtilVariant &vrOther);
41 CMIUtilVariant &operator=(CMIUtilVariant &&vrwOther);
45 //++ ----------------------------------------------------------------------
46 // Details: Base class wrapper to hold the variant's data object when
47 // assigned to it by the Set() function. Do not use the CDataObjectBase
48 // to create objects, use only CDataObjectBase derived objects,
49 // see CDataObject() class.
55 /* ctor */ CDataObjectBase(void);
56 /* ctor */ CDataObjectBase(const CDataObjectBase &vrOther);
57 /* ctor */ CDataObjectBase(CDataObjectBase &vrOther);
58 /* ctor */ CDataObjectBase(CDataObjectBase &&vrwOther);
60 CDataObjectBase &operator=(const CDataObjectBase &vrOther);
61 CDataObjectBase &operator=(CDataObjectBase &&vrwOther);
65 virtual ~CDataObjectBase(void);
66 virtual CDataObjectBase *CreateCopyOfSelf(void);
67 virtual bool GetIsDerivedClass(void) const;
71 virtual void Copy(const CDataObjectBase &vrOther);
72 virtual void Destroy(void);
75 //++ ----------------------------------------------------------------------
76 // Details: Derived from CDataObjectBase, this class is the wrapper for the
77 // data object as it has an aggregate of type T which is a copy
78 // of the data object assigned to the variant object.
80 template <typename T> class CDataObject : public CDataObjectBase
84 /* ctor */ CDataObject(void);
85 /* ctor */ CDataObject(const T &vArg);
86 /* ctor */ CDataObject(const CDataObject &vrOther);
87 /* ctor */ CDataObject(CDataObject &vrOther);
88 /* ctor */ CDataObject(CDataObject &&vrwOther);
90 CDataObject &operator=(const CDataObject &vrOther);
91 CDataObject &operator=(CDataObject &&vrwOther);
93 T &GetDataObject(void);
97 // From CDataObjectBase
98 ~CDataObject(void) override;
99 CDataObjectBase *CreateCopyOfSelf(void) override;
100 bool GetIsDerivedClass(void) const override;
104 virtual void Duplicate(const CDataObject &vrOther);
108 // From CDataObjectBase
109 void Destroy(void) override;
119 void Copy(const CMIUtilVariant &vrOther);
123 CDataObjectBase *m_pDataObject;
126 //---------------------------------------------------------------------------------------
127 //---------------------------------------------------------------------------------------
128 //---------------------------------------------------------------------------------------
130 //++ ------------------------------------------------------------------------------------
131 // Details: CDataObject constructor.
133 // Args: T - The object's type.
137 template <typename T> CMIUtilVariant::CDataObject<T>::CDataObject(void)
141 //++ ------------------------------------------------------------------------------------
142 // Details: CDataObject constructor.
144 // Args: T - The object's type.
145 // vArg - (R) The data object to be stored in the variant object.
149 template <typename T> CMIUtilVariant::CDataObject<T>::CDataObject(const T &vArg)
154 //++ ------------------------------------------------------------------------------------
155 // Details: CDataObject destructor.
157 // Args: T - The object's type.
161 template <typename T> CMIUtilVariant::CDataObject<T>::~CDataObject(void)
166 //++ ------------------------------------------------------------------------------------
167 // Details: Retrieve the data object hold by *this object wrapper.
169 // Args: T - The object's type.
170 // Return: T & - Reference to the data object.
173 template <typename T>
175 CMIUtilVariant::CDataObject<T>::GetDataObject(void)
180 //++ ------------------------------------------------------------------------------------
181 // Details: Create a new copy of *this class.
183 // Args: T - The object's type.
184 // Return: CDataObjectBase * - Pointer to a new object.
187 template <typename T>
188 CMIUtilVariant::CDataObjectBase *
189 CMIUtilVariant::CDataObject<T>::CreateCopyOfSelf(void)
191 CDataObject *pCopy = new CDataObject<T>(m_dataObj);
196 //++ ------------------------------------------------------------------------------------
197 // Details: Determine if *this object is a derived from CDataObjectBase.
199 // Args: T - The object's type.
200 // Return: bool - True = *this is derived from CDataObjectBase
201 // - False = *this is an instance of the base class.
204 template <typename T>
206 CMIUtilVariant::CDataObject<T>::GetIsDerivedClass(void) const
211 //++ ------------------------------------------------------------------------------------
212 // Details: Perform a bitwise copy of *this object.
213 // Type: Overrideable.
214 // Args: T - The object's type.
215 // vrOther - (R) The other object.
219 template <typename T>
221 CMIUtilVariant::CDataObject<T>::Duplicate(const CDataObject &vrOther)
223 CDataObjectBase::Copy(vrOther);
224 m_dataObj = vrOther.m_dataObj;
227 //++ ------------------------------------------------------------------------------------
228 // Details: Release any resources used by *this object.
234 template <typename T>
236 CMIUtilVariant::CDataObject<T>::Destroy(void)
238 CDataObjectBase::Destroy();
241 //---------------------------------------------------------------------------------------
242 //---------------------------------------------------------------------------------------
243 //---------------------------------------------------------------------------------------
245 //++ ------------------------------------------------------------------------------------
246 // Details: Assign to the variant an object of a specified type.
247 // Type: Template method.
248 // Args: T - The object's type.
249 // vArg - (R) The object to store.
253 template <typename T>
255 CMIUtilVariant::Set(const T &vArg)
257 m_pDataObject = new CDataObject<T>(vArg);
260 //++ ------------------------------------------------------------------------------------
261 // Details: Retrieve the data object from *this variant.
262 // Type: Template method.
263 // Args: T - The object's type.
264 // Return: T * - Pointer the data object, NULL = data object not assigned to *this variant.
267 template <typename T>
269 CMIUtilVariant::Get(void) const
271 if ((m_pDataObject != nullptr) && m_pDataObject->GetIsDerivedClass())
273 CDataObject<T> *pDataObj = static_cast<CDataObject<T> *>(m_pDataObject);
274 return &pDataObj->GetDataObject();
277 // Do not use a CDataObjectBase object, use only CDataObjectBase derived objects