rttrorg / rttr

C++ Reflection Library
https://www.rttr.org
MIT License
3.17k stars 439 forks source link

How to register external function as method for a type. #251

Open scarlink opened 5 years ago

scarlink commented 5 years ago

I'm trying to attach external functions as methods to a type in order to extend what can I do on a type without touching the class definition, e.g:

struct myType { int data; RTTR_ENABLE() }

void dump_myType(myType* src) { ... }

RTTR_REGISTRATION
{
    rttr::registration::class_<myType>("myType")
         .constructor<>()
         .property("data", &myType::data)
         .method("dump", &dump_myType);
}

And then i call it with:

myType t;
method tdump = type::get(t).get_method("dump");
tdump.invoke(t,&t);

And it actually works, i also did this for integers, std::string and so on

RTTR_REGISTRATION
{
    rttr::registration::class_<int>("int")
         .method("dump", &dump_int);
}

Is this the correct way to achieve this behaviour?