2014-06-25 00:35:50 +08:00
|
|
|
//===-- MIUtilMapIdToVariant.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
|
|
|
|
|
|
|
|
// Third party headers:
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
// In-house headers:
|
|
|
|
#include "MICmnBase.h"
|
|
|
|
#include "MICmnResources.h"
|
2014-11-18 02:06:21 +08:00
|
|
|
#include "MIUtilString.h"
|
2014-06-25 00:35:50 +08:00
|
|
|
#include "MIUtilVariant.h"
|
|
|
|
|
|
|
|
//++ ============================================================================
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: MI common code utility class. Map type container that hold general
|
|
|
|
// object types (by being wrappin an variant wrapper)
|
|
|
|
// objects by ID.
|
|
|
|
// Gotchas: None.
|
|
|
|
// Authors: Illya Rudkin 19/06/2014.
|
|
|
|
// Changes: None.
|
2014-06-25 00:35:50 +08:00
|
|
|
//--
|
|
|
|
class CMIUtilMapIdToVariant : public CMICmnBase
|
|
|
|
{
|
2014-11-18 02:06:21 +08:00
|
|
|
// Methods:
|
|
|
|
public:
|
|
|
|
/* ctor */ CMIUtilMapIdToVariant(void);
|
|
|
|
|
|
|
|
template <typename T> bool Add(const CMIUtilString &vId, const T &vData);
|
|
|
|
void Clear(void);
|
|
|
|
template <typename T> bool Get(const CMIUtilString &vId, T &vrwData, bool &vrwbFound) const;
|
|
|
|
bool HaveAlready(const CMIUtilString &vId) const;
|
|
|
|
bool IsEmpty(void) const;
|
|
|
|
bool Remove(const CMIUtilString &vId);
|
|
|
|
|
|
|
|
// Overridden:
|
|
|
|
public:
|
|
|
|
// From CMICmnBase
|
|
|
|
/* dtor */ virtual ~CMIUtilMapIdToVariant(void);
|
|
|
|
|
|
|
|
// Typdefs:
|
|
|
|
private:
|
|
|
|
typedef std::map<CMIUtilString, CMIUtilVariant> MapKeyToVariantValue_t;
|
|
|
|
typedef std::pair<CMIUtilString, CMIUtilVariant> MapPairKeyToVariantValue_t;
|
|
|
|
|
|
|
|
// Methods:
|
|
|
|
private:
|
|
|
|
bool IsValid(const CMIUtilString &vId) const;
|
|
|
|
|
|
|
|
// Attributes:
|
|
|
|
MapKeyToVariantValue_t m_mapKeyToVariantValue;
|
2014-06-25 00:35:50 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
//++ ------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: Add to *this container a data object of general type identified by an ID.
|
|
|
|
// If the data with that ID already exists in the container it is replace with
|
|
|
|
// the new data specified.
|
|
|
|
// Type: Method.
|
|
|
|
// Args: T - The data object's variable type.
|
|
|
|
// vId - (R) Unique ID i.e. GUID.
|
|
|
|
// vData - (R) The general data object to be stored of some type.
|
|
|
|
// Return: MIstatus::success - Functional succeeded.
|
|
|
|
// MIstatus::failure - Functional failed.
|
|
|
|
// Throws: None.
|
2014-06-25 00:35:50 +08:00
|
|
|
//--
|
2014-11-18 02:06:21 +08:00
|
|
|
template <typename T>
|
|
|
|
bool
|
|
|
|
CMIUtilMapIdToVariant::Add(const CMIUtilString &vId, const T &vData)
|
2014-06-25 00:35:50 +08:00
|
|
|
{
|
2014-11-18 02:06:21 +08:00
|
|
|
if (!IsValid(vId))
|
|
|
|
{
|
|
|
|
SetErrorDescription(CMIUtilString::Format(MIRSRC(IDS_VARIANT_ERR_MAP_KEY_INVALID), vId.c_str()));
|
|
|
|
return MIstatus::failure;
|
|
|
|
}
|
|
|
|
|
|
|
|
const bool bOk = HaveAlready(vId) ? Remove(vId) : MIstatus::success;
|
|
|
|
if (bOk)
|
|
|
|
{
|
|
|
|
CMIUtilVariant data;
|
|
|
|
data.Set<T>(vData);
|
|
|
|
MapPairKeyToVariantValue_t pr(vId, data);
|
|
|
|
m_mapKeyToVariantValue.insert(pr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return bOk;
|
2014-06-25 00:35:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//++ ------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: Retrieve a data object from *this container identified by the specified ID.
|
|
|
|
// Type: Method.
|
|
|
|
// Args: T - The data object's variable type.
|
|
|
|
// vId - (R) Unique ID i.e. GUID.
|
|
|
|
// vrwData - (W) Copy of the data object held.
|
|
|
|
// vrwbFound - (W) True = data found, false = data not found.
|
|
|
|
// Return: MIstatus::success - Functional succeeded.
|
|
|
|
// MIstatus::failure - Functional failed.
|
|
|
|
// Throws: None.
|
2014-06-25 00:35:50 +08:00
|
|
|
//--
|
2014-11-18 02:06:21 +08:00
|
|
|
template <typename T>
|
|
|
|
bool
|
|
|
|
CMIUtilMapIdToVariant::Get(const CMIUtilString &vId, T &vrwData, bool &vrwbFound) const
|
2014-06-25 00:35:50 +08:00
|
|
|
{
|
2014-11-18 02:06:21 +08:00
|
|
|
vrwbFound = false;
|
|
|
|
|
|
|
|
if (!IsValid(vId))
|
|
|
|
{
|
|
|
|
SetErrorDescription(CMIUtilString::Format(MIRSRC(IDS_VARIANT_ERR_MAP_KEY_INVALID), vId.c_str()));
|
|
|
|
return MIstatus::failure;
|
|
|
|
}
|
|
|
|
|
|
|
|
const MapKeyToVariantValue_t::const_iterator it = m_mapKeyToVariantValue.find(vId);
|
|
|
|
if (it != m_mapKeyToVariantValue.end())
|
|
|
|
{
|
|
|
|
const CMIUtilVariant &rData = (*it).second;
|
|
|
|
const T *pDataObj = rData.Get<T>();
|
|
|
|
if (pDataObj != nullptr)
|
|
|
|
{
|
|
|
|
vrwbFound = true;
|
|
|
|
vrwData = *pDataObj;
|
|
|
|
return MIstatus::success;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SetErrorDescription(MIRSRC(IDS_VARIANT_ERR_USED_BASECLASS));
|
|
|
|
return MIstatus::failure;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return MIstatus::success;
|
2014-06-25 00:35:50 +08:00
|
|
|
}
|