flexferrum / autoprogrammer

C++ to C++ code generation tool (enum2string conversion, serialization, reflection etc.)
MIT License
91 stars 15 forks source link

Project status #8

Open blockspacer opened 5 years ago

blockspacer commented 5 years ago

This project is very interesting.

Some of autoprogrammer's features in TODO status, so i wanted to add them.

I want to use some proposed features (like metaclasses) without waiting 3+ years for new C++ standard and can`t find stable (ready) solution.

Inspired by autoprogrammer's approach i created https://github.com/blockspacer/CXXCTP

Maybe some ideas from that project will be useful for autoprogrammer.

// $apply is just macro that adds attribute

class $apply(make_interface;make_removefuncbody;make_reflect) SomeInterfaceName { virtual ~SomeInterfaceName() = 0; /int f ( ) { // {} int i = 6; { // { // } } return i; };/ int foo(); virtual void foobar(int& arg1) = 0; virtual inline void zoobar(int& arg2); //int m_bar; //int m_bar2 = 2; };


In attribute i can list needed metaclasses. Each metaclass is function (like `make_interface`) that returns modified source code or generated file. 

+ I wanted to create tool not focused on predefined set of source transformation rules. That's why i added per-project `ctp_scripts` folder. If `ctp_scripts` used by separate project - all rules from  `ctp_scripts` must be imported into Cling, so it behaves like interpreted plugin system. Because interpreted code runs slower - i added `custom_plugins.cmake.example`, so 'plugin' may be compiled and used as built-in tool plugin. Maybe it is better to create .dll/.so based plugin system.
flexferrum commented 5 years ago

Thank you for interesting in the project. I'm going to uplift the autoprogrammer project till the upcoming CoreHard++ conference where I'm giving the talk about modern code generation tools (not only autoprogrammer). Regarding your suggestions:

I found what jinja to C++ binding produced a lot of boilerplate code. I replaced jinja with https://github.com/blockspacer/CXTPL

In the production projects I used this tool I generated not only C++ files, but C files, JSON samples etc. as well. So, template engine which is stuck to the C++ isn't a good choice for me. Another point is that I can ship templates with the production project, not with the tool. Jinja2 is a widely-used notation, template engine is fast and independent. Lot of the boilerplaite for the reflection isn't a problem till this boilerplate can be generated. :)

I can't find docs about autoprogrammer's C++ interpreter, so i used Cling as C++ interpreter.

Yes, there are no docs except my presentation. One of the reasons is that C++ interperter here is still in progress. Actually, it utilizes the idea of constexpr evaluator from the clang with one but significant addition: it allows mixing of injected and interpretable code. I. e. you can write:

inline void BoostSerializable(meta::ClassInfo dst, const meta::ClassInfo& src)
{
    for (auto& v : src.variables())
        $_inject_v(dst, public) v;

    $_inject_v(dst, public) [&, name="serialize"](auto& ar, unsigned int ver) -> void
    {
        $_constexpr for (auto& v : src.variables())
            $_inject(_) ar & $_v(v.name());
    };
};

Here $_constexpr for (auto& v : src.variables()) will be interpreted in the context of the injected method. I'm not sure the Cling allows such kind of tricks.

I wanted to chain multiple metaclasses, so i added custom C++ attribute syntax, very similar to calling C++ functions.

That's not a problem with the current autoprogrammer implementation: $_class(SomeVisitor, CRTPVisitor<A, B, C>, Interface)

I wanted to create tool not focused on predefined set of source transformation rules.

I've been thinking about the external customization/plugins. But I'm considering something like python (in addition to the Jinja2 templates). Binding isn't a big problem.

blockspacer commented 5 years ago

@flexferrum

  1. about CXTPL output. About JSON - i can't imagine useful usage examples with JSON. Can you please provide link to similar tool what can output JSON with some usage examples? boilerplate can be generated - that may solve a lot of problems. Can Jinja2Cpp generate boilerplate code?

  2. mixing of injected and interpretable code seems less verbose than current Cling based approach. In theory, adding some utility functions or macro may produce similar syntax.

autoprogrammer's syntax seems beautiful, but complex for me. Maybe i`m wrong and some docs / updates will solve complexity issue.

flexferrum commented 5 years ago
  1. Unfortunately, no, I can't. It was a proprietary project I worked on in my previous company. There was a jinja2-based template which produced sample JSON settings file based on structs read from the source code. Jinja2 itself is a library. It can't generate it without proper binding. For this project I'm considering the three-steps build. On the first step core code generation library is built. Then, on the 2nd step, special bootstrap tool is compiled on the top of the core library. This tool generates all boilerplate code for the project. On the 3rd step main autoprogrammer tool is produced.

  2. May by. BTW, this approach needs special support from the interpreter.

Docs will come up. In some future. :)

flexferrum commented 4 years ago

Here is the sample how jinja2 templates can be used for serialization boilerplate code generation: https://github.com/flexferrum/autoprogrammer/tree/master/test/serialization

zamazan4ik commented 4 years ago

https://github.com/jinja2cpp/Jinja2Cpp/issues/195

blockspacer commented 4 years ago

I'm sorry to hear this sad news of @flexferrum 's passing.

If anyone interested in development of tools similar to https://github.com/flexferrum/autoprogrammer or https://github.com/blockspacer/CXXCTP - please contact me. I'll be happy to contribute. E-mail: derofim hosted on yandex.ru