rttrorg / rttr

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

How do I get around shared ptr wrapper - Registration/invoke without original signature #308

Closed robertblaketaylor closed 2 years ago

robertblaketaylor commented 3 years ago

I've got a scenario where I'd like to not include any of the original type definitions and work purely off of what is reflected via RTTR. Ex:

Original signature (simplifying for post, didn't compile this exact example so may have some errors):

struct FooCreateInfo {
  std::string name = {}
};

class Foo {
public:
  virtual ~Foo();

  static bool create(const FooCreateInfo& createInfo, std::unique_ptr<Foo>& outFoo);
};

// registered like
RTTR_REGISTRATION {
  rttr::registration::classs_<FooCreateInfo>("FooCreateInfo")
    .constructor<>()
    .property("name", &FooCreateInfo::name);

  rttr::registration::class_<Foo>("Foo")
    .method("create", &Foo::create);
};

Then later trying to invoke create without original signature:

rttr::type fooCreateInfoType = rttr::type::get_by_name("FooCreateInfo");
rttr::variant fooCreateInfo = fooCreateInfoType.create(); // shared pointer of struct FooCreateInfo

rttr::property nameProp = fooCreateInfoType.get_property("name");
nameProp.set_value(fooCreateInfo, std::string("FooName"));

rttr::type fooType = rttr::type::get_by_name("Foo");
rttr::variant foo;
// this obviously doesn't work, is this type of invoke even possible without extracting the concrete type of fooCreateInfo?
// for the out parameter, unclear if it is even possible to bind that out param to the variant
const rttr::variant returnValue = fooType.invoke("create", {}, {fooCreateInfo, foo});

Appreciate the feedback, thanks!

robertblaketaylor commented 3 years ago

I was able to answer my first question, found the policy::ctor::as_object which resolved the shared pointer vs. object. Am hunting through the code to figure out if I can bind a variant to that unique_ptr& as an out param.