Simbody  3.6
SimTK::ReinitOnCopy< T > Class Template Reference

Ensures that a data member of type T is automatically reinitialized to a given initial value upon copy construction or copy assignment. This allows the compiler-generated default copy methods to be used. More...

+ Inheritance diagram for SimTK::ReinitOnCopy< T >:

Public Member Functions

 ReinitOnCopy ()=delete
 Default constructor is deleted; use ResetOnCopy instead. More...
 
 ReinitOnCopy (const T &value)
 Construct or implicitly convert from an object of type T. This sets both the current and remembered initial value to the given value. More...
 
 ReinitOnCopy (T &&value)
 Construct or implicitly convert from an rvalue object of type T. This sets both the current and remembered initial value to the given value. More...
 
 ReinitOnCopy (const ReinitOnCopy &source)
 Copy constructor sets the value and remembered initial value to the initial value in the source, using type T's copy constructor. More...
 
 ReinitOnCopy (ReinitOnCopy &&source)
 Move constructor is simply a pass-through to the move constructor of the contained object for both the current and initial values. More...
 
ReinitOnCopyoperator= (const ReinitOnCopy &ignored)
 Copy assignment reinitializes this object to its original condition; the source argument is ignored. More...
 
ReinitOnCopyoperator= (ReinitOnCopy &&source)
 Move assignment uses type T's move assignment for the current value but does not change the remembered initial value here. More...
 
ReinitOnCopyoperator= (const T &value)
 Assignment from an object of type T uses T's copy assignment operator; affects only the current value but does not change the remembered initial value. More...
 
ReinitOnCopyoperator= (T &&value)
 Assignment from an rvalue object of type T uses T's move or copy assignment operator; affects only the current value but does not change the remembered initial value. More...
 
const T & getT () const
 Return a const reference to the contained object of type T. More...
 
T & updT ()
 Return a writable reference to the contained object of type T. More...
 
const T & getReinitT () const
 (Advanced) Return a const reference to the stored initial value. More...
 
T & updReinitT ()
 (Advanced) Return a writable reference to the stored initial value. Use of this should be rare and restricted to constructors. More...
 

Detailed Description

template<class T>
class SimTK::ReinitOnCopy< T >

Ensures that a data member of type T is automatically reinitialized to a given initial value upon copy construction or copy assignment. This allows the compiler-generated default copy methods to be used.

The template type T here is required to be CopyAssignable, CopyConstructible, and Destructible, and cannot be an array type. There is space overhead here for one extra copy of T used to hold the initial value. The default constructor is suppressed here; if you just need the data member reset to zero or to its default-constructed value, use class ResetOnCopy<T> instead. That class has zero overhead and accepts a wider range of template arguments.

Here are some usage examples:

class Thing {
// Must provide an initial value; no default construction allowed.
ReinitOnCopy<int> m_which{-1}; // reinit to -1 on copy
ReinitOnCopy<string> m_name{"unknown"}; // back to "unknown" on copy
ReinitOnCopy<const char*> m_desc{"none given"}; // similar
ReinitOnCopy<Color> m_color{Blue}; // works for enums
// An example where ResetOnCopy is better.
ResetOnCopy<unsigned> m_count1; // reset to 0 on copy; no overhead
ReinitOnCopy<unsigned> m_count2; // error; no default construction
ReinitOnCopy<unsigned> m_count3{0}; // ok, but has space overhead
// An example where ResetOnCopy is necessary (T doesn't allow copying).
ResetOnCopy<std::unique_ptr<Something>> m_myThing;
};

Other than copy behavior, an object of type ReinitOnCopy<T> behaves just like the underlying object of type T. It will implicitly convert to T when needed, and inherit constructors, assignment, and other methods from T. Move construction and move assignment behave as they would for T, and an assignment from an object of type T to an object of type ReinitOnCopy<T> will invoke T's ordinary copy assignment operator if there is one, and fail to compile if an attempt is made to use a non-existent assignment operator.

Template Parameters
TTemplate type that is a numeric, character, enum, or pointer built-in type, or a class type that is CopyConstructible, CopyAssignable, and Destructible. Array types are not allowed.
See also
ResetOnCopy if you only need to reinitialize to the default value.

Constructor & Destructor Documentation

◆ ReinitOnCopy() [1/5]

template<class T >
SimTK::ReinitOnCopy< T >::ReinitOnCopy ( )
delete

Default constructor is deleted; use ResetOnCopy instead.

◆ ReinitOnCopy() [2/5]

template<class T >
SimTK::ReinitOnCopy< T >::ReinitOnCopy ( const T &  value)
inline

Construct or implicitly convert from an object of type T. This sets both the current and remembered initial value to the given value.

◆ ReinitOnCopy() [3/5]

template<class T >
SimTK::ReinitOnCopy< T >::ReinitOnCopy ( T &&  value)
inline

Construct or implicitly convert from an rvalue object of type T. This sets both the current and remembered initial value to the given value.

◆ ReinitOnCopy() [4/5]

template<class T >
SimTK::ReinitOnCopy< T >::ReinitOnCopy ( const ReinitOnCopy< T > &  source)
inline

Copy constructor sets the value and remembered initial value to the initial value in the source, using type T's copy constructor.

The current value of the source is ignored.

◆ ReinitOnCopy() [5/5]

template<class T >
SimTK::ReinitOnCopy< T >::ReinitOnCopy ( ReinitOnCopy< T > &&  source)
inline

Move constructor is simply a pass-through to the move constructor of the contained object for both the current and initial values.

Member Function Documentation

◆ operator=() [1/4]

template<class T >
ReinitOnCopy& SimTK::ReinitOnCopy< T >::operator= ( const ReinitOnCopy< T > &  ignored)
inline

Copy assignment reinitializes this object to its original condition; the source argument is ignored.

◆ operator=() [2/4]

template<class T >
ReinitOnCopy& SimTK::ReinitOnCopy< T >::operator= ( ReinitOnCopy< T > &&  source)
inline

Move assignment uses type T's move assignment for the current value but does not change the remembered initial value here.

◆ operator=() [3/4]

template<class T >
ReinitOnCopy& SimTK::ReinitOnCopy< T >::operator= ( const T &  value)
inline

Assignment from an object of type T uses T's copy assignment operator; affects only the current value but does not change the remembered initial value.

◆ operator=() [4/4]

template<class T >
ReinitOnCopy& SimTK::ReinitOnCopy< T >::operator= ( T &&  value)
inline

Assignment from an rvalue object of type T uses T's move or copy assignment operator; affects only the current value but does not change the remembered initial value.

◆ getT()

template<class T >
const T& SimTK::ReinitOnCopy< T >::getT ( ) const
inline

Return a const reference to the contained object of type T.

◆ updT()

template<class T >
T& SimTK::ReinitOnCopy< T >::updT ( )
inline

Return a writable reference to the contained object of type T.

◆ getReinitT()

template<class T >
const T& SimTK::ReinitOnCopy< T >::getReinitT ( ) const
inline

(Advanced) Return a const reference to the stored initial value.

◆ updReinitT()

template<class T >
T& SimTK::ReinitOnCopy< T >::updReinitT ( )
inline

(Advanced) Return a writable reference to the stored initial value. Use of this should be rare and restricted to constructors.


The documentation for this class was generated from the following file: