1 #ifndef SimTK_SimTKCOMMON_REINIT_ON_COPY_H_ 2 #define SimTK_SimTKCOMMON_REINIT_ON_COPY_H_ 32 #include <type_traits> 44 template <
class T,
bool IsScalarType>
99 static_assert(!std::is_array<T>::value,
100 "ReinitOnCopy<T> does not allow T to be an array. Use an array " 101 "of ReinitOnCopy<E> instead, where E is the element type.");
103 static_assert( std::is_copy_constructible<T>::value
104 && std::is_copy_assignable<T>::value,
105 "ReinitOnCopy<T> requires type T to have an accessible copy " 106 "constructor and copy assignment operator. Use ResetOnCopy<T> instead " 107 "to reinitialize using only default construction.");
109 static_assert(std::is_destructible<T>::value,
110 "ReinitOnCopy<T> requires type T to have an accessible destructor.");
115 using Super::operator=;
143 { Super::operator=(static_cast<const Super&>(ignored));
return *
this; }
148 { Super::operator=(static_cast<Super&&>(source));
return *
this; }
154 { Super::operator=(value);
return *
this; }
160 { Super::operator=(std::move(value));
return *
this; }
163 const T&
getT()
const {
return Super::getT();}
166 T&
updT() {
return Super::updT();}
185 class ReinitOnCopyHelper<T,true> {
191 ReinitOnCopyHelper() {}
196 ReinitOnCopyHelper(
const ReinitOnCopyHelper& source)
197 : ReinitOnCopyHelper(source.m_reinitValue) {}
200 ReinitOnCopyHelper(ReinitOnCopyHelper&& source)
201 : m_value(
std::move(source.m_value)),
202 m_reinitValue(
std::move(source.m_reinitValue)) {}
206 explicit ReinitOnCopyHelper(
const T& value)
207 : m_value(value), m_reinitValue(value) {}
211 ReinitOnCopyHelper& operator=(ReinitOnCopyHelper&& source)
212 { m_value = std::move(source.m_value);
return *
this; }
216 ReinitOnCopyHelper& operator=(
const ReinitOnCopyHelper&)
217 { m_value = m_reinitValue;
return *
this; }
221 ReinitOnCopyHelper& operator=(
const T& value)
222 { m_value = value;
return *
this; }
226 operator const T&()
const {
return getT();}
230 operator T&() {
return updT();}
232 const T& getT()
const {
return m_value;}
233 T& updT() {
return m_value;}
236 const T& getReinitT()
const {
return m_reinitValue;}
240 T& updReinitT() {
return m_reinitValue;}
244 const T m_reinitValue{};
253 class ReinitOnCopyHelper<T,false> :
public T {
260 ReinitOnCopyHelper() : T(), m_reinitValue() {}
264 ReinitOnCopyHelper(ReinitOnCopyHelper&& source)
265 : T(
std::move(source)), m_reinitValue(
std::move(source.m_reinitValue)) {}
269 explicit ReinitOnCopyHelper(
const T& value)
270 : T(value), m_reinitValue(value) {}
274 explicit ReinitOnCopyHelper(T&& value)
275 : T(value), m_reinitValue(static_cast<const T&>(*this)) {}
280 ReinitOnCopyHelper(
const ReinitOnCopyHelper& source)
281 : ReinitOnCopyHelper(source.m_reinitValue) {}
285 ReinitOnCopyHelper& operator=(ReinitOnCopyHelper&& source)
286 { T::operator=(std::move(source));
return *
this; }
290 ReinitOnCopyHelper& operator=(
const ReinitOnCopyHelper&)
291 { T::operator=(m_reinitValue);
return *
this; }
295 ReinitOnCopyHelper& operator=(
const T& value)
296 { T::operator=(value);
return *
this; }
300 ReinitOnCopyHelper& operator=(T&& value)
301 { T::operator=(std::move(value));
return *
this; }
303 const T& getT()
const {
return static_cast<const T&
>(*this);}
304 T& updT() {
return static_cast<T&
>(*this);}
307 const T& getReinitT()
const {
return m_reinitValue;}
311 T& updReinitT() {
return m_reinitValue;}
317 const T m_reinitValue {
static_cast<const T&
>(*this)};
324 #endif // SimTK_SimTKCOMMON_REINIT_ON_COPY_H_ ReinitOnCopy & operator=(T &&value)
Assignment from an rvalue object of type T uses T's move or copy assignment operator; affects only th...
Definition: ReinitOnCopy.h:159
ReinitOnCopy & operator=(const ReinitOnCopy &ignored)
Copy assignment reinitializes this object to its original condition; the source argument is ignored...
Definition: ReinitOnCopy.h:142
This is the top-level SimTK namespace into which all SimTK names are placed to avoid collision with o...
Definition: Assembler.h:37
ReinitOnCopy(T &&value)
Construct or implicitly convert from an rvalue object of type T. This sets both the current and remem...
Definition: ReinitOnCopy.h:127
const T & getT() const
Return a const reference to the contained object of type T.
Definition: ReinitOnCopy.h:163
T & updReinitT()
(Advanced) Return a writable reference to the stored initial value. Use of this should be rare and re...
Definition: ReinitOnCopy.h:173
ReinitOnCopy & operator=(const T &value)
Assignment from an object of type T uses T's copy assignment operator; affects only the current value...
Definition: ReinitOnCopy.h:153
Mandatory first inclusion for any Simbody source or header file.
const T & getReinitT() const
(Advanced) Return a const reference to the stored initial value.
Definition: ReinitOnCopy.h:169
Ensures that a data member of type T is automatically reinitialized to a given initial value upon cop...
Definition: ReinitOnCopy.h:94
T & updT()
Return a writable reference to the contained object of type T.
Definition: ReinitOnCopy.h:166
ReinitOnCopy(const ReinitOnCopy &source)
Copy constructor sets the value and remembered initial value to the initial value in the source...
Definition: ReinitOnCopy.h:132
ReinitOnCopy(ReinitOnCopy &&source)
Move constructor is simply a pass-through to the move constructor of the contained object for both th...
Definition: ReinitOnCopy.h:137
This helper class is used only by ReinitOnCopy and is specialized as necessary to support a variety o...
Definition: ReinitOnCopy.h:45
ReinitOnCopy(const T &value)
Construct or implicitly convert from an object of type T. This sets both the current and remembered i...
Definition: ReinitOnCopy.h:122
ReinitOnCopy & operator=(ReinitOnCopy &&source)
Move assignment uses type T's move assignment for the current value but does not change the remembere...
Definition: ReinitOnCopy.h:147
ReinitOnCopy()=delete
Default constructor is deleted; use ResetOnCopy instead.