3 #ifndef DUNE_FUNCTIONS_COMMON_POLYMORPHICSMALLOBJECT_HH
4 #define DUNE_FUNCTIONS_COMMON_POLYMORPHICSMALLOBJECT_HH
9 #include <dune/common/std/type_traits.hh>
10 #include <dune/common/hybridutilities.hh>
46 template<
class Base,
size_t bufferSize>
62 template<
class Derived,
63 typename std::enable_if<std::is_base_of<Base, std::remove_cv_t<
64 std::remove_reference_t<Derived>>>::value,
int>::type = 0>
67 using namespace Dune::Hybrid;
68 auto useBuffer = Dune::Std::bool_constant<(sizeof(Derived) <= bufferSize)>();
69 ifElse(useBuffer, [&](
auto id) {
70 p_ =
new (&buffer_) Derived(std::forward<Derived>(derived));
72 p_ =
new Derived(std::forward<Derived>(derived));
79 moveToWrappedObject(std::move(other));
85 copyToWrappedObject(other);
91 destroyWrappedObject();
99 destroyWrappedObject();
100 copyToWrappedObject(other);
108 destroyWrappedObject();
109 moveToWrappedObject(std::move(other));
114 explicit operator bool()
const
122 return ((
void*) (p_) == (
void*)(&buffer_));
139 void destroyWrappedObject()
152 if (other.bufferUsed())
153 p_ = other.p_->move(&buffer_);
171 if (other.bufferUsed())
172 p_ = other.p_->clone(&buffer_);
174 p_ = other.p_->clone();
177 std::aligned_storage_t<bufferSize> buffer_;
Definition: polynomial.hh:10
A wrapper providing small object optimization with polymorphic types.
Definition: polymorphicsmallobject.hh:48
PolymorphicSmallObject & operator=(PolymorphicSmallObject &&other)
Move assignment from other PolymorphicSmallObject.
Definition: polymorphicsmallobject.hh:106
bool bufferUsed() const
Check if object is stored in internal stack buffer.
Definition: polymorphicsmallobject.hh:120
PolymorphicSmallObject(Derived &&derived)
Construct from object.
Definition: polymorphicsmallobject.hh:65
Base & get()
Obtain mutable reference to stored object.
Definition: polymorphicsmallobject.hh:132
PolymorphicSmallObject & operator=(const PolymorphicSmallObject &other)
Copy assignment from other PolymorphicSmallObject.
Definition: polymorphicsmallobject.hh:95
PolymorphicSmallObject(const PolymorphicSmallObject &other)
Copy constructor from other PolymorphicSmallObject.
Definition: polymorphicsmallobject.hh:83
~PolymorphicSmallObject()
Destructor.
Definition: polymorphicsmallobject.hh:89
const Base & get() const
Obtain reference to stored object.
Definition: polymorphicsmallobject.hh:126
PolymorphicSmallObject(PolymorphicSmallObject &&other)
Move constructor from other PolymorphicSmallObject.
Definition: polymorphicsmallobject.hh:77
PolymorphicSmallObject()
Default constructor.
Definition: polymorphicsmallobject.hh:52