Giter Site home page Giter Site logo

reflectpp's Introduction

Reflectpp

An easy to use reflection library for C++

Features

  • User friendly, just a couple of lines on the declaration of your clases
  • Portable, works on any c++ compiler
  • Declaration+Reflection on the same line
  • public, private and protected fields
  • Inheritance
  • Composition
  • Supported types:
    • Basic types: bool, char, short, int, long, float, double
    • std::string
    • std::vector
    • arrays, multidimensional arrays
    • enums
    • properties (get/set)
    • pointers
  • json import/export

Installation

Copy the folder Reflectpp/ into your project and add it to your include path

Usage

Instead of declaring your classes like

enum Level
{
	LOW = 1,
	MEDIUM = 5,
	HIGH = 10,
};

class N {
public:
	int n_int;
	float n_float;
	std::string n_string;
	Vector3 n_vector3;
	std::vector< int > n_float_vector_array[2];
	Level level;

	N() : n_int(0), n_float(1.0f), n_string("str"), level(LOW) {
		n_float_vector_array[0].push_back(1);
		n_float_vector_array[0].push_back(2);

		n_float_vector_array[1].push_back(10);
		n_float_vector_array[1].push_back(20);
		n_float_vector_array[1].push_back(30);
	}
};

Use the corresponding Reflectpp macros

#define ENUM_NAME Level
#define ENUM_ENTRIES \
	ENUM_ENTRY(LOW, 1) \
	ENUM_ENTRY(MEDIUM, 5) \
	ENUM_ENTRY(HIGH, 10)
#include "ReflectEnumDecl.h"

REFLECTABLE_CLASS(N)
	#define REFLECTION_DATA   \
		SERIALIZED_FIELD(public, int, n_int)\
		SERIALIZED_FIELD(public, float, n_float)\
		SERIALIZED_FIELD(public, std::string, n_string)\
		SERIALIZED_FIELD(public, Vector3, n_vector3)\
		SERIALIZED_FIELD(public, std::vector< int >, n_float_vector_array[2])\
		SERIALIZED_FIELD(public, Level, level)
	#include "ReflectDecl.h"

	N() : n_int(0), n_float(1.0f), n_string("str"), level(LOW) {
		n_float_vector_array[0].push_back(1);
		n_float_vector_array[0].push_back(2);

		n_float_vector_array[1].push_back(10);
		n_float_vector_array[1].push_back(20);
		n_float_vector_array[1].push_back(30);
	}
};

then you will be able to

N n;

//Iterate N contents
ReflectInfoIterator it(&n);
ReflectField info;
while((info = it.Next()).reflectable)
{
	printf("%s: %s\n", info.infos->id, info.ToString().c_str());
}


//Access fields using reflection
n.Get("n_int").Set< int >(4); //n.n_int == 4
n.Get("n_float_vector_array[0][0]").Set< int >(999); //n_float_vector_array[0][0] == 99
n.Get("n_float_vector_array[0]").GetVectorHandler()->Push(); //added new element into n_float_vector_array[0] 
PrintReflectable(&n);

//Serialize into json
Serialize(&n, "n.json");

//Deserialize from json
Deserialize(&n, "n_mod.json");
PrintReflectable(&n);

Please refer to the wiki for detailed instructions

reflectpp's People

Contributors

zal0 avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

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.