Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 5 additions & 33 deletions src/base/serialiser.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@

#pragma once

#include <toml11/toml.hpp>

#include "templates/keyedVector.h"
#include "templates/orderedMap.h"
#include "templates/resolvableKeyedVector.h"
#include <map>
#include <toml11/toml.hpp>
#include <vector>

// The type we use for the nodes of our serialisation tree
Expand All @@ -19,43 +18,16 @@ using SerialisedValue = toml::basic_value<toml::discard_comments, dissolve::Orde
template <typename T>
concept serialisablePointer = requires(T a, std::string tag, SerialisedValue target) { a->serialise(tag, target); };

// The associated context for type T This type does double duty.
// First, since the struct has not actual members, it is a Unit type
// (a type with only one possible value). This is why the default
// inner type is SerialisableContext<void> - says that we have no
// context and the type has no size. The second duty is acting as a
// type level function. Normally, you would just add an inner type to
// a class (e.g. NodeValue::Context) to perform this function.
// Unfortunately, you can't add an inner type to a primative type
// (e.g. float). By specialising this template, we can create a
// mapping between types and the context that they require.
template <typename T> struct SerialisableContext
{
using type = SerialisableContext<void>;
};

// An interface for classes that can be serialised into an input file
template <typename... Contexts> class Serialisable
class Serialisable
{
public:
Serialisable() = default;
virtual ~Serialisable() = default;
// Express as a serialisable value
virtual void serialise(std::string tag, SerialisedValue &target) const = 0;
// Read values from a serialisable value
virtual void deserialise(const SerialisedValue &node, Contexts... context) { return; }

// When a type has only one context and that context is empty
// (i.e. is a Unit type), then we can create a simple overload
// that skips the need to add the second parameter. This does not
// conflict with the definition above because it only becomes
// instantiated when the Contexts pack is not empty, so the above
// definition of deserialise takes two parameters. We also insist
// that it is only called for empty types to ensure that we do not
// accidentally create a value with its default constructor.
template <typename = std::enable_if<sizeof...(Contexts) == 1 && (std::is_empty<Contexts>::value && ...)>>
void deserialise(const SerialisedValue &node)
{
deserialise(node, {});
}
virtual void deserialise(const SerialisedValue &node) {}

/* Functions that hook into the toml11 library */
// Wrapper for deserialise that toml11 will check for
Expand Down
12 changes: 6 additions & 6 deletions src/classes/atom.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ class AtomBase
};

