1 /********************* */
4 ** Top contributors (to current version):
5 ** Morgan Deters, Clark Barrett, Mathias Preiner
6 ** This file is part of the CVC4 project.
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.\endverbatim
12 ** \brief A context-dependent object.
14 ** A context-dependent object.
17 #include "cvc4_private.h"
19 #ifndef CVC4__CONTEXT__CDO_H
20 #define CVC4__CONTEXT__CDO_H
22 #include "context/context.h"
28 * Most basic template for context-dependent objects. Simply makes a copy
29 * (using the copy constructor) of class T when saving, and copies it back
30 * (using operator=) during restore.
33 class CDO
: public ContextObj
{
36 * The data of type T being stored in this context-dependent object.
43 * Copy constructor - it's private to ensure it is only used by save().
44 * Basic CDO objects, cannot be copied-they have to be unique.
46 CDO(const CDO
<T
>& cdo
) : ContextObj(cdo
), d_data(cdo
.d_data
) {}
49 * operator= for CDO is private to ensure CDO object is not copied.
51 CDO
<T
>& operator=(const CDO
<T
>& cdo
) = delete;
54 * Implementation of mandatory ContextObj method save: simply copies the
55 * current data to a copy using the copy constructor. Memory is allocated
56 * using the ContextMemoryManager.
58 ContextObj
* save(ContextMemoryManager
* pCMM
) override
60 Debug("context") << "save cdo " << this;
61 ContextObj
* p
= new(pCMM
) CDO
<T
>(*this);
62 Debug("context") << " to " << p
<< std::endl
;
67 * Implementation of mandatory ContextObj method restore: simply copies the
68 * saved data back from the saved copy using operator= for T.
70 void restore(ContextObj
* pContextObj
) override
72 //Debug("context") << "restore cdo " << this;
73 CDO
<T
>* p
= static_cast<CDO
<T
>*>(pContextObj
);
75 //Debug("context") << " to " << get() << std::endl;
76 // Explicitly call destructor as it will not otherwise get called.
83 * Main constructor - uses default constructor for T to create the initial
86 CDO(Context
* context
) :
92 * Main constructor - uses default constructor for T to create the
93 * initial value of d_data.
95 * This version takes an argument that specifies whether this CDO<>
96 * was itself allocated in context memory. If it was, it is linked
97 * with the current scope rather than scope 0.
99 * WARNING: Read the notes in src/context/context.h on "Gotchas when
100 * allocating contextual objects with non-standard allocators."
102 CDO(bool allocatedInCMM
, Context
* context
) :
103 ContextObj(allocatedInCMM
, context
),
108 * Constructor from object of type T. Creates a ContextObj and sets the data
109 * to the given data value. Note that this value is only valid in the
110 * current Scope. If the Scope is popped, the value will revert to whatever
111 * is assigned by the default constructor for T
113 CDO(Context
* context
, const T
& data
) :
121 * Constructor from object of type T. Creates a ContextObj and sets the data
122 * to the given data value. Note that this value is only valid in the
123 * current Scope. If the Scope is popped, the value will revert to whatever
124 * is assigned by the default constructor for T.
126 * This version takes an argument that specifies whether this CDO<>
127 * was itself allocated in context memory. If it was, it is linked
128 * with the current scope rather than scope 0.
130 * WARNING: Read the notes in src/context/context.h on "Gotchas when
131 * allocating contextual objects with non-standard allocators."
133 CDO(bool allocatedInCMM
, Context
* context
, const T
& data
) :
134 ContextObj(allocatedInCMM
, context
),
141 * Destructor - call destroy() method
143 ~CDO() { destroy(); }
146 * Set the data in the CDO. First call makeCurrent.
148 void set(const T
& data
) {
154 * Get the current data from the CDO. Read-only.
156 const T
& get() const { return d_data
; }
159 * For convenience, define operator T() to be the same as get().
161 operator T() { return get(); }
164 * For convenience, define operator const T() to be the same as get().
166 operator const T() const { return get(); }
169 * For convenience, define operator= that takes an object of type T.
171 CDO
<T
>& operator=(const T
& data
) {
178 } // namespace context
181 #endif /* CVC4__CONTEXT__CDO_H */