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

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

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

Public Member Functions

 ResetOnCopy ()
 Default constructor performs zero-initialization for built-in types; default initialization for class types. More...
 
 ResetOnCopy (const T &value)
 Construct or implicitly convert from an object of type T if there is a suitable copy constructor available. More...
 
 ResetOnCopy (T &&value)
 Construct or implicitly convert from an rvalue object of type T if thereis a suitable move constructor available, otherwise use the copy constructor if available, otherwise fails to compile. More...
 
 ResetOnCopy (const ResetOnCopy &)
 Copy constructor behaves identically to the default constructor; the supplied source argument is ignored. More...
 
 ResetOnCopy (ResetOnCopy &&source)
 Move constructor is simply a pass-through to the move constructor of the contained object so behaves normally. More...
 
ResetOnCopyoperator= (const ResetOnCopy &ignored)
 Copy assignment reinitializes this object to its default-constructed condition; the source argument is ignored. More...
 
ResetOnCopyoperator= (ResetOnCopy &&source)
 Move assignment is simply a pass-through to the move assignment of the contained object so behaves normally. More...
 
ResetOnCopyoperator= (const T &value)
 Assignment from an object of type T uses T's copy assignment operator if there is a suitable copy assignment operator available. More...
 
ResetOnCopyoperator= (T &&value)
 Assignment from an rvalue object of type T uses T's move assignment operator if available, otherwise uses copy assignment if available, otherwise fails to compile. 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...
 

Detailed Description

template<class T>
class SimTK::ResetOnCopy< T >

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

Here are some usage examples:

class Thing {
// Able to use default or initializing constructors, but note that any
// initial values shown here disappear when this is copied; all members will
// be default-initialized in the copy.
ResetOnCopy<int> m_defint;
ResetOnCopy<char> m_charZ = 'z';
ResetOnCopy<string> m_defstr;
ResetOnCopy<string> m_strHelloC = "hello";
ResetOnCopy<string> m_strGoodbyeS = "goodbye"s;
ResetOnCopy<short> m_shArr[3] = {9,8,7};
ResetOnCopy<SubsystemIndex> m_subIx{5};
ResetOnCopy<std::vector<string>> m_vstr {"one", "two", "three"};
// Caution: not a smart pointer; will be cleared but not deleted.
ResetOnCopy<const int*> m_ptr;
// Must use a smart pointer like this for automatic delete on copy.
ResetOnCopy<unique_ptr<Array_<int>>> m_up{new Array_<int>({1,2,3})};
};

Other than copy behavior, an object of type ResetOnCopy<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 ResetOnCopy<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.

If T is a C++ built-in "scalar" type (arithmetic, character, or pointer type) it will be reset to T(0) or nullptr when copy constructed or copy assigned. We don't allow T to be an enumerated type here since resetting an enum to zero isn't always reasonable; use ReinitOnCopy<T> instead and provide an initializing enumerator. For class types T, copy construction and copy assignment will use the default constructor. ResetOnCopy<T> adds CopyConstructible and CopyAssignable concepts to move-only classes like std::unique_ptr, but those operations just reset the object to its default-constructed condition.

Template Parameters
TTemplate type that is a numeric, character, or pointer built-in type, or a class type that is DefaultConstructible and Destructible. Enum and array types are not allowed.
See also
ReinitOnCopy if you want to reset to a non-default value or use an enum.

Constructor & Destructor Documentation

◆ ResetOnCopy() [1/5]

template<class T >
SimTK::ResetOnCopy< T >::ResetOnCopy ( )
inline

Default constructor performs zero-initialization for built-in types; default initialization for class types.

◆ ResetOnCopy() [2/5]

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

Construct or implicitly convert from an object of type T if there is a suitable copy constructor available.

◆ ResetOnCopy() [3/5]

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

Construct or implicitly convert from an rvalue object of type T if thereis a suitable move constructor available, otherwise use the copy constructor if available, otherwise fails to compile.

◆ ResetOnCopy() [4/5]

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

Copy constructor behaves identically to the default constructor; the supplied source argument is ignored.

◆ ResetOnCopy() [5/5]

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

Move constructor is simply a pass-through to the move constructor of the contained object so behaves normally.

Member Function Documentation

◆ operator=() [1/4]

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

Copy assignment reinitializes this object to its default-constructed condition; the source argument is ignored.

◆ operator=() [2/4]

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

Move assignment is simply a pass-through to the move assignment of the contained object so behaves normally.

◆ operator=() [3/4]

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

Assignment from an object of type T uses T's copy assignment operator if there is a suitable copy assignment operator available.

◆ operator=() [4/4]

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

Assignment from an rvalue object of type T uses T's move assignment operator if available, otherwise uses copy assignment if available, otherwise fails to compile.

◆ getT()

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

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

◆ updT()

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

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


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