dune-grid  2.2.0
geometrygrid/backuprestore.hh
Go to the documentation of this file.
00001 #ifndef DUNE_GEOGRID_BACKUPRESTORE_HH
00002 #define DUNE_GEOGRID_BACKUPRESTORE_HH
00003 
00004 #include <dune/grid/common/backuprestore.hh>
00005 #include <dune/grid/utility/grapedataioformattypes.hh>
00006 
00007 #include <dune/grid/geometrygrid/declaration.hh>
00008 #include <dune/grid/geometrygrid/capabilities.hh>
00009 
00010 namespace Dune
00011 {
00012 
00013   namespace GeoGrid
00014   {
00015 
00016     // BackupRestoreFacilities
00017     // -----------------------
00018 
00019     template< class Grid, bool hasBackupRestoreFacilities = Capabilities::hasBackupRestoreFacilities< Grid > ::v >
00020     class BackupRestoreFacilities
00021     {};
00022 
00023     template< class Grid >
00024     class BackupRestoreFacilities< Grid, true >
00025     {
00026       typedef BackupRestoreFacilities< Grid, true > This;
00027 
00028     protected:
00029       BackupRestoreFacilities ()
00030       {}
00031 
00032     private:
00033       BackupRestoreFacilities ( const This & );
00034       This &operator= ( const This & );
00035 
00036     public:
00037       template< GrapeIOFileFormatType type >
00038       bool writeGrid ( const std::string &filename, double time ) const
00039       {
00040         return asImp().hostGrid().template writeGrid< type >( filename, time );
00041       }
00042 
00043       template< GrapeIOFileFormatType type >
00044       bool readGrid ( const std::string &filename, double &time )
00045       {
00046         const bool success
00047           = asImp().hostGrid().template readGrid< type >( filename, time );
00048         asImp().update();
00049         return success;
00050       }
00051 
00052     protected:
00053       const Grid &asImp () const
00054       {
00055         return static_cast< const Grid & >( *this );
00056       }
00057 
00058       Grid &asImp ()
00059       {
00060         return static_cast< Grid & >( *this );
00061       }
00062     };
00063 
00064   } // namespace GeoGrid
00065 
00066 
00067 
00068   // BackupRestoreFacility for GeometryGrid
00069   // --------------------------------------
00070 
00071   template< class HostGrid, class CoordFunction, class Allocator >
00072   struct BackupRestoreFacility< GeometryGrid< HostGrid, CoordFunction, Allocator > >
00073   {
00074     typedef GeometryGrid< HostGrid, CoordFunction, Allocator > Grid;
00075     typedef BackupRestoreFacility< HostGrid > HostBackupRestoreFacility;
00076 
00077     static void backup ( const Grid &grid, const std::string &path, const std::string &fileprefix )
00078     {
00079       // notice: We should also backup the coordinate function
00080       HostBackupRestoreFacility::backup( grid.hostGrid(), path, fileprefix );
00081     }
00082 
00083     static void backup ( const Grid &grid, const std::ostream &stream )
00084     {
00085       // notice: We should also backup the coordinate function
00086       HostBackupRestoreFacility::backup( grid.hostGrid(), stream );
00087     }
00088 
00089     static Grid *restore ( const std::string &path, const std::string &fileprefix )
00090     {
00091       // notice: We should also restore the coordinate function
00092       HostGrid *hostGrid = HostBackupRestoreFacility::restore( path, fileprefix );
00093       CoordFunction *coordFunction = new CoordFunction();
00094       return new Grid( hostGrid, coordFunction );
00095     }
00096 
00097     static Grid *restore ( const std::istream &stream )
00098     {
00099       // notice: We should also restore the coordinate function
00100       HostGrid *hostGrid = HostBackupRestoreFacility::restore( stream );
00101       CoordFunction *coordFunction = new CoordFunction();
00102       return new Grid( hostGrid, coordFunction );
00103     }
00104   };
00105 
00106 } // namespace Dune
00107 
00108 #endif // #ifndef DUNE_GEOGRID_BACKUPRESTORE_HH