2014-06-25 00:35:50 +08:00
|
|
|
//===-- 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:
|
2014-11-18 02:06:21 +08:00
|
|
|
#include "MIDataTypes.h"
|
2014-06-25 00:35:50 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
//++
|
|
|
|
//============================================================================
|
2014-11-18 02:06:21 +08:00
|
|
|
// 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.
|
2014-06-25 00:35:50 +08:00
|
|
|
//--
|
2016-09-07 04:57:50 +08:00
|
|
|
class CMIUtilVariant {
|
|
|
|
// Methods:
|
|
|
|
public:
|
|
|
|
/* ctor */ CMIUtilVariant();
|
|
|
|
/* ctor */ CMIUtilVariant(const CMIUtilVariant &vrOther);
|
|
|
|
/* ctor */ CMIUtilVariant(CMIUtilVariant &vrOther);
|
|
|
|
/* ctor */ CMIUtilVariant(CMIUtilVariant &&vrwOther);
|
|
|
|
/* dtor */ ~CMIUtilVariant();
|
2014-06-25 00:35:50 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
template <typename T> void Set(const T &vArg);
|
|
|
|
template <typename T> T *Get() const;
|
2014-06-25 00:35:50 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
CMIUtilVariant &operator=(const CMIUtilVariant &vrOther);
|
|
|
|
CMIUtilVariant &operator=(CMIUtilVariant &&vrwOther);
|
2014-06-25 00:35:50 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
// 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);
|
2014-06-25 00:35:50 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
// Overrideable:
|
|
|
|
public:
|
|
|
|
virtual ~CDataObjectBase();
|
|
|
|
virtual CDataObjectBase *CreateCopyOfSelf();
|
|
|
|
virtual bool GetIsDerivedClass() const;
|
2014-11-06 21:42:49 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
// Overrideable:
|
|
|
|
protected:
|
|
|
|
virtual void Copy(const CDataObjectBase &vrOther);
|
|
|
|
virtual void Destroy();
|
|
|
|
};
|
2014-06-25 00:35:50 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
//++ ----------------------------------------------------------------------
|
|
|
|
// 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();
|
2014-06-25 00:35:50 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
// Overridden:
|
|
|
|
public:
|
|
|
|
// From CDataObjectBase
|
|
|
|
~CDataObject() override;
|
|
|
|
CDataObjectBase *CreateCopyOfSelf() override;
|
|
|
|
bool GetIsDerivedClass() const override;
|
2014-06-25 00:35:50 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
// Overrideable:
|
|
|
|
private:
|
|
|
|
virtual void Duplicate(const CDataObject &vrOther);
|
2014-11-18 02:06:21 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
// Overridden:
|
2014-11-18 02:06:21 +08:00
|
|
|
private:
|
2016-09-07 04:57:50 +08:00
|
|
|
// From CDataObjectBase
|
|
|
|
void Destroy() override;
|
2014-11-18 02:06:21 +08:00
|
|
|
|
|
|
|
// Attributes:
|
|
|
|
private:
|
2016-09-07 04:57:50 +08:00
|
|
|
T m_dataObj;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Methods
|
|
|
|
private:
|
|
|
|
void Destroy();
|
|
|
|
void Copy(const CMIUtilVariant &vrOther);
|
|
|
|
|
|
|
|
// Attributes:
|
|
|
|
private:
|
|
|
|
CDataObjectBase *m_pDataObject;
|
2014-06-25 00:35:50 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------
|
|
|
|
//---------------------------------------------------------------------------------------
|
|
|
|
//---------------------------------------------------------------------------------------
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
//++
|
|
|
|
//------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: CDataObject constructor.
|
|
|
|
// Type: Method.
|
|
|
|
// Args: T - The object's type.
|
|
|
|
// Return: None.
|
|
|
|
// Throws: None.
|
2014-06-25 00:35:50 +08:00
|
|
|
//--
|
2016-09-07 04:57:50 +08:00
|
|
|
template <typename T> CMIUtilVariant::CDataObject<T>::CDataObject() {}
|
2014-06-25 00:35:50 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
//++
|
|
|
|
//------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// 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.
|
2014-06-25 00:35:50 +08:00
|
|
|
//--
|
2016-09-07 04:57:50 +08:00
|
|
|
template <typename T>
|
|
|
|
CMIUtilVariant::CDataObject<T>::CDataObject(const T &vArg) {
|
|
|
|
m_dataObj = vArg;
|
2014-06-25 00:35:50 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
//++
|
|
|
|
//------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: CDataObject destructor.
|
|
|
|
// Type: Overridden.
|
|
|
|
// Args: T - The object's type.
|
|
|
|
// Return: None.
|
|
|
|
// Throws: None.
|
2014-06-25 00:35:50 +08:00
|
|
|
//--
|
2016-09-07 04:57:50 +08:00
|
|
|
template <typename T> CMIUtilVariant::CDataObject<T>::~CDataObject() {
|
|
|
|
Destroy();
|
2014-06-25 00:35:50 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
//++
|
|
|
|
//------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// 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.
|
2014-06-25 00:35:50 +08:00
|
|
|
//--
|
2016-09-07 04:57:50 +08:00
|
|
|
template <typename T> T &CMIUtilVariant::CDataObject<T>::GetDataObject() {
|
|
|
|
return m_dataObj;
|
2014-06-25 00:35:50 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
//++
|
|
|
|
//------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// 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.
|
2014-06-25 00:35:50 +08:00
|
|
|
//--
|
2014-11-18 02:06:21 +08:00
|
|
|
template <typename T>
|
|
|
|
CMIUtilVariant::CDataObjectBase *
|
2016-09-07 04:57:50 +08:00
|
|
|
CMIUtilVariant::CDataObject<T>::CreateCopyOfSelf() {
|
|
|
|
CDataObject *pCopy = new CDataObject<T>(m_dataObj);
|
2014-11-18 02:06:21 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
return pCopy;
|
2014-06-25 00:35:50 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
//++
|
|
|
|
//------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// 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.
|
2014-06-25 00:35:50 +08:00
|
|
|
//--
|
2014-11-18 02:06:21 +08:00
|
|
|
template <typename T>
|
2016-09-07 04:57:50 +08:00
|
|
|
bool CMIUtilVariant::CDataObject<T>::GetIsDerivedClass() const {
|
|
|
|
return true;
|
2014-06-25 00:35:50 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
//++
|
|
|
|
//------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// 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.
|
2014-06-25 00:35:50 +08:00
|
|
|
//--
|
2014-11-18 02:06:21 +08:00
|
|
|
template <typename T>
|
2016-09-07 04:57:50 +08:00
|
|
|
void CMIUtilVariant::CDataObject<T>::Duplicate(const CDataObject &vrOther) {
|
|
|
|
CDataObjectBase::Copy(vrOther);
|
|
|
|
m_dataObj = vrOther.m_dataObj;
|
2014-06-25 00:35:50 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
//++
|
|
|
|
//------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: Release any resources used by *this object.
|
|
|
|
// Type: Overridden.
|
|
|
|
// Args: None.
|
|
|
|
// Return: None.
|
|
|
|
// Throws: None.
|
2014-08-09 00:47:42 +08:00
|
|
|
//--
|
2016-09-07 04:57:50 +08:00
|
|
|
template <typename T> void CMIUtilVariant::CDataObject<T>::Destroy() {
|
|
|
|
CDataObjectBase::Destroy();
|
2014-08-09 00:47:42 +08:00
|
|
|
}
|
|
|
|
|
2014-06-25 00:35:50 +08:00
|
|
|
//---------------------------------------------------------------------------------------
|
|
|
|
//---------------------------------------------------------------------------------------
|
|
|
|
//---------------------------------------------------------------------------------------
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
//++
|
|
|
|
//------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// 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.
|
2014-06-25 00:35:50 +08:00
|
|
|
//--
|
2016-09-07 04:57:50 +08:00
|
|
|
template <typename T> void CMIUtilVariant::Set(const T &vArg) {
|
|
|
|
m_pDataObject = new CDataObject<T>(vArg);
|
2014-06-25 00:35:50 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
//++
|
|
|
|
//------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: Retrieve the data object from *this variant.
|
|
|
|
// Type: Template method.
|
|
|
|
// Args: T - The object's type.
|
2016-09-07 04:57:50 +08:00
|
|
|
// Return: T * - Pointer the data object, NULL = data object not assigned to
|
|
|
|
// *this variant.
|
2014-11-18 02:06:21 +08:00
|
|
|
// Throws: None.
|
2014-06-25 00:35:50 +08:00
|
|
|
//--
|
2016-09-07 04:57:50 +08:00
|
|
|
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();
|
|
|
|
}
|
2014-06-25 00:35:50 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
// Do not use a CDataObjectBase object, use only CDataObjectBase derived
|
|
|
|
// objects
|
|
|
|
return nullptr;
|
2014-08-09 00:47:42 +08:00
|
|
|
}
|