Library: allow to immediately emplace a subclass type into InPlaceBuffer
Up to now, InPlaceBuffer used to default construct an instance of the Interface class, and then you'd need to invoke the `create()` function to actually create the desired subclass. This is not only inefficient, but rules out the use of abstract interfaces / base classes. Unfortunately, there is no way in C++ to specify an explicit template argument list on ctor calls, so we resort to the trick of passing an additional dummy marker argument
This commit is contained in:
parent
c9a8b82dff
commit
f0de986399
1 changed files with 19 additions and 5 deletions
|
|
@ -616,16 +616,30 @@ namespace lib {
|
|||
|
||||
|
||||
public:
|
||||
InPlaceBuffer ()
|
||||
{
|
||||
placeDefault();
|
||||
}
|
||||
|
||||
~InPlaceBuffer ()
|
||||
{
|
||||
destroy();
|
||||
}
|
||||
|
||||
InPlaceBuffer ()
|
||||
{
|
||||
placeDefault();
|
||||
}
|
||||
|
||||
/** immediately emplace an embedded subclass type */
|
||||
template<class TY, typename...ARGS>
|
||||
InPlaceBuffer (TY*, ARGS&& ...args)
|
||||
{
|
||||
new(&buf_) TY (std::forward<ARGS> (args)...);
|
||||
}
|
||||
|
||||
/** helper to mark the subclass type to create.
|
||||
* @remarks we can not specify explicit template arguments on ctor calls,
|
||||
* so the only way is to use a dummy marker argument to pass the type.
|
||||
* Use as `InPlaceBuffer(embedType<XYZ>, arg1, arg2, arg3)` */
|
||||
template<typename SUB>
|
||||
static auto embedType() { return (SUB*) nullptr; }
|
||||
|
||||
|
||||
/** Abbreviation for placement new */
|
||||
template<class TY, typename...ARGS>
|
||||
|
|
|
|||
Loading…
Reference in a new issue