Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to resolve the non-const signature of a class method. #81

Open
rpatters1 opened this issue Mar 25, 2023 · 2 comments
Open

How to resolve the non-const signature of a class method. #81

rpatters1 opened this issue Mar 25, 2023 · 2 comments

Comments

@rpatters1
Copy link
Contributor

rpatters1 commented Mar 25, 2023

Suppose I have this class:

class Foo
{
   const char* getPtr();
   char* getPtr();
}

REFL_AUTO(type(Foo), func(getPtr))

I would like write my code always to resolve the non-const version of the function, if there is an overload.

EDIT:

I was able to get resolve to work by looking at some of the tests for an example. (I think the documentation could be clearer.) My challenge is that it required hard-coding the function call signature with which to resolve it. I am processing many classes each with many functions in a centralized function. I would like somehow to pass in the calling sequences for the members that need resolution. It almost certainly would have to be in the REFL_AUTO macro. The question is how. At the moment I am stumped.

What would be ideas is if there were exactly 2 versions of the method, one const and one non-const, refl-cpp offered a way to resolve to the non-const without my code having to know the function signature.

@rpatters1
Copy link
Contributor Author

Would it be possible to add a version of REFL_FUNC that takes a function pointer? That seems like the most efficient way to handle overloads. Usage would look something like:

REFL_FUNC_PTR(MyMethod, static_cast<const char*(*)(int, bool)>(&MyClass::MyMethod))

@rpatters1
Copy link
Contributor Author

rpatters1 commented Apr 6, 2023

EDIT: Success! It works with refl::descriptor::get_attribute<func_ptr>. The code as modified below works.

template <typename Func>
struct func_ptr : refl::attr::usage::member
{
   Func func;
   constexpr func_ptr(Func func) : func(func) {}
  
};

REFL_AUTO(type(Foo), func(getPtr, func_ptr(static_cast<char*(foo::*)()>(&getPtr))))

// and in the central handler routine:

   constexpr auto type = refl::reflect<tinyxml2::XMLNode>();
   constexpr auto members = get_members(type);
   for_each_unordered(members, [&](auto member)
   {
      if constexpr(refl::descriptor::has_attribute<func_ptr>(member))
      {
         const auto fptr = refl::descriptor::get_attribute<func_ptr>(member).func;
      }
      else
      {
         const auto fptr = refl::descriptor::get_pointer(member);
      }
   });

It also works if you want to use a proxy function instead of the class member. Unfortunately the REFL_FUNC macro does not appear to be compatible with lambdas or std::function, but that's a small quibble.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant