The wrap class#
-
template<typename IFace, auto Cfg = default_config>
requires std::is_class_v<IFace> && std::same_as<IFace, std::remove_cv_t<IFace>> && valid_config<Cfg>
class wrap# -
wrap()#
Default constructor.
The default constructor is enabled in the following circumstances:
the configuration option
invalid_default_ctorinCfgis set totrue. In this case, the default constructor initialises into the invalid state. Otherwise,a non-
voiddefault-initialisableDefaultValueTypewith a valid, default-initialisable interface implementation forIFacehas been specified as first template argument inCfg. In this case, the default constructor value-initialises an instance ofDefaultValueTypeas the internal type-erased value. When employing value semantics, the copyability, movability and swappability ofDefaultValueTypemust be consistent with the corresponding settings inCfgin order for this constructor to be enabled.
In both cases, the reference interface must be default-initialisable in order for this constructor to be available.
- Throws:
any exception thrown by the default constructor of the interface implementation or of the reference interface, by the value-initialisation of a non-
voidDefaultValueTypeor by memory allocation errors if the non-voidDefaultValueTypedoes not fit in static storage or if reference semantics is being used. If it can be determined at compile time that none of these conditions can occurr, then this constructor is markednoexcept.
-
explicit wrap(invalid_wrap_t)#
Explicit initialisation into the invalid state.
This constructor is enabled only if the reference interface is default-initialisable.
- Throws:
any exception thrown by the default constructor of the reference interface. If the default constructor of the reference interface does not throw, then this constructor is marked
noexcept.
-
template<typename T>
wrap(T &&x)# Generic constructor.
This constructor will create a
wrapfrom the input valuex.This constructor is enabled only if all the following conditions are satisfied:
the reference interface is default-initialisable;
after the removal of reference and cv-qualifiers,
Tis notthe same as
wrap(so that this constructor does not interfere with the copy/move constructors),an instance of
std::in_place_type_t(so that this constructor does not interfere with the in-place constructor),the same as
invalid_wrap_t(so that this constructor does not interfere with the constructor into the invalid state);
the interface
IFacehas a valid, default-initialisable implementation for the value typeT(see theiface_with_implconcept);xcan be perfectly-forwarded to construct an instance of the value type;when employing value semantics, the copyability, movability and swappability of the value type are consistent with the corresponding settings in
Cfg.
This constructor is marked
explicitif either:Tis astd::reference_wrapperand the value ofexplicit_ctorinCfgifwrap_ctor::always_explicit, orTis not astd::reference_wrapperand the value ofexplicit_ctorinCfgis less thanwrap_ctor::always_implicit.
Otherwise, the constructor is implicit.
- Parameters:
x – the input value.
- Throws:
any exception thrown by the default constructor of the interface implementation or of the reference interface, by the construction of the value type or by memory allocation errors if the value type does not fit in static storage or if reference semantics is being used. If it can be determined at compile time that none of these conditions can occurr, then this constructor is marked
noexcept.
-
template<typename T, typename ...U>
explicit wrap(std::in_place_type_t<T>, U&&... args)# Generic in-place constructor.
This constructor will create a
wrapcontaining a type-erased value of typeTconstructed from the input argument(s)args. If no input arguments are provided, the internal value will be value-initialised.This constructor is enabled only if all the following conditions are satisfied:
Tis an object type without cv qualifications;the reference interface is default-initialisable;
the interface
IFacehas a valid, default-initialisable implementation for the value typeT(see theiface_with_implconcept);argscan be perfectly-forwarded to construct an instance of the value typeT;when employing value semantics, the copyability, movability and swappability of the value type
Tare consistent with the corresponding settings inCfg.
- Parameters:
args – the input construction arguments.
- Throws:
any exception thrown by the default constructor of the interface implementation or of the reference interface, by the construction of the value type or by memory allocation errors if the value type does not fit in static storage or if reference semantics is being used. If it can be determined at compile time that none of these conditions can occurr, then this constructor is marked
noexcept.
-
wrap(const wrap &other)#
Copy constructor.
When employing value semantics, the copy constructor will copy-construct the type-erased value from
other. Otherwise, awrapsharing ownership of the type-erased value withotherwill be constructed.This constructor is enabled only if the following conditions are satisfied:
the reference interface is default-initialisable;
when employing value semantics, the
copyableoption inCfgis activated.
- Parameters:
other – the
wrapto be copied.- Throws:
any exception thrown by the default constructor of the interface implementation or of the reference interface, or by the copy-construction of the value type or by memory allocation errors when value semantics is being used. This constructor is marked
noexceptwhen using reference semantics and if the reference interface’s default constructor is markednoexcept.
-
[[nodiscard]] friend bool is_invalid(const wrap &w) noexcept#
This function will return
trueifwis in the invalid state,falseotherwise.- Parameters:
w – the input argument.
- Returns:
the validity status for
w.
-
[[nodiscard]] friend const IFace *iface_ptr(const wrap &w) noexcept#
-
[[nodiscard]] friend const IFace *iface_ptr(const wrap &&w) noexcept#
-
[[nodiscard]] friend IFace *iface_ptr(wrap &w) noexcept#
-
[[nodiscard]] friend IFace *iface_ptr(wrap &&w) noexcept#
Fetch a pointer to the interface.
These functions will return a pointer to the instance of the interface
IFacestored within awrap. Ifwis in the invalid state, thennullptrwill be returned.- Parameters:
w – the input argument.
- Returns:
a pointer to the interface.
-
template<typename T, typename ...Args>
friend void emplace(wrap &w, Args&&... args)# Emplace a value into a
wrap.This function will first destroy the value in
w(ifwis not already in the invalid state). It will then construct inwa value of typeTusing the construction argumentsArgs.This function is enabled only if the following conditions are satisfied:
Tis an object type without cv qualifications;the interface
IFacehas a valid, default-initialisable implementation for the value typeT(see theiface_with_implconcept);when employing value semantics, the copyability, movability and swappability of the value type
Tare consistent with the corresponding settings inCfg.
Passing
was an argument inargs(e.g., attempting to emplacewinto itself) will lead to undefined behaviour.This function is
noexceptif all these conditions are satisfied:wis using value semantics,the static size and alignment of
ware large enough to store an instance ofT,the invoked constructor of
Tdoes not throw.
If an exception is thrown,
wmay be left in the invalid state.
-
[[nodiscard]] friend bool has_static_storage(const wrap &w) noexcept#
Query the storage type of a
wrap.
-
[[nodiscard]] friend wrap copy(const wrap &w)#
requires (Cfg.semantics == wrap_semantics::reference) Make a deep-copy of a
wrapemploying reference semantics.This function will return a new
wrapcontaining a copy of the value stored inw.
-
[[nodiscard]] friend bool same_value(const wrap &w1, const wrap &w2) noexcept#
requires (Cfg.semantics == wrap_semantics::reference) Check if two
wrapobjects employing reference semantics share ownership of the internal value.
-
wrap()#
-
[[nodiscard]] bool is_valid(const wrap &w) noexcept#
This function will return
falseifwis in the invalid state,trueotherwise.- Parameters:
w – the input argument.
- Returns:
the validity status for
w.
-
template<typename IFace, auto Cfg>
bool has_dynamic_storage(const wrap<IFace, Cfg> &w) noexcept# Query the storage type of a
wrap.
-
struct invalid_wrap_t#
A tag structure used to set a
wrapto the invalid state. This is a trivial empty struct.
-
inline constexpr auto invalid_wrap = invalid_wrap_t{}#
A global instance of
invalid_wrap_t.
-
template<typename T, typename IFace, wrap_semantics Sem>
struct holder# Holder class for type-erased values.
Note
This class is to be regarded as an implementation detail, and as such it is left undocumented on purpose.