3#ifndef DUNE_FUNCTIONS_COMMON_POLYMORPHICSMALLOBJECT_HH
4#define DUNE_FUNCTIONS_COMMON_POLYMORPHICSMALLOBJECT_HH
43template<
class Base,
size_t bufferSize>
59 template<
class Derived,
60 typename std::enable_if<std::is_base_of<Base, std::remove_cv_t<
61 std::remove_reference_t<Derived>>>::value,
int>::type = 0>
64 constexpr bool useBuffer =
sizeof(Derived) <= bufferSize;
65 if constexpr (useBuffer) {
66 p_ =
new (&buffer_) Derived(std::forward<Derived>(derived));
68 p_ =
new Derived(std::forward<Derived>(derived));
75 moveToWrappedObject(std::move(other));
81 copyToWrappedObject(other);
87 destroyWrappedObject();
95 destroyWrappedObject();
96 copyToWrappedObject(other);
104 destroyWrappedObject();
105 moveToWrappedObject(std::move(other));
110 explicit operator bool()
const
118 return ((
void*) (p_) == (
void*)(&buffer_));
135 void destroyWrappedObject() noexcept
148 if (other.bufferUsed())
149 p_ = other.p_->move(&buffer_);
167 if (other.bufferUsed())
168 p_ = other.p_->clone(&buffer_);
170 p_ = other.p_->clone();
173 std::aligned_storage_t<bufferSize> buffer_;
Definition: polynomial.hh:10
A wrapper providing small object optimization with polymorphic types.
Definition: polymorphicsmallobject.hh:45
const Base & get() const
Obtain reference to stored object.
Definition: polymorphicsmallobject.hh:122
bool bufferUsed() const
Check if object is stored in internal stack buffer.
Definition: polymorphicsmallobject.hh:116
PolymorphicSmallObject(Derived &&derived)
Construct from object.
Definition: polymorphicsmallobject.hh:62
PolymorphicSmallObject(PolymorphicSmallObject &&other) noexcept
Move constructor from other PolymorphicSmallObject.
Definition: polymorphicsmallobject.hh:73
PolymorphicSmallObject & operator=(const PolymorphicSmallObject &other)
Copy assignment from other PolymorphicSmallObject.
Definition: polymorphicsmallobject.hh:91
PolymorphicSmallObject & operator=(PolymorphicSmallObject &&other) noexcept
Move assignment from other PolymorphicSmallObject.
Definition: polymorphicsmallobject.hh:102
PolymorphicSmallObject(const PolymorphicSmallObject &other)
Copy constructor from other PolymorphicSmallObject.
Definition: polymorphicsmallobject.hh:79
~PolymorphicSmallObject()
Destructor.
Definition: polymorphicsmallobject.hh:85
PolymorphicSmallObject()
Default constructor.
Definition: polymorphicsmallobject.hh:49
Base & get()
Obtain mutable reference to stored object.
Definition: polymorphicsmallobject.hh:128