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 //===----------------------------------------------------------------------===//
13 #include "MIDataTypes.h"
16 //============================================================================
17 // Details: MI common code utility class. The class implements behaviour of a
18 // variant object which holds any data object of type T. A copy of the
19 // data object specified is made and stored in *this wrapper. When the
20 // *this object is destroyed the data object hold within calls its
21 // destructor should it have one.
23 class CMIUtilVariant {
26 /* ctor */ CMIUtilVariant();
27 /* ctor */ CMIUtilVariant(const CMIUtilVariant &vrOther);
28 /* ctor */ CMIUtilVariant(CMIUtilVariant &vrOther);
29 /* ctor */ CMIUtilVariant(CMIUtilVariant &&vrwOther);
30 /* dtor */ ~CMIUtilVariant();
32 template <typename T> void Set(const T &vArg);
33 template <typename T> T *Get() const;
35 CMIUtilVariant &operator=(const CMIUtilVariant &vrOther);
36 CMIUtilVariant &operator=(CMIUtilVariant &&vrwOther);
40 //++ ----------------------------------------------------------------------
41 // Details: Base class wrapper to hold the variant's data object when
42 // assigned to it by the Set() function. Do not use the
44 // to create objects, use only CDataObjectBase derived objects,
45 // see CDataObject() class.
47 class CDataObjectBase {
50 /* ctor */ CDataObjectBase();
51 /* ctor */ CDataObjectBase(const CDataObjectBase &vrOther);
52 /* ctor */ CDataObjectBase(CDataObjectBase &vrOther);
53 /* ctor */ CDataObjectBase(CDataObjectBase &&vrwOther);
55 CDataObjectBase &operator=(const CDataObjectBase &vrOther);
56 CDataObjectBase &operator=(CDataObjectBase &&vrwOther);
60 virtual ~CDataObjectBase();
61 virtual CDataObjectBase *CreateCopyOfSelf();
62 virtual bool GetIsDerivedClass() const;
66 virtual void Copy(const CDataObjectBase &vrOther);
67 virtual void Destroy();
70 //++ ----------------------------------------------------------------------
71 // Details: Derived from CDataObjectBase, this class is the wrapper for the
72 // data object as it has an aggregate of type T which is a copy
73 // of the data object assigned to the variant object.
75 template <typename T> class CDataObject : public CDataObjectBase {
78 /* ctor */ CDataObject();
79 /* ctor */ CDataObject(const T &vArg);
80 /* ctor */ CDataObject(const CDataObject &vrOther);
81 /* ctor */ CDataObject(CDataObject &vrOther);
82 /* ctor */ CDataObject(CDataObject &&vrwOther);
84 CDataObject &operator=(const CDataObject &vrOther);
85 CDataObject &operator=(CDataObject &&vrwOther);
91 // From CDataObjectBase
92 ~CDataObject() override;
93 CDataObjectBase *CreateCopyOfSelf() override;
94 bool GetIsDerivedClass() const override;
98 virtual void Duplicate(const CDataObject &vrOther);
102 // From CDataObjectBase
103 void Destroy() override;
113 void Copy(const CMIUtilVariant &vrOther);
117 CDataObjectBase *m_pDataObject;
120 //---------------------------------------------------------------------------------------
121 //---------------------------------------------------------------------------------------
122 //---------------------------------------------------------------------------------------
125 //------------------------------------------------------------------------------------
126 // Details: CDataObject constructor.
128 // Args: T - The object's type.
132 template <typename T> CMIUtilVariant::CDataObject<T>::CDataObject() {}
135 //------------------------------------------------------------------------------------
136 // Details: CDataObject constructor.
138 // Args: T - The object's type.
139 // vArg - (R) The data object to be stored in the variant object.
143 template <typename T>
144 CMIUtilVariant::CDataObject<T>::CDataObject(const T &vArg) {
149 //------------------------------------------------------------------------------------
150 // Details: CDataObject destructor.
152 // Args: T - The object's type.
156 template <typename T> CMIUtilVariant::CDataObject<T>::~CDataObject() {
161 //------------------------------------------------------------------------------------
162 // Details: Retrieve the data object hold by *this object wrapper.
164 // Args: T - The object's type.
165 // Return: T & - Reference to the data object.
168 template <typename T> T &CMIUtilVariant::CDataObject<T>::GetDataObject() {
173 //------------------------------------------------------------------------------------
174 // Details: Create a new copy of *this class.
176 // Args: T - The object's type.
177 // Return: CDataObjectBase * - Pointer to a new object.
180 template <typename T>
181 CMIUtilVariant::CDataObjectBase *
182 CMIUtilVariant::CDataObject<T>::CreateCopyOfSelf() {
183 CDataObject *pCopy = new CDataObject<T>(m_dataObj);
189 //------------------------------------------------------------------------------------
190 // Details: Determine if *this object is a derived from CDataObjectBase.
192 // Args: T - The object's type.
193 // Return: bool - True = *this is derived from CDataObjectBase
194 // - False = *this is an instance of the base class.
197 template <typename T>
198 bool CMIUtilVariant::CDataObject<T>::GetIsDerivedClass() const {
203 //------------------------------------------------------------------------------------
204 // Details: Perform a bitwise copy of *this object.
205 // Type: Overrideable.
206 // Args: T - The object's type.
207 // vrOther - (R) The other object.
211 template <typename T>
212 void CMIUtilVariant::CDataObject<T>::Duplicate(const CDataObject &vrOther) {
213 CDataObjectBase::Copy(vrOther);
214 m_dataObj = vrOther.m_dataObj;
218 //------------------------------------------------------------------------------------
219 // Details: Release any resources used by *this object.
225 template <typename T> void CMIUtilVariant::CDataObject<T>::Destroy() {
226 CDataObjectBase::Destroy();
229 //---------------------------------------------------------------------------------------
230 //---------------------------------------------------------------------------------------
231 //---------------------------------------------------------------------------------------
234 //------------------------------------------------------------------------------------
235 // Details: Assign to the variant an object of a specified type.
236 // Type: Template method.
237 // Args: T - The object's type.
238 // vArg - (R) The object to store.
242 template <typename T> void CMIUtilVariant::Set(const T &vArg) {
243 m_pDataObject = new CDataObject<T>(vArg);
247 //------------------------------------------------------------------------------------
248 // Details: Retrieve the data object from *this variant.
249 // Type: Template method.
250 // Args: T - The object's type.
251 // Return: T * - Pointer the data object, NULL = data object not assigned to
255 template <typename T> T *CMIUtilVariant::Get() const {
256 if ((m_pDataObject != nullptr) && m_pDataObject->GetIsDerivedClass()) {
257 CDataObject<T> *pDataObj = static_cast<CDataObject<T> *>(m_pDataObject);
258 return &pDataObj->GetDataObject();
261 // Do not use a CDataObjectBase object, use only CDataObjectBase derived