1 //===-- MIUtilMapIdToVariant.h ----------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #pragma once
11
12 // Third party headers:
13 #include <map>
14
15 // In-house headers:
16 #include "MICmnBase.h"
17 #include "MICmnResources.h"
18 #include "MIUtilString.h"
19 #include "MIUtilVariant.h"
20
21 //++
22 //============================================================================
23 // Details: MI common code utility class. Map type container that hold general
24 // object types (by being a variant wrapper)
25 // objects by ID.
26 //--
27 class CMIUtilMapIdToVariant : public CMICmnBase {
28 // Methods:
29 public:
30 /* ctor */ CMIUtilMapIdToVariant();
31
32 template <typename T> bool Add(const CMIUtilString &vId, const T &vData);
33 void Clear();
34 template <typename T>
35 bool Get(const CMIUtilString &vId, T &vrwData, bool &vrwbFound) const;
36 bool HaveAlready(const CMIUtilString &vId) const;
37 bool IsEmpty() const;
38 bool Remove(const CMIUtilString &vId);
39
40 // Overridden:
41 public:
42 // From CMICmnBase
43 /* dtor */ ~CMIUtilMapIdToVariant() override;
44
45 // Typedefs:
46 private:
47 typedef std::map<CMIUtilString, CMIUtilVariant> MapKeyToVariantValue_t;
48 typedef std::pair<CMIUtilString, CMIUtilVariant> MapPairKeyToVariantValue_t;
49
50 // Methods:
51 private:
52 bool IsValid(const CMIUtilString &vId) const;
53
54 // Attributes:
55 MapKeyToVariantValue_t m_mapKeyToVariantValue;
56 };
57
58 //++
59 //------------------------------------------------------------------------------------
60 // Details: Add to *this container a data object of general type identified by
61 // an ID.
62 // If the data with that ID already exists in the container it is
63 // replace with
64 // the new data specified.
65 // Type: Method.
66 // Args: T - The data object's variable type.
67 // vId - (R) Unique ID i.e. GUID.
68 // vData - (R) The general data object to be stored of some type.
69 // Return: MIstatus::success - Function succeeded.
70 // MIstatus::failure - Function failed.
71 // Throws: None.
72 //--
73 template <typename T>
Add(const CMIUtilString & vId,const T & vData)74 bool CMIUtilMapIdToVariant::Add(const CMIUtilString &vId, const T &vData) {
75 if (!IsValid(vId)) {
76 SetErrorDescription(CMIUtilString::Format(
77 MIRSRC(IDS_VARIANT_ERR_MAP_KEY_INVALID), vId.c_str()));
78 return MIstatus::failure;
79 }
80
81 const bool bOk = HaveAlready(vId) ? Remove(vId) : MIstatus::success;
82 if (bOk) {
83 CMIUtilVariant data;
84 data.Set<T>(vData);
85 MapPairKeyToVariantValue_t pr(vId, data);
86 m_mapKeyToVariantValue.insert(pr);
87 }
88
89 return bOk;
90 }
91
92 //++
93 //------------------------------------------------------------------------------------
94 // Details: Retrieve a data object from *this container identified by the
95 // specified ID.
96 // Type: Method.
97 // Args: T - The data object's variable type.
98 // vId - (R) Unique ID i.e. GUID.
99 // vrwData - (W) Copy of the data object held.
100 // vrwbFound - (W) True = data found, false = data not found.
101 // Return: MIstatus::success - Function succeeded.
102 // MIstatus::failure - Function failed.
103 // Throws: None.
104 //--
105 template <typename T>
Get(const CMIUtilString & vId,T & vrwData,bool & vrwbFound)106 bool CMIUtilMapIdToVariant::Get(const CMIUtilString &vId, T &vrwData,
107 bool &vrwbFound) const {
108 vrwbFound = false;
109
110 if (!IsValid(vId)) {
111 SetErrorDescription(CMIUtilString::Format(
112 MIRSRC(IDS_VARIANT_ERR_MAP_KEY_INVALID), vId.c_str()));
113 return MIstatus::failure;
114 }
115
116 const MapKeyToVariantValue_t::const_iterator it =
117 m_mapKeyToVariantValue.find(vId);
118 if (it != m_mapKeyToVariantValue.end()) {
119 const CMIUtilVariant &rData = (*it).second;
120 const T *pDataObj = rData.Get<T>();
121 if (pDataObj != nullptr) {
122 vrwbFound = true;
123 vrwData = *pDataObj;
124 return MIstatus::success;
125 } else {
126 SetErrorDescription(MIRSRC(IDS_VARIANT_ERR_USED_BASECLASS));
127 return MIstatus::failure;
128 }
129 }
130
131 return MIstatus::success;
132 }
133