Skip to content

Accessing addresses to members #184

Open

Description

@very-cool-name

Hey, I have some pybinded structs, that have a lot of repeating boilerplate:

struct MyStruct {
  int x;
  char c;
};

py::class_< MyStruct >(module, "MyStruct")
  .def_readonly("x", &MyStruct::x)
  .def_readonly("c", &MyStruct::c);

I was thinking about using boost::pfr to automate this, but I don't think there is currently any way, since boost::pfr doesn't have an interface to access member addresses i.e. something like this:

auto cls = py::class_< MyStruct >(module, "MyStruct");
boost::pfr::for_each_member_address_with_name<MyStruct>([cls](std::string_view name, auto address) {
  cls.def_readonly(name, address);
});

Is there any interest in having such interface? And if so - I think I can make a PR. If you could give me some hints on possible implementation - that would be greatly appreciated.

Thanks!

Activity

  1. added
    help wanted
    on Oct 17, 2024
  2. apolukhin commented on Oct 17, 2024

    @apolukhin
    Member

    Looks quite interesting. Please provide a PR.

  3. very-cool-name commented on Oct 29, 2024

    @very-cool-name
    Author

    Gave it some thought and I don't really know how to implement it. Structured bindings won't help here, since essentially:

    struct S { char c; };
    
    S s;
    auto& [c] = s;
    // there is no way to get from ref c to char S::*
    

    If you are aware of maybe compiler specific tricks that can let me do that - please help me.

  4. hansalemaos commented on Nov 7, 2024

    @hansalemaos

    Maybe something like this? Probably not the safest version, but it works :)

    template <typename Ts, typename Us>
    void change_data_without_a_check(Ts& mystruct, const std::string_view column_name, const Us new_data) {
    	size_t column_id = 0;
    	constexpr auto field_names = pfr::names_as_array<Ts>();
    	for (size_t i = 0; i < field_names.size(); i++) {
    		if (field_names[i].compare(column_name) == 0) {
    			column_id = i;
    		}
    	}
    	size_t i = 0;
    
    	pfr::for_each_field(mystruct, [&](auto& f, auto meta) {
    		if (i == column_id) {
    			void* voidptr = reinterpret_cast<void*>(&(pfr::get<meta>(mystruct))); // this part gives me the mem address of each field (msvc - c++20)
    			(*(Us*)(voidptr)) = new_data; // ... and some "don't try this at home" assignments
    		}
    		i++;
    
    		});
    
    }
    struct S { char c; uint8_t u; };
    int main(int argc, const char* argv[]) {
    
    	auto s2x = S('A');
    	uint8_t newvaluex = 100;
    	std::string mycolumnx = "c";
    	change_data_without_a_check(s2x, mycolumnx, newvaluex);
    	char newvaluex2 = 'A';
    	std::string mycolumnx2 = "u";
    	change_data_without_a_check(s2x, mycolumnx2, newvaluex2);
    	std::cout << s2x.c << " " << s2x.u << std::endl; } ```
  5. very-cool-name commented on Nov 7, 2024

    @very-cool-name
    Author

    As far as I understand, in the code above there is no point at which we have pointer of type Us Ts::*.
    This reinterpret_cast<void*>(&(pfr::get<meta>(mystruct))); is indeed the memory address, but I don't know of any way to make a hop from memory address to a pointer-to-member.

  6. hansalemaos commented on Nov 7, 2024

    @hansalemaos

    Got it, I thought your main concern was getting the memory address of each member.

  7. mnemolyst commented on Jul 3, 2026

    @mnemolyst

    I just opened #237 , which can be used like this:

    struct MyStruct {
        int x;
        char c;
        std::string name;
    };
    
    auto cls = py::class_<MyStruct>(m, "MyStruct");
    
    boost::pfr::for_each_name<T>([&cls](std::string_view name, auto index) {
        cls.def_property_readonly(
            std::string{name}.c_str(),
            [](const T& self) -> const auto& {
                return boost::pfr::get<decltype(index)::value>(self);
            },
            py::return_value_policy::reference_internal);
    });
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions