Templated implementation of the Visitor Pattern for C++11
John Wellbelove
by John Wellbelove
10M ago
This is an update to the previous post about templating the Visitor pattern. Templated implementation of the Visitor Pattern for C++03 I won’t repeat the bulk of the post as the examples will be identical. The new code uses ‘variadic temples’ that were introduced with C++11, and as such, greatly simplifies the code required. Visitable [pastacode lang=”cpp” manual=”%20%20%2F%2F*****************************************************************%0A%20%20%2F%2F%2F%20The%20visitable%20class%20for%20N%20types.%0A%20%20%2F%2F*****************************************************************%0A%20%20temp ..read more
Visit website
Implementing a moving average
John Wellbelove
by John Wellbelove
10M ago
Known by many names, such as as Cumulative Moving Average, Rolling Average, Running Average, Moving Mean or Rolling Mean, the object is to update an average value on receipt of a new value over a set window size. Example: Imagine we have accumulated 10 values and our window size is 10. [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] The average of this sequence is (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10) / 10 = 5.5 We now add a new sample, 11 to the sequence. As the windows size is 10, we drop the first value. [ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ] The average of this sequence is (2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 ..read more
Visit website
C++ wrapper for legacy C arrays
John Wellbelove
by John Wellbelove
10M ago
If you are writing in C++ you will sometimes be presented with an array defined by a section of C code. Using C++ you will have got used to having member functions to allow access to a container in a more error free fashion than raw C arrays. Using this template we can create a zero cost wrapper around a C array which will give it an STL style interface. The template has no member variables and has functions that return fixed values that will certainly be optimised away at compile time. The template may be used for access to both const and non-const arrays. Template parameters may not only be ..read more
Visit website
The Embedded Template Library
John Wellbelove
by John Wellbelove
10M ago
Why did I start it? I wrote the Embedded Template Library (ETL), and all the others I have written over the years, because I’m lazy. Yes, lazy! But in a good way I believe. See here. Don’t Re-invent The Wheel I hate having to code yet another variation of the same thing in a slightly different way time and and time again. Reinventing the wheel every time is daft for many reasons. Code bloat Multiple instances of slight variations of a theme results in an increase in code size due to no commonality of functionality. Testing, more testing or no testing Are all the variants tested to the sa ..read more
Visit website
There are four types of programmer
John Wellbelove
by John Wellbelove
10M ago
Their is a story about a German general that declared that there were four kinds of people, only three of which he wanted in his army. Here’s my take on it from a programmer’s angle.   Intelligent LazyIntelligent lazy people hate having to reinvent a solution to a problem that has already been solved. Moreover they don’t always go for the first solution that comes to mind. They would prefer the best solution; the one that means you don’t have to go through all of the problem solving work all over again when a slight variation turns up. They’re the ones that build a generic library with bu ..read more
Visit website
Largest / Smallest. Part 2
John Wellbelove
by John Wellbelove
10M ago
In the last post i showed you how to implement a ‘Largest’ traits template using pre-C++11. In this post I will show you how to do the same using variadic templates. First we declare the ‘bare’ template. It tells the compiler that ‘Largest’ take an unknown number of template parameters. [pastacode lang=”cpp” manual=”template%20%3Ctypename…%20Types%3E%0Astruct%20Largest%20%7B%7D%3B” message=”” highlight=”” provider=”manual”/] Next, we create helper templates as we did before. In fact they are identical. [pastacode lang=”cpp” manual=”%2F%2F%20Declaration.%0Atemplate%20%3Cconst%20bool%20Boolean%2 ..read more
Visit website
Largest / Smallest. Part 1
John Wellbelove
by John Wellbelove
10M ago
Sometimes, when multiple types are sent as template parameters, some information about the types needs to be interrogated to be able to configure another property of the template. The STL’s type traits header does a good job of supplying many of the more useful ones, but there is room for extension. Say, for instance, the template implements a type of variant container; a type of smart union. The container may store one of several types declared in the template parameter list. We need to allocate storage for this, but how determine the size? [pastacode lang=”cpp” manual=”template%20typename%20 ..read more
Visit website
Templated implementation of the Observer Pattern
John Wellbelove
by John Wellbelove
10M ago
The purpose of this template is to simplify the creation of classes that implement the Observer Pattern and attempt to eliminate certain runtime errors by turning them into compile time errors. The pattern consists of two template classes. Observer and Observable. Observable The class derived from this will be observed by classes derived from Observer. It keeps a list of registered observers and will notify all of them with the raised notifications. It is parameterised by a supplied observer type. void AddObserver(TObserver& observer) Registers an observer with the observable cla ..read more
Visit website
Designing an efficient and low resource messaging framework. Part 3
John Wellbelove
by John Wellbelove
10M ago
In the last post I showed how to shrink the vtable overhead and reduce the coupling of the framework. This time I shall show how to reduce the vtable hit even further using another technique called the Curiously Recurring Template Pattern or CRTP. This oddly name pattern is a way of replacing runtime virtual calls with compile time dispatch. It achieves this by passing the derived class type as a template type parameter to the base class. [pastacode lang=”cpp” manual=”template%20%3Ctypename%20TDerived%3E%0Aclass%20Base%0A%7B%0A%7D%3B%0A%0Aclass%20Derived%20%3A%20public%20Base%3CDerived%3E%0A%7 ..read more
Visit website
C++ wrapper for legacy C arrays
John Wellbelove
by John Wellbelove
4y ago
If you are writing in C++ you will sometimes be presented with an array defined by a section of C code. Using C++ you will have got used to having member functions to allow access to a container in a more error free fashion than raw C arrays. Using this template we can create a zero cost wrapper around a C array which will give it an STL style interface. The template has no member variables and has functions that return fixed values that will certainly be optimised away at compile time. The template may be used for access to both const and non-const arrays. Template parameters may not only be ..read more
Visit website

Follow John Wellbelove on FeedSpot

Continue with Google
Continue with Apple
OR