Erasure.
Class templates to assist implementing the type erasure pattern. The pairwise composition of concepts is also supported.
Type erasure pattern in general https://www.modernescpp.com/index.php/c-core-guidelines-type-erasure-with-templates This particular implementation was inspired by https://aherrmann.github.io/programming/2014/10/19/type-erasure-with-merged-concepts/
Basic agents are
- Concept – an interface
- Model – a wrapper implementing Concept
- Holder – a simple storage of an instance of a concrete implementation of Concept
- Container – a Model stored in an appropriate Holder
- Specification – a structure providing Concept, Model and Interface provided by the programmer
- TypeErasure – a template directing the type erasure composition from a specification
- MergeSpecifications – a template to combine two specifications into one
struct PrintableSpecification {
struct Concept {
virtual ~Concept() = default;
virtual void print() const = 0;
};
template <class Holder>
struct Model : public Holder, public virtual Concept {
using Holder::Holder;
void print() const override {
Holder::get().print();
};
};
template <class Container>
using Container::Container;
void print() const {
Container::get().print();
};
};
};
using PrintableType = TypeErasure::TypeErasure<PrintableSpecification>
using PrintableExportableType =
TypeErasure::TypeErasure<TypeErasure::MergeSpecifications<PrintableSpecification,
ExportableSpecification>>
Class templates to assist implementing the type erasure pattern.