21 template <
bool Precise,
typename T,
typename TEnabler =
void>
24 template <
bool Precise,
typename T>
25 struct SerializedType<Precise, T,
std::enable_if_t<std::is_integral<T>::value || std::is_enum<T>::value>> {
27 using Type = std::conditional_t<Precise, int64_t, int32_t>;
29 template <
bool Precise,
typename T>
32 using Type = std::conditional_t<Precise, double, float>;
34 template <
bool Precise, Size N>
37 using Type = std::add_lvalue_reference_t<const char[N]>;
40 template <
bool Precise,
typename T>
46 template <
bool Precise>
54 : ofs(path.native(),
std::ios::binary) {}
56 template <
typename... TArgs>
57 void write(
const TArgs&... args) {
59 this->serializeImpl(buffer, args...);
60 ofs.write(&buffer[0], buffer.
size());
66 ofs.write(&buffer[0], size);
74 template <
typename T0,
typename... TArgs>
75 void serializeImpl(
Array<char>& bytes,
const T0& t0,
const TArgs&... args) {
79 bytes.
resize(size +
sizeof(ActType));
80 ActType serializable =
static_cast<ActType
>(t0);
81 const char* c =
reinterpret_cast<const char*
>(&serializable);
82 for (
Size i = 0; i <
sizeof(ActType); ++i) {
83 bytes[size + i] = c[i];
85 this->serializeImpl(bytes, args...);
88 template <
typename... TArgs>
89 void serializeImpl(
Array<char>& bytes,
const std::string& s,
const TArgs&... args) {
91 bytes.
resize(size + s.size() + 1);
92 const char* c = s.c_str();
93 for (
Size i = 0; i < s.size() + 1; ++i) {
94 bytes[size + i] = c[i];
97 this->serializeImpl(bytes, args...);
114 this->error +=
" at position " + std::to_string(
position);
117 virtual const char*
what() const noexcept
override {
118 return error.c_str();
123 template <
bool Precise>
132 : ifs(path.native(),
std::ios::binary) {
134 this->fail(
"Cannot open file " + path.
native() +
" for reading.");
143 template <
typename... TArgs>
145 return this->readImpl(args...);
150 if (!ifs.seekg(size, ifs.cur)) {
151 this->fail(
"Failed to skip " + std::to_string(size) +
" bytes in the stream");
156 template <
typename T0,
typename... TArgs>
157 void readImpl(T0& t0, TArgs&... args) {
158 static_assert(!std::is_array<T0>::value,
"String must be read as std::string");
160 static_assert(
sizeof(ActType) > 0,
"Invalid size of ActType");
161 buffer.
resize(
sizeof(ActType));
162 if (!ifs.read(&buffer[0],
sizeof(ActType))) {
164 this->fail(
"Failed to read a primitive of size " + std::to_string(
sizeof(ActType)));
166 t0 = T0(
reinterpret_cast<ActType&
>(buffer[0]));
167 this->readImpl(args...);
170 template <std::size_t
N,
typename... TArgs>
171 void readImpl(
char (&ar)[
N], TArgs&... args) {
172 if (!ifs.read(ar,
N)) {
173 this->fail(
"Failed to read an array of size " + std::to_string(
N));
175 this->readImpl(args...);
178 template <
typename... TArgs>
179 void readImpl(std::string& s, TArgs&... args) {
182 while (ifs.read(&c, 1) && c !=
'\0') {
188 this->fail(
"Error while deseralizing string from stream, got: " + s);
190 this->readImpl(args...);
195 void fail(
const std::string& error) {
Generic dynamically allocated resizable storage.
#define SPH_ASSERT(x,...)
uint32_t Size
Integral type used to index arrays (by default).
#define NAMESPACE_SPH_END
Wrapper of type value of which may or may not be present.
Object representing a path on a filesystem, similar to std::filesystem::path in c++17.
void resize(const TCounter newSize)
Resizes the array to new size.
INLINE void push(U &&u)
Adds new element to the end of the array, resizing the array if necessary.
void fill(const T &t)
Sets all elements of the array to given value.
void clear()
Removes all elements from the array, but does NOT release the memory.
INLINE TCounter size() const noexcept
Object for reading serialized primitives from input stream.
void skip(const Size size)
Skip a number of bytes in the stream; used to skip unused parameters or padding bytes.
void read(TArgs &... args)
Deserializer(const Path &path)
Opens a stream associated with given path and prepares the file for deserialization.
Object representing a path on a filesystem.
std::string native() const
Returns the native version of the path.
Exception thrown by Deserializer on failure.
SerializerException(const std::string &error, const Size position)
virtual const char * what() const noexcept override
Object providing serialization of primitives into a stream.
Serializer(const Path &path)
void addPadding(const Size size)
void write(const TArgs &... args)
Generic storage and input/output routines of settings.
typename SerializedType< Precise, T >::Type Serialized
Vector position(const Float a, const Float e, const Float u)
Computes the position on the elliptic trajectory.
Overload of std::swap for Sph::Array.
std::conditional_t< Precise, double, float > Type
std::conditional_t< Precise, int64_t, int32_t > Type
std::add_lvalue_reference_t< const char[N]> Type
Type trait for serialization/deserialization of types.
Object with deleted copy constructor and copy operator.