1#ifndef DUNE_FEM_PERSISTENCEMANAGER_HH
2#define DUNE_FEM_PERSISTENCEMANAGER_HH
12#include <dune/fem/io/file/iointerface.hh>
13#include <dune/fem/io/parameter.hh>
14#include <dune/fem/io/streams/binarystreams.hh>
15#include <dune/fem/io/streams/tuples.hh>
16#include <dune/fem/storage/singleton.hh>
94 class PersistenceManager;
119 virtual void *pointer ()
127 template<
class ObjectType >
130 static const bool value = std::is_convertible< ObjectType *, PersistentObject * >::value;
143 template <
class ObjectType,
bool isPersistent>
148#ifdef FEM_PERSISTENCEMANAGERSTREAMTRAITS
149 typedef FEM_PERSISTENCEMANAGERSTREAMTRAITS :: BackupStreamType
BackupStreamType;
150 typedef FEM_PERSISTENCEMANAGERSTREAMTRAITS :: RestoreStreamType
RestoreStreamType;
151 static const bool singleBackupRestoreFile = FEM_PERSISTENCEMANAGERSTREAMTRAITS ::
152 singleBackupRestoreFile ;
156 static const bool singleBackupRestoreFile = false ;
160 typedef std::list< std::pair< PersistentObject *, unsigned int > > PersistentType;
161 typedef PersistentType::iterator IteratorType;
163 PersistentType objects_;
164 int fileCounter_,lineNo_;
166 std::ifstream inAsciStream_;
167 std::ofstream outAsciStream_;
168 bool closed_,invalid_;
188 assert( backupStream_ );
189 return *backupStream_ ;
194 assert( restoreStream_ );
195 return *restoreStream_ ;
199 template<
class ObjectType >
200 void insertObject( ObjectType &
object,
const bool pushFront =
false )
202 IteratorType end = objects_.end();
203 for( IteratorType it = objects_.begin(); it != end; ++it )
205 if( it->first->pointer() != &
object )
213 if ( closed_ && ! pushFront )
216 std::cerr <<
"WARNING: new object added to PersistenceManager "
217 <<
"although backup/restore has been called - "
218 <<
"Object will be ignored!" << std::endl;
224 WrapObject< ObjectType, IsPersistent< ObjectType > :: value >
231 objects_.push_front( std :: make_pair( obj, 1 ) );
233 objects_.push_back( std :: make_pair( obj, 1 ) );
236 template<
class ObjectType >
237 void removeObject ( ObjectType &
object )
239 IteratorType end = objects_.end();
240 for( IteratorType it = objects_.begin(); it != end; ++it )
242 if( it->first->pointer() != &
object )
246 if( it->second == 0 )
248 if (closed_) invalid_=
true;
252 objects_.erase( it );
253 if( !IsPersistent< ObjectType > :: value )
260 void backupObjects (
const std::string& path )
265 std::cerr <<
"WARNING: backup called although objects "
266 <<
"have been removed from the PersistenceManager! "
267 <<
"Backup ignored!" << std::endl;
273 typedef PersistentType::iterator IteratorType;
275 for( IteratorType it = objects_.begin(); it != objects_.end(); ++it )
281 void restoreObjects (
const std::string &path )
285 std::cerr <<
"WARNING: restore called although objects "
286 <<
"have been removed from the PersistenceManager! "
287 <<
"Restore ignored!" << std::endl;
292 startRestoreImpl( path );
293 typedef PersistentType :: iterator IteratorType;
295 for( IteratorType it = objects_.begin(); it != objects_.end(); ++it )
296 it->first->restore( );
301 std::string getUniqueFileName (
const std::string &tag )
303 return generateFilename( path_ +
"/" + tag, ++fileCounter_ );
306 std::string getUniqueTag (
const std::string &tag )
308 return generateFilename( tag, ++fileCounter_ );
312 void backup (
const std::string &token,
const T &value )
314 backupStreamObj() << token;
315 backupStreamObj() << value;
319 void restore (
const std::string &token, T &value )
321 std::string readToken ;
322 restoreStreamObj() >> readToken;
323 restoreStreamObj() >> value;
324 if( token != readToken )
349 static BackupStreamType& backupStream()
351 return instance ().backupStreamObj();
354 static RestoreStreamType& restoreStream()
356 return instance ().restoreStreamObj();
359 static void insert ( PersistentObject &
object,
const bool pushFront =
false )
361 instance().insertObject(
object, pushFront );
364 static void remove ( PersistentObject &
object )
369 static void backup (
const std::string& path )
374 static void restore (
const std::string& path )
379 static void startRestore (
const std::string& path )
381 instance().startRestoreImpl( path );
384 static std::string uniqueFileName(
const std::string& tag =
"" )
386 return instance().getUniqueFileName( tag );
389 static std::string uniqueTag(
const std::string& tag =
"" )
391 return instance().getUniqueTag( tag );
395 static void backupValue (
const std::string &token,
const T &value )
401 static void restoreValue (
const std::string &token, T &value )
407 const char* myTag()
const {
return "persistentobjects"; }
410 std::string createFilename(
const std::string& path,
412 const int size )
const
415 const int number = ( singleBackupRestoreFile ) ?
size : rank ;
416 s << path << myTag() <<
"." << number ;
420 void startBackup (
const std::string &path )
424 if( createDirectory( path_ ) )
426 const int rank = MPIManager :: rank() ;
427 const int size = MPIManager :: size() ;
428 std::string filename( createFilename( path_, rank,
size ) );
430 assert( backupStream_ == 0 );
431 backupStream_ = Fem :: StreamFactory<BackupStreamType> :: create( filename );
435 std::ofstream paramfile( (path_ +
"parameter").c_str() );
439 Parameter::write( paramfile,
true );
444 std::cerr <<
"Error: Unable to create '" << path_ <<
"'" << std::endl;
447 void startRestoreImpl (
const std::string &path )
449 if( restoreStream_ == 0 )
452 const int rank = MPIManager :: rank();
453 const int size = MPIManager :: size();
454 std::string filename( createFilename( path_, rank,
size ) );
456 restoreStream_ = Fem :: StreamFactory<RestoreStreamType> :: create( filename );
459 std::cout <<
"Restore from " << filename << std::endl;
461 if( ! restoreStream_ )
463 std::cout <<
"Error opening global stream: " << path_+myTag()
469 Parameter::container().clear();
478 backupStream_->
flush();
479 delete backupStream_;
485 delete restoreStream_;
495 PersistenceManager &persistenceManager __attribute__((unused)) = PersistenceManager::instance();
499 template<
class ObjectType >
500 inline PersistenceManager &
501 operator<< ( PersistenceManager &pm, ObjectType &
object )
503 static_assert( !std::is_pointer< ObjectType >::value,
"Do not add pointers to PersistenceManager." );
504 pm.insertObject(
object );
509 template<
class ObjectType >
510 inline PersistenceManager &
511 operator>> ( PersistenceManager &pm, ObjectType &
object )
513 pm.removeObject(
object );
518 template<
class ObjectType >
519 struct PersistenceManager::WrapObject< ObjectType, true >
521 static PersistentObject *apply( ObjectType &obj )
528 template<
class ObjectType >
529 struct PersistenceManager::WrapObject< ObjectType, false >
530 :
public PersistentObject
532 typedef WrapObject< ObjectType, false > ThisType;
533 typedef PersistentObject BaseType;
539 WrapObject( ObjectType &obj )
542 token_(
"_token"+PersistenceManager::uniqueTag() )
546 virtual ~WrapObject ()
549 virtual void backup ()
const
551 PersistenceManager::backupValue( token_, obj_ );
554 virtual void restore ()
556 PersistenceManager::restoreValue( token_, obj_ );
560 virtual void *pointer ()
566 static PersistentObject *apply ( ObjectType &obj )
568 return new ThisType( obj );
587 PersistenceManager::insert( *
this );
592 PersistenceManager::insert( *
this );
597 PersistenceManager::remove( *
this );
base class for auto persistent objects
Definition: persistencemanager.hh:580
Definition: binarystreams.hh:57
Definition: binarystreams.hh:16
static bool verbose()
obtain the cached value for fem.verbose with default verbosity level 2
Definition: parameter.hh:466
static void append(int &argc, char **argv)
add parameters from the command line RangeType gRight;
Definition: parameter.hh:219
class with singleton instance managing all persistent objects
Definition: persistencemanager.hh:141
void reset()
clear all objects registered to PersistenceManager
Definition: persistencemanager.hh:331
base class for persistent objects
Definition: persistencemanager.hh:101
virtual void insertSubData()
insert possible sub data of object
Definition: persistencemanager.hh:115
virtual void backup() const =0
backup persistent object
virtual void restore()=0
restore persistent object
virtual void removeSubData()
remove possible sub data of object
Definition: persistencemanager.hh:117
return singleton instance of given Object type.
Definition: singleton.hh:93
static DUNE_EXPORT Object & instance(Args &&... args)
return singleton instance of given Object type.
Definition: singleton.hh:123
void flush()
flush the stream
Definition: standardstreams.hh:88
Default exception if a function was called while the object is not in a valid state for that function...
Definition: exceptions.hh:281
Stream & operator>>(Stream &stream, std::tuple< Ts... > &t)
Read a std::tuple.
Definition: streamoperators.hh:43
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
Dune namespace.
Definition: alignedallocator.hh:13
constexpr std::integral_constant< std::size_t, sizeof...(II)> size(std::integer_sequence< T, II... >)
Return the size of the sequence.
Definition: integersequence.hh:75