1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 | //===-- MIUtilVariant.h -----------------------------------------*- C++ -*-===// // // The LLVM Compiler Infrastructure // // This file is distributed under the University of Illinois Open Source // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// #pragma once // In-house headers: #include "MIDataTypes.h" //++ //============================================================================ // Details: MI common code utility class. The class implements behaviour of a // variant object which holds any data object of type T. A copy of the // data object specified is made and stored in *this wrapper. When the // *this object is destroyed the data object hold within calls its // destructor should it have one. //-- class CMIUtilVariant { // Methods: public: /* ctor */ CMIUtilVariant(); /* ctor */ CMIUtilVariant(const CMIUtilVariant &vrOther); /* ctor */ CMIUtilVariant(CMIUtilVariant &vrOther); /* ctor */ CMIUtilVariant(CMIUtilVariant &&vrwOther); /* dtor */ ~CMIUtilVariant(); template <typename T> void Set(const T &vArg); template <typename T> T *Get() const; CMIUtilVariant &operator=(const CMIUtilVariant &vrOther); CMIUtilVariant &operator=(CMIUtilVariant &&vrwOther); // Classes: private: //++ ---------------------------------------------------------------------- // Details: Base class wrapper to hold the variant's data object when // assigned to it by the Set() function. Do not use the // CDataObjectBase // to create objects, use only CDataObjectBase derived objects, // see CDataObject() class. //-- class CDataObjectBase { // Methods: public: /* ctor */ CDataObjectBase(); /* ctor */ CDataObjectBase(const CDataObjectBase &vrOther); /* ctor */ CDataObjectBase(CDataObjectBase &vrOther); /* ctor */ CDataObjectBase(CDataObjectBase &&vrwOther); // CDataObjectBase &operator=(const CDataObjectBase &vrOther); CDataObjectBase &operator=(CDataObjectBase &&vrwOther); // Overrideable: public: virtual ~CDataObjectBase(); virtual CDataObjectBase *CreateCopyOfSelf(); virtual bool GetIsDerivedClass() const; // Overrideable: protected: virtual void Copy(const CDataObjectBase &vrOther); virtual void Destroy(); }; //++ ---------------------------------------------------------------------- // Details: Derived from CDataObjectBase, this class is the wrapper for the // data object as it has an aggregate of type T which is a copy // of the data object assigned to the variant object. //-- template <typename T> class CDataObject : public CDataObjectBase { // Methods: public: /* ctor */ CDataObject(); /* ctor */ CDataObject(const T &vArg); /* ctor */ CDataObject(const CDataObject &vrOther); /* ctor */ CDataObject(CDataObject &vrOther); /* ctor */ CDataObject(CDataObject &&vrwOther); // CDataObject &operator=(const CDataObject &vrOther); CDataObject &operator=(CDataObject &&vrwOther); // T &GetDataObject(); // Overridden: public: // From CDataObjectBase ~CDataObject() override; CDataObjectBase *CreateCopyOfSelf() override; bool GetIsDerivedClass() const override; // Overrideable: private: virtual void Duplicate(const CDataObject &vrOther); // Overridden: private: // From CDataObjectBase void Destroy() override; // Attributes: private: T m_dataObj; }; // Methods private: void Destroy(); void Copy(const CMIUtilVariant &vrOther); // Attributes: private: CDataObjectBase *m_pDataObject; }; //--------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------- //++ //------------------------------------------------------------------------------------ // Details: CDataObject constructor. // Type: Method. // Args: T - The object's type. // Return: None. // Throws: None. //-- template <typename T> CMIUtilVariant::CDataObject<T>::CDataObject() {} //++ //------------------------------------------------------------------------------------ // Details: CDataObject constructor. // Type: Method. // Args: T - The object's type. // vArg - (R) The data object to be stored in the variant object. // Return: None. // Throws: None. //-- template <typename T> CMIUtilVariant::CDataObject<T>::CDataObject(const T &vArg) { m_dataObj = vArg; } //++ //------------------------------------------------------------------------------------ // Details: CDataObject destructor. // Type: Overridden. // Args: T - The object's type. // Return: None. // Throws: None. //-- template <typename T> CMIUtilVariant::CDataObject<T>::~CDataObject() { Destroy(); } //++ //------------------------------------------------------------------------------------ // Details: Retrieve the data object hold by *this object wrapper. // Type: Method. // Args: T - The object's type. // Return: T & - Reference to the data object. // Throws: None. //-- template <typename T> T &CMIUtilVariant::CDataObject<T>::GetDataObject() { return m_dataObj; } //++ //------------------------------------------------------------------------------------ // Details: Create a new copy of *this class. // Type: Overridden. // Args: T - The object's type. // Return: CDataObjectBase * - Pointer to a new object. // Throws: None. //-- template <typename T> CMIUtilVariant::CDataObjectBase * CMIUtilVariant::CDataObject<T>::CreateCopyOfSelf() { CDataObject *pCopy = new CDataObject<T>(m_dataObj); return pCopy; } //++ //------------------------------------------------------------------------------------ // Details: Determine if *this object is a derived from CDataObjectBase. // Type: Overridden. // Args: T - The object's type. // Return: bool - True = *this is derived from CDataObjectBase // - False = *this is an instance of the base class. // Throws: None. //-- template <typename T> bool CMIUtilVariant::CDataObject<T>::GetIsDerivedClass() const { return true; } //++ //------------------------------------------------------------------------------------ // Details: Perform a bitwise copy of *this object. // Type: Overrideable. // Args: T - The object's type. // vrOther - (R) The other object. // Return: None. // Throws: None. //-- template <typename T> void CMIUtilVariant::CDataObject<T>::Duplicate(const CDataObject &vrOther) { CDataObjectBase::Copy(vrOther); m_dataObj = vrOther.m_dataObj; } //++ //------------------------------------------------------------------------------------ // Details: Release any resources used by *this object. // Type: Overridden. // Args: None. // Return: None. // Throws: None. //-- template <typename T> void CMIUtilVariant::CDataObject<T>::Destroy() { CDataObjectBase::Destroy(); } //--------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------- //++ //------------------------------------------------------------------------------------ // Details: Assign to the variant an object of a specified type. // Type: Template method. // Args: T - The object's type. // vArg - (R) The object to store. // Return: None. // Throws: None. //-- template <typename T> void CMIUtilVariant::Set(const T &vArg) { m_pDataObject = new CDataObject<T>(vArg); } //++ //------------------------------------------------------------------------------------ // Details: Retrieve the data object from *this variant. // Type: Template method. // Args: T - The object's type. // Return: T * - Pointer the data object, NULL = data object not assigned to // *this variant. // Throws: None. //-- template <typename T> T *CMIUtilVariant::Get() const { if ((m_pDataObject != nullptr) && m_pDataObject->GetIsDerivedClass()) { CDataObject<T> *pDataObj = static_cast<CDataObject<T> *>(m_pDataObject); return &pDataObj->GetDataObject(); } // Do not use a CDataObjectBase object, use only CDataObjectBase derived // objects return nullptr; } |