Persistent.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #ifndef PERSISTENTINCLUDE
  2. #define PERSISTENTINCLUDE
  3. #include <iostream>
  4. #include <sstream>
  5. #include <string>
  6. #include <fstream>
  7. #include <algorithm>
  8. #include "core/basics/Exception.h"
  9. namespace NICE {
  10. DEFINE_NICE_EXCEPTION ( IOException )
  11. /**
  12. * @class Persistent
  13. * @brief Persistent interface inspired by EGST
  14. * @author
  15. */
  16. class Persistent {
  17. public:
  18. // Interface specifications
  19. virtual void restore ( std::istream & is, int format = 0 ) = 0;
  20. virtual void store ( std::ostream & os, int format = 0 ) const = 0;
  21. virtual void clear () = 0;
  22. // Provided functions and overloaded stream operators
  23. virtual ~Persistent () {};
  24. // just to prevent senseless compiler warnings
  25. Persistent() {};
  26. inline std::ostream& operator>> ( std::ostream& os )
  27. {
  28. store ( os );
  29. return os;
  30. }
  31. inline std::istream& operator<< ( std::istream& is )
  32. {
  33. clear ();
  34. restore ( is );
  35. return is;
  36. }
  37. inline std::istream& operator>= ( std::istream& is )
  38. {
  39. restore ( is );
  40. return is;
  41. }
  42. inline std::string & operator<< ( std::string& s )
  43. {
  44. std::ostringstream os;
  45. store ( os );
  46. s = os.str();
  47. return s;
  48. }
  49. inline const std::string & operator>> ( const std::string & s )
  50. {
  51. std::istringstream is ( s );
  52. clear ();
  53. restore ( is );
  54. return s;
  55. }
  56. inline const std::string & operator>= ( const std::string & s )
  57. {
  58. std::istringstream is ( s );
  59. restore ( is );
  60. return s;
  61. }
  62. /** read */
  63. virtual inline void read ( const std::string& s, int format = 0 )
  64. {
  65. std::ifstream ifs ( s.c_str(), std::ios::in );
  66. if ( ! ifs.is_open() )
  67. fthrow ( IOException, "Persistent: unable to read data file " + s + " !!\n" );
  68. clear();
  69. restore ( ifs, format );
  70. ifs.close();
  71. }
  72. virtual inline void load ( const std::string& s, int format = 0 )
  73. {
  74. read ( s, format );
  75. }
  76. virtual inline void readWithoutOverwrite ( const std::string& s, int format = 0 )
  77. {
  78. std::ifstream ifs ( s.c_str(), std::ios::in );
  79. if ( ! ifs.is_open() )
  80. fthrow ( IOException, "Persistent: unable to read data file " + s + " !!\n" );
  81. restore ( ifs, format );
  82. ifs.close();
  83. }
  84. virtual inline void save ( const std::string& s, int format = 0 ) const
  85. {
  86. std::ofstream ofs ( s.c_str(), std::ios::out );
  87. if ( ! ofs.is_open() )
  88. fthrow ( IOException, "Persistent: unable to save data file " + s + " !!\n" );
  89. store ( ofs, format );
  90. ofs.close();
  91. }
  92. virtual inline void write ( const std::string& s, int format = 0 ) const
  93. {
  94. save ( s, format );
  95. }
  96. };
  97. } // namespace
  98. #endif