GithubHelp home page GithubHelp logo

cpp-jinja-enums's Introduction

C++ reflection with clang, Python and Jinja (demo)

Templates are one the distinguishing feature of C++. Feared and (sometimes?) loved, they are are a great tool for generic programming.

Still, they fall short on other types of meta programming and something which behaves like a Python enum usually involves some error-prone boilerplate. Or you use the C pre-processor and re-invent your own small language, that is how Better Enums solves this.

Meanwhile, Rust programmers have a macro system where they can manipulate the abstract tree of program instead of dumb strings.

A famous example is serde and its derive macro:

#[derive(Serialize, Deserialize)]
struct Point {
    x: i32,
    y: i32,
}

Here, the structure is defined in native rust, in contrast with the usual pre-processor solutions.

But, we also have a lot of overpowered tools like libclang and... Python?

Concept

It uses libclang binding in python to get an AST, extract information from select tree nodes and then feed this to a Jinja template to generate C++ code!

It looks like:

#include "message_generated.h"

enum class [[clang::annotate("from_string")]] Message {
     PING,
     PONG,
};

int main() {
    std::cout << Message::PING << "\n";  // "PING"
}

Advantages:

  • reflection!
  • type declaration is a normal C++ enum in the code
  • writing (jinja) templated code is almost like writing regular C++ code, no dark pre-processor magic required
  • easy to integrate in existing project with custom "templates"

Still Annoying:

  • we cannot use a plain C++ attribute [[from_string]] as libclang only exposes the attributes it knows about. But we can re-purpose clang::annotate.

See also

  • serde-cpp follows the same approach but uses an external parser, cppast, which does not have the limitation around unknown attributes. The generator is in C++ itself, it seems intended to be used as a standalone serialization library, not as a repurposeable building block to include in another project.
  • open proposals for reflection in C++: N4856 and P1240R2

cpp-jinja-enums's People

Contributors

lbonn avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.