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