Go to the documentation of this file.
3 #ifndef DUNE_FUNCTIONS_COMMON_POLYMORPHICSMALLOBJECT_HH
4 #define DUNE_FUNCTIONS_COMMON_POLYMORPHICSMALLOBJECT_HH
8 #include <dune/common/std/type_traits.hh>
9 #include <dune/common/hybridutilities.hh>
45 template<
class Base,
size_t bufferSize>
61 template<
class Derived>
64 using namespace Dune::Hybrid;
65 auto useBuffer = Dune::Std::bool_constant<(sizeof(Derived)<bufferSize)>();
66 ifElse(useBuffer, [&](
auto id) {
67 p_ =
new (buffer_) Derived(std::forward<Derived>(derived));
69 p_ =
new Derived(std::forward<Derived>(derived));
76 moveToWrappedObject(std::move(other));
82 copyToWrappedObject(other);
88 destroyWrappedObject();
94 destroyWrappedObject();
95 copyToWrappedObject(other);
102 destroyWrappedObject();
103 moveToWrappedObject(std::move(other));
108 explicit operator bool()
const
116 return ((
void*) (p_) == (
void*)(&buffer_));
133 void destroyWrappedObject()
146 if (other.bufferUsed())
147 p_ = other.p_->move(buffer_);
167 if (other.bufferUsed())
168 p_ = other.p_->clone(buffer_);
170 p_ = other.p_->clone();
174 alignas(Base)
char buffer_[bufferSize];
182 #endif // DUNE_FUNCTIONS_COMMON_POLYMORPHICSMALLOBJECT_HH
PolymorphicSmallObject()
Default constructor.
Definition: polymorphicsmallobject.hh:51
Definition: polynomial.hh:7
const Base & get() const
Obtain reference to stored object.
Definition: polymorphicsmallobject.hh:120
Base & get()
Obtain mutable reference to stored object.
Definition: polymorphicsmallobject.hh:126
A wrapper providing small object optimization with polymorphic types.
Definition: polymorphicsmallobject.hh:46
PolymorphicSmallObject(PolymorphicSmallObject &&other)
Move constructor from other PolymorphicSmallObject.
Definition: polymorphicsmallobject.hh:74
PolymorphicSmallObject(const PolymorphicSmallObject &other)
Copy constructor from other PolymorphicSmallObject.
Definition: polymorphicsmallobject.hh:80
PolymorphicSmallObject & operator=(PolymorphicSmallObject &&other)
Move assignment from other PolymorphicSmallObject.
Definition: polymorphicsmallobject.hh:100
~PolymorphicSmallObject()
Destructor.
Definition: polymorphicsmallobject.hh:86
PolymorphicSmallObject & operator=(const PolymorphicSmallObject &other)
Copy assignment from other PolymorphicSmallObject.
Definition: polymorphicsmallobject.hh:92
bool bufferUsed() const
Check if object is stored in internal stack buffer.
Definition: polymorphicsmallobject.hh:114
PolymorphicSmallObject(Derived &&derived)
Construct from object.
Definition: polymorphicsmallobject.hh:62