Giter Site home page Giter Site logo

hjreflection's Introduction

HJReflection

HJReflection is a simple tool that adds reflection for C++ code. Like Qt MOC and Unreal Header Tool, HJReflection reads C++ header/source files and generates additional reflection meta data for classes/properties that are tagged with certain macros.

Example of constructing a reflectable class HJTest

in header file:

class HJOBJECT HJTest : public HJObject{ //HJOBJECT macro indicates HJTest is a reflectable class
	HJOBJECT_BODY // HJOBJECT_BODY macro add additional members
public:
	HJTest()=default;
	HJPROPERTY int id = 10010; //HJPROPERTY indicates id is a reflectable property
	HJPROPERTY std::string name;
	HJPROPERTY float grade;
	static HJObject* instantiate(void* _parameter = nullptr); //User-must-implement instantiate function
};

in source file:

#include "HJTest.h"
#include "HJTest.generated.h" //a header file automatically generated by HJReflectionTool
HJObject* HJTest::instantiate(void* _parameter){
	return new HJTest();
}

in main.cpp

HJObject* test=HJObject::instantiateObject("HJTest", nullptr);
cout<<"Object test is a "<<test->getClassName()<<endl;
dynamic_cast<HJTest*>(test)->name="huajige";
dynamic_cast<HJTest*>(test)->grade=59.9;
auto ID=test->getValue("id");
auto NAME=test->getValue("name");
cout<<NAME.getValue<string>()<<" has id = "<<ID.getValue<int>()<<endl;
HJVariant GRADE(60.0f);
test->setValue("grade",GRADE);
cout<<"student "<<dynamic_cast<HJTest*>(test)->name<<" has grade  = "<<dynamic_cast<HJTest*>(test)->grade<<endl;

you got output

Object test is a HJTest
huajige has id = 10010
student huajige has grade  = 60

configure CMakeLists.txt to run HJReflectionTool automatically before building the target

#add sub projects
add_subdirectory(HJReflectionTool)
add_subdirectory(HJReflection)

#configure Reflection test project
add_executable(Reflection main.cpp HJTest.cpp ${HJREFLECTION_SRC_FILES})
target_include_directories(Reflection PUBLIC ${HJREFLECTION_INCLUDE_DIR})

set(EXCLUDED_PATH ${HJREFLECTION_EXCLUDED_FILES} ${HJREFLECTIONTOOL_SOURCE_DIR} "and other files your don't wish to parse")
#let libclang to find stl lib and other system include files
set(SYSTEM_INCLUDE_PATH "the directories of standard libraries and other header files in your computer")
set(INCLUDE_DIR ${HJREFLECTION_INCLUDE_DIR} ${SYSTEM_INCLUDE_PATH} "and other header files your wish to parse")
# Method 1 to call HJReflectionTool automatically
add_custom_target(HJReflectionToolTarget ALL
        COMMAND HJReflectionTool "${CMAKE_SOURCE_DIR}" "-E${EXCLUDED_PATH}" "-I${INCLUDE_DIR}"
        DEPENDS HJReflectionTool
        WORKING_DIRECTORY "${HJREFLECTIONTOOL_BINARY_DIR}"
        COMMENT "using add_custom_target to run HJReflectionTool to process path ${CMAKE_SOURCE_DIR}"
        VERBATIM
        )
add_dependencies(Reflection HJReflectionToolTarget)


# Method 2 to call HJReflectionTool automatically
#add_custom_command(TARGET Reflection PRE_BUILD
#        COMMAND HJReflectionTool "${CMAKE_SOURCE_DIR}" "-E${EXCLUDED_PATH}" "-I${INCLUDE_DIR}"
#        VERBATIM
#        WORKING_DIRECTORY "${HJREFLECTIONTOOL_BINARY_DIR}"
#        COMMENT "using add_custom_command to run HJReflectionTool to process path ${CMAKE_SOURCE_DIR}"
#        DEPENDS HJReflectionTool)

hjreflection's People

Contributors

m-idev-0792 avatar

Watchers

 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.