ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Persistence.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File Persistence.h ---------------------------*/
3/*--------------------------------------------------------------------------*/
81/*--------------------------------------------------------------------------*/
82/*----------------------------- DEFINITIONS --------------------------------*/
83/*--------------------------------------------------------------------------*/
84
85#ifndef __ADS_Persistence
86 #define __ADS_Persistence
87/* self-identification: #endif constant */
88
89/*--------------------------------------------------------------------------*/
90/*------------------------------ INCLUDES ----------------------------------*/
91/*--------------------------------------------------------------------------*/
92
93#include "ADS/Portable.h"
95
96#include <array>
97#include <bit>
98#include <concepts>
99#include <cstddef>
100#include <cstdint>
101#include <filesystem>
102#include <initializer_list>
103#include <iosfwd>
104#include <span>
105#include <string>
106#include <string_view>
107#include <type_traits>
108#include <utility>
109#include <vector>
110
111/*--------------------------------------------------------------------------*/
112/*--------------------------- NAMESPACE ------------------------------------*/
113/*--------------------------------------------------------------------------*/
114
115namespace ADS
116{
117
119enum class ByteOrder : std::uint8_t {
120 little = 1 ,
121 big = 2 ,
123 native = std::endian::native == std::endian::big ? 2 : 1
124 };
125
126namespace detail
127{
128
131struct PersistProbe
132{
133 static constexpr bool loading = false;
134 template< class... X >
135 void operator()( X &... );
136 void require( bool , const char * ) const;
137 };
138
140template< class T >
141concept HasPersist = requires( T & x , PersistProbe & a )
142{ x.persist( a ); };
143
145template< class T >
146concept PersistMakeable = std::move_constructible< T > &&
147 ( std::default_initializable< T > || requires {
148 { T::persistEmpty() } -> std::same_as< T >;
149 } );
150
152template< class T >
153concept PersistNamed = requires {
154 { T::persistName } -> std::convertible_to< std::string_view >;
155 };
156
158template< class T >
159[[nodiscard]] constexpr std::uint32_t persistVersion( void )
160{
161 if constexpr( requires { T::persistVersion; } )
162 return( std::uint32_t( T::persistVersion ) );
163 else
164 return( 1 );
165 }
166
169template< class T >
170[[nodiscard]] constexpr std::uint32_t persistOldestVersion( void )
171{
172 if constexpr( requires { T::persistOldestVersion; } )
173 return( std::uint32_t( T::persistOldestVersion ) );
174 else
175 return( persistVersion< T >() );
176 }
177
179template< class T >
180[[nodiscard]] constexpr std::uint64_t persistTag( void )
181{
182 if constexpr( requires { T::persistTag(); } )
183 return( std::uint64_t( T::persistTag() ) );
184 else
185 return( 0 );
186 }
187
190template< class A >
191concept AdoptsMapping = requires { typename A::adopts_mapping; } &&
192 std::constructible_from< A , MappedFile >;
193
197class PersistChecksum
198{
199 public:
201 void update( const std::byte * p , std::size_t m );
202
204 [[nodiscard]] std::uint64_t value( void ) const;
205
206 private:
207 static constexpr std::uint64_t P1 = 0x9E3779B185EBCA87ULL ,
208 P2 = 0xC2B2AE3D27D4EB4FULL ,
209 P3 = 0x165667B19E3779F9ULL ,
210 P4 = 0x85EBCA77C2B2AE63ULL;
211
212 void round( std::uint64_t w );
213
214 std::uint64_t f_acc[ 4 ] = { P1 + P2 , P2 , 0 , 0 - P1 };
215 unsigned f_lane = 0;
216 std::uint64_t f_bytes = 0;
217 std::array< std::byte , 8 > f_tail{};
218 unsigned f_ntail = 0;
219 };
220
223struct PersistFormat
224{
225 static constexpr std::array< unsigned char , 8 > magic = { 0x89 , 'A' , 'D' ,
226 'S' , '\r' , '\n' ,
227 0x1A , '\n' };
228 static constexpr std::array< unsigned char , 8 > endMagic = { 'A' , 'D' ,
229 'S' ,
230 '-' , 'E' , 'N' ,
231 'D' , '\n' };
232 static constexpr std::uint16_t version = 1;
233 static constexpr std::size_t header = 128 , trailer = 32 , name = 100 ,
234 align = 64;
235 };
236
239template< class E >
240void persistSwap( E * p , std::size_t n );
241
244template< class T >
245[[nodiscard]] T persistBlank( void )
246{
247 if constexpr( requires { T::persistEmpty(); } )
248 return( T::persistEmpty() );
249 else
250 return( T() );
251 }
252
255struct PersistIO;
256
257} // end( namespace detail )
258
259/*--------------------------------------------------------------------------*/
260/*---------------------------- CONCEPT Persistent --------------------------*/
261/*--------------------------------------------------------------------------*/
262
266template< class T >
267concept Persistent = detail::PersistMakeable< T > &&
268 detail::PersistNamed< T > && detail::HasPersist< T >;
269
270/*--------------------------------------------------------------------------*/
271/*-------------------------- CLASS PersistWriter ---------------------------*/
272/*--------------------------------------------------------------------------*/
273
276{
277
278/*--------------------------------------------------------------------------*/
279 public:
281 static constexpr bool loading = false;
282
285 PersistWriter( std::ostream & s , ByteOrder o );
286
289 PersistWriter( std::vector< std::byte > & b , ByteOrder o );
290
292 template< class... X >
293 void operator()( X &... x ) {
294 ( item( x ) , ... );
295 }
296
298 void require( bool , const char * ) const noexcept { }
299
300/*--------------------------------------------------------------------------*/
301 private:
302 friend struct detail::PersistIO; // which writes the header and trailer
303
305 template< class T >
306 void header( void );
307
309 void trailer( void );
310
312 void bytes( const void * p , std::size_t m , bool sum = true );
313
315 void pad( std::size_t a );
316
318 void word( std::uint64_t w );
319
321 template< class E >
322 void numbers( const E * p , std::size_t n );
323
325 template< class T >
326 void item( T & x );
327
328 std::ostream * f_s = nullptr;
329 std::vector< std::byte > * f_b = nullptr;
330 ByteOrder f_o;
331 detail::PersistChecksum f_sum;
332 std::uint64_t f_off = 0;
333
334 }; // end( class PersistWriter )
335
336/*--------------------------------------------------------------------------*/
337/*-------------------------- CLASS PersistReader ---------------------------*/
338/*--------------------------------------------------------------------------*/
339
342{
343
344/*--------------------------------------------------------------------------*/
345 public:
347 static constexpr bool loading = true;
348
350 PersistReader( std::istream & s , std::string w );
351
354 PersistReader( std::span< const std::byte > b , std::string w ,
355 MappedFile m = MappedFile() );
356
358 template< class... X >
359 void operator()( X &... x ) {
360 ( item( x ) , ... );
361 }
362
364 void require( bool c , const char * what ) const {
365 if( ! c ) [[unlikely]]
366 fail( std::string( "corrupted: " ) + what );
367 }
368
371 [[nodiscard]] std::uint32_t version( void ) const { return( f_version ); }
372
373/*--------------------------------------------------------------------------*/
374 private:
375 friend struct detail::PersistIO; // which reads the header and trailer
376
378 [[noreturn]] void fail( const std::string & m ) const;
379
381 template< class T >
382 void header( void );
383
386 void trailerFirst( bool verify );
387
389 void trailerLast( void );
390
392 void ended( void ) const;
393
395 void bytes( void * p , std::size_t m , bool sum = true );
396
398 void skip( std::size_t a );
399
401 [[nodiscard]] std::uint64_t word( void );
402
404 [[nodiscard]] std::uint64_t left( void ) const;
405
407 template< class T >
408 void item( T & x );
409
411 template< class E , class A >
412 void numbers( std::vector< E , A > & v );
413
414 std::istream * f_s = nullptr;
415 std::span< const std::byte > f_b;
416 MappedFile f_map;
417 std::string f_where;
418 bool f_swap = false;
419 bool f_sumOn = true;
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;
424
425 }; // end( class PersistReader )
426
427/*--------------------------------------------------------------------------*/
428/*------------------------------- FUNCTIONS --------------------------------*/
429/*--------------------------------------------------------------------------*/
430
434template< Persistent T >
435void store( const T & x , std::ostream & s , ByteOrder o = ByteOrder::native );
436
440template< Persistent T >
441void store( const T & x , const std::filesystem::path & p ,
443
446template< Persistent T >
447[[nodiscard]] std::vector< std::byte >
448store( const T & x , ByteOrder o = ByteOrder::native );
449
452template< Persistent T >
453[[nodiscard]] T restore( std::istream & s );
454
457template< Persistent T >
458[[nodiscard]] T restore( std::span< const std::byte > b );
459
462template< Persistent T >
463[[nodiscard]] T restore( const std::filesystem::path & p );
464
470template< Persistent T >
471[[nodiscard]] T restoreMapped( const std::filesystem::path & p ,
472 bool verify = false );
473
474/*--------------------------------------------------------------------------*/
475
476} // end( namespace ADS )
477
478/*--------------------------------------------------------------------------*/
479/*-------------------------- IMPLEMENTATION --------------------------------*/
480/*--------------------------------------------------------------------------*/
481
482#include "ADS/external/impl/Persistence.tpp"
483
484/*--------------------------------------------------------------------------*/
485
486#endif /* Persistence.h included */
487
488/*--------------------------------------------------------------------------*/
489/*------------------------- End File Persistence.h -------------------------*/
490/*--------------------------------------------------------------------------*/
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
Definition ADT.h:96