Skip to content

galtza/mpml

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Meta-Programming Mini Library (mpml)

Overview

Small meta-programming library that is mainly used to generate the list of parents of a determined type. Basically from lass K we want to retrieve [ F, H, J, I, K ]

                   F
      A          // \
     / \         H   \
    B   C      // \\  \
   /   / \     I   J   G
  T   D   E    \\ //  / \
                <K>  L   Z
                 | 
                 W

On the core level, it has the following common meta-programming features:

  1. Type-list definition
  2. Basic operations like push_back, pop_front, at, etc.
  3. More complex operations like get_filtered by predicate, get_the_best according to a comparator or get_ancestors given a type and a type-list
  4. Helper macros to help use some features

Usage

As a compilation unit is being processed we can create, add and read typelists. This can be used for many purposes like class registry.

Use MPML_DECLARE, on the global or namespace scope in order to declare a list:

#include <iostream>
#include <iomanip>
#include "mpml.h"

MPML_DECLARE(REG_TYPES); // <--- Here we declare the type list which is initially empty

...

After that, and again in the global or namespace scope, we can keep on adding types to the typelist by using MPML_ADD like this:

struct AType {
    ...
};

MPML_ADD(AType, REG_TYPES); // <--- Here we add the type 'AType'

struct AnotherType {
    ...
};

MPML_ADD(AnotherType, REG_TYPES); // <--- Here we add the another type called 'AnotherType'

Finally, we can use MPML_TYPES anywhere to get the typelist so far.

In the example, given an instance of a class X we calculate the ancestors and iterate over the types. On each iteration we cast our pointer to the current iteration type and print out the type info. However, we can do more productive things like for instance serializing the class.

References

For further explanation about how this library was built, please refer to hierarchy-inspector article, where you can find a thorough explanation.