]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/tools/lldb-mi/MIUtilMapIdToVariant.h
Merge lldb trunk r366426, resolve conflicts, and update FREEBSD-Xlist.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / tools / lldb-mi / MIUtilMapIdToVariant.h
1 //===-- MIUtilMapIdToVariant.h ----------------------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8
9 #pragma once
10
11 // Third party headers:
12 #include <map>
13
14 // In-house headers:
15 #include "MICmnBase.h"
16 #include "MICmnResources.h"
17 #include "MIUtilString.h"
18 #include "MIUtilVariant.h"
19
20 //++
21 //============================================================================
22 // Details: MI common code utility class. Map type container that hold general
23 //          object types (by being a variant wrapper)
24 //          objects by ID.
25 //--
26 class CMIUtilMapIdToVariant : public CMICmnBase {
27   // Methods:
28 public:
29   /* ctor */ CMIUtilMapIdToVariant();
30
31   template <typename T> bool Add(const CMIUtilString &vId, const T &vData);
32   void Clear();
33   template <typename T>
34   bool Get(const CMIUtilString &vId, T &vrwData, bool &vrwbFound) const;
35   bool HaveAlready(const CMIUtilString &vId) const;
36   bool IsEmpty() const;
37   bool Remove(const CMIUtilString &vId);
38
39   // Overridden:
40 public:
41   // From CMICmnBase
42   /* dtor */ ~CMIUtilMapIdToVariant() override;
43
44   // Typedefs:
45 private:
46   typedef std::map<CMIUtilString, CMIUtilVariant> MapKeyToVariantValue_t;
47   typedef std::pair<CMIUtilString, CMIUtilVariant> MapPairKeyToVariantValue_t;
48
49   // Methods:
50 private:
51   bool IsValid(const CMIUtilString &vId) const;
52
53   // Attributes:
54   MapKeyToVariantValue_t m_mapKeyToVariantValue;
55 };
56
57 //++
58 // Details: Add to *this container a data object of general type identified by
59 // an ID.
60 //          If the data with that ID already exists in the container it is
61 //          replace with
62 //          the new data specified.
63 // Type:    Method.
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.
69 // Throws:  None.
70 //--
71 template <typename T>
72 bool CMIUtilMapIdToVariant::Add(const CMIUtilString &vId, const T &vData) {
73   if (!IsValid(vId)) {
74     SetErrorDescription(CMIUtilString::Format(
75         MIRSRC(IDS_VARIANT_ERR_MAP_KEY_INVALID), vId.c_str()));
76     return MIstatus::failure;
77   }
78
79   const bool bOk = HaveAlready(vId) ? Remove(vId) : MIstatus::success;
80   if (bOk) {
81     CMIUtilVariant data;
82     data.Set<T>(vData);
83     MapPairKeyToVariantValue_t pr(vId, data);
84     m_mapKeyToVariantValue.insert(pr);
85   }
86
87   return bOk;
88 }
89
90 //++
91 // Details: Retrieve a data object from *this container identified by the
92 // specified ID.
93 // Type:    Method.
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.
100 // Throws:  None.
101 //--
102 template <typename T>
103 bool CMIUtilMapIdToVariant::Get(const CMIUtilString &vId, T &vrwData,
104                                 bool &vrwbFound) const {
105   vrwbFound = false;
106
107   if (!IsValid(vId)) {
108     SetErrorDescription(CMIUtilString::Format(
109         MIRSRC(IDS_VARIANT_ERR_MAP_KEY_INVALID), vId.c_str()));
110     return MIstatus::failure;
111   }
112
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) {
119       vrwbFound = true;
120       vrwData = *pDataObj;
121       return MIstatus::success;
122     } else {
123       SetErrorDescription(MIRSRC(IDS_VARIANT_ERR_USED_BASECLASS));
124       return MIstatus::failure;
125     }
126   }
127
128   return MIstatus::success;
129 }