1 //===-- MIUtilMapIdToVariant.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 //===----------------------------------------------------------------------===//
11 // Third party headers:
15 #include "MICmnBase.h"
16 #include "MICmnResources.h"
17 #include "MIUtilString.h"
18 #include "MIUtilVariant.h"
21 //============================================================================
22 // Details: MI common code utility class. Map type container that hold general
23 // object types (by being a variant wrapper)
26 class CMIUtilMapIdToVariant : public CMICmnBase {
29 /* ctor */ CMIUtilMapIdToVariant();
31 template <typename T> bool Add(const CMIUtilString &vId, const T &vData);
34 bool Get(const CMIUtilString &vId, T &vrwData, bool &vrwbFound) const;
35 bool HaveAlready(const CMIUtilString &vId) const;
37 bool Remove(const CMIUtilString &vId);
42 /* dtor */ ~CMIUtilMapIdToVariant() override;
46 typedef std::map<CMIUtilString, CMIUtilVariant> MapKeyToVariantValue_t;
47 typedef std::pair<CMIUtilString, CMIUtilVariant> MapPairKeyToVariantValue_t;
51 bool IsValid(const CMIUtilString &vId) const;
54 MapKeyToVariantValue_t m_mapKeyToVariantValue;
58 // Details: Add to *this container a data object of general type identified by
60 // If the data with that ID already exists in the container it is
62 // the new data specified.
64 // Args: T - The data object's variable type.
65 // vId - (R) Unique ID i.e. GUID.
66 // vData - (R) The general data object to be stored of some type.
67 // Return: MIstatus::success - Function succeeded.
68 // MIstatus::failure - Function failed.
72 bool CMIUtilMapIdToVariant::Add(const CMIUtilString &vId, const T &vData) {
74 SetErrorDescription(CMIUtilString::Format(
75 MIRSRC(IDS_VARIANT_ERR_MAP_KEY_INVALID), vId.c_str()));
76 return MIstatus::failure;
79 const bool bOk = HaveAlready(vId) ? Remove(vId) : MIstatus::success;
83 MapPairKeyToVariantValue_t pr(vId, data);
84 m_mapKeyToVariantValue.insert(pr);
91 // Details: Retrieve a data object from *this container identified by the
94 // Args: T - The data object's variable type.
95 // vId - (R) Unique ID i.e. GUID.
96 // vrwData - (W) Copy of the data object held.
97 // vrwbFound - (W) True = data found, false = data not found.
98 // Return: MIstatus::success - Function succeeded.
99 // MIstatus::failure - Function failed.
102 template <typename T>
103 bool CMIUtilMapIdToVariant::Get(const CMIUtilString &vId, T &vrwData,
104 bool &vrwbFound) const {
108 SetErrorDescription(CMIUtilString::Format(
109 MIRSRC(IDS_VARIANT_ERR_MAP_KEY_INVALID), vId.c_str()));
110 return MIstatus::failure;
113 const MapKeyToVariantValue_t::const_iterator it =
114 m_mapKeyToVariantValue.find(vId);
115 if (it != m_mapKeyToVariantValue.end()) {
116 const CMIUtilVariant &rData = (*it).second;
117 const T *pDataObj = rData.Get<T>();
118 if (pDataObj != nullptr) {
121 return MIstatus::success;
123 SetErrorDescription(MIRSRC(IDS_VARIANT_ERR_USED_BASECLASS));
124 return MIstatus::failure;
128 return MIstatus::success;