Giter Site home page Giter Site logo

reflect's Introduction

C++ reflection and serialization framework

This is a simple library that allows for simple reflection and serialization of C++ types. It is designed for use in video game engines, or other rich environments in which runtime object creation and reflection is useful.

Features

  • No meta-object compiler / preprocessing build step.
  • Composite types ("aspect oriented programming") with rich interface casts.
  • Simple serialization (to JSON at the moment).
  • Simple and efficient signal/slot implementation included.
  • Type-safe.
  • C++11 compliant and extensible -- for instance, serializers for custom types can be easily defined, without modification to those types.

Planned/pending features

  • YAML serialization.
  • XML serialization.
  • Built-in support for serialization of more standard library types.

Limitations

  • Objects must derive from the Object type and include the REFLECT tag in their definition.
  • Requires runtime type information.
  • Members of objects that aren't described by a property(member, name, description) will not be serialized/deserialized.
  • Class reflection/serialization with properties does not currently support multiple inheritance with non-interface (non-abstract) base classes. Use composite objects if needed. A class can derived from multiple base classes, but only one of them may derive from Object.

Examples

In foo.hpp:

#include "object.hpp"

class Foo : public Object {
    REFLECT;
public:
    Foo() : an_integer_property(123) {}
    void add_number(float32 n) { a_list_of_floats.push_back(n); }
    void say_hi() { std::cout << "Hi!\n"; }
private:
    int32                an_integer_property;
    Array<float32> a_list_of_floats;
};

In foo.cpp:

#include "foo.hpp"
#include "reflect.hpp"

BEGIN_TYPE_INFO(Foo)
    property(&Foo::an_integer_property, "Number",     "An integer property.");
    property(&Foo::a_list_of_floats,    "Float list", "A list of floats.");

    slot(&Foo::say_hi, "Say Hi!", "A slot that says hi.");
END_TYPE_INFO()

In main.cpp:

int f() {
      // Create a universe for our objects to live in.
      TestUniverse universe;

    // Print the type name and all properties.
    auto foo_type = get_type<Foo>();
    std::cout << foo_type->name() << '\n'; // prints "Foo"
    for (auto attribute: foo_type->attribute()) {
        std::cout << attribute->name() << ": " << attribute->type()->name() << '\n';
    }
    std::cout << '\n';

    // Create a composite type consisting of two Foos.
    CompositeType* composite = new CompositeType("FooFoo");
    composite->add_aspect(get_type<Foo>());
    composite->add_aspect(get_type<Foo>());
    Object* c = universe.create_object(composite, "My object");
    Foo* f = aspect_cast<Foo>(c); // get a pointer to the first Foo in c.
    f->add_number(7);
    f->say_hi();
    
    // Serialize the composite as JSON, and write to stdout.
    JSONArchive archive;
    archive.serialize(c, universe);
    archive.write(std::cout);
}

Output:

Foo
Number: int32
Float list: float32[]

{ "root": {
      "id": "My object",
      "aspects": [
        {
	        "class": "Foo",
	        "Float list": [7],
	        "Number": 123
        },
        {
          "class": "Foo",
          "Float list": null,
          "Number": 123
        }
      ],
      "class": "FooFoo"
    }
}

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.