Skip to content
Draft
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
2 changes: 0 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,7 @@ set(BASIC_LINK_LIBS
kernels
neta
analyser
keywords
expression
items
base
math
data
Expand Down
2 changes: 0 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ add_subdirectory(base)
add_subdirectory(classes)
add_subdirectory(data)
add_subdirectory(expression)
add_subdirectory(items)
add_subdirectory(kernels)
add_subdirectory(keywords)
add_subdirectory(main)
add_subdirectory(math)
add_subdirectory(neta)
Expand Down
52 changes: 16 additions & 36 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 All @@ -82,6 +54,14 @@ template <typename... Contexts> class Serialisable

return false;
}
// Place the named value into the supplied object, but only if it exists
template <typename T, typename U> bool getIfPresent(const SerialisedValue &node, std::string name, U &destination)
{
if (!node.contains(name))
return false;
destination = toml::find<T>(node, name);
return true;
}
// A helper function to add elements of a vector to a node under the named heading
template <serialisablePointer T>
static void fromVectorToTable(const std::vector<T> &vector, std::string name, SerialisedValue &node)
Expand Down Expand Up @@ -204,14 +184,14 @@ template <typename... Contexts> class Serialisable
SerialisedValue result;
for (auto &[key, value] : map)
if constexpr (serialisablePointer<V>)
value->serialise(std::string(key), result);
value->serialise(std::format("{}", key), result);
else if constexpr (std::is_base_of_v<Serialisable, V>)
value.serialise(key, result);
value.serialise(std::format("{}", key), result);
else
// We use the direct value (with casting) instead of
// value.serialise() to handle the case where the value
// is a raw type (e.g. int)
result[std::string(key)] = value;
result[std::format("{}", key)] = value;
if (!map.empty())
node[name] = result;
}
Expand Down
1 change: 0 additions & 1 deletion src/classes/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ add_library(
configuration.cpp
configuration_box.cpp
configuration_contents.cpp
configuration_io.cpp
configuration_sites.cpp
configuration_upkeep.cpp
configurationAtom.cpp
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
34 changes: 0 additions & 34 deletions src/classes/braggReflection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

#include "classes/braggReflection.h"
#include "base/lineParser.h"
#include "items/deserialisers.h"
#include "items/serialisers.h"

BraggReflectionVector::BraggReflectionVector(const BraggReflectionVector &other) : reflections_(other.reflections_) {}

Expand Down Expand Up @@ -139,24 +137,6 @@ const Vector3i &BraggReflection::hkl() const { return hkl_; }
* Serialisation
*/

// Read data through specified parser
bool BraggReflection::deserialise(LineParser &parser)
{
// Read index, Q centre, and number of contributing K-vectors
if (parser.getArgsDelim(LineParser::Defaults) != LineParser::Success)
return false;
index_ = parser.argi(0);
q_ = parser.argd(1);
nKVectors_ = parser.argi(2);
hkl_ = parser.arg3i(3);

// Read intensities array
if (!GenericItemDeserialiser::deserialise<Array2D<double>>(intensities_, parser))
return false;

return true;
}

// Read values from a serialisable value
void BraggReflection::deserialise(const SerialisedValue &node)
{
Expand All @@ -177,17 +157,3 @@ void BraggReflection::serialise(std::string tag, SerialisedValue &target) const
braggReflection["nKVectors"] = nKVectors_;
hkl_.serialise(tag, target);
}

// Write data through specified parser
bool BraggReflection::serialise(LineParser &parser) const
{
// Write index, Q centre, and number of contributing K-vectors
if (!parser.writeLineF("{} {} {} {} {} {}\n", index_, q_, nKVectors_, hkl_.x, hkl_.y, hkl_.z))
return false;

// Write intensities array
if (!GenericItemSerialiser::serialise<Array2D<double>>(intensities_, parser))
return false;

return true;
}
12 changes: 4 additions & 8 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 @@ -67,18 +67,14 @@ class BraggReflection : public Serialisable<>
* Serialisation
*/
public:
// Read data through specified parser
bool deserialise(LineParser &parser);
// Read values from a serialisable value
void deserialise(const SerialisedValue &node) override;
// 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 +101,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 @@ -22,7 +22,7 @@ class ProcessPool;
class Species;

// Configuration
class Configuration : public Serialisable<const CoreData &>
class Configuration : public Serialisable
{
public:
Configuration();
Expand Down Expand Up @@ -230,5 +230,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;
};
Loading
Loading