• Stars
    star
    127
  • Rank 282,790 (Top 6 %)
  • Language
    C++
  • Created over 12 years ago
  • Updated almost 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A simple C++11 reflection/serialization framework.

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"
    }
}

More Repositories

1

w

A simple C++ web framework based on Libevent.
C++
236
star
2

rx-ranges

rx::ranges is a minimalist ranges library for C++17
C++
123
star
3

snow

An stackless implementation of the Snow programming language written in C (Note: This repository was previously named 'snow-c')
C
34
star
4

simongc

A generational garbage collector designed for LLVM.
C
23
star
5

simonask.github.com

22
star
6

snow-deprecated

(ATTENTION WATCHERS: This repository is deprecated. Please watch 'snow' instead.) Snow is a simple, dynamic, and expressive programming language with an emphasis on speed and simplicity. Snow is fully object-oriented, and fully function-oriented at the same time.
C
21
star
7

rust-reflect

Runtime Reflection library for Rust 0.10-pre
Rust
20
star
8

fatso

Fatso Package Manager
C
16
star
9

snow-llvm

An reimplementation of the Snow Programming Language based on LLVM.
C++
12
star
10

glamour

Strongly typed vector math with glam
Rust
9
star
11

js

A V8-based JavaScript interpreter and Standard Library
C++
8
star
12

ftl

It's a Fucking Template Library. A collection of container classes for C++0x, similar to STL, but with more jazz and less fail.
C++
7
star
13

wings

a compact 2d game engine written in C++ with OpenGL
5
star
14

gl2d

Simple 2D game engine, written in C++ and OpenGL with SDL
3
star
15

w2

C++
3
star
16

reflect-steroids

bevy_reflect on steroids
Rust
2
star
17

grace-base

Base library for the Grace engine.
C++
2
star
18

rust-markdown

Markdown rendering for Rust, using Hoedown
Rust
2
star
19

stupid

Stupid is a retarded web framework for Ruby 1.9, based on Rack
2
star
20

grabscreen.rb.app

Grab a screenshot, upload it to imgur.com, and copy the URL to the clipboard in a single click
Ruby
1
star