enums as object fields
psydevascender opened this issue · 5 comments
how to make enum class (or just old school enums) serializable?
enum class Gender {
male, female
};
class Person {
Gender gender_;
friend class hiberlite::access;
template <class Archive>
void hibernate(Archive & ar) {
ar & HIBERLITE_NVP(gender_);
}
gives compilation error
Visitor.h:12:4: Member reference base type 'ARGender' is not a structure or union
static void hibernate(A& a, C& c) { c.hibernate(a); }
I assume that it lacks template specialization similar to POD types like int, etc
one can think of an extensible conversion. Inspired by hiberlite some time ago I've made something similar for json:
namespace picojson {
namespace convert {
template<> struct value_converter<Example::Status> {
static value to_value(Example::Status v) {
return value(static_cast<double>(v));
}
static void from_value(value const& ov, Example::Status& v) {
if ( ov.is<double>() )
v = Example::Status(static_cast<int>(ov.get<double>()));
}
};
}
}
haven't looked at hiberlite for a long time, but it should be doable. Will look at it in spare time
include/some_types.h might give a hint on how one would extend the user code to support custom type conversion
Privet Dmitriy
Yes, I really do like your picojson's way to make value_converter template specialization.
But I really don't understand now how to apply this to the hiberlite?
It uses its own macros to make name-value pairs without converter class and without to_value()/ from_value() methods.
So where should I plug the code?
To make it extendible and not to modify the library's headers directly? Ideally just to plug in some additional template specializations in my own .h file
Privet Sergey :)
As with picojson, before registering your class with hiberlite, your template specialization of template<class A> void ::hiberlite::hibernate
as it's done in the HIBERLITE_DEF_DB_ATOM
macro should be visible. I think, if this works, it'll just go into the README. There probably should be no need to extend hiberlite source
cool, thanks for the example :) I tried something similar but completely forgot about the namespace scope. And now it works perfectly!
p.s IMHO definitely worth adding it to the README