refactor:Serialisible concept - #2560
Conversation
99234da to
608a471
Compare
trisyoungs
left a comment
There was a problem hiding this comment.
Direction is good. Made a couple of comments.
| value["sites"] = | ||
| vector(sites_, [](const auto &sites) { return vector(sites, [](const auto isoWeight) { return isoWeight; }); }); | ||
| target[tag] = value; | ||
| } | ||
|
|
||
| // Read values from a serialisable value | ||
| void SpeciesSites::deserialise(const SerialisedValue &node) | ||
| { | ||
| using namespace Deserialisable; | ||
| clear(); | ||
|
|
||
| toMap(node, "set", | ||
| [&](const std::string &speciesName, const SerialisedValue &sites) | ||
| { | ||
| auto &set = sites_[speciesName]; | ||
| toMap(sites, [&](const std::string &siteName, const SerialisedValue &population) | ||
| { set[siteName] = population.as_floating(); }); | ||
| }); | ||
| map(node, "set", | ||
| [&](const std::string &speciesName, const SerialisedValue &sites) | ||
| { | ||
| auto &set = sites_[speciesName]; | ||
| map(sites, [&](const std::string &siteName, const SerialisedValue &population) | ||
| { set[siteName] = population.as_floating(); }); | ||
| }); |
There was a problem hiding this comment.
I still have a problem with clarity here. I think this refactoring illustrates it pretty well - I'm supposedly serialising a vector, but deserialising a map. Admittedly this might be caused from the underlying type of sites_ being ResolvableKeyedVector, but you see my point!
d0a488a to
3c68748
Compare
a84b581 to
eee891d
Compare
trisyoungs
left a comment
There was a problem hiding this comment.
Looking really good I think. Couple of moderate comments to consider.
| template <typename T> | ||
| concept SerialisablePointer = requires(T a, std::string tag, SerialisedValue target) { a->serialise(tag, target); }; |
There was a problem hiding this comment.
I actually wonder if this is even required now? In Dissolve1 a module had to write out, e.g., a Species* target which it did by just serialising the name. In Dissolve2, I don't think there are any remaining examples of us needing to serialise a pointer value - all of our data is either a concrete member or passed via an Edge (bypassing the need for direct serialisation in the Dissolve1 style). The only possible example might be in the ForcefieldNode, but this could be factored out (if I haven't done so already).
There was a problem hiding this comment.
It is still required, but usually indirectly, as in that we're usually serialising concrete objects, but some of those objects then contain pointers. As a quick example, the atomTypes_ vector in Species is a vector of shared_ptr.
There was a problem hiding this comment.
Yep, fair comment. Didn't consider the atom type pointers.
| // template <typename T> | ||
| // concept SerialisableFromInto = requires(T a, |
There was a problem hiding this comment.
Yep. That was part of an attempt to apply the serialisable concept to vectors of serialisable, but you can't recurse concepts like that.
| index_ = Deserialisable::de<int>(node.at("index")); | ||
|
|
||
| set(Deserialisable::de<Elements::Element>(node.at("z")), Deserialisable::de<Vector3>(node.at("r")), | ||
| Deserialisable::de_or<double>(node, "q", 0)); |
There was a problem hiding this comment.
OK, so dumb question - I don't see de or de_or obviously declared anywhere? Presumably the de is for deserialise, but I'm not sure I like the brevity in this case as it harms readability.
There was a problem hiding this comment.
de is defined on line 190 of serialiserLibrary.h. I'd originally went with the shorter name hoping that the brevity might improve legibility, becoming essentially invisible against the structure of the data. However, that only worked while we were importing the namespaces. Without that, the benefits of brevity are lost. I'm going to move to deser and deser_or, as a parallel to ser.
Change toVector to simply vector Switch fromVector to just vector
This breaks the linking and the compiler ignores the *inline* directive when deciding whether to inline
eee891d to
ceb43a0
Compare
This PR replaces the old Serialisable class with class with a pair of concepts. This provides the following advantages
fromVectorandtoVectormethods, we now haveSerialisable::vectorandDeserialisable::vectormethodsfoo.serialise, which would fail iffoowas a string or a double. The newserialiseOntomethod works equally well with classes and raw typesThe fundamental theory of this library is that the overloaded
serialiseOntofunction serialises things anddeserialiseOntodeserialises things. Making a type serialisable merely involves adding another overload to these two functions. For simplicity, I've added a templated overload that works on any type that had the oldserialiseanddeserialisemethods that we used before, so most of the code is largely unchanged.I have the following changes I would like to perform before I pull this PR out of draft
As a guide for preliminary reviews, the best files to look at will be:
src/base/serialiser.his the new minimal header and establishes the new namespaces and theserialiseOntoanddeserialiseOntomethod signaturessrc/base/serialiserLibrary.hcontains both the actual serialisation concepts and the old helper functionssrc/classes/speciesSite.cppgives a good example on how the new library works (mostly the same with cleaner names).