Coder Social home page Coder Social logo

leandros / metareflect Goto Github PK

View Code? Open in Web Editor NEW
226.0 9.0 31.0 39 KB

Metareflect is a lightweight reflection system for C++, based on LLVM and Clangs libtooling.

Home Page: https://arvid.io

License: MIT License

C++ 99.52% CMake 0.48%
c-plus-plus c-plus-plus-11 llvm clang reflection

metareflect's Introduction

MetaReflect

Metareflect is a lightweight reflection system for C++, based on LLVM and Clangs libtooling.

  • Lookup members of reflected classes: Once reflection data has been generated, you can query the members of the class, inspect their types, size and where they're located in the class, as well as qualifiers and storage class.
  • Control over which classes are reflected: By annotating your class with CLASS, and each member which you want to be reflected with PROPERTY, you have fine-grained control over what you want to reflect, and what you want to keep completely private.
  • Extensible: It's easy to add new flags, which can be queried at runtime. This allows for easily adding any feature you like. Currently used to provide serialization from and to a byte stream for annotated classes.

Getting Started

To get started download the latest release here.

Setting Up The Runtime

Metareflect requires the runtime, it's consisting of the interface and a couple helper macros for annotating your classes. To include it into your project, simply copy over the /metareflect folder into your project. Unfortunately, the runtime is not fully header-only and requires you to compile the files ending in .cxx.

Annotating Your Classes

Every class which you want to be reflected needs to be annotated.

point.hxx:

#include <metareflect/metareflect.hxx>

CLASS() Point
{
public:
    PROPERTY()
    int x;

    PROPERTY()
    int y;

    PROPERTY()
    int z;

    FUNCTION()
    size_t Hash() const
    {
        return x ^ y ^ z;
    }
};

point.cxx:

#include "point.hxx"
#include "point.generated.hxx"

/* rest of the code */

For a full example take a look at our /example.

Run The Metareflect Tool

To provide the reflection data, the metareflect tool generates a header-file which contains the reflection data in a format consumable by the runtime. The generated file needs to be in your include path and included into the implementation file of the reflected class.

Since metareflect is based on libtooling, all the flags common to libtooling tools apply to it to. That means if you have a build system which can generate a compile_commands.json (for example: ninja or CMake) you can simply provide it the path to your compilation database and metareflect will pick the correct flags automatically. For further information, consult the LLVM documentation for libtooling.

To generate a compilation database using CMake, pass it -DCMAKE_EXPORT_COMPILE_COMMANDS=ON while generating your build files.

Contributing

Any contribution is welcome. Take a look at the open tickets to get started with the development of metareflect.

Requirements

  • A clone of the LLVM 6.0.0 source code, including clang and clang-extra-tools. See LLVM_SETUP how to setup LLVM for development.
  • A clone of the metareflect repository
  • A beefy computer, otherwise compiling LLVM will take some time

Structure

Metareflect consists of two parts, the runtime and the libtooling tool.

The runtime lives in the metareflect/ folder, while the tooling/ folder contains the source code for the tool.

Getting Started

Once you've cloned the LLVM repo (by following the guide at LLVM_SETUP), navigate to path/to/llvm/tools/clang/tools/extra/metareflect/metareflect. The directory contains the source code for metareflect and anything you need to get started developing metareflect.

The following resources give an insight into how to develop an libtooling application:

To contribute your changes back, please open a pull request! We welcome any contribution.

Inspiration

Parts of the design and how the tool works has been based upon prior research done in Unreal Engine 4s UHT (Unreal Header Tool) and Qts moc. If you've used either of the two, you might spot the similarities.

License

MIT License

Copyright (c) 2018 Arvid Gerstmann.

metareflect's People

Contributors

leandros avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

metareflect's Issues

metareflect/tooling in CMakeList.txt

I think you mean metareflect/tooling to add to the CMakeList.txt under
llvm-project/clang-tools-extra in the mono-repo version or in llvm/clang/tools/extra in the svn version... ?

Best Regard and Great Talk at CppMeeting!

problems running metareflect

I cannot quite get it to work:
I compiled it with clang8.0.0 (head), it works with some minor CMakeList Adjustments (will send a PR...)

$BD/bin/metareflect test.hxx

Output:

Error while trying to load a compilation database:
Could not auto-detect compilation database for file "test.hxx"
No compilation database found in /Users/gabrielnuetzi/Documents/metareflect/example or any parent directory
fixed-compilation-database: Error while opening fixed database: No such file or directory
json-compilation-database: Error while opening JSON database: No such file or directory
Running without flags.
warning: /Users/gabrielnuetzi/Documents/metareflect/example/test.hxx: 'linker' input unused [-Wunused-command-line-argument]
error: unable to handle compilation, expected exactly one compiler job in ''
Error while processing metareflect/example/test.hxx.

Do you know the source of the problem?
Thanks a lot!

compilation errors when compiling against latest llvm in StringRef class

reflectedclass.cpp:164:15: error: no member named 'equals_lower' in 'llvm::StringRef'
if (s.equals_lower("serialized")) {
~ ^
/Volumes/Pahethon-George/llvm-project2/llvm-project/clang-tools-extra/metareflect/tooling/reflectedclass.cpp:167:22: error: no member named 'startswith_lower' in 'llvm::StringRef'
} else if (s.startswith_lower("width")) {
~ ^
/Volumes/Pahethon-George/llvm-project2/llvm-project/clang-tools-extra/metareflect/tooling/reflectedclass.cpp:175:22: error: no member named 'equals_lower' in 'llvm::StringRef'
} else if (s.equals_lower("cstring")) {
~ ^
/Volumes/Pahethon-George/llvm-project2/llvm-project/clang-tools-extra/metareflect/tooling/reflectedclass.cpp:195:15: error: no member named 'equals_lower' in 'llvm::StringRef'
if (s.equals_lower("replicated")) {

classfinder onEndOfTranslationUnit() assert failure

I've been messing around with this tool, as part of some personal research into clang and llvm. I think the reflection possibilities are really interesting and I like how it has a similar interface to UHT. Anyways I'm having an issue when I try to run the tool on a test header file, which is properly annotated.

It looks like the classfinder m_filename is never set, which results in an assert failure in the classfinder override of onEndOfTranslationUnit(). From what I've been able to gather the classfinder FoundRecord function seems to set this filename, but it never gets called. So it looks like for whatever reason the AST isn't finding matches and I cannot figure out why.

I know this is kind of a shot in the dark since this doesn't seem to be a very active repo, but I was wondering if anyone else has run into this. Thanks!

Not enough positional command line arguments specified error

First of all, I've never used CMake, Clangs libtooling or LLVM, so this error might be an obvious one. I went through the LLVM_SETUP, following all the steps, even adding metareflect/tooling as a subdirectory instead of metareflect/metareflect, generated the project with CMake on windows 10, on visual studio 2019, compiled LLVM, ran it and got this:

LLVM ERROR: CommonOptionsParser: failed to parse command-line arguments. [CommonOptionsParser]: metareflect.exe: Not enough positional command line arguments specified!

Must specify at least 1 positional argument: See: D:\LLVM\build\Debug\bin\metareflect.exe --help

Also, I have set metareflect as the single starting project.
Secondly, I really enjoyed your talk at CppCon 2018 and got inspired to try this out.
Any help with this would be greatly appreciated. Thanks in advance.

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.