I would like to highlight a technique in C++ called mixin layers and present a use case, where it is very useful.
Mixin layers is a template-based design, where the layers are used a building blocks to quickly construct applications with reusable code. My favourite example is a networking application, where headers constructed/are added at the sender side, and parsed/removed.
In the application code, it would look something like this:
To use the layers, one simply includes the needed headers, and create a #stack# with the typedef shown above. The stack then defines two primary functions to be used: send_buf() and recv_buf().
When calling those functions, each layer intercepts the call, and does whatever it has to do with the buffer, before forwarding the call to the next layer. In our example, the first layer to intercept send_buf() is header_seq, which looks like this:
The class hooks into the stack by inheriting from the template class super. By doing this, it "his" send_buf() declared in lower layers.
The mixin technique becomes useful when maximum flexibility is needed. Like when I build prototypes for research: I can simply build up a network application for testing a protocol by plugging a bunch of layers together.
The above source code is from an example application I wrote for this blog post. You can see the entire source code here. Compile it like this: