#ifndef DUNE_GEOGRID_BACKUPRESTORE_HH
#define DUNE_GEOGRID_BACKUPRESTORE_HH

#include <dune/grid/common/backuprestore.hh>
#include <dune/grid/utility/grapedataioformattypes.hh>

#include <dune/grid/geometrygrid/declaration.hh>
#include <dune/grid/geometrygrid/capabilities.hh>

namespace Dune
{

  namespace GeoGrid
  {

    // BackupRestoreFacilities
    // -----------------------

    template< class Grid, bool hasBackupRestoreFacilities = Capabilities::hasBackupRestoreFacilities< Grid > ::v >
    class BackupRestoreFacilities
    {};

    template< class Grid >
    class BackupRestoreFacilities< Grid, true >
    {
      typedef BackupRestoreFacilities< Grid, true > This;

    protected:
      BackupRestoreFacilities ()
      {}

    private:
      BackupRestoreFacilities ( const This & );
      This &operator= ( const This & );

    public:
      template< GrapeIOFileFormatType type >
      bool writeGrid ( const std::string &filename, double time ) const
      {
        return asImp().hostGrid().template writeGrid< type >( filename, time );
      }

      template< GrapeIOFileFormatType type >
      bool readGrid ( const std::string &filename, double &time )
      {
        const bool success
          = asImp().hostGrid().template readGrid< type >( filename, time );
        asImp().update();
        return success;
      }

    protected:
      const Grid &asImp () const
      {
        return static_cast< const Grid & >( *this );
      }

      Grid &asImp ()
      {
        return static_cast< Grid & >( *this );
      }
    };

  } // namespace GeoGrid



  // BackupRestoreFacility for GeometryGrid
  // --------------------------------------

  template< class HostGrid, class CoordFunction, class Allocator >
  struct BackupRestoreFacility< GeometryGrid< HostGrid, CoordFunction, Allocator > >
  {
    typedef GeometryGrid< HostGrid, CoordFunction, Allocator > Grid;
    typedef BackupRestoreFacility< HostGrid > HostBackupRestoreFacility;

    static void backup ( const Grid &grid, const std::string &path, const std::string &fileprefix )
    {
      // notice: We should also backup the coordinate function
      HostBackupRestoreFacility::backup( grid.hostGrid(), path, fileprefix );
    }

    static void backup ( const Grid &grid, const std::ostream &stream )
    {
      // notice: We should also backup the coordinate function
      HostBackupRestoreFacility::backup( grid.hostGrid(), stream );
    }

    static Grid *restore ( const std::string &path, const std::string &fileprefix )
    {
      // notice: We should also restore the coordinate function
      HostGrid *hostGrid = HostBackupRestoreFacility::restore( path, fileprefix );
      CoordFunction *coordFunction = new CoordFunction();
      return new Grid( hostGrid, coordFunction );
    }

    static Grid *restore ( const std::istream &stream )
    {
      // notice: We should also restore the coordinate function
      HostGrid *hostGrid = HostBackupRestoreFacility::restore( stream );
      CoordFunction *coordFunction = new CoordFunction();
      return new Grid( hostGrid, coordFunction );
    }
  };

} // namespace Dune

#endif // #ifndef DUNE_GEOGRID_BACKUPRESTORE_HH
