Lumiera
0.pre.03
»edit your freedom«
|
Go to the source code of this file.
Helper for building »virtual copy« operations.
Especially in conjunction with type erasure, sometimes we have to deal with situations, where we want to copy or assign an object without knowing the full implementation type. Obviously, the default (compiler generated) operations will not work in such situations – even worse, they will slice the objects on copy. The reason is: we must know the full implementation and storage layout of a type do provide any meaningful copy, assignment or move operation.
A possible workaround is to call into the actual implementation type through a virtual function (which requires a VTable): Even if everyone else has ceded ("erased") any knowledge regarding the exact implementation type, the function pointers in the VTable still implicitly hold onto that precise implementation type, since they were set up during construction, where the type was still available. Such a scheme of dealing with "opaque" copy operations is known as virtual copy — it can be dangerous and tricky to get right and preferably it is used only in flat and effectively closed class hierarchies.
This helper template simplifies the construction of such a scheme.
downcast(Base&)
to perform a suitable dynamic or static downcast from the interface type to the concrete implementation type.The provided virtual operations are to be used in "backward" direction: invoked on the source and affecting the target. The source, through the VTable, knows its precise type and data layout. The target is handed in as parameter and treated by the downcast()
function — which preferably performs a dynamic cast (or at least asserts the correct type). This whole scheme can only work for copy and assignment of objects, which actually have the same implementation type – it will never be possible to "cross copy" to an completely unrelated target type, at least not generically.
Usage example
In this example, the concrete implementation of the Buffer interface will mix in the Policy template with the implementations of the virtual copy operations. The copy constructor uses the virtual copyInto(void*)
to perform a "placement new", whereas the assignment operator calls the virtual copyInto(Buffer&)
to downcast the target Buffer and in the end invokes the assignment operator on the concrete Buffer implementation subclass.
Definition in file virtual-copy-support.hpp.
Classes | |
class | CloneSupport< I, D, B > |
struct | CopySupport< X, SEL > |
Policy to pick a suitable implementation of "virtual copy operations". More... | |
struct | CopySupport< X, enable_if< supports_cloning< X > > > |
struct | CopySupport< X, enable_if< supports_copy_and_assignment< X > > > |
struct | CopySupport< X, enable_if< supports_only_move< X > > > |
struct | EmptyBase |
class | FullCopySupport< I, D, B > |
class | MoveSupport< I, D, B > |
class | NoCopyMoveSupport< I, D, B > |
struct | supports_cloning< X > |
struct | supports_copy_and_assignment< X > |
struct | supports_only_move< X > |
class | VirtualCopySupportInterface< IFA, BASE > |
Namespaces | |
lib | |
Implementation namespace for support and library code. | |