85#ifndef __ADS_Persistence
86 #define __ADS_Persistence
102#include <initializer_list>
106#include <string_view>
107#include <type_traits>
123 native = std::endian::native == std::endian::big ? 2 : 1
133 static constexpr bool loading =
false;
134 template<
class... X >
135 void operator()( X &... );
136 void require(
bool ,
const char * )
const;
141concept HasPersist =
requires( T & x , PersistProbe & a )
146concept PersistMakeable = std::move_constructible< T > &&
147 ( std::default_initializable< T > ||
requires {
148 { T::persistEmpty() } -> std::same_as< T >;
153concept PersistNamed =
requires {
154 { T::persistName } -> std::convertible_to< std::string_view >;
159[[nodiscard]]
constexpr std::uint32_t persistVersion(
void )
161 if constexpr(
requires { T::persistVersion; } )
162 return( std::uint32_t( T::persistVersion ) );
170[[nodiscard]]
constexpr std::uint32_t persistOldestVersion(
void )
172 if constexpr(
requires { T::persistOldestVersion; } )
173 return( std::uint32_t( T::persistOldestVersion ) );
175 return( persistVersion< T >() );
180[[nodiscard]]
constexpr std::uint64_t persistTag(
void )
182 if constexpr(
requires { T::persistTag(); } )
183 return( std::uint64_t( T::persistTag() ) );
191concept AdoptsMapping =
requires {
typename A::adopts_mapping; } &&
192 std::constructible_from< A , MappedFile >;
201 void update(
const std::byte * p , std::size_t m );
204 [[nodiscard]] std::uint64_t value(
void )
const;
207 static constexpr std::uint64_t P1 = 0x9E3779B185EBCA87ULL ,
208 P2 = 0xC2B2AE3D27D4EB4FULL ,
209 P3 = 0x165667B19E3779F9ULL ,
210 P4 = 0x85EBCA77C2B2AE63ULL;
212 void round( std::uint64_t w );
214 std::uint64_t f_acc[ 4 ] = { P1 + P2 , P2 , 0 , 0 - P1 };
216 std::uint64_t f_bytes = 0;
217 std::array< std::byte , 8 > f_tail{};
218 unsigned f_ntail = 0;
225 static constexpr std::array< unsigned char , 8 > magic = { 0x89 ,
'A' ,
'D' ,
228 static constexpr std::array< unsigned char , 8 > endMagic = {
'A' ,
'D' ,
232 static constexpr std::uint16_t version = 1;
233 static constexpr std::size_t header = 128 , trailer = 32 , name = 100 ,
240void persistSwap( E * p , std::size_t n );
245[[nodiscard]] T persistBlank(
void )
247 if constexpr(
requires { T::persistEmpty(); } )
248 return( T::persistEmpty() );
268 detail::PersistNamed< T > && detail::HasPersist< T >;
292 template<
class... X >
298 void require(
bool ,
const char * )
const noexcept { }
302 friend struct detail::PersistIO;
309 void trailer(
void );
312 void bytes(
const void * p , std::size_t m ,
bool sum =
true );
315 void pad( std::size_t a );
318 void word( std::uint64_t w );
322 void numbers(
const E * p , std::size_t n );
328 std::ostream * f_s =
nullptr;
329 std::vector< std::byte > * f_b =
nullptr;
331 detail::PersistChecksum f_sum;
332 std::uint64_t f_off = 0;
358 template<
class... X >
364 void require(
bool c ,
const char * what )
const {
365 if( ! c ) [[unlikely]]
366 fail( std::string(
"corrupted: " ) + what );
371 [[nodiscard]] std::uint32_t
version(
void )
const {
return( f_version ); }
375 friend struct detail::PersistIO;
378 [[noreturn]]
void fail(
const std::string & m )
const;
386 void trailerFirst(
bool verify );
389 void trailerLast(
void );
392 void ended(
void )
const;
395 void bytes(
void * p , std::size_t m ,
bool sum =
true );
398 void skip( std::size_t a );
401 [[nodiscard]] std::uint64_t word(
void );
404 [[nodiscard]] std::uint64_t left(
void )
const;
411 template<
class E ,
class A >
412 void numbers( std::vector< E , A > & v );
414 std::istream * f_s =
nullptr;
415 std::span< const std::byte > f_b;
420 detail::PersistChecksum f_sum;
421 std::uint64_t f_off = 0;
422 std::uint64_t f_end = ~std::uint64_t( 0 );
423 std::uint32_t f_version = 0;
434template< Persistent T >
440template< Persistent T >
441void store(
const T & x ,
const std::filesystem::path & p ,
446template< Persistent T >
447[[nodiscard]] std::vector< std::byte >
452template< Persistent T >
457template< Persistent T >
458[[nodiscard]] T
restore( std::span< const std::byte > b );
462template< Persistent T >
463[[nodiscard]] T
restore(
const std::filesystem::path & p );
470template< Persistent T >
472 bool verify =
false );
482#include "ADS/external/impl/Persistence.tpp"
A file mapped in memory, shared by the structures that are read from it in place [see Persistence....
The primitives of the processor that the compilers spell each in their own way, in one form for all: ...
a whole file mapped privately in memory, shared by its copies
Definition MappedFile.h:94
static constexpr bool loading
a reader, never a writer
Definition Persistence.h:347
PersistReader(std::span< const std::byte > b, std::string w, MappedFile m=MappedFile())
the reader of the bytes , named in the errors, which make its arrays views over them if they lie in ...
void operator()(X &... x)
the members read in order
Definition Persistence.h:359
PersistReader(std::istream &s, std::string w)
the reader of the stream , named in the errors
std::uint32_t version(void) const
the version in the file of the structure being read, between its persistOldestVersion and its persist...
Definition Persistence.h:371
void require(bool c, const char *what) const
the file rejected, as corrupted in what, unless
Definition Persistence.h:364
PersistWriter(std::ostream &s, ByteOrder o)
the writer of a payload in the order to the stream
void require(bool, const char *) const noexcept
nothing, since the members written are those of a structure
Definition Persistence.h:298
void operator()(X &... x)
the members written in order
Definition Persistence.h:293
static constexpr bool loading
a writer, never a reader
Definition Persistence.h:281
PersistWriter(std::vector< std::byte > &b, ByteOrder o)
the writer of a payload in the order appended to the bytes
a structure that store() writes and restore() reads: it names itself, lists its members in persist(),...
Definition Persistence.h:267
@ native
Definition Capabilities.h:553
void store(const T &x, std::ostream &s, ByteOrder o=ByteOrder::native)
the structure written to the stream , its payload in the order
T restoreMapped(const std::filesystem::path &p, bool verify=false)
the structure of the file mapped in memory, its arrays views over the mapping (copies if the file is...
ByteOrder
the byte order of the payload of a file
Definition Persistence.h:119
T restore(std::istream &s)
the structure read from the stream , its checksum checked
@ native
the order of the machine
Definition Persistence.h:123
@ little
the least significant byte first
Definition Persistence.h:120
@ big
the most significant byte first
Definition Persistence.h:121