1 //===-- MIUtilVariant.h -----------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
12 #include "MIDataTypes.h"
15 //============================================================================
16 // Details: MI common code utility class. The class implements behaviour of a
17 // variant object which holds any data object of type T. A copy of the
18 // data object specified is made and stored in *this wrapper. When the
19 // *this object is destroyed the data object hold within calls its
20 // destructor should it have one.
22 class CMIUtilVariant {
25 /* ctor */ CMIUtilVariant();
26 /* ctor */ CMIUtilVariant(const CMIUtilVariant &vrOther);
27 /* ctor */ CMIUtilVariant(CMIUtilVariant &vrOther);
28 /* ctor */ CMIUtilVariant(CMIUtilVariant &&vrwOther);
29 /* dtor */ ~CMIUtilVariant();
31 template <typename T> void Set(const T &vArg);
32 template <typename T> T *Get() const;
34 CMIUtilVariant &operator=(const CMIUtilVariant &vrOther);
35 CMIUtilVariant &operator=(CMIUtilVariant &&vrwOther);
39 //++ ----------------------------------------------------------------------
40 // Details: Base class wrapper to hold the variant's data object when
41 // assigned to it by the Set() function. Do not use the
43 // to create objects, use only CDataObjectBase derived objects,
44 // see CDataObject() class.
46 class CDataObjectBase {
49 /* ctor */ CDataObjectBase();
50 /* ctor */ CDataObjectBase(const CDataObjectBase &vrOther);
51 /* ctor */ CDataObjectBase(CDataObjectBase &vrOther);
52 /* ctor */ CDataObjectBase(CDataObjectBase &&vrwOther);
54 CDataObjectBase &operator=(const CDataObjectBase &vrOther);
55 CDataObjectBase &operator=(CDataObjectBase &&vrwOther);
59 virtual ~CDataObjectBase();
60 virtual CDataObjectBase *CreateCopyOfSelf();
61 virtual bool GetIsDerivedClass() const;
65 virtual void Copy(const CDataObjectBase &vrOther);
66 virtual void Destroy();
69 //++ ----------------------------------------------------------------------
70 // Details: Derived from CDataObjectBase, this class is the wrapper for the
71 // data object as it has an aggregate of type T which is a copy
72 // of the data object assigned to the variant object.
74 template <typename T> class CDataObject : public CDataObjectBase {
77 /* ctor */ CDataObject();
78 /* ctor */ CDataObject(const T &vArg);
79 /* ctor */ CDataObject(const CDataObject &vrOther);
80 /* ctor */ CDataObject(CDataObject &vrOther);
81 /* ctor */ CDataObject(CDataObject &&vrwOther);
83 CDataObject &operator=(const CDataObject &vrOther);
84 CDataObject &operator=(CDataObject &&vrwOther);
90 // From CDataObjectBase
91 ~CDataObject() override;
92 CDataObjectBase *CreateCopyOfSelf() override;
93 bool GetIsDerivedClass() const override;
97 virtual void Duplicate(const CDataObject &vrOther);
101 // From CDataObjectBase
102 void Destroy() override;
112 void Copy(const CMIUtilVariant &vrOther);
116 CDataObjectBase *m_pDataObject;
121 // Details: CDataObject constructor.
123 // Args: T - The object's type.
127 template <typename T> CMIUtilVariant::CDataObject<T>::CDataObject() {}
130 // Details: CDataObject constructor.
132 // Args: T - The object's type.
133 // vArg - (R) The data object to be stored in the variant object.
137 template <typename T>
138 CMIUtilVariant::CDataObject<T>::CDataObject(const T &vArg) {
143 // Details: CDataObject destructor.
145 // Args: T - The object's type.
149 template <typename T> CMIUtilVariant::CDataObject<T>::~CDataObject() {
154 // Details: Retrieve the data object hold by *this object wrapper.
156 // Args: T - The object's type.
157 // Return: T & - Reference to the data object.
160 template <typename T> T &CMIUtilVariant::CDataObject<T>::GetDataObject() {
165 // Details: Create a new copy of *this class.
167 // Args: T - The object's type.
168 // Return: CDataObjectBase * - Pointer to a new object.
171 template <typename T>
172 CMIUtilVariant::CDataObjectBase *
173 CMIUtilVariant::CDataObject<T>::CreateCopyOfSelf() {
174 CDataObject *pCopy = new CDataObject<T>(m_dataObj);
180 // Details: Determine if *this object is a derived from CDataObjectBase.
182 // Args: T - The object's type.
183 // Return: bool - True = *this is derived from CDataObjectBase
184 // - False = *this is an instance of the base class.
187 template <typename T>
188 bool CMIUtilVariant::CDataObject<T>::GetIsDerivedClass() const {
193 // Details: Perform a bitwise copy of *this object.
194 // Type: Overrideable.
195 // Args: T - The object's type.
196 // vrOther - (R) The other object.
200 template <typename T>
201 void CMIUtilVariant::CDataObject<T>::Duplicate(const CDataObject &vrOther) {
202 CDataObjectBase::Copy(vrOther);
203 m_dataObj = vrOther.m_dataObj;
207 // Details: Release any resources used by *this object.
213 template <typename T> void CMIUtilVariant::CDataObject<T>::Destroy() {
214 CDataObjectBase::Destroy();
219 // Details: Assign to the variant an object of a specified type.
220 // Type: Template method.
221 // Args: T - The object's type.
222 // vArg - (R) The object to store.
226 template <typename T> void CMIUtilVariant::Set(const T &vArg) {
227 m_pDataObject = new CDataObject<T>(vArg);
231 // Details: Retrieve the data object from *this variant.
232 // Type: Template method.
233 // Args: T - The object's type.
234 // Return: T * - Pointer the data object, NULL = data object not assigned to
238 template <typename T> T *CMIUtilVariant::Get() const {
239 if ((m_pDataObject != nullptr) && m_pDataObject->GetIsDerivedClass()) {
240 CDataObject<T> *pDataObj = static_cast<CDataObject<T> *>(m_pDataObject);
241 return &pDataObj->GetDataObject();
244 // Do not use a CDataObjectBase object, use only CDataObjectBase derived