How to use Glaze with classes and private/protected members? #294
Replies: 2 comments
-
If you have a getter that returns a non-const reference you can use a lambda to register the value: class my_class
{
public:
auto& get_x() {
return x;
}
private:
double x{};
};
template <>
struct glz::meta<my_class>
{
using T = my_class;
static constexpr auto value = object("x", [](my_class& c) -> auto& { return c.get_x(); });
}; If you need to call separate getter and setter functions then you can use custom serialization for your class: docs/custom-serialization.md This requires a significant effort from the user, and we will probably make it simpler in the future. However, Glaze is aimed at efficiency and therefore it is not generally a good idea to use setters, because this forces copies to happen. Additionally, Glaze is concerned with memory mapped interfaces, which becomes invalid by hiding mutable memory access. So, adding setters reduces the feature set of Glaze (e.g. JSON pointer syntax calls). |
Beta Was this translation helpful? Give feedback.
-
Thanks, it works. |
Beta Was this translation helpful? Give feedback.
-
How to use Glaze with classes and private/protected members?
Beta Was this translation helpful? Give feedback.
All reactions