Home - GerdHirsch/Layers GitHub Wiki

This Repository is an example for implementing multi exchangeable layers with C++ templates CRTP.

next

This project is intended to be used as an area to experiment with the C++ template based (CRTP) implementation of multi layers.

The technique used is based on Template Method CRTP.

To implement multiple exchangable layers in the conventional way using dynamic polymorphism with virtual functions you need some interfaces for each layer:

  • the service provided by the layer used from the upper layer and
  • the service required by the layer implemented by the upper layer as callback
  • the service required by the layer implemented by the lower layer as service

With C++ templates and the CRTP all could be inlined. The examples shows how to implement the different layers.

There are three different kinds of layers:

The layer on the top, which is just a template:

template<class LowerLayer>
class TopLayer : LowerLayer::template LayerImpl<TopLayer<LowerLayer>>{...}

Don't worry about the syntax, you will soon be used to it;-)

The layer on the bottom (Layer0), a type with an inner template. Layer0 is provided by the framework and can be used as it is: (defined in Layer0.h ) The TopLayer is handed over thru all layers to the Layer0.

struct Layer0{
    template<class TopLayer>
    struct LayerImpl {..};
};

And the layers between them which are templates with inner templates:

template<class LowerLayer>
struct Layer4{
    template<class TopLayer>
    struct LayerImpl : LowerLayer::template LayerImpl< TopLayer > {..};
};

With this in place, the top layer can use the services from the LowerLayers and the LowerLayers can send messages to the UpperLayers via This()-> which is provided from Layer0! And all without the overhead of virtual functions, and if possible not even any function call at all, because of inlining.

next

⚠️ **GitHub.com Fallback** ⚠️