]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/tools/lldb-mi/MIUtilVariant.h
Update lldb to upstream trunk r242221.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / tools / lldb-mi / MIUtilVariant.h
1 //===-- MIUtilVariant.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 // Copyright:   None.
10 //--
11
12 #pragma once
13
14 // In-house headers:
15 #include "MIDataTypes.h"
16
17 //++ ============================================================================
18 // Details: MI common code utility class. The class implements behaviour of a
19 //          variant object which holds any data object of type T. A copy of the
20 //          data object specified is made and stored in *this wrapper. When the
21 //          *this object is destroyed the data object hold within calls its
22 //          destructor should it have one.
23 // Gotchas: None.
24 // Authors: Illya Rudkin 18/06/2014.
25 // Changes: None.
26 //--
27 class CMIUtilVariant
28 {
29     // Methods:
30   public:
31     /* ctor */ CMIUtilVariant(void);
32     /* ctor */ CMIUtilVariant(const CMIUtilVariant &vrOther);
33     /* ctor */ CMIUtilVariant(CMIUtilVariant &vrOther);
34     /* ctor */ CMIUtilVariant(CMIUtilVariant &&vrwOther);
35     /* dtor */ ~CMIUtilVariant(void);
36
37     template <typename T> void Set(const T &vArg);
38     template <typename T> T *Get(void) const;
39
40     CMIUtilVariant &operator=(const CMIUtilVariant &vrOther);
41     CMIUtilVariant &operator=(CMIUtilVariant &&vrwOther);
42
43     // Classes:
44   private:
45     //++ ----------------------------------------------------------------------
46     // Details: Base class wrapper to hold the variant's data object when
47     //          assigned to it by the Set() function. Do not use the CDataObjectBase
48     //          to create objects, use only CDataObjectBase derived objects,
49     //          see CDataObject() class.
50     //--
51     class CDataObjectBase
52     {
53         // Methods:
54       public:
55         /* ctor */ CDataObjectBase(void);
56         /* ctor */ CDataObjectBase(const CDataObjectBase &vrOther);
57         /* ctor */ CDataObjectBase(CDataObjectBase &vrOther);
58         /* ctor */ CDataObjectBase(CDataObjectBase &&vrwOther);
59         //
60         CDataObjectBase &operator=(const CDataObjectBase &vrOther);
61         CDataObjectBase &operator=(CDataObjectBase &&vrwOther);
62
63         // Overrideable:
64       public:
65         virtual ~CDataObjectBase(void);
66         virtual CDataObjectBase *CreateCopyOfSelf(void);
67         virtual bool GetIsDerivedClass(void) const;
68
69         // Overrideable:
70       protected:
71         virtual void Copy(const CDataObjectBase &vrOther);
72         virtual void Destroy(void);
73     };
74
75     //++ ----------------------------------------------------------------------
76     // Details: Derived from CDataObjectBase, this class is the wrapper for the
77     //          data object as it has an aggregate of type T which is a copy
78     //          of the data object assigned to the variant object.
79     //--
80     template <typename T> class CDataObject : public CDataObjectBase
81     {
82         // Methods:
83       public:
84         /* ctor */ CDataObject(void);
85         /* ctor */ CDataObject(const T &vArg);
86         /* ctor */ CDataObject(const CDataObject &vrOther);
87         /* ctor */ CDataObject(CDataObject &vrOther);
88         /* ctor */ CDataObject(CDataObject &&vrwOther);
89         //
90         CDataObject &operator=(const CDataObject &vrOther);
91         CDataObject &operator=(CDataObject &&vrwOther);
92         //
93         T &GetDataObject(void);
94
95         // Overridden:
96       public:
97         // From CDataObjectBase
98         ~CDataObject(void) override;
99         CDataObjectBase *CreateCopyOfSelf(void) override;
100         bool GetIsDerivedClass(void) const override;
101
102         // Overrideable:
103       private:
104         virtual void Duplicate(const CDataObject &vrOther);
105
106         // Overridden:
107       private:
108         // From CDataObjectBase
109         void Destroy(void) override;
110
111         // Attributes:
112       private:
113         T m_dataObj;
114     };
115
116     // Methods
117   private:
118     void Destroy(void);
119     void Copy(const CMIUtilVariant &vrOther);
120
121     // Attributes:
122   private:
123     CDataObjectBase *m_pDataObject;
124 };
125
126 //---------------------------------------------------------------------------------------
127 //---------------------------------------------------------------------------------------
128 //---------------------------------------------------------------------------------------
129
130 //++ ------------------------------------------------------------------------------------
131 // Details: CDataObject constructor.
132 // Type:    Method.
133 // Args:    T   - The object's type.
134 // Return:  None.
135 // Throws:  None.
136 //--
137 template <typename T> CMIUtilVariant::CDataObject<T>::CDataObject(void)
138 {
139 }
140
141 //++ ------------------------------------------------------------------------------------
142 // Details: CDataObject constructor.
143 // Type:    Method.
144 // Args:    T       - The object's type.
145 //          vArg    - (R) The data object to be stored in the variant object.
146 // Return:  None.
147 // Throws:  None.
148 //--
149 template <typename T> CMIUtilVariant::CDataObject<T>::CDataObject(const T &vArg)
150 {
151     m_dataObj = vArg;
152 }
153
154 //++ ------------------------------------------------------------------------------------
155 // Details: CDataObject destructor.
156 // Type:    Overridden.
157 // Args:    T   - The object's type.
158 // Return:  None.
159 // Throws:  None.
160 //--
161 template <typename T> CMIUtilVariant::CDataObject<T>::~CDataObject(void)
162 {
163     Destroy();
164 }
165
166 //++ ------------------------------------------------------------------------------------
167 // Details: Retrieve the data object hold by *this object wrapper.
168 // Type:    Method.
169 // Args:    T   - The object's type.
170 // Return:  T & - Reference to the data object.
171 // Throws:  None.
172 //--
173 template <typename T>
174 T &
175 CMIUtilVariant::CDataObject<T>::GetDataObject(void)
176 {
177     return m_dataObj;
178 }
179
180 //++ ------------------------------------------------------------------------------------
181 // Details: Create a new copy of *this class.
182 // Type:    Overridden.
183 // Args:    T   - The object's type.
184 // Return:  CDataObjectBase *   - Pointer to a new object.
185 // Throws:  None.
186 //--
187 template <typename T>
188 CMIUtilVariant::CDataObjectBase *
189 CMIUtilVariant::CDataObject<T>::CreateCopyOfSelf(void)
190 {
191     CDataObject *pCopy = new CDataObject<T>(m_dataObj);
192
193     return pCopy;
194 }
195
196 //++ ------------------------------------------------------------------------------------
197 // Details: Determine if *this object is a derived from CDataObjectBase.
198 // Type:    Overridden.
199 // Args:    T   - The object's type.
200 // Return:  bool    - True = *this is derived from CDataObjectBase
201 //                  - False = *this is an instance of the base class.
202 // Throws:  None.
203 //--
204 template <typename T>
205 bool
206 CMIUtilVariant::CDataObject<T>::GetIsDerivedClass(void) const
207 {
208     return true;
209 }
210
211 //++ ------------------------------------------------------------------------------------
212 // Details: Perform a bitwise copy of *this object.
213 // Type:    Overrideable.
214 // Args:    T       - The object's type.
215 //          vrOther - (R) The other object.
216 // Return:  None.
217 // Throws:  None.
218 //--
219 template <typename T>
220 void
221 CMIUtilVariant::CDataObject<T>::Duplicate(const CDataObject &vrOther)
222 {
223     CDataObjectBase::Copy(vrOther);
224     m_dataObj = vrOther.m_dataObj;
225 }
226
227 //++ ------------------------------------------------------------------------------------
228 // Details: Release any resources used by *this object.
229 // Type:    Overridden.
230 // Args:    None.
231 // Return:  None.
232 // Throws:  None.
233 //--
234 template <typename T>
235 void
236 CMIUtilVariant::CDataObject<T>::Destroy(void)
237 {
238     CDataObjectBase::Destroy();
239 }
240
241 //---------------------------------------------------------------------------------------
242 //---------------------------------------------------------------------------------------
243 //---------------------------------------------------------------------------------------
244
245 //++ ------------------------------------------------------------------------------------
246 // Details: Assign to the variant an object of a specified type.
247 // Type:    Template method.
248 // Args:    T       - The object's type.
249 //          vArg    - (R) The object to store.
250 // Return:  None.
251 // Throws:  None.
252 //--
253 template <typename T>
254 void
255 CMIUtilVariant::Set(const T &vArg)
256 {
257     m_pDataObject = new CDataObject<T>(vArg);
258 }
259
260 //++ ------------------------------------------------------------------------------------
261 // Details: Retrieve the data object from *this variant.
262 // Type:    Template method.
263 // Args:    T   - The object's type.
264 // Return:  T * - Pointer the data object, NULL = data object not assigned to *this variant.
265 // Throws:  None.
266 //--
267 template <typename T>
268 T *
269 CMIUtilVariant::Get(void) const
270 {
271     if ((m_pDataObject != nullptr) && m_pDataObject->GetIsDerivedClass())
272     {
273         CDataObject<T> *pDataObj = static_cast<CDataObject<T> *>(m_pDataObject);
274         return &pDataObj->GetDataObject();
275     }
276
277     // Do not use a CDataObjectBase object, use only CDataObjectBase derived objects
278     return nullptr;
279 }