Giter Club home page Giter Club logo

ufosky / cpp-reflection Goto Github PK

View Code? Open in Web Editor NEW

This project forked from radif/cpp-reflection

0.0 2.0 0.0 64 KB

Reflection is not supported by current c++ standard, This examples demonstrates how to adopt reflection in your c++ code, particularly, string fromClass and classFromString. It relies on templates and a couple of short macros. The Factory functions are indexed in a map against the class string. The map is static, therefore is instantiated before int main() is given a chance to run. Making the reflection available right at the start without any setup code being required to run.

cpp-reflection's Introduction

CPP-Reflection

Reflection is not supported by current c++ standard, This examples demonstrates how to adopt reflection in your c++ code, particularly, string fromClass and classFromString. It relies on templates and a couple of short macros. The Factory functions are indexed in a map against the class string. The map is static, therefore is instantiated before int main() is given a chance to run. Making the reflection available right at the start without any setup code being required to run.

Here is an example on how to use the reflection:

//templated instantiation, no casting required
{
    std::cout <<std::endl<<"templated instantiation, no casting required"<<std::endl<<std::endl;
    auto base(mcb::PlatformSupport::Base::create<mcb::PlatformSupport::Base *>("Base"));
    auto derived1(mcb::PlatformSupport::Base::create<mcb::PlatformSupport::Derived1 *>("Derived1"));
    auto derived2(mcb::PlatformSupport::Base::create<mcb::PlatformSupport::Derived2 *>("Derived2"));


    std::cout << base->stringFromClass() << std::endl;
    std::cout << derived1->stringFromClass() << std::endl;
    std::cout << derived2->stringFromClass() << std::endl;
}


//instantiation of the pointers casted to the base class, heavily relying on virtual functions
{
    std::cout <<std::endl<<"instantiation of the pointers casted to the base class, heavily relying on virtual functions"<<std::endl<<std::endl;

    auto base(mcb::PlatformSupport::Base::create("Base"));
    auto derived1(mcb::PlatformSupport::Base::create("Derived1"));
    auto derived2(mcb::PlatformSupport::Base::create("Derived2"));


    std::cout << base->stringFromClass() << std::endl;
    std::cout << derived1->stringFromClass() << std::endl;
    std::cout << derived2->stringFromClass() << std::endl;
}

To add your class into the reflection map, you must inherit from Base, however with little code modification this requirement can go away. I personally prefer having a base class to rely on certain behavior even if I don't have the class information/declaration included.

in .h file:

#include "Base.h"
namespace mcb{namespace PlatformSupport{
    class Derived1 : public Base{
    public:
        REGISTER_CLASS_FOR_REFLECTION_H(Derived1);
    };
}}

in .cpp file:

#include "Derived1.h"
namespace mcb{namespace PlatformSupport{
    REGISTER_CLASS_FOR_REFLECTION_CPP(Derived1);
}}

cpp-reflection's People

Contributors

radif avatar

Watchers

Chica avatar James Cloos 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.