Giter Site home page Giter Site logo

cpp-codegen-rs's Introduction

Generate Code from C++ Classes Build StatusBuild statusCoverage Status

cpp-codegen-rs uses libclang to read C++ class definitions and generate code. Example use cases include the generation of Google Mock Classes, Reflection Libraries, (De)Serialization, RPC Frameworks, and anything else that suffers from a lack of proper compile-time reflection in C++. The underlying concept is that it's sometimes preferrable to use actual code as the IDL to generate these things as supposed to a dedicated IDL.

Project Status

This is currently alpha. Expect bugs and API changes.

Usage

cpp-codegen-rs is a source-to-source compiler. While the input has to be very specific (C++ classes), the output can be anything ranging from more C++ code, bindings for other languages to documentation.

The following example creates GoogleTest mock objects for C++ classes. Usually code generation is run before the actual compilation.

Given the following C++ header:

#pragma once

struct Interface {
    virtual ~Interface() = default;
    virtual void method(int foo) = 0;
    virtual int foo(double) = 0;
};

The corresponding GoogleTest mock object would look like this:

#pragma once
#include <gmock/gmock.h>

class MockInterface : public Interface {
  MOCK_METHOD1(method, void(int));
  MOCK_METHOD1(foo, void(double));
};

cpp-codegen-rs parses the C++ header, and creates a Model object out of the abstract syntax tree defined by the header. The Model is then passed to a template file. To achieve the desired transformation, a suitable template looks like this:

// THIS FILE IS GENERATED, CHANGING IT IS FUTILE
#pragma once
#include <gmock/gmock.h>

{{#each interfaces ~}}
class Mock{{name}} : public {{name}} {
{{#each methods ~}}
    MOCK_METHOD{{len arguments}}({{name}}, {{return_type}}({{#each arguments}}{{argument_type}}{{#unless @last}}, {{/unless}}{{/each}}));
{{/each ~}}
};
{{/each ~}}

The templating language is based on the HandleBars Rust library, which should be consulted for documentation on how to write templates. A complete template for GoogleTest mock objects, which also deals with namespaces and class templates, can be found in gmock.hbs.

In order to perform the actual compilation, cpp-codegen-rs is invoked with the following parameters

cpp_codegen interface.h -t templates/gmock.hbs

Generated code is written to stdout.

Distribution

My goal is to supply statically linked (against libclang) release binaries for Linux, OS X and Windows to ease deployment.

License

MIT

cpp-codegen-rs's People

Contributors

jupp0r avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

cpp-codegen-rs's Issues

CMake macros

It would be nice to supply CMake macros with this program to allow easy consumption for CMake users.

Auto-detect built-in includes

It would be nice if built-in includes from existing clang/gcc/msvc installations were autodetected. Otherwise, they'd have to be specified manually by users in order to be able to parse STL includes.

Read commandline args from file

Windows supports only 8191 characters of commandline. This is not enough to pass arguments to cpp-codegen-rs if the project is large. Reading arguments from a file or stdin would enable longer arguments.

Pass compiler arguments

In order to enable proper parsing, users need to be able to pass arbitrary compiler command line arguments to cpp-codegen-rs. To separate these arguments from cpp-codegen-rs arguments, a -- separator should be used.

cpp-codegen-rs interface.h -t template.hbs -- -I/usr/local/include -DFOOBAR

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.