1 |
|
/****************************************************************************** |
2 |
|
* Top contributors (to current version): |
3 |
|
* Morgan Deters, Clark Barrett, Mathias Preiner |
4 |
|
* |
5 |
|
* This file is part of the cvc5 project. |
6 |
|
* |
7 |
|
* Copyright (c) 2009-2021 by the authors listed in the file AUTHORS |
8 |
|
* in the top-level source directory and their institutional affiliations. |
9 |
|
* All rights reserved. See the file COPYING in the top-level source |
10 |
|
* directory for licensing information. |
11 |
|
* **************************************************************************** |
12 |
|
* |
13 |
|
* A context-dependent object. |
14 |
|
*/ |
15 |
|
|
16 |
|
#include "cvc5_private.h" |
17 |
|
|
18 |
|
#ifndef CVC5__CONTEXT__CDO_H |
19 |
|
#define CVC5__CONTEXT__CDO_H |
20 |
|
|
21 |
|
#include "context/context.h" |
22 |
|
|
23 |
|
namespace cvc5 { |
24 |
|
namespace context { |
25 |
|
|
26 |
|
/** |
27 |
|
* Most basic template for context-dependent objects. Simply makes a copy |
28 |
|
* (using the copy constructor) of class T when saving, and copies it back |
29 |
|
* (using operator=) during restore. |
30 |
|
*/ |
31 |
|
template <class T> |
32 |
|
class CDO : public ContextObj { |
33 |
|
|
34 |
|
/** |
35 |
|
* The data of type T being stored in this context-dependent object. |
36 |
|
*/ |
37 |
|
T d_data; |
38 |
|
|
39 |
|
protected: |
40 |
|
|
41 |
|
/** |
42 |
|
* Copy constructor - it's private to ensure it is only used by save(). |
43 |
|
* Basic CDO objects, cannot be copied-they have to be unique. |
44 |
|
*/ |
45 |
39493496 |
CDO(const CDO<T>& cdo) : ContextObj(cdo), d_data(cdo.d_data) {} |
46 |
|
|
47 |
|
/** |
48 |
|
* operator= for CDO is private to ensure CDO object is not copied. |
49 |
|
*/ |
50 |
|
CDO<T>& operator=(const CDO<T>& cdo) = delete; |
51 |
|
|
52 |
|
/** |
53 |
|
* Implementation of mandatory ContextObj method save: simply copies the |
54 |
|
* current data to a copy using the copy constructor. Memory is allocated |
55 |
|
* using the ContextMemoryManager. |
56 |
|
*/ |
57 |
39493496 |
ContextObj* save(ContextMemoryManager* pCMM) override |
58 |
|
{ |
59 |
39493496 |
Debug("context") << "save cdo " << this; |
60 |
39493496 |
ContextObj* p = new(pCMM) CDO<T>(*this); |
61 |
39493496 |
Debug("context") << " to " << p << std::endl; |
62 |
39493496 |
return p; |
63 |
|
} |
64 |
|
|
65 |
|
/** |
66 |
|
* Implementation of mandatory ContextObj method restore: simply copies the |
67 |
|
* saved data back from the saved copy using operator= for T. |
68 |
|
*/ |
69 |
39493496 |
void restore(ContextObj* pContextObj) override |
70 |
|
{ |
71 |
|
//Debug("context") << "restore cdo " << this; |
72 |
39493496 |
CDO<T>* p = static_cast<CDO<T>*>(pContextObj); |
73 |
39493496 |
d_data = p->d_data; |
74 |
|
//Debug("context") << " to " << get() << std::endl; |
75 |
|
// Explicitly call destructor as it will not otherwise get called. |
76 |
4611226 |
p->d_data.~T(); |
77 |
39493496 |
} |
78 |
|
|
79 |
|
public: |
80 |
|
|
81 |
|
/** |
82 |
|
* Main constructor - uses default constructor for T to create the initial |
83 |
|
* value of d_data. |
84 |
|
*/ |
85 |
451343 |
CDO(Context* context) : |
86 |
|
ContextObj(context), |
87 |
451343 |
d_data(T()) { |
88 |
451343 |
} |
89 |
|
|
90 |
|
/** |
91 |
|
* Main constructor - uses default constructor for T to create the |
92 |
|
* initial value of d_data. |
93 |
|
* |
94 |
|
* This version takes an argument that specifies whether this CDO<> |
95 |
|
* was itself allocated in context memory. If it was, it is linked |
96 |
|
* with the current scope rather than scope 0. |
97 |
|
* |
98 |
|
* WARNING: Read the notes in src/context/context.h on "Gotchas when |
99 |
|
* allocating contextual objects with non-standard allocators." |
100 |
|
*/ |
101 |
10098 |
CDO(bool allocatedInCMM, Context* context) : |
102 |
|
ContextObj(allocatedInCMM, context), |
103 |
10098 |
d_data(T()) { |
104 |
10098 |
} |
105 |
|
|
106 |
|
/** |
107 |
|
* Constructor from object of type T. Creates a ContextObj and sets the data |
108 |
|
* to the given data value. Note that this value is only valid in the |
109 |
|
* current Scope. If the Scope is popped, the value will revert to whatever |
110 |
|
* is assigned by the default constructor for T |
111 |
|
*/ |
112 |
2848997 |
CDO(Context* context, const T& data) : |
113 |
|
ContextObj(context), |
114 |
2848997 |
d_data(T()) { |
115 |
2848997 |
makeCurrent(); |
116 |
2848997 |
d_data = data; |
117 |
2848997 |
} |
118 |
|
|
119 |
|
/** |
120 |
|
* Constructor from object of type T. Creates a ContextObj and sets the data |
121 |
|
* to the given data value. Note that this value is only valid in the |
122 |
|
* current Scope. If the Scope is popped, the value will revert to whatever |
123 |
|
* is assigned by the default constructor for T. |
124 |
|
* |
125 |
|
* This version takes an argument that specifies whether this CDO<> |
126 |
|
* was itself allocated in context memory. If it was, it is linked |
127 |
|
* with the current scope rather than scope 0. |
128 |
|
* |
129 |
|
* WARNING: Read the notes in src/context/context.h on "Gotchas when |
130 |
|
* allocating contextual objects with non-standard allocators." |
131 |
|
*/ |
132 |
|
CDO(bool allocatedInCMM, Context* context, const T& data) : |
133 |
|
ContextObj(allocatedInCMM, context), |
134 |
|
d_data(T()) { |
135 |
|
makeCurrent(); |
136 |
|
d_data = data; |
137 |
|
} |
138 |
|
|
139 |
|
/** |
140 |
|
* Destructor - call destroy() method |
141 |
|
*/ |
142 |
3310438 |
~CDO() { destroy(); } |
143 |
|
|
144 |
|
/** |
145 |
|
* Set the data in the CDO. First call makeCurrent. |
146 |
|
*/ |
147 |
253579582 |
void set(const T& data) { |
148 |
253579582 |
makeCurrent(); |
149 |
253579582 |
d_data = data; |
150 |
253579582 |
} |
151 |
|
|
152 |
|
/** |
153 |
|
* Get the current data from the CDO. Read-only. |
154 |
|
*/ |
155 |
1469651320 |
const T& get() const { return d_data; } |
156 |
|
|
157 |
|
/** |
158 |
|
* For convenience, define operator T() to be the same as get(). |
159 |
|
*/ |
160 |
1033626364 |
operator T() { return get(); } |
161 |
|
|
162 |
|
/** |
163 |
|
* For convenience, define operator const T() to be the same as get(). |
164 |
|
*/ |
165 |
175421807 |
operator const T() const { return get(); } |
166 |
|
|
167 |
|
/** |
168 |
|
* For convenience, define operator= that takes an object of type T. |
169 |
|
*/ |
170 |
253009910 |
CDO<T>& operator=(const T& data) { |
171 |
253009910 |
set(data); |
172 |
253009910 |
return *this; |
173 |
|
} |
174 |
|
|
175 |
|
};/* class CDO */ |
176 |
|
|
177 |
|
} // namespace context |
178 |
|
} // namespace cvc5 |
179 |
|
|
180 |
|
#endif /* CVC5__CONTEXT__CDO_H */ |