https://github.com/jupp0r/cpp-codegen-rs
Generate code from C++ classes
https://github.com/jupp0r/cpp-codegen-rs
code-generator cpp libclang rust
Last synced: 8 months ago
JSON representation
Generate code from C++ classes
- Host: GitHub
- URL: https://github.com/jupp0r/cpp-codegen-rs
- Owner: jupp0r
- License: mit
- Created: 2016-07-17T21:34:14.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-03-04T21:39:30.000Z (about 9 years ago)
- Last Synced: 2025-07-06T11:41:08.872Z (9 months ago)
- Topics: code-generator, cpp, libclang, rust
- Language: Rust
- Homepage:
- Size: 50.8 KB
- Stars: 10
- Watchers: 1
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Generate Code from C++ Classes [](https://travis-ci.org/jupp0r/cpp-codegen-rs)[](https://ci.appveyor.com/project/jupp0r/cpp-codegen-rs/branch/master)[](https://coveralls.io/github/jupp0r/cpp-codegen-rs?branch=master)
===================================
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](https://github.com/google/googletest) mock objects for
C++ classes. Usually code generation is run before the actual
compilation.
Given the following C++ header:
``` c++
#pragma once
struct Interface {
virtual ~Interface() = default;
virtual void method(int foo) = 0;
virtual int foo(double) = 0;
};
```
The corresponding [GoogleTest](https://github.com/google/googletest)
mock object would look like this:
``` c++
#pragma once
#include
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:
``` c++
// THIS FILE IS GENERATED, CHANGING IT IS FUTILE
#pragma once
#include
{{#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](https://github.com/sunng87/handlebars-rust) library,
which should be consulted for documentation on how to write
templates. A complete template for
[GoogleTest](https://github.com/google/googletest) mock objects, which
also deals with namespaces and class templates, can be found in
[gmock.hbs](templates/gmock.hbs).
In order to perform the actual compilation, cpp-codegen-rs is invoked with the following parameters
``` bash
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