// Atom
template <typename BondClass> class Atom : public AtomBase, public Serialisable<>
template <typename BondClass> class Atom : public AtomBase, public Serialisable
{
public:
Atom() = default;
virtual ~Atom() = default;
virtual ~Atom() override = default;

/*
* Coordinate Manipulation Operators
Expand Down Expand Up @@ -125,9 +125,9 @@ template <typename BondClass> class Atom : public AtomBase, public Serialisable<
return nullptr;
}
// Return number of bonds
int nBonds() const { return bonds_.size(); }
int nBonds() const override { return bonds_.size(); }
// Return indices of other AtomBases to which this one is connected
std::vector<AtomBase *> connectedAtoms() const
std::vector<AtomBase *> connectedAtoms() const override
{
std::vector<AtomBase *> connections;
for (const auto *bond : bonds_)
Expand All @@ -140,12 +140,12 @@ template <typename BondClass> class Atom : public AtomBase, public Serialisable<
*/
public:
// Express as a serialisable value
void serialise(std::string tag, SerialisedValue &target) const
void serialise(std::string tag, SerialisedValue &target) const override
{
target[tag] = {{"index", index_}, {"z", Z_}, {"r", r_}, {"q", q_}};
}
// Read values from a serialisable value
void deserialise(const SerialisedValue &node)
void deserialise(const SerialisedValue &node) override
{
index_ = toml::find<int>(node, "index");

Expand Down
2 changes: 1 addition & 1 deletion src/classes/atomType.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <vector>

// AtomType Definition
class AtomType : public Serialisable<>, public std::enable_shared_from_this<AtomType>
class AtomType : public Serialisable, public std::enable_shared_from_this<AtomType>
{
public:
AtomType(Elements::Element Z = Elements::Unknown);
Expand Down
2 changes: 1 addition & 1 deletion src/classes/bond.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class AtomBase;

// Bond
template <class AtomClass> class Bond : public Serialisable<>
template <class AtomClass> class Bond : public Serialisable
{
public:
Bond(AtomClass *i = nullptr, AtomClass *j = nullptr) : i_(i), j_(j) {}
Expand Down
2 changes: 1 addition & 1 deletion src/classes/box.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Cell;
class Data1D;

// Basic Box Definition
class Box : public Serialisable<>
class Box : public Serialisable
{
public:
// Box Type Enum
Expand Down
8 changes: 4 additions & 4 deletions src/classes/braggReflection.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "templates/array2D.h"

// BraggReflection Class
class BraggReflection : public Serialisable<>
class BraggReflection : public Serialisable
{
public:
BraggReflection();
Expand Down Expand Up @@ -74,11 +74,11 @@ class BraggReflection : public Serialisable<>
// Write data through specified parser
bool serialise(LineParser &parser) const;
// Express as a serialisable value
void serialise(std::string tag, SerialisedValue &target) const;
void serialise(std::string tag, SerialisedValue &target) const override;
};

// BraggReflectionVector class
class BraggReflectionVector : public Serialisable<>
class BraggReflectionVector : public Serialisable
{
public:
BraggReflectionVector() = default;
Expand All @@ -105,7 +105,7 @@ class BraggReflectionVector : public Serialisable<>
*/
public:
// Express as a serialisable value
void serialise(std::string tag, SerialisedValue &target) const;
void serialise(std::string tag, SerialisedValue &target) const override;
// Read values from a serialisable value
void deserialise(const SerialisedValue &node) override;
};
2 changes: 1 addition & 1 deletion src/classes/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void Configuration::serialise(std::string tag, SerialisedValue &target) const
}

// Read values from a serialisable value
void Configuration::deserialise(const SerialisedValue &node, const CoreData &data)
void Configuration::deserialise(const SerialisedValue &node)
{
setTemperature(toml::find_or<double>(node, "temperature", defaultTemperature_));
requestedSizeFactor_ = toml::find_or<double>(node, "sizeFactor", defaultSizeFactor_);
Expand Down
4 changes: 2 additions & 2 deletions src/classes/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ProcessPool;
class Species;

// Configuration
class Configuration : public Serialisable<const CoreData &>
class Configuration : public Serialisable
{
public:
Configuration();
Expand Down Expand Up @@ -252,5 +252,5 @@ class Configuration : public Serialisable<const CoreData &>
// Express as a serialisable value
void serialise(std::string tag, SerialisedValue &target) const override;
// Read values from a serialisable value
void deserialise(const SerialisedValue &node, const CoreData &data) override;
void deserialise(const SerialisedValue &node) override;
};
2 changes: 1 addition & 1 deletion src/classes/isotopologue.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class CoreData;
/*
* Isotopologue Definition
*/
class Isotopologue : public Serialisable<>
class Isotopologue : public Serialisable
{
public:
Isotopologue(const Species *parent, std::string name = "");
Expand Down
8 changes: 1 addition & 7 deletions src/classes/isotopologueSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ void IsotopologueSet::serialise(std::string tag, SerialisedValue &target) const
}

// Read values from a serialisable value
void IsotopologueSet::deserialise(const SerialisedValue &node, const CoreData &coreData)
void IsotopologueSet::deserialise(const SerialisedValue &node)
{
clear();

Expand All @@ -113,12 +113,6 @@ void IsotopologueSet::deserialise(const SerialisedValue &node, const CoreData &c
toMap(topes, [&](const std::string &isoName, const SerialisedValue &population)
{ set[isoName] = population.as_floating(); });
});

// Need to resolve species
std::map<std::string, const Species *> speciesMap;
for (const auto &sp : coreData.species())
speciesMap[std::string(sp.get()->name())] = sp.get();
resolve(speciesMap);
}

// Resolve internal resolvable name references with supplied data
Expand Down
4 changes: 2 additions & 2 deletions src/classes/isotopologueSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Isotopologue;
class LineParser;

// IsotopologueSet - Isotopologues for one or more Species
class IsotopologueSet : public Serialisable<const CoreData &>, ResolvableContext
class IsotopologueSet : public Serialisable, ResolvableContext
{
public:
IsotopologueSet() = default;
Expand Down Expand Up @@ -54,7 +54,7 @@ class IsotopologueSet : public Serialisable<const CoreData &>, ResolvableContext
// Express as a serialisable value
void serialise(std::string tag, SerialisedValue &target) const override;
// Read values from a serialisable value
void deserialise(const SerialisedValue &node, const CoreData &coreData) override;
void deserialise(const SerialisedValue &node) override;
// Resolve internal resolvable name references with supplied data
void resolve(const std::map<std::string, const Species *> &speciesInScope) override;
};
4 changes: 2 additions & 2 deletions src/classes/pairPotential.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
class AtomType;

// PairPotential Definition
class PairPotential : Serialisable<>
class PairPotential : public Serialisable
{
public:
PairPotential(std::string_view nameI = {}, std::string_view nameJ = {});
Expand Down Expand Up @@ -222,5 +222,5 @@ class PairPotential : Serialisable<>
// Express as a serialisable value
void serialise(std::string tag, SerialisedValue &target) const override;
// Read values from a serialisable value
void deserialise(const SerialisedValue &node);
void deserialise(const SerialisedValue &node) override;
};
4 changes: 2 additions & 2 deletions src/classes/pairPotentialOverride.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <string>

// PairPotential Override Definition
class PairPotentialOverride : public Serialisable<>
class PairPotentialOverride : public Serialisable
{
public:
// Override Types
Expand Down Expand Up @@ -58,5 +58,5 @@ class PairPotentialOverride : public Serialisable<>
// Express as a serialisable value
void serialise(std::string tag, SerialisedValue &target) const override;
// Read values from a serialisable value
void deserialise(const SerialisedValue &node);
void deserialise(const SerialisedValue &node) override;
};
2 changes: 1 addition & 1 deletion src/classes/partialSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "templates/resolvable.h"

// Set of Partials
class PartialSet : public Serialisable<>, ResolvableContext
class PartialSet : public Serialisable, ResolvableContext
{
public:
PartialSet() = default;
Expand Down
2 changes: 1 addition & 1 deletion src/classes/potentialSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class AtomType;

// Set of Potentials
class PotentialSet : public Serialisable<>
class PotentialSet : public Serialisable
{
public:
PotentialSet();
Expand Down
4 changes: 2 additions & 2 deletions src/classes/species.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class CommonImproper;
class Structure;

// Species Definition
class Species : public Serialisable<>
class Species : public Serialisable
{
public:
Species(std::string name = "Unnamed");
Expand Down Expand Up @@ -341,5 +341,5 @@ class Species : public Serialisable<>
// Express as a serialisable value
void serialise(std::string tag, SerialisedValue &target) const override;
// Read values from a serialisable value
void deserialise(const SerialisedValue &node);
void deserialise(const SerialisedValue &node) override;
};
2 changes: 1 addition & 1 deletion src/classes/speciesIntra.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class SpeciesAtom;
class Species;

// Base class for intramolecular interactions within Species
template <class Intra, class Functions> class SpeciesIntra : public Serialisable<>
template <class Intra, class Functions> class SpeciesIntra : public Serialisable
{
public:
explicit SpeciesIntra(Species *parent, typename Functions::Form form) : parent_(parent), interactionPotential_(form) {};
Expand Down
2 changes: 1 addition & 1 deletion src/classes/speciesSite.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Species;
class SpeciesAtom;

// Species Site Definition
class SpeciesSite : public Serialisable<>
class SpeciesSite : public Serialisable
{
public:
// Site Type
Expand Down
2 changes: 1 addition & 1 deletion src/classes/speciesSites.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class SpeciesSite;
class Species;

// SpeciesSites - Sites from one or more Species
class SpeciesSites : public Serialisable<>, ResolvableContext
class SpeciesSites : public Serialisable, ResolvableContext
{
public:
SpeciesSites() = default;
Expand Down
4 changes: 2 additions & 2 deletions src/classes/structure.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class StructureAtom : public Atom<Bond<StructureAtom>>
};

// Structure
class Structure : public Serialisable<>
class Structure : public Serialisable
{
public:
Structure();
Expand Down Expand Up @@ -123,5 +123,5 @@ class Structure : public Serialisable<>
// Express as a serialisable value
void serialise(std::string tag, SerialisedValue &target) const override;
// Read values from a serialisable value
void deserialise(const SerialisedValue &node);
void deserialise(const SerialisedValue &node) override;
};
2 changes: 1 addition & 1 deletion src/expression/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <string>

// Expression Value
class ExpressionValue : public Serialisable<>
class ExpressionValue : public Serialisable
{
public:
ExpressionValue();
Expand Down
4 changes: 2 additions & 2 deletions src/expression/variable.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "expression/value.h"

// Variable
class ExpressionVariable : public Serialisable<>
class ExpressionVariable : public Serialisable
{
public:
ExpressionVariable(const ExpressionValue &value = ExpressionValue());
Expand Down Expand Up @@ -48,5 +48,5 @@ class ExpressionVariable : public Serialisable<>
// Express as a serialisable value
void serialise(std::string tag, SerialisedValue &target) const override;
// Read values from a serialisable value
void deserialise(const SerialisedValue &node);
void deserialise(const SerialisedValue &node) override;
};
4 changes: 2 additions & 2 deletions src/keywords/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Species;
class SpeciesSite;

// Keyword Base Class
class KeywordBase : public Serialisable<CoreData const &>
class KeywordBase : public Serialisable
{
public:
KeywordBase(const std::type_index typeIndex);
Expand Down Expand Up @@ -72,7 +72,7 @@ class KeywordBase : public Serialisable<CoreData const &>
// Express as a serialisable value
virtual void serialise(std::string tag, SerialisedValue &target) const override = 0;
// Read values from a serialisable value
virtual void deserialise(const SerialisedValue &node, const CoreData &coreData) override {};
virtual void deserialise(const SerialisedValue &node) override {};

/*
* Keyword Types
Expand Down
2 changes: 1 addition & 1 deletion src/keywords/bool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ bool BoolKeyword::serialise(LineParser &parser, std::string_view keywordName, st
void BoolKeyword::serialise(std::string tag, SerialisedValue &target) const { target[tag] = data_; }

// Read values from a serialisable value
void BoolKeyword::deserialise(const SerialisedValue &node, const CoreData &coreData) { data_ = node.as_boolean(); }
void BoolKeyword::deserialise(const SerialisedValue &node) { data_ = node.as_boolean(); }
Loading
Loading