dune-functions 2.9.0
|
A wrapper providing small object optimization with polymorphic types. More...
#include <dune/functions/common/polymorphicsmallobject.hh>
Public Member Functions | |
PolymorphicSmallObject () | |
Default constructor. More... | |
template<class Derived , typename std::enable_if< std::is_base_of< Base, std::remove_cv_t< std::remove_reference_t< Derived > > >::value, int >::type = 0> | |
PolymorphicSmallObject (Derived &&derived) | |
Construct from object. More... | |
PolymorphicSmallObject (PolymorphicSmallObject &&other) noexcept | |
Move constructor from other PolymorphicSmallObject. More... | |
PolymorphicSmallObject (const PolymorphicSmallObject &other) | |
Copy constructor from other PolymorphicSmallObject. More... | |
~PolymorphicSmallObject () | |
Destructor. More... | |
PolymorphicSmallObject & | operator= (const PolymorphicSmallObject &other) |
Copy assignment from other PolymorphicSmallObject. More... | |
PolymorphicSmallObject & | operator= (PolymorphicSmallObject &&other) noexcept |
Move assignment from other PolymorphicSmallObject. More... | |
operator bool () const | |
Check if *this is not empty. More... | |
bool | bufferUsed () const |
Check if object is stored in internal stack buffer. More... | |
const Base & | get () const |
Obtain reference to stored object. More... | |
Base & | get () |
Obtain mutable reference to stored object. More... | |
A wrapper providing small object optimization with polymorphic types.
Base | Base class type of wrapped objects |
bufferSize | Size of small object buffer |
This class encapsulates small object optimization for polymorphic types. The type of objects passed to the constructor must be derived from the base class type Base.
If the size of the derived type fits into the static buffer, then the wrapped object is stored there, otherwise it is allocated dynamically.
Notice that this class does implement use type erasure for destructors, copy/move constructors and copy/move assignment. Hence it requires that Base has a virtual destructor.
In order to make the copy constructor work for polymorphic types, Base must provide virtual methods Base* clone()
and Base* clone(void*)
. The former should return a pointer to a dynamically allocated clone, while the latter should call the appropriate placement-new with the passed pointer.
Similarly the polymorphic type has to implement a virtual Base* move(void*)
method. This should call placement-new and can std::move all the data but leave the object in a valid and probably unusable state.
|
inline |
Default constructor.
|
inline |
Construct from object.
Derived | Type of object to be stored, must be derived from Base |
derived | Object to be stored |
|
inlinenoexcept |
Move constructor from other PolymorphicSmallObject.
|
inline |
Copy constructor from other PolymorphicSmallObject.
|
inline |
Destructor.
|
inline |
Check if object is stored in internal stack buffer.
|
inline |
Obtain mutable reference to stored object.
|
inline |
Obtain reference to stored object.
|
inlineexplicit |
Check if *this is not empty.
|
inline |
Copy assignment from other PolymorphicSmallObject.
|
inlinenoexcept |
Move assignment from other PolymorphicSmallObject.