2018-12-08 20:10:29 +05:00
|
|
|
#ifndef NETGEN_CORE_ARCHIVE_HPP
|
|
|
|
#define NETGEN_CORE_ARCHIVE_HPP
|
|
|
|
|
2021-09-29 01:34:11 +05:00
|
|
|
#include <any>
|
2019-04-25 01:21:05 +05:00
|
|
|
#include <array> // for array
|
2018-12-10 18:37:27 +05:00
|
|
|
#include <complex> // for complex
|
|
|
|
#include <cstring> // for size_t, strlen
|
2018-12-28 17:43:15 +05:00
|
|
|
#include <fstream> // for ifstream, ofstream
|
2018-12-10 18:37:27 +05:00
|
|
|
#include <functional> // for function
|
2018-12-28 17:43:15 +05:00
|
|
|
#include <map> // for map
|
|
|
|
#include <memory> // for shared_ptr
|
2021-11-10 16:08:56 +05:00
|
|
|
#include <optional> // for optional
|
2018-12-28 17:43:15 +05:00
|
|
|
#include <string> // for string
|
2019-02-13 19:18:23 +05:00
|
|
|
#include <type_traits> // for declval, enable_if_t, false_type, is_co...
|
2021-06-25 21:58:25 +05:00
|
|
|
#include <cstddef> // for std::byte
|
2018-12-10 18:37:27 +05:00
|
|
|
#include <typeinfo> // for type_info
|
|
|
|
#include <utility> // for move, swap, pair
|
|
|
|
#include <vector> // for vector
|
|
|
|
|
2018-12-28 19:54:04 +05:00
|
|
|
#include "exception.hpp" // for UnreachableCodeException, Exception
|
2018-12-28 17:43:15 +05:00
|
|
|
#include "logging.hpp" // for logger
|
2019-01-02 22:38:03 +05:00
|
|
|
#include "ngcore_api.hpp" // for NGCORE_API
|
2018-12-10 18:37:27 +05:00
|
|
|
#include "type_traits.hpp" // for all_of_tmpl
|
2019-01-02 22:38:03 +05:00
|
|
|
#include "utils.hpp" // for Demangle, unlikely
|
2018-12-10 18:37:27 +05:00
|
|
|
#include "version.hpp" // for VersionInfo
|
2018-11-29 22:35:30 +05:00
|
|
|
|
2018-12-28 17:43:15 +05:00
|
|
|
#ifdef NETGEN_PYTHON
|
2019-07-11 19:23:22 +05:00
|
|
|
namespace pybind11
|
|
|
|
{
|
|
|
|
class object;
|
|
|
|
}
|
2018-12-28 17:43:15 +05:00
|
|
|
#endif // NETGEN_PYTHON
|
2018-12-20 21:01:27 +05:00
|
|
|
|
2018-11-29 22:35:30 +05:00
|
|
|
namespace ngcore
|
|
|
|
{
|
2018-12-08 20:10:29 +05:00
|
|
|
|
2021-09-29 01:34:11 +05:00
|
|
|
#ifdef NETGEN_PYTHON
|
|
|
|
pybind11::object CastAnyToPy(const std::any& a);
|
|
|
|
#endif // NETGEN_PYTHON
|
2018-12-08 20:10:29 +05:00
|
|
|
|
2021-09-29 01:34:11 +05:00
|
|
|
class NGCORE_API Archive;
|
2018-12-12 15:05:17 +05:00
|
|
|
namespace detail
|
|
|
|
{
|
|
|
|
// create new pointer of type T if it is default constructible, else throw
|
|
|
|
template<typename T, typename ...Rest>
|
|
|
|
T* constructIfPossible_impl(Rest... /*unused*/)
|
2018-12-28 19:54:04 +05:00
|
|
|
{ throw Exception(std::string(Demangle(typeid(T).name())) + " is not default constructible!"); }
|
2018-12-08 20:10:29 +05:00
|
|
|
|
2019-02-13 19:18:23 +05:00
|
|
|
template<typename T, typename= std::enable_if_t<std::is_constructible<T>::value>>
|
2018-12-12 15:05:17 +05:00
|
|
|
T* constructIfPossible_impl(int /*unused*/) { return new T; } // NOLINT
|
2018-12-08 20:10:29 +05:00
|
|
|
|
2018-12-12 15:05:17 +05:00
|
|
|
template<typename T>
|
|
|
|
T* constructIfPossible() { return constructIfPossible_impl<T>(int{}); }
|
2018-12-08 20:10:29 +05:00
|
|
|
|
2018-12-12 15:05:17 +05:00
|
|
|
//Type trait to check if a class implements a 'void DoArchive(Archive&)' function
|
|
|
|
template<typename T>
|
|
|
|
struct has_DoArchive
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
template<typename T2>
|
|
|
|
static constexpr auto check(T2*) ->
|
|
|
|
typename std::is_same<decltype(std::declval<T2>().DoArchive(std::declval<Archive&>())),void>::type;
|
|
|
|
template<typename>
|
|
|
|
static constexpr std::false_type check(...);
|
|
|
|
using type = decltype(check<T>(nullptr)); // NOLINT
|
|
|
|
public:
|
|
|
|
NGCORE_API static constexpr bool value = type::value;
|
|
|
|
};
|
2018-12-08 20:10:29 +05:00
|
|
|
|
2018-12-12 15:05:17 +05:00
|
|
|
// Check if class is archivable
|
|
|
|
template<typename T>
|
|
|
|
struct is_Archivable_struct
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
template<typename T2>
|
|
|
|
static constexpr auto check(T2*) ->
|
|
|
|
typename std::is_same<decltype(std::declval<Archive>() & std::declval<T2&>()),Archive&>::type;
|
|
|
|
template<typename>
|
|
|
|
static constexpr std::false_type check(...);
|
|
|
|
using type = decltype(check<T>(nullptr)); // NOLINT
|
|
|
|
public:
|
|
|
|
NGCORE_API static constexpr bool value = type::value;
|
|
|
|
};
|
2018-12-08 20:10:29 +05:00
|
|
|
|
2018-12-12 15:05:17 +05:00
|
|
|
struct ClassArchiveInfo
|
|
|
|
{
|
|
|
|
// create new object of this type and return a void* pointer that is points to the location
|
|
|
|
// of the (base)class given by type_info
|
|
|
|
std::function<void*(const std::type_info&)> creator;
|
|
|
|
// This caster takes a void* pointer to the type stored in this info and casts it to a
|
|
|
|
// void* pointer pointing to the (base)class type_info
|
|
|
|
std::function<void*(const std::type_info&, void*)> upcaster;
|
|
|
|
// This caster takes a void* pointer to the (base)class type_info and returns void* pointing
|
|
|
|
// to the type stored in this info
|
|
|
|
std::function<void*(const std::type_info&, void*)> downcaster;
|
2021-09-29 01:34:11 +05:00
|
|
|
|
|
|
|
#ifdef NETGEN_PYTHON
|
|
|
|
std::function<pybind11::object(const std::any&)> anyToPyCaster;
|
|
|
|
#endif // NETGEN_PYTHON
|
2018-12-12 15:05:17 +05:00
|
|
|
};
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
constexpr bool is_archivable = detail::is_Archivable_struct<T>::value;
|
2018-12-08 20:10:29 +05:00
|
|
|
|
2021-06-28 04:35:23 +05:00
|
|
|
|
|
|
|
template <typename T, typename ... Trest>
|
|
|
|
constexpr size_t TotSize ()
|
|
|
|
{
|
|
|
|
if constexpr (sizeof...(Trest) == 0)
|
|
|
|
return sizeof(T);
|
|
|
|
else
|
|
|
|
return sizeof(T) + TotSize<Trest...> ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-08 20:10:29 +05:00
|
|
|
// Base Archive class
|
|
|
|
class NGCORE_API Archive
|
|
|
|
{
|
2018-12-12 15:05:17 +05:00
|
|
|
const bool is_output;
|
2018-12-08 20:10:29 +05:00
|
|
|
// how many different shared_ptr/pointer have been (un)archived
|
2018-12-28 19:52:51 +05:00
|
|
|
int shared_ptr_count{0}, ptr_count{0};
|
2018-12-08 20:10:29 +05:00
|
|
|
// maps for archived shared pointers and pointers
|
2018-12-28 19:52:51 +05:00
|
|
|
std::map<void*, int> shared_ptr2nr{}, ptr2nr{};
|
2018-12-08 20:10:29 +05:00
|
|
|
// vectors for storing the unarchived (shared) pointers
|
2018-12-28 19:52:51 +05:00
|
|
|
std::vector<std::shared_ptr<void>> nr2shared_ptr{};
|
|
|
|
std::vector<void*> nr2ptr{};
|
2018-12-20 21:01:27 +05:00
|
|
|
protected:
|
|
|
|
bool shallow_to_python = false;
|
2018-12-28 19:52:51 +05:00
|
|
|
std::map<std::string, VersionInfo> version_map = GetLibraryVersions();
|
2019-01-16 18:33:48 +05:00
|
|
|
std::shared_ptr<Logger> logger = GetLogger("Archive");
|
2018-12-08 20:10:29 +05:00
|
|
|
public:
|
2018-12-10 18:37:27 +05:00
|
|
|
Archive() = delete;
|
|
|
|
Archive(const Archive&) = delete;
|
|
|
|
Archive(Archive&&) = delete;
|
2018-12-28 19:52:51 +05:00
|
|
|
Archive (bool ais_output) : is_output(ais_output) { ; }
|
2018-12-10 14:59:46 +05:00
|
|
|
|
2018-12-08 20:10:29 +05:00
|
|
|
virtual ~Archive() { ; }
|
|
|
|
|
2018-12-28 17:43:15 +05:00
|
|
|
// If the object is pickled, all shallow archived objects will be pickled as a list,
|
|
|
|
// instead of written as a binary archive. This allows pickle to serialize every object only
|
|
|
|
// once and put them together correctly afterwards. Therefore all objects that may live in
|
|
|
|
// Python should be archived using this Shallow function. If Shallow is called from C++ code
|
|
|
|
// it archives the object normally.
|
2019-12-13 15:20:52 +05:00
|
|
|
#ifdef NETGEN_PYTHON
|
2018-12-20 21:01:27 +05:00
|
|
|
template<typename T>
|
2019-07-11 19:23:22 +05:00
|
|
|
Archive& Shallow(T& val); // implemented in python_ngcore.hpp
|
2019-12-13 15:20:52 +05:00
|
|
|
#else // NETGEN_PYTHON
|
|
|
|
template<typename T>
|
|
|
|
Archive& Shallow(T& val)
|
|
|
|
{
|
|
|
|
static_assert(detail::is_any_pointer<T>, "ShallowArchive must be given pointer type!");
|
|
|
|
*this & val;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
#endif // NETGEN_PYTHON
|
2018-12-20 21:01:27 +05:00
|
|
|
|
2018-12-28 17:43:15 +05:00
|
|
|
#ifdef NETGEN_PYTHON
|
|
|
|
virtual void ShallowOutPython(const pybind11::object& /*unused*/)
|
2018-12-28 19:54:04 +05:00
|
|
|
{ throw UnreachableCodeException{}; }
|
2019-07-11 19:23:22 +05:00
|
|
|
virtual void ShallowInPython(pybind11::object &)
|
2018-12-28 19:54:04 +05:00
|
|
|
{ throw UnreachableCodeException{}; }
|
2018-12-28 17:43:15 +05:00
|
|
|
#endif // NETGEN_PYTHON
|
2018-12-20 21:01:27 +05:00
|
|
|
|
2018-12-10 18:37:27 +05:00
|
|
|
Archive& operator=(const Archive&) = delete;
|
|
|
|
Archive& operator=(Archive&&) = delete;
|
|
|
|
|
2018-12-12 15:05:17 +05:00
|
|
|
bool Output () const { return is_output; }
|
|
|
|
bool Input () const { return !is_output; }
|
2018-12-28 19:52:51 +05:00
|
|
|
const VersionInfo& GetVersion(const std::string& library)
|
|
|
|
{ return version_map[library]; }
|
2018-12-08 20:10:29 +05:00
|
|
|
|
2019-01-23 14:19:43 +05:00
|
|
|
// only used for PyArchive
|
2019-01-25 12:26:18 +05:00
|
|
|
virtual void NeedsVersion(const std::string& /*unused*/, const std::string& /*unused*/) {}
|
2019-01-23 14:19:43 +05:00
|
|
|
|
2018-12-08 20:10:29 +05:00
|
|
|
// Pure virtual functions that have to be implemented by In-/OutArchive
|
2021-06-24 10:37:53 +05:00
|
|
|
virtual Archive & operator & (std::byte & d) = 0;
|
2021-03-29 16:55:23 +05:00
|
|
|
virtual Archive & operator & (float & d) = 0;
|
2018-12-08 20:10:29 +05:00
|
|
|
virtual Archive & operator & (double & d) = 0;
|
|
|
|
virtual Archive & operator & (int & i) = 0;
|
|
|
|
virtual Archive & operator & (long & i) = 0;
|
|
|
|
virtual Archive & operator & (size_t & i) = 0;
|
|
|
|
virtual Archive & operator & (short & i) = 0;
|
|
|
|
virtual Archive & operator & (unsigned char & i) = 0;
|
|
|
|
virtual Archive & operator & (bool & b) = 0;
|
|
|
|
virtual Archive & operator & (std::string & str) = 0;
|
|
|
|
virtual Archive & operator & (char *& str) = 0;
|
|
|
|
|
2018-12-28 19:52:51 +05:00
|
|
|
Archive & operator & (VersionInfo & version)
|
2018-12-12 21:18:52 +05:00
|
|
|
{
|
|
|
|
if(Output())
|
|
|
|
(*this) << version.to_string();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::string s;
|
|
|
|
(*this) & s;
|
|
|
|
version = VersionInfo(s);
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
2018-12-08 20:10:29 +05:00
|
|
|
|
|
|
|
// Archive std classes ================================================
|
|
|
|
template<typename T>
|
|
|
|
Archive& operator & (std::complex<T>& c)
|
|
|
|
{
|
2018-12-12 15:05:17 +05:00
|
|
|
if(Output())
|
2018-12-08 20:10:29 +05:00
|
|
|
(*this) << c.real() << c.imag();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
T tmp;
|
|
|
|
(*this) & tmp;
|
|
|
|
c.real(tmp);
|
|
|
|
(*this) & tmp;
|
|
|
|
c.imag(tmp);
|
|
|
|
}
|
|
|
|
return (*this);
|
|
|
|
}
|
|
|
|
template<typename T>
|
|
|
|
Archive& operator & (std::vector<T>& v)
|
|
|
|
{
|
|
|
|
size_t size;
|
2018-12-12 15:05:17 +05:00
|
|
|
if(Output())
|
2018-12-08 20:10:29 +05:00
|
|
|
size = v.size();
|
|
|
|
(*this) & size;
|
2018-12-12 15:05:17 +05:00
|
|
|
if(Input())
|
2018-12-08 20:10:29 +05:00
|
|
|
v.resize(size);
|
|
|
|
Do(&v[0], size);
|
|
|
|
return (*this);
|
|
|
|
}
|
2021-06-26 15:14:17 +05:00
|
|
|
|
2019-01-28 15:02:46 +05:00
|
|
|
// archive implementation for enums
|
|
|
|
template<typename T>
|
2019-02-13 19:18:23 +05:00
|
|
|
auto operator & (T& val) -> std::enable_if_t<std::is_enum<T>::value, Archive&>
|
2019-01-28 15:02:46 +05:00
|
|
|
{
|
|
|
|
int enumval;
|
|
|
|
if(Output())
|
|
|
|
enumval = int(val);
|
|
|
|
*this & enumval;
|
|
|
|
if(Input())
|
|
|
|
val = T(enumval);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-01-02 22:21:52 +05:00
|
|
|
// vector<bool> has special implementation (like a bitarray) therefore
|
|
|
|
// it needs a special overload (this could probably be more efficient, but we
|
|
|
|
// don't use it that often anyway)
|
|
|
|
Archive& operator& (std::vector<bool>& v)
|
|
|
|
{
|
2019-01-14 17:41:38 +05:00
|
|
|
logger->debug("In special archive for std::vector<bool>");
|
2019-01-02 22:21:52 +05:00
|
|
|
size_t size;
|
|
|
|
if(Output())
|
|
|
|
size = v.size();
|
|
|
|
(*this) & size;
|
|
|
|
if(Input())
|
|
|
|
{
|
|
|
|
v.resize(size);
|
|
|
|
bool b;
|
|
|
|
for(size_t i=0; i<size; i++)
|
|
|
|
{
|
|
|
|
(*this) & b;
|
|
|
|
v[i] = b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for(bool b : v)
|
|
|
|
(*this) & b;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
2018-12-08 20:10:29 +05:00
|
|
|
template<typename T1, typename T2>
|
|
|
|
Archive& operator& (std::map<T1, T2>& map)
|
|
|
|
{
|
2018-12-12 15:05:17 +05:00
|
|
|
if(Output())
|
2018-12-08 20:10:29 +05:00
|
|
|
{
|
|
|
|
(*this) << size_t(map.size());
|
|
|
|
for(auto& pair : map)
|
|
|
|
(*this) << pair.first << pair.second;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-12 21:18:52 +05:00
|
|
|
size_t size = 0;
|
2018-12-08 20:10:29 +05:00
|
|
|
(*this) & size;
|
|
|
|
T1 key; T2 val;
|
|
|
|
for(size_t i = 0; i < size; i++)
|
|
|
|
{
|
|
|
|
T1 key; T2 val;
|
|
|
|
(*this) & key & val;
|
|
|
|
map[key] = val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (*this);
|
|
|
|
}
|
2021-11-10 16:08:56 +05:00
|
|
|
template<typename T>
|
|
|
|
Archive& operator& (std::optional<T>& opt)
|
|
|
|
{
|
|
|
|
bool has_value = opt.has_value();
|
|
|
|
(*this) & has_value;
|
|
|
|
if(has_value)
|
|
|
|
{
|
|
|
|
if(Output())
|
|
|
|
(*this) << *opt;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
T value;
|
|
|
|
(*this) & value;
|
|
|
|
opt = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (*this);
|
|
|
|
}
|
2018-12-08 20:10:29 +05:00
|
|
|
// Archive arrays =====================================================
|
|
|
|
// this functions can be overloaded in Archive implementations for more efficiency
|
2019-02-13 19:18:23 +05:00
|
|
|
template <typename T, typename = std::enable_if_t<is_archivable<T>>>
|
2018-12-08 20:10:29 +05:00
|
|
|
Archive & Do (T * data, size_t n)
|
2018-12-10 18:37:27 +05:00
|
|
|
{ for (size_t j = 0; j < n; j++) { (*this) & data[j]; }; return *this; }; // NOLINT
|
2018-12-08 20:10:29 +05:00
|
|
|
|
2021-06-24 10:37:53 +05:00
|
|
|
virtual Archive & Do (std::byte * d, size_t n)
|
|
|
|
{ for (size_t j = 0; j < n; j++) { (*this) & d[j]; }; return *this; }; // NOLINT
|
|
|
|
|
2018-12-08 20:10:29 +05:00
|
|
|
virtual Archive & Do (double * d, size_t n)
|
2018-12-10 18:37:27 +05:00
|
|
|
{ for (size_t j = 0; j < n; j++) { (*this) & d[j]; }; return *this; }; // NOLINT
|
2018-12-08 20:10:29 +05:00
|
|
|
|
|
|
|
virtual Archive & Do (int * i, size_t n)
|
2018-12-10 18:37:27 +05:00
|
|
|
{ for (size_t j = 0; j < n; j++) { (*this) & i[j]; }; return *this; }; // NOLINT
|
2018-12-08 20:10:29 +05:00
|
|
|
|
|
|
|
virtual Archive & Do (long * i, size_t n)
|
2018-12-10 18:37:27 +05:00
|
|
|
{ for (size_t j = 0; j < n; j++) { (*this) & i[j]; }; return *this; }; // NOLINT
|
2018-12-08 20:10:29 +05:00
|
|
|
|
|
|
|
virtual Archive & Do (size_t * i, size_t n)
|
2018-12-10 18:37:27 +05:00
|
|
|
{ for (size_t j = 0; j < n; j++) { (*this) & i[j]; }; return *this; }; // NOLINT
|
2018-12-08 20:10:29 +05:00
|
|
|
|
|
|
|
virtual Archive & Do (short * i, size_t n)
|
2018-12-10 18:37:27 +05:00
|
|
|
{ for (size_t j = 0; j < n; j++) { (*this) & i[j]; }; return *this; }; // NOLINT
|
2018-12-08 20:10:29 +05:00
|
|
|
|
|
|
|
virtual Archive & Do (unsigned char * i, size_t n)
|
2018-12-10 18:37:27 +05:00
|
|
|
{ for (size_t j = 0; j < n; j++) { (*this) & i[j]; }; return *this; }; // NOLINT
|
2018-12-08 20:10:29 +05:00
|
|
|
|
|
|
|
virtual Archive & Do (bool * b, size_t n)
|
2018-12-10 18:37:27 +05:00
|
|
|
{ for (size_t j = 0; j < n; j++) { (*this) & b[j]; }; return *this; }; // NOLINT
|
2018-12-08 20:10:29 +05:00
|
|
|
|
|
|
|
// Archive a class implementing a (void DoArchive(Archive&)) method =======
|
2018-12-12 15:05:17 +05:00
|
|
|
template<typename T, typename=std::enable_if_t<detail::has_DoArchive<T>::value>>
|
2018-12-08 20:10:29 +05:00
|
|
|
Archive& operator & (T& val)
|
|
|
|
{
|
|
|
|
val.DoArchive(*this); return *this;
|
|
|
|
}
|
|
|
|
|
2021-06-28 04:35:23 +05:00
|
|
|
|
|
|
|
|
|
|
|
|
2021-06-27 15:32:51 +05:00
|
|
|
// pack elements to binary
|
|
|
|
template <typename ... Types>
|
|
|
|
Archive & DoPacked (Types & ... args)
|
|
|
|
{
|
|
|
|
if (true) // (isbinary)
|
|
|
|
{
|
2021-06-28 04:35:23 +05:00
|
|
|
constexpr size_t totsize = TotSize<Types...>(); // (args...);
|
|
|
|
std::byte mem[totsize];
|
2021-06-27 15:32:51 +05:00
|
|
|
if (is_output)
|
|
|
|
{
|
|
|
|
CopyToBin (&mem[0], args...);
|
2021-06-28 04:35:23 +05:00
|
|
|
Do(&mem[0], totsize);
|
2021-06-27 15:32:51 +05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-06-28 04:35:23 +05:00
|
|
|
Do(&mem[0], totsize);
|
2021-06-27 15:32:51 +05:00
|
|
|
CopyFromBin (&mem[0], args...);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// else
|
|
|
|
// cout << "DoPacked of non-binary called --> individual pickling" << endl;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T, typename ... Trest>
|
|
|
|
constexpr void CopyToBin (std::byte * ptr, T & first, Trest & ...rest) const
|
|
|
|
{
|
|
|
|
memcpy (ptr, &first, sizeof(first));
|
|
|
|
CopyToBin(ptr+sizeof(first), rest...);
|
|
|
|
}
|
|
|
|
constexpr void CopyToBin (std::byte * ptr) const { }
|
|
|
|
|
|
|
|
template <typename T, typename ... Trest>
|
|
|
|
constexpr void CopyFromBin (std::byte * ptr, T & first, Trest & ...rest) const
|
|
|
|
{
|
|
|
|
memcpy (&first, ptr, sizeof(first));
|
|
|
|
CopyFromBin(ptr+sizeof(first), rest...);
|
|
|
|
}
|
|
|
|
constexpr void CopyFromBin (std::byte * ptr) const { }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-12-08 20:10:29 +05:00
|
|
|
// Archive shared_ptrs =================================================
|
|
|
|
template <typename T>
|
|
|
|
Archive& operator & (std::shared_ptr<T>& ptr)
|
|
|
|
{
|
|
|
|
if(Output())
|
|
|
|
{
|
2019-01-14 17:41:38 +05:00
|
|
|
logger->debug("Store shared ptr of type {}", Demangle(typeid(T).name()));
|
2018-12-08 20:10:29 +05:00
|
|
|
// save -2 for nullptr
|
|
|
|
if(!ptr)
|
2018-12-28 17:43:15 +05:00
|
|
|
{
|
2019-01-14 17:41:38 +05:00
|
|
|
logger->debug("Storing nullptr");
|
2018-12-28 17:43:15 +05:00
|
|
|
return (*this) << -2;
|
|
|
|
}
|
2018-12-08 20:10:29 +05:00
|
|
|
|
|
|
|
void* reg_ptr = ptr.get();
|
|
|
|
bool neededDowncast = false;
|
|
|
|
// Downcasting is only possible for our registered classes
|
|
|
|
if(typeid(T) != typeid(*ptr))
|
|
|
|
{
|
2019-01-15 13:49:19 +05:00
|
|
|
logger->debug("Typids are different: {} vs {}",
|
|
|
|
Demangle(typeid(T).name()),
|
|
|
|
Demangle(typeid(*ptr).name()));
|
2018-12-12 15:05:17 +05:00
|
|
|
if(!IsRegistered(Demangle(typeid(*ptr).name())))
|
2018-12-28 19:54:04 +05:00
|
|
|
throw Exception(std::string("Archive error: Polymorphic type ")
|
|
|
|
+ Demangle(typeid(*ptr).name())
|
|
|
|
+ " not registered for archive");
|
2018-12-12 15:05:17 +05:00
|
|
|
reg_ptr = GetArchiveRegister(Demangle(typeid(*ptr).name())).downcaster(typeid(T), ptr.get());
|
2018-12-08 20:10:29 +05:00
|
|
|
// if there was a true downcast we have to store more information
|
2018-12-28 17:43:15 +05:00
|
|
|
if(reg_ptr != static_cast<void*>(ptr.get()))
|
|
|
|
{
|
2019-01-14 17:41:38 +05:00
|
|
|
logger->debug("Multiple/Virtual inheritance involved, need to cast pointer");
|
2018-12-08 20:10:29 +05:00
|
|
|
neededDowncast = true;
|
2018-12-28 17:43:15 +05:00
|
|
|
}
|
2018-12-08 20:10:29 +05:00
|
|
|
}
|
|
|
|
auto pos = shared_ptr2nr.find(reg_ptr);
|
|
|
|
// if not found store -1 and the pointer
|
|
|
|
if(pos == shared_ptr2nr.end())
|
|
|
|
{
|
2019-01-14 17:41:38 +05:00
|
|
|
logger->debug("Didn't find the shared_ptr, create new registry entry at {}",
|
|
|
|
shared_ptr_count);
|
2018-12-08 20:10:29 +05:00
|
|
|
auto p = ptr.get();
|
|
|
|
(*this) << -1;
|
|
|
|
(*this) & neededDowncast & p;
|
|
|
|
// if we did downcast we store the true type as well
|
|
|
|
if(neededDowncast)
|
2018-12-12 15:05:17 +05:00
|
|
|
(*this) << Demangle(typeid(*ptr).name());
|
2018-12-08 20:10:29 +05:00
|
|
|
shared_ptr2nr[reg_ptr] = shared_ptr_count++;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
// if found store the position and if it has to be downcasted and how
|
2019-01-14 17:41:38 +05:00
|
|
|
logger->debug("Found shared_ptr at position {}", pos->second);
|
2018-12-08 20:10:29 +05:00
|
|
|
(*this) << pos->second << neededDowncast;
|
|
|
|
if(neededDowncast)
|
2018-12-12 15:05:17 +05:00
|
|
|
(*this) << Demangle(typeid(*ptr).name());
|
2018-12-08 20:10:29 +05:00
|
|
|
}
|
|
|
|
else // Input
|
|
|
|
{
|
2019-01-14 17:41:38 +05:00
|
|
|
logger->debug("Reading shared_ptr of type {}", Demangle(typeid(T).name()));
|
2018-12-08 20:10:29 +05:00
|
|
|
int nr;
|
|
|
|
(*this) & nr;
|
|
|
|
// -2 restores a nullptr
|
|
|
|
if(nr == -2)
|
|
|
|
{
|
2019-01-14 17:41:38 +05:00
|
|
|
logger->debug("Reading a nullptr");
|
2018-12-08 20:10:29 +05:00
|
|
|
ptr = nullptr;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
// -1 restores a new shared ptr by restoring the inner pointer and creating a shared_ptr to it
|
2018-12-10 18:37:27 +05:00
|
|
|
if (nr == -1)
|
2018-12-08 20:10:29 +05:00
|
|
|
{
|
2019-01-14 17:41:38 +05:00
|
|
|
logger->debug("Createing new shared_ptr");
|
2018-12-12 15:05:17 +05:00
|
|
|
T* p = nullptr;
|
2018-12-08 20:10:29 +05:00
|
|
|
bool neededDowncast;
|
|
|
|
(*this) & neededDowncast & p;
|
|
|
|
ptr = std::shared_ptr<T>(p);
|
|
|
|
// if we did downcast we need to store a shared_ptr<void> to the true object
|
|
|
|
if(neededDowncast)
|
|
|
|
{
|
2019-01-14 17:41:38 +05:00
|
|
|
logger->debug("Shared pointer needed downcasting");
|
2018-12-08 20:10:29 +05:00
|
|
|
std::string name;
|
|
|
|
(*this) & name;
|
|
|
|
auto info = GetArchiveRegister(name);
|
|
|
|
// for this we use an aliasing constructor to create a shared pointer sharing lifetime
|
|
|
|
// with our shared ptr, but pointing to the true object
|
|
|
|
nr2shared_ptr.push_back(std::shared_ptr<void>(std::static_pointer_cast<void>(ptr),
|
|
|
|
info.downcaster(typeid(T),
|
|
|
|
ptr.get())));
|
|
|
|
}
|
|
|
|
else
|
2018-12-28 17:43:15 +05:00
|
|
|
{
|
2019-01-14 17:41:38 +05:00
|
|
|
logger->debug("Shared pointer didn't need downcasting");
|
2018-12-28 17:43:15 +05:00
|
|
|
nr2shared_ptr.push_back(ptr);
|
|
|
|
}
|
2018-12-08 20:10:29 +05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-14 17:41:38 +05:00
|
|
|
logger->debug("Reading already existing pointer at entry {}", nr);
|
2018-12-08 20:10:29 +05:00
|
|
|
auto other = nr2shared_ptr[nr];
|
|
|
|
bool neededDowncast;
|
|
|
|
(*this) & neededDowncast;
|
|
|
|
if(neededDowncast)
|
|
|
|
{
|
2019-01-14 17:41:38 +05:00
|
|
|
logger->debug("Shared pointer needed pointer downcast");
|
2018-12-08 20:10:29 +05:00
|
|
|
// if there was a downcast we can expect the class to be registered (since archiving
|
|
|
|
// wouldn't have worked else)
|
|
|
|
std::string name;
|
|
|
|
(*this) & name;
|
|
|
|
auto info = GetArchiveRegister(name);
|
|
|
|
// same trick as above, create a shared ptr sharing lifetime with
|
|
|
|
// the shared_ptr<void> in the register, but pointing to our object
|
|
|
|
ptr = std::static_pointer_cast<T>(std::shared_ptr<void>(other,
|
|
|
|
info.upcaster(typeid(T),
|
|
|
|
other.get())));
|
|
|
|
}
|
|
|
|
else
|
2018-12-28 17:43:15 +05:00
|
|
|
{
|
2019-01-14 17:41:38 +05:00
|
|
|
logger->debug("Shared pointer didn't need pointer casts");
|
2018-12-28 17:43:15 +05:00
|
|
|
ptr = std::static_pointer_cast<T>(other);
|
|
|
|
}
|
2018-12-08 20:10:29 +05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Archive pointers =======================================================
|
|
|
|
template <typename T>
|
|
|
|
Archive & operator& (T *& p)
|
|
|
|
{
|
|
|
|
if (Output())
|
|
|
|
{
|
2019-01-14 17:41:38 +05:00
|
|
|
logger->debug("Store pointer of type {}",Demangle(typeid(T).name()));
|
2018-12-08 20:10:29 +05:00
|
|
|
// if the pointer is null store -2
|
|
|
|
if (!p)
|
2018-12-28 17:43:15 +05:00
|
|
|
{
|
2019-01-14 17:41:38 +05:00
|
|
|
logger->debug("Storing nullptr");
|
2018-12-28 17:43:15 +05:00
|
|
|
return (*this) << -2;
|
|
|
|
}
|
2018-12-12 19:35:19 +05:00
|
|
|
auto reg_ptr = static_cast<void*>(p);
|
2018-12-08 20:10:29 +05:00
|
|
|
if(typeid(T) != typeid(*p))
|
|
|
|
{
|
2019-01-14 17:41:38 +05:00
|
|
|
logger->debug("Typeids are different: {} vs {}",
|
|
|
|
Demangle(typeid(T).name()),
|
|
|
|
Demangle(typeid(*p).name()));
|
2018-12-12 15:05:17 +05:00
|
|
|
if(!IsRegistered(Demangle(typeid(*p).name())))
|
2018-12-28 19:54:04 +05:00
|
|
|
throw Exception(std::string("Archive error: Polymorphic type ")
|
|
|
|
+ Demangle(typeid(*p).name())
|
|
|
|
+ " not registered for archive");
|
2018-12-12 15:05:17 +05:00
|
|
|
reg_ptr = GetArchiveRegister(Demangle(typeid(*p).name())).downcaster(typeid(T), static_cast<void*>(p));
|
2018-12-28 17:43:15 +05:00
|
|
|
if(reg_ptr != static_cast<void*>(p))
|
|
|
|
{
|
2019-01-14 17:41:38 +05:00
|
|
|
logger->debug("Multiple/Virtual inheritance involved, need to cast pointer");
|
2018-12-28 17:43:15 +05:00
|
|
|
}
|
2018-12-08 20:10:29 +05:00
|
|
|
}
|
|
|
|
auto pos = ptr2nr.find(reg_ptr);
|
|
|
|
// if the pointer is not found in the map create a new entry
|
|
|
|
if (pos == ptr2nr.end())
|
|
|
|
{
|
2019-01-15 13:49:19 +05:00
|
|
|
logger->debug("Didn't find pointer, create new registry entry at {}",
|
|
|
|
ptr_count);
|
2018-12-08 20:10:29 +05:00
|
|
|
ptr2nr[reg_ptr] = ptr_count++;
|
|
|
|
if(typeid(*p) == typeid(T))
|
|
|
|
if (std::is_constructible<T>::value)
|
|
|
|
{
|
2019-01-14 17:41:38 +05:00
|
|
|
logger->debug("Store standard class pointer (no virt. inh,...)");
|
2018-12-08 20:10:29 +05:00
|
|
|
return (*this) << -1 & (*p);
|
|
|
|
}
|
|
|
|
else
|
2018-12-28 19:54:04 +05:00
|
|
|
throw Exception(std::string("Archive error: Class ") +
|
|
|
|
Demangle(typeid(*p).name()) + " does not provide a default constructor!");
|
2018-12-08 20:10:29 +05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// if a pointer to a base class is archived, the class hierarchy must be registered
|
|
|
|
// to avoid compile time issues we allow this behaviour only for "our" classes that
|
|
|
|
// implement a void DoArchive(Archive&) member function
|
|
|
|
// To recreate the object we need to store the true type of it
|
2018-12-12 15:05:17 +05:00
|
|
|
if(!IsRegistered(Demangle(typeid(*p).name())))
|
2018-12-28 19:54:04 +05:00
|
|
|
throw Exception(std::string("Archive error: Polymorphic type ")
|
|
|
|
+ Demangle(typeid(*p).name())
|
|
|
|
+ " not registered for archive");
|
2019-01-14 17:41:38 +05:00
|
|
|
logger->debug("Store a possibly more complicated pointer");
|
2018-12-12 15:05:17 +05:00
|
|
|
return (*this) << -3 << Demangle(typeid(*p).name()) & (*p);
|
2018-12-08 20:10:29 +05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
(*this) & pos->second;
|
2018-12-10 15:30:45 +05:00
|
|
|
bool downcasted = !(reg_ptr == static_cast<void*>(p) );
|
2019-01-14 17:41:38 +05:00
|
|
|
logger->debug("Store a the existing position in registry at {}", pos->second);
|
|
|
|
logger->debug("Pointer {} downcasting", downcasted ? "needs" : "doesn't need");
|
2018-12-08 20:10:29 +05:00
|
|
|
// store if the class has been downcasted and the name
|
2018-12-12 15:05:17 +05:00
|
|
|
(*this) << downcasted << Demangle(typeid(*p).name());
|
2018-12-08 20:10:29 +05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-14 17:41:38 +05:00
|
|
|
logger->debug("Reading pointer of type {}", Demangle(typeid(T).name()));
|
2018-12-08 20:10:29 +05:00
|
|
|
int nr;
|
|
|
|
(*this) & nr;
|
|
|
|
if (nr == -2) // restore a nullptr
|
2018-12-28 17:43:15 +05:00
|
|
|
{
|
2019-01-14 17:41:38 +05:00
|
|
|
logger->debug("Loading a nullptr");
|
2018-12-08 20:10:29 +05:00
|
|
|
p = nullptr;
|
2018-12-28 17:43:15 +05:00
|
|
|
}
|
2018-12-08 20:10:29 +05:00
|
|
|
else if (nr == -1) // create a new pointer of standard type (no virtual or multiple inheritance,...)
|
|
|
|
{
|
2019-01-14 17:41:38 +05:00
|
|
|
logger->debug("Load a new pointer to a simple class");
|
2018-12-12 15:05:17 +05:00
|
|
|
p = detail::constructIfPossible<T>();
|
2018-12-08 20:10:29 +05:00
|
|
|
nr2ptr.push_back(p);
|
|
|
|
(*this) & *p;
|
|
|
|
}
|
|
|
|
else if(nr == -3) // restore one of our registered classes that can have multiple inheritance,...
|
|
|
|
{
|
2019-01-14 17:41:38 +05:00
|
|
|
logger->debug("Load a new pointer to a potentially more complicated class "
|
|
|
|
"(allows for multiple/virtual inheritance,...)");
|
2018-12-08 20:10:29 +05:00
|
|
|
// As stated above, we want this special behaviour only for our classes that implement DoArchive
|
|
|
|
std::string name;
|
|
|
|
(*this) & name;
|
2019-01-14 18:00:52 +05:00
|
|
|
logger->debug("Name = {}", name);
|
2018-12-08 20:10:29 +05:00
|
|
|
auto info = GetArchiveRegister(name);
|
|
|
|
// the creator creates a new object of type name, and returns a void* pointing
|
|
|
|
// to T (which may have an offset)
|
2018-12-10 15:50:36 +05:00
|
|
|
p = static_cast<T*>(info.creator(typeid(T)));
|
2018-12-08 20:10:29 +05:00
|
|
|
// we store the downcasted pointer (to be able to find it again from
|
|
|
|
// another class in a multiple inheritance tree)
|
|
|
|
nr2ptr.push_back(info.downcaster(typeid(T),p));
|
|
|
|
(*this) & *p;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-14 17:41:38 +05:00
|
|
|
logger->debug("Restoring pointer to already existing object at registry position {}", nr);
|
2018-12-08 20:10:29 +05:00
|
|
|
bool downcasted;
|
|
|
|
std::string name;
|
|
|
|
(*this) & downcasted & name;
|
2019-01-14 17:41:38 +05:00
|
|
|
logger->debug("{} object of type {}", downcasted ? "Downcasted" : "Not downcasted", name);
|
2018-12-08 20:10:29 +05:00
|
|
|
if(downcasted)
|
|
|
|
{
|
|
|
|
// if the class has been downcasted we can assume it is in the register
|
|
|
|
auto info = GetArchiveRegister(name);
|
2018-12-10 15:50:36 +05:00
|
|
|
p = static_cast<T*>(info.upcaster(typeid(T), nr2ptr[nr]));
|
2018-12-08 20:10:29 +05:00
|
|
|
}
|
|
|
|
else
|
2018-12-10 15:50:36 +05:00
|
|
|
p = static_cast<T*>(nr2ptr[nr]);
|
2018-12-08 20:10:29 +05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// const ptr
|
|
|
|
template<typename T>
|
|
|
|
Archive& operator &(const T*& t)
|
|
|
|
{
|
2018-12-10 17:32:03 +05:00
|
|
|
return (*this) & const_cast<T*&>(t); // NOLINT
|
2018-12-08 20:10:29 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write a read only variable
|
|
|
|
template <typename T>
|
|
|
|
Archive & operator << (const T & t)
|
|
|
|
{
|
|
|
|
T ht(t);
|
|
|
|
(*this) & ht;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void FlushBuffer() {}
|
|
|
|
|
2020-08-19 17:50:11 +05:00
|
|
|
bool parallel = false;
|
|
|
|
bool IsParallel() const { return parallel; }
|
|
|
|
void SetParallel (bool _parallel) { parallel = _parallel; }
|
|
|
|
|
2018-12-08 20:10:29 +05:00
|
|
|
private:
|
2018-12-12 21:45:06 +05:00
|
|
|
template<typename T, typename ... Bases>
|
|
|
|
friend class RegisterClassForArchive;
|
|
|
|
|
2021-09-29 01:34:11 +05:00
|
|
|
#ifdef NETGEN_PYTHON
|
|
|
|
friend pybind11::object CastAnyToPy(const std::any&);
|
|
|
|
#endif // NETGEN_PYTHON
|
|
|
|
|
2018-12-12 21:45:06 +05:00
|
|
|
// Returns ClassArchiveInfo of Demangled typeid
|
|
|
|
static const detail::ClassArchiveInfo& GetArchiveRegister(const std::string& classname);
|
|
|
|
// Set ClassArchiveInfo for Demangled typeid, this is done by creating an instance of
|
|
|
|
// RegisterClassForArchive<type, bases...>
|
|
|
|
static void SetArchiveRegister(const std::string& classname, const detail::ClassArchiveInfo& info);
|
|
|
|
static bool IsRegistered(const std::string& classname);
|
|
|
|
|
|
|
|
// Helper class for up-/downcasting
|
|
|
|
template<typename T, typename ... Bases>
|
|
|
|
struct Caster{};
|
|
|
|
|
2018-12-08 20:10:29 +05:00
|
|
|
template<typename T>
|
|
|
|
struct Caster<T>
|
|
|
|
{
|
2018-12-10 17:32:03 +05:00
|
|
|
static void* tryUpcast (const std::type_info& /*unused*/, T* /*unused*/)
|
2018-12-08 20:10:29 +05:00
|
|
|
{
|
2018-12-28 19:54:04 +05:00
|
|
|
throw Exception("Upcast not successful, some classes are not registered properly for archiving!");
|
2018-12-08 20:10:29 +05:00
|
|
|
}
|
2018-12-10 17:32:03 +05:00
|
|
|
static void* tryDowncast (const std::type_info& /*unused*/, void* /*unused*/)
|
2018-12-08 20:10:29 +05:00
|
|
|
{
|
2018-12-28 19:54:04 +05:00
|
|
|
throw Exception("Downcast not successful, some classes are not registered properly for archiving!");
|
2018-12-08 20:10:29 +05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T, typename B1, typename ... Brest>
|
|
|
|
struct Caster<T,B1,Brest...>
|
|
|
|
{
|
|
|
|
static void* tryUpcast(const std::type_info& ti, T* p)
|
|
|
|
{
|
|
|
|
try
|
2018-12-12 20:57:48 +05:00
|
|
|
{ return GetArchiveRegister(Demangle(typeid(B1).name())).
|
|
|
|
upcaster(ti, static_cast<void*>(dynamic_cast<B1*>(p))); }
|
2018-12-28 19:54:04 +05:00
|
|
|
catch(const Exception&)
|
2018-12-08 20:10:29 +05:00
|
|
|
{ return Caster<T, Brest...>::tryUpcast(ti, p); }
|
|
|
|
}
|
|
|
|
|
|
|
|
static void* tryDowncast(const std::type_info& ti, void* p)
|
|
|
|
{
|
|
|
|
if(typeid(B1) == ti)
|
2018-12-10 15:50:36 +05:00
|
|
|
return dynamic_cast<T*>(static_cast<B1*>(p));
|
2018-12-08 20:10:29 +05:00
|
|
|
try
|
2018-12-12 20:57:48 +05:00
|
|
|
{
|
|
|
|
return dynamic_cast<T*>(static_cast<B1*>(GetArchiveRegister(Demangle(typeid(B1).name())).
|
|
|
|
downcaster(ti, p)));
|
|
|
|
}
|
2018-12-28 19:54:04 +05:00
|
|
|
catch(const Exception&)
|
2018-12-12 20:57:48 +05:00
|
|
|
{
|
|
|
|
return Caster<T, Brest...>::tryDowncast(ti, p);
|
|
|
|
}
|
2018-12-08 20:10:29 +05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2018-11-29 22:35:30 +05:00
|
|
|
// BinaryOutArchive ======================================================================
|
2018-12-07 17:08:00 +05:00
|
|
|
class NGCORE_API BinaryOutArchive : public Archive
|
2018-11-29 22:35:30 +05:00
|
|
|
{
|
2018-12-10 18:37:27 +05:00
|
|
|
static constexpr size_t BUFFERSIZE = 1024;
|
2019-04-25 01:21:05 +05:00
|
|
|
std::array<char,BUFFERSIZE> buffer{};
|
2018-12-14 18:40:01 +05:00
|
|
|
size_t ptr = 0;
|
2018-12-20 21:01:27 +05:00
|
|
|
protected:
|
|
|
|
std::shared_ptr<std::ostream> stream;
|
2018-11-29 22:35:30 +05:00
|
|
|
public:
|
2018-12-10 18:37:27 +05:00
|
|
|
BinaryOutArchive() = delete;
|
|
|
|
BinaryOutArchive(const BinaryOutArchive&) = delete;
|
|
|
|
BinaryOutArchive(BinaryOutArchive&&) = delete;
|
2018-12-20 21:01:27 +05:00
|
|
|
BinaryOutArchive(std::shared_ptr<std::ostream>&& astream)
|
|
|
|
: Archive(true), stream(std::move(astream))
|
|
|
|
{ }
|
2018-12-10 18:37:27 +05:00
|
|
|
BinaryOutArchive(const std::string& filename)
|
2018-11-29 22:35:30 +05:00
|
|
|
: BinaryOutArchive(std::make_shared<std::ofstream>(filename)) {}
|
2018-12-10 18:37:27 +05:00
|
|
|
~BinaryOutArchive () override { FlushBuffer(); }
|
|
|
|
|
|
|
|
BinaryOutArchive& operator=(const BinaryOutArchive&) = delete;
|
|
|
|
BinaryOutArchive& operator=(BinaryOutArchive&&) = delete;
|
2018-11-29 22:35:30 +05:00
|
|
|
|
2018-12-03 20:28:04 +05:00
|
|
|
using Archive::operator&;
|
2021-06-24 10:37:53 +05:00
|
|
|
Archive & operator & (std::byte & d) override
|
|
|
|
{ return Write(d); }
|
2021-03-29 16:55:23 +05:00
|
|
|
Archive & operator & (float & f) override
|
|
|
|
{ return Write(f); }
|
2018-12-10 15:49:38 +05:00
|
|
|
Archive & operator & (double & d) override
|
2018-11-29 22:35:30 +05:00
|
|
|
{ return Write(d); }
|
2018-12-10 15:49:38 +05:00
|
|
|
Archive & operator & (int & i) override
|
2018-11-29 22:35:30 +05:00
|
|
|
{ return Write(i); }
|
2018-12-10 15:49:38 +05:00
|
|
|
Archive & operator & (short & i) override
|
2018-11-29 22:35:30 +05:00
|
|
|
{ return Write(i); }
|
2018-12-10 15:49:38 +05:00
|
|
|
Archive & operator & (long & i) override
|
2021-04-01 13:48:13 +05:00
|
|
|
{
|
|
|
|
// for platform independence
|
|
|
|
int64_t tmp = i;
|
|
|
|
return Write(tmp);
|
|
|
|
}
|
2018-12-10 15:49:38 +05:00
|
|
|
Archive & operator & (size_t & i) override
|
2018-11-29 22:35:30 +05:00
|
|
|
{ return Write(i); }
|
2018-12-10 15:49:38 +05:00
|
|
|
Archive & operator & (unsigned char & i) override
|
2018-11-29 22:35:30 +05:00
|
|
|
{ return Write(i); }
|
2018-12-10 15:49:38 +05:00
|
|
|
Archive & operator & (bool & b) override
|
2018-11-29 22:35:30 +05:00
|
|
|
{ return Write(b); }
|
2021-06-27 15:32:51 +05:00
|
|
|
|
2018-12-10 15:49:38 +05:00
|
|
|
Archive & operator & (std::string & str) override
|
2018-11-29 22:35:30 +05:00
|
|
|
{
|
|
|
|
int len = str.length();
|
2018-12-06 21:53:44 +05:00
|
|
|
(*this) & len;
|
|
|
|
FlushBuffer();
|
|
|
|
if(len)
|
2018-12-20 21:01:27 +05:00
|
|
|
stream->write (&str[0], len);
|
2018-11-29 22:35:30 +05:00
|
|
|
return *this;
|
|
|
|
}
|
2018-12-10 15:49:38 +05:00
|
|
|
Archive & operator & (char *& str) override
|
2018-11-29 22:35:30 +05:00
|
|
|
{
|
2019-04-25 01:21:05 +05:00
|
|
|
long len = str ? static_cast<long>(strlen (str)) : -1;
|
2018-12-06 21:53:44 +05:00
|
|
|
(*this) & len;
|
|
|
|
FlushBuffer();
|
|
|
|
if(len > 0)
|
2018-12-20 21:01:27 +05:00
|
|
|
stream->write (&str[0], len); // NOLINT
|
2018-11-29 22:35:30 +05:00
|
|
|
return *this;
|
|
|
|
}
|
2018-12-10 15:49:38 +05:00
|
|
|
void FlushBuffer() override
|
2018-11-29 22:35:30 +05:00
|
|
|
{
|
|
|
|
if (ptr > 0)
|
|
|
|
{
|
2018-12-20 21:01:27 +05:00
|
|
|
stream->write(&buffer[0], ptr);
|
2018-11-29 22:35:30 +05:00
|
|
|
ptr = 0;
|
|
|
|
}
|
|
|
|
}
|
2021-06-27 15:32:51 +05:00
|
|
|
Archive & Do (std::byte * d, size_t n) override
|
|
|
|
{
|
|
|
|
FlushBuffer();
|
|
|
|
stream->write(reinterpret_cast<char*>(d), n*sizeof(std::byte)); return *this;
|
|
|
|
}
|
2018-11-29 22:35:30 +05:00
|
|
|
|
|
|
|
private:
|
|
|
|
template <typename T>
|
|
|
|
Archive & Write (T x)
|
|
|
|
{
|
2021-04-01 13:48:13 +05:00
|
|
|
static_assert(sizeof(T) < BUFFERSIZE, "Cannot write large types with this function!");
|
2018-11-29 22:35:30 +05:00
|
|
|
if (unlikely(ptr > BUFFERSIZE-sizeof(T)))
|
|
|
|
{
|
2018-12-20 21:01:27 +05:00
|
|
|
stream->write(&buffer[0], ptr);
|
2021-04-01 13:48:13 +05:00
|
|
|
ptr = 0;
|
2018-11-29 22:35:30 +05:00
|
|
|
}
|
2021-04-01 13:48:13 +05:00
|
|
|
memcpy(&buffer[ptr], &x, sizeof(T));
|
2018-11-29 22:35:30 +05:00
|
|
|
ptr += sizeof(T);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// BinaryInArchive ======================================================================
|
2018-12-07 17:08:00 +05:00
|
|
|
class NGCORE_API BinaryInArchive : public Archive
|
2018-11-29 22:35:30 +05:00
|
|
|
{
|
2018-12-20 21:01:27 +05:00
|
|
|
protected:
|
|
|
|
std::shared_ptr<std::istream> stream;
|
2018-11-29 22:35:30 +05:00
|
|
|
public:
|
2018-12-20 21:01:27 +05:00
|
|
|
BinaryInArchive (std::shared_ptr<std::istream>&& astream)
|
|
|
|
: Archive(false), stream(std::move(astream))
|
|
|
|
{ }
|
2018-12-10 18:37:27 +05:00
|
|
|
BinaryInArchive (const std::string& filename)
|
2018-12-05 18:20:24 +05:00
|
|
|
: BinaryInArchive(std::make_shared<std::ifstream>(filename)) { ; }
|
|
|
|
|
2018-12-03 20:28:04 +05:00
|
|
|
using Archive::operator&;
|
2021-06-24 10:37:53 +05:00
|
|
|
Archive & operator & (std::byte & d) override
|
|
|
|
{ Read(d); return *this; }
|
2021-03-29 16:55:23 +05:00
|
|
|
Archive & operator & (float & f) override
|
|
|
|
{ Read(f); return *this; }
|
2018-12-10 15:49:38 +05:00
|
|
|
Archive & operator & (double & d) override
|
2018-11-29 22:35:30 +05:00
|
|
|
{ Read(d); return *this; }
|
2018-12-10 15:49:38 +05:00
|
|
|
Archive & operator & (int & i) override
|
2018-11-29 22:35:30 +05:00
|
|
|
{ Read(i); return *this; }
|
2018-12-10 15:49:38 +05:00
|
|
|
Archive & operator & (short & i) override
|
2018-11-29 22:35:30 +05:00
|
|
|
{ Read(i); return *this; }
|
2018-12-10 15:49:38 +05:00
|
|
|
Archive & operator & (long & i) override
|
2021-04-01 13:48:13 +05:00
|
|
|
{
|
|
|
|
int64_t tmp;
|
|
|
|
Read(tmp);
|
|
|
|
i = tmp;
|
|
|
|
return *this;
|
|
|
|
}
|
2018-12-10 15:49:38 +05:00
|
|
|
Archive & operator & (size_t & i) override
|
2018-11-29 22:35:30 +05:00
|
|
|
{ Read(i); return *this; }
|
2018-12-10 15:49:38 +05:00
|
|
|
Archive & operator & (unsigned char & i) override
|
2018-11-29 22:35:30 +05:00
|
|
|
{ Read(i); return *this; }
|
2018-12-10 15:49:38 +05:00
|
|
|
Archive & operator & (bool & b) override
|
2018-11-29 22:35:30 +05:00
|
|
|
{ Read(b); return *this; }
|
2018-12-10 15:49:38 +05:00
|
|
|
Archive & operator & (std::string & str) override
|
2018-11-29 22:35:30 +05:00
|
|
|
{
|
|
|
|
int len;
|
2018-12-06 21:53:44 +05:00
|
|
|
(*this) & len;
|
2018-11-29 22:35:30 +05:00
|
|
|
str.resize(len);
|
2018-12-06 21:53:44 +05:00
|
|
|
if(len)
|
2018-12-20 21:01:27 +05:00
|
|
|
stream->read(&str[0], len); // NOLINT
|
2018-11-29 22:35:30 +05:00
|
|
|
return *this;
|
|
|
|
}
|
2018-12-10 15:49:38 +05:00
|
|
|
Archive & operator & (char *& str) override
|
2018-11-29 22:35:30 +05:00
|
|
|
{
|
2018-12-06 21:53:44 +05:00
|
|
|
long len;
|
|
|
|
(*this) & len;
|
|
|
|
if(len == -1)
|
|
|
|
str = nullptr;
|
|
|
|
else
|
|
|
|
{
|
2018-12-10 18:37:27 +05:00
|
|
|
str = new char[len+1]; // NOLINT
|
2018-12-20 21:01:27 +05:00
|
|
|
stream->read(&str[0], len); // NOLINT
|
2018-12-10 18:37:27 +05:00
|
|
|
str[len] = '\0'; // NOLINT
|
2018-12-06 21:53:44 +05:00
|
|
|
}
|
2018-11-29 22:35:30 +05:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-06-26 15:14:17 +05:00
|
|
|
Archive & Do (std::byte * d, size_t n) override
|
|
|
|
{ stream->read(reinterpret_cast<char*>(d), n*sizeof(std::byte)); return *this; } // NOLINT
|
2018-12-10 15:49:38 +05:00
|
|
|
Archive & Do (double * d, size_t n) override
|
2018-12-20 21:01:27 +05:00
|
|
|
{ stream->read(reinterpret_cast<char*>(d), n*sizeof(double)); return *this; } // NOLINT
|
2018-12-10 15:49:38 +05:00
|
|
|
Archive & Do (int * i, size_t n) override
|
2018-12-20 21:01:27 +05:00
|
|
|
{ stream->read(reinterpret_cast<char*>(i), n*sizeof(int)); return *this; } // NOLINT
|
2018-12-10 15:49:38 +05:00
|
|
|
Archive & Do (size_t * i, size_t n) override
|
2018-12-20 21:01:27 +05:00
|
|
|
{ stream->read(reinterpret_cast<char*>(i), n*sizeof(size_t)); return *this; } // NOLINT
|
2018-11-29 22:35:30 +05:00
|
|
|
|
|
|
|
private:
|
|
|
|
template<typename T>
|
|
|
|
inline void Read(T& val)
|
2018-12-20 21:01:27 +05:00
|
|
|
{ stream->read(reinterpret_cast<char*>(&val), sizeof(T)); } // NOLINT
|
2018-11-29 22:35:30 +05:00
|
|
|
};
|
|
|
|
|
|
|
|
// TextOutArchive ======================================================================
|
2018-12-07 17:08:00 +05:00
|
|
|
class NGCORE_API TextOutArchive : public Archive
|
2018-11-29 22:35:30 +05:00
|
|
|
{
|
2018-12-20 21:01:27 +05:00
|
|
|
protected:
|
|
|
|
std::shared_ptr<std::ostream> stream;
|
2018-11-29 22:35:30 +05:00
|
|
|
public:
|
2018-12-20 21:01:27 +05:00
|
|
|
TextOutArchive (std::shared_ptr<std::ostream>&& astream)
|
|
|
|
: Archive(true), stream(std::move(astream))
|
|
|
|
{ }
|
2018-12-10 18:37:27 +05:00
|
|
|
TextOutArchive (const std::string& filename) :
|
2018-12-08 20:10:29 +05:00
|
|
|
TextOutArchive(std::make_shared<std::ofstream>(filename)) { }
|
2018-11-29 22:35:30 +05:00
|
|
|
|
|
|
|
using Archive::operator&;
|
2021-06-24 10:37:53 +05:00
|
|
|
Archive & operator & (std::byte & d) override
|
|
|
|
{ *stream << std::hex << int(d) << ' '; return *this; }
|
2021-03-29 16:55:23 +05:00
|
|
|
Archive & operator & (float & f) override
|
|
|
|
{ *stream << f << '\n'; return *this; }
|
2018-12-10 15:49:38 +05:00
|
|
|
Archive & operator & (double & d) override
|
2018-12-20 21:01:27 +05:00
|
|
|
{ *stream << d << '\n'; return *this; }
|
2018-12-10 15:49:38 +05:00
|
|
|
Archive & operator & (int & i) override
|
2018-12-20 21:01:27 +05:00
|
|
|
{ *stream << i << '\n'; return *this; }
|
2018-12-10 15:49:38 +05:00
|
|
|
Archive & operator & (short & i) override
|
2018-12-20 21:01:27 +05:00
|
|
|
{ *stream << i << '\n'; return *this; }
|
2018-12-10 15:49:38 +05:00
|
|
|
Archive & operator & (long & i) override
|
2018-12-20 21:01:27 +05:00
|
|
|
{ *stream << i << '\n'; return *this; }
|
2018-12-10 15:49:38 +05:00
|
|
|
Archive & operator & (size_t & i) override
|
2018-12-20 21:01:27 +05:00
|
|
|
{ *stream << i << '\n'; return *this; }
|
2018-12-10 15:49:38 +05:00
|
|
|
Archive & operator & (unsigned char & i) override
|
2018-12-20 21:01:27 +05:00
|
|
|
{ *stream << int(i) << '\n'; return *this; }
|
2018-12-10 15:49:38 +05:00
|
|
|
Archive & operator & (bool & b) override
|
2018-12-20 21:01:27 +05:00
|
|
|
{ *stream << (b ? 't' : 'f') << '\n'; return *this; }
|
2018-12-10 15:49:38 +05:00
|
|
|
Archive & operator & (std::string & str) override
|
2018-11-29 22:35:30 +05:00
|
|
|
{
|
|
|
|
int len = str.length();
|
2018-12-20 21:01:27 +05:00
|
|
|
*stream << len << '\n';
|
2018-12-05 18:20:24 +05:00
|
|
|
if(len)
|
|
|
|
{
|
2018-12-20 21:01:27 +05:00
|
|
|
stream->write(&str[0], len); // NOLINT
|
|
|
|
*stream << '\n';
|
2018-12-05 18:20:24 +05:00
|
|
|
}
|
2018-11-29 22:35:30 +05:00
|
|
|
return *this;
|
|
|
|
}
|
2018-12-10 15:49:38 +05:00
|
|
|
Archive & operator & (char *& str) override
|
2018-11-29 22:35:30 +05:00
|
|
|
{
|
2019-04-25 01:21:05 +05:00
|
|
|
long len = str ? static_cast<long>(strlen (str)) : -1;
|
2018-12-06 21:53:44 +05:00
|
|
|
*this & len;
|
|
|
|
if(len > 0)
|
2018-12-05 18:20:24 +05:00
|
|
|
{
|
2018-12-20 21:01:27 +05:00
|
|
|
stream->write (&str[0], len); // NOLINT
|
|
|
|
*stream << '\n';
|
2018-12-05 18:20:24 +05:00
|
|
|
}
|
2018-11-29 22:35:30 +05:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// TextInArchive ======================================================================
|
2018-12-07 17:08:00 +05:00
|
|
|
class NGCORE_API TextInArchive : public Archive
|
2018-11-29 22:35:30 +05:00
|
|
|
{
|
2018-12-20 21:01:27 +05:00
|
|
|
protected:
|
|
|
|
std::shared_ptr<std::istream> stream;
|
2018-11-29 22:35:30 +05:00
|
|
|
public:
|
2018-12-20 21:01:27 +05:00
|
|
|
TextInArchive (std::shared_ptr<std::istream>&& astream) :
|
|
|
|
Archive(false), stream(std::move(astream))
|
|
|
|
{ }
|
2018-12-10 18:37:27 +05:00
|
|
|
TextInArchive (const std::string& filename)
|
2018-11-29 22:35:30 +05:00
|
|
|
: TextInArchive(std::make_shared<std::ifstream>(filename)) {}
|
|
|
|
|
|
|
|
using Archive::operator&;
|
2021-06-24 10:37:53 +05:00
|
|
|
Archive & operator & (std::byte & d) override
|
|
|
|
{ int tmp; *stream >> std::hex >> tmp; d = std::byte(tmp); return *this; }
|
2021-03-29 16:55:23 +05:00
|
|
|
Archive & operator & (float & f) override
|
|
|
|
{ *stream >> f; return *this; }
|
2018-12-10 15:49:38 +05:00
|
|
|
Archive & operator & (double & d) override
|
2018-12-20 21:01:27 +05:00
|
|
|
{ *stream >> d; return *this; }
|
2018-12-10 15:49:38 +05:00
|
|
|
Archive & operator & (int & i) override
|
2018-12-20 21:01:27 +05:00
|
|
|
{ *stream >> i; return *this; }
|
2018-12-10 15:49:38 +05:00
|
|
|
Archive & operator & (short & i) override
|
2018-12-20 21:01:27 +05:00
|
|
|
{ *stream >> i; return *this; }
|
2018-12-10 15:49:38 +05:00
|
|
|
Archive & operator & (long & i) override
|
2018-12-20 21:01:27 +05:00
|
|
|
{ *stream >> i; return *this; }
|
2018-12-10 15:49:38 +05:00
|
|
|
Archive & operator & (size_t & i) override
|
2018-12-20 21:01:27 +05:00
|
|
|
{ *stream >> i; return *this; }
|
2018-12-10 15:49:38 +05:00
|
|
|
Archive & operator & (unsigned char & i) override
|
2018-12-20 21:01:27 +05:00
|
|
|
{ int _i; *stream >> _i; i = _i; return *this; }
|
2018-12-10 15:49:38 +05:00
|
|
|
Archive & operator & (bool & b) override
|
2018-12-20 21:01:27 +05:00
|
|
|
{ char c; *stream >> c; b = (c=='t'); return *this; }
|
2018-12-10 15:49:38 +05:00
|
|
|
Archive & operator & (std::string & str) override
|
2018-11-29 22:35:30 +05:00
|
|
|
{
|
|
|
|
int len;
|
2018-12-20 21:01:27 +05:00
|
|
|
*stream >> len;
|
2018-11-29 22:35:30 +05:00
|
|
|
char ch;
|
2018-12-20 21:01:27 +05:00
|
|
|
stream->get(ch); // '\n'
|
2018-11-29 22:35:30 +05:00
|
|
|
str.resize(len);
|
2018-12-05 18:20:24 +05:00
|
|
|
if(len)
|
2018-12-20 21:01:27 +05:00
|
|
|
stream->get(&str[0], len+1, '\0');
|
2018-11-29 22:35:30 +05:00
|
|
|
return *this;
|
|
|
|
}
|
2018-12-10 15:49:38 +05:00
|
|
|
Archive & operator & (char *& str) override
|
2018-11-29 22:35:30 +05:00
|
|
|
{
|
2018-12-06 21:53:44 +05:00
|
|
|
long len;
|
|
|
|
(*this) & len;
|
2018-11-29 22:35:30 +05:00
|
|
|
char ch;
|
2018-12-06 21:53:44 +05:00
|
|
|
if(len == -1)
|
|
|
|
{
|
|
|
|
str = nullptr;
|
|
|
|
return (*this);
|
|
|
|
}
|
2018-12-10 18:37:27 +05:00
|
|
|
str = new char[len+1]; // NOLINT
|
2018-12-05 18:20:24 +05:00
|
|
|
if(len)
|
2018-12-06 21:53:44 +05:00
|
|
|
{
|
2018-12-20 21:01:27 +05:00
|
|
|
stream->get(ch); // \n
|
|
|
|
stream->get(&str[0], len+1, '\0'); // NOLINT
|
2018-12-06 21:53:44 +05:00
|
|
|
}
|
2018-12-10 18:37:27 +05:00
|
|
|
str[len] = '\0'; // NOLINT
|
2018-11-29 22:35:30 +05:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
};
|
2018-12-20 21:01:27 +05:00
|
|
|
|
2020-08-29 18:38:03 +05:00
|
|
|
// HashArchive =================================================================
|
|
|
|
// This class enables to easily create hashes for archivable objects by xoring
|
|
|
|
// threw its data
|
|
|
|
|
|
|
|
class NGCORE_API HashArchive : public Archive
|
|
|
|
{
|
2020-11-26 02:07:07 +05:00
|
|
|
size_t hash_value = 0;
|
2020-08-29 18:38:03 +05:00
|
|
|
char* h;
|
2020-09-01 14:59:37 +05:00
|
|
|
int offset = 0;
|
2020-08-29 18:38:03 +05:00
|
|
|
public:
|
|
|
|
HashArchive() : Archive(true)
|
|
|
|
{ h = (char*)&hash_value; }
|
|
|
|
|
|
|
|
using Archive::operator&;
|
2021-06-24 16:21:43 +05:00
|
|
|
Archive & operator & (std::byte & d) override { return ApplyHash(d); }
|
2021-03-29 16:55:23 +05:00
|
|
|
Archive & operator & (float & f) override { return ApplyHash(f); }
|
2020-08-29 18:38:03 +05:00
|
|
|
Archive & operator & (double & d) override { return ApplyHash(d); }
|
|
|
|
Archive & operator & (int & i) override { return ApplyHash(i); }
|
|
|
|
Archive & operator & (short & i) override { return ApplyHash(i); }
|
|
|
|
Archive & operator & (long & i) override { return ApplyHash(i); }
|
|
|
|
Archive & operator & (size_t & i) override { return ApplyHash(i); }
|
|
|
|
Archive & operator & (unsigned char & i) override { return ApplyHash(i); }
|
|
|
|
Archive & operator & (bool & b) override { return ApplyHash(b); }
|
|
|
|
Archive & operator & (std::string & str) override
|
|
|
|
{ for(auto c : str) ApplyHash(c); return *this; }
|
|
|
|
Archive & operator & (char *& str) override
|
|
|
|
{ char* s = str; while(*s != '\0') ApplyHash(*(s++)); return *this; }
|
|
|
|
|
2020-08-29 19:19:45 +05:00
|
|
|
// HashArchive can be used in const context
|
|
|
|
template<typename T>
|
|
|
|
Archive & operator& (const T& val) const
|
|
|
|
{ return (*this) & const_cast<T&>(val); }
|
|
|
|
|
2020-08-29 18:38:03 +05:00
|
|
|
size_t GetHash() const { return hash_value; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
template<typename T>
|
|
|
|
Archive& ApplyHash(T val)
|
|
|
|
{
|
2021-02-18 14:30:01 +05:00
|
|
|
size_t n = sizeof(T);
|
2020-08-29 18:38:03 +05:00
|
|
|
char* pval = (char*)&val;
|
2021-02-18 14:30:01 +05:00
|
|
|
for(size_t i = 0; i < n; i++)
|
2020-08-29 18:38:03 +05:00
|
|
|
{
|
|
|
|
h[offset++] ^= pval[i];
|
|
|
|
offset %= 8;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-12-08 20:10:29 +05:00
|
|
|
} // namespace ngcore
|
|
|
|
|
|
|
|
#endif // NETGEN_CORE_ARCHIVE_HPP
|