GithubHelp home page GithubHelp logo

austinbrunkhorst / cpp-reflection Goto Github PK

View Code? Open in Web Editor NEW
634.0 49.0 91.0 212 KB

C++ Reflection Parser / Runtime Skeleton

License: MIT License

C++ 93.66% Objective-C 0.20% C 0.40% HTML 2.60% CMake 3.14%
cmake libclang c-plus-plus meta reflection automation build

cpp-reflection's Introduction

C++ Reflection

Join the chat at https://gitter.im/CPP-Reflection/Lobby

Preface

I worked on a complete reflection pipeline starting in the summer of 2015 for a game project / editor. My intent by creating this repository was to share my experience and how I came about developing it. The response from the community motivated me to make it a tad bit more official by allowing others to consume and build it easily, rather than just giving you code and saying "fill in the pieces".

I created a blog where I talk more in detail about the process and try to share my experiences as best as possible. You can find the blog here -

https://austinbrunkhorst.com/cpp-reflection-part-1/

Building

There are three buildable sections in this repository - Runtime, Parser and Examples. I setup an environment for building using CMake - yes it's insane but it's also awesome so let's just go with it. All examples are assuming you're working from the root of this repository.

Requirements

  • LLVM 3.8.0+ (for libClang)
  • Boost 1.59+
  • A C++11 compliant compiler - I've tested on MSVC 14, G++ 4.8 and Clang++ 3.6.

Runtime

There are no dependencies in the runtime so building is pretty straightforward.

Create a build directory.

mkdir Build && cd Build

Generate a build system using any desired generator in CMake.

cmake -G "<Desired Generator>" ../Source/Runtime

Build - you can use any IDE if applicable to the generator, but you can also just build straight from CMake.

cmake --build . --target MetaRuntime

Parser

There are more moving parts in this because the parser actually has dependencies and builds to an executable. Don't worry though! I'm here to walk you through this.

Install LLVM 3.8 for LibClang

Windows - download the 32 bit or 64 bit pre-built binaries.

Unix based systems - find the appropriate package. On Linux Mint I just did the following.

sudo apt-get install libclang-3.8-dev

The installation should be located in /usr/lib/llvm-3.8

Once installed, set an environment variable LLVM_ROOT to the root of the installation directory. You can skip this step, but an environment variable makes the CMake command simpler.

Install Boost 1.59

This part sucks, but we've gotta do it. Download the sources and build it using these instructions.

Once installed, set an environment variable to BOOST_ROOT like we did for LLVM.

Create a build directory.

mkdir Build && cd Build

Generate a build system using any desired generator in CMake.

cmake -G "<Desired Generator>" ../Source/Parser

If you skipped creating environment variables, you'll have to define variables for resolution in CMake directly - just add these two switches in the command above.

-DLLVM_ROOT=<PATH> -DBOOST_ROOT=<PATH>

Build - you can use any IDE if applicable to the generator, but you can also just build straight from CMake.

cmake --build . --target MetaParser

Examples

You will need to follow the same steps for setting up the dependencies explained in the Parser build instructions.

Just like the other two targets you'll do the following -

Create a build directory.

mkdir Build && cd Build

Generate a build system using any desired generator in CMake.

cmake -G "<Desired Generator>" ../Examples

Build - you can use any IDE if applicable to the generator, but you can also just build straight from CMake.

cmake --build .

Remember to add the extra switches for defining LLVM_ROOT and BOOST_ROOT if you skipped adding them as an environment variable.

All of the examples build to a simple executable that demonstrates the specific features/functionality in use.

I don't have immediate intentions of documenting the interfaces and such with the runtime library, but hopefully the examples cover all parts of the runtime and people can get the whole picture there. My blog posts cover the development process and the reason I made the decisions I did.

Feel free to contact me with any questions or concerns! Also, pull requests are very welcome!

cpp-reflection's People

Contributors

austinbrunkhorst 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  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

cpp-reflection's Issues

can you add some document about cmake rule?

it's a cool library, thanks.
i use the library in my project, i write my cmake file on the basis of example's cmake file.
i generate the vs project, and generate the needed meta files, but these files is empty.
i can't debug this, i don't know where is wrong.
can you add some document about cmake rule?
for example, explain the "meta_parser_prebuild" and "meta_parser_build".
Thanks.

The examples can not work on Mac OS!

I found the content of generated files is empty!
the compile error is :

Undefined symbols for architecture x86_64:
"meta_generated::ModuleTestModule::ModuleTestModule(ursine::meta::ReflectionDatabase&)", referenced from:
_main in Enums.o
"meta_generated::ModuleTestModule::~ModuleTestModule()", referenced from:
_main in Enums.o

Documentation issue (CMake)

In README.md you state

Note: this assumes you have created empty files for ${META_GENERATED_HEADER} and ${META_GENERATED_SOURCE} and added it to the list of sources when calling add_executable( ). Otherwise, CMake will complain about the files not existing initially.

The recommended approach is not to create empty files for generated files (especially as you will be tempted to put them in the source directory instead of the build directory). Instead you should set the GENERATED on the corresponding sources:

set_source_files_properties(
    ${META_GENERATED_HEADER} ${META_GENERATED_SOURCE}
    PROPERTIES
    GENERATED ON
)

Furthermore you use a PRE_BUILD TARGET add_custom_command which only works on Visual Studio. Using a non-TARGET add_custom_command might be better (and also sets the GENERATED property automatically).

Get Type with string name Problem.

In Test Projects With MetaProperties.The Code Say:
// you can also use type meta::Type::Get( "SoundEffect" ) based on a string name Type soundEffectType = typeof( SoundEffect );

But,when i Replace the code with:
Type soundEffectType = ursine::meta::Type::Get("SoundEffect");

The soundEffectType Result not Right! SoundEffectType.m_id is 0,and then the program crash with null pointer read!

Can this project to be used as a code generator?

Does this project supposed to:

  1. Parse the C++ source code with full language features via clang? Which means it could parse code with template, macro etc.
  2. Get all the information about all the classes and user defined meta data?
  3. Generate code based on the information with customizable template?

TypeInfo.hpp DecayedType

I think this line should be:

typedef typename std::decay<T>::type Decayed;

rather than:

typedef std::decay<T> Decayed;

Because you wanna check if the decayed type of T is a primitive/enum/class/etc.. and not check if the instantiated template std::decay<T> is a primitive/enum/class/etc.. (which would always generate the same results: class = true, primitive = false, enum =false).

Type Meta Data not work

I have read the section of Type Meta Data, but I can't make it work.

Range is inherit from Object, but there are two pure virtual method:

        virtual Type GetType(void) const = 0;
        virtual Object *Clone(void) const = 0;

so, I can't got a instance of Range

And I can't find any code in the Parser to handle the annotate attribute of libclang

So, I want to know, is the annotate attribute is ready to be use or some code must be modify before I can use it.

Thank's very much

Examples fail to link

Hi @AustinBrunkhorst
When compiling the Examples, everything seems to go fine (both Parser and Runtime compile) but the Serialization.cpp fails to link. This is the output I get. If I look at the files of the Generated folder, all of them but the *.includes one are empty. I think there is something wrong with the meta_parser_build command, like if it was not triggering the Parser. Any ideas?

[ 23%] Built target MetaParser
[ 55%] Built target MetaRuntime
[ 61%] Built target TestModule
[ 62%] Linking CXX executable Serialization
CMakeFiles/Serialization.dir/Serialization.cpp.o: En la función `main':
Serialization.cpp:(.text+0xcb): referencia a `meta_generated::ModuleTestModule::ModuleTestModule(ursine::meta::ReflectionDatabase&)' sin definir
Serialization.cpp:(.text+0xda): referencia a `meta_generated::ModuleTestModule::~ModuleTestModule()' sin definir
collect2: error: ld devolvió el estado de salida 1
make[2]: *** [CMakeFiles/Serialization.dir/build.make:193: Serialization] Error 1
make[1]: *** [CMakeFiles/Makefile2:70: CMakeFiles/Serialization.dir/all] Error 2

I am under linux, using GCC 6.2.0, libclang 3.8.1 and boost 1.6.1
Best,
Arnau

UrisinePrecompiled.h missing

This header is missing from repo.
Is this file essential to get things up and working or it is safe to remove.

Asking in advance, before I start messing with and things fall to pieces (;.

Can not set struct as array

I defined two struct to test.

struct arrStruct
{
    int c;
    int d;

} Meta(Enable);

struct TestStruct
{
    int a;
    int b;

    struct arrStruct arr[2];
} Meta(Enable);

test code:

void ShowVec(const vector<Field>& valList)
{
    for (auto iter = valList.cbegin(); iter != valList.cend(); iter++)
    {
        Field f = *iter;
        Type t = f.GetType();
        if (t.IsArray())
        {
            auto arrayCtor = t.GetArrayConstructor();
            auto instance = arrayCtor.Invoke();
            auto wrapper = instance.GetArray();
            std::cout << "array's size:" << wrapper.Size() << std::endl;
        }
        if (t.IsClass())
        {
            std::vector<Field> fields = t.GetFields();
            ShowVec(fields);
        }
        std::cout << "field's name:" << (*iter).GetName() << std::endl;
    }
}


when compile:

source\runtime\Impl/FieldSetter.hpp(64): error C3863

Is this a work-in-progress source?

It has tons of problems and bugs, this uploaded version is clearly not working. I had to insert a lot of modifications to make it compile and the Meta attributes is still not working as well.

Conan.io

It looks like much of the dependency setup outlined in your readme could be done automatically by using conan.io.

The runtime type limit?

I test "Serialization" Project ,change the ComplexType struct's ursine::Array<int> arrayValue; with
std::list<int> arraryValue; and also change the generate code with type.AddField<ComplexType, std::list<int>>

Then complile and run, the Result of arraryValue is empty.I look at the function SerializeJson(const Variant &instance, bool invokeHook) ,and find that's no std::list serialize function.

So,can you tell me all types, that i can use with runtime?

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.