Streamable.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * NICE-Core - efficient algebra and computer vision methods
  3. * - libfbasics - library of some basic tools
  4. * See file License for license information.
  5. */
  6. /*****************************************************************************/
  7. #ifndef _STREAMABLE_FBASICS_H
  8. #define _STREAMABLE_FBASICS_H
  9. #include <iostream>
  10. #include <string>
  11. namespace NICE {
  12. extern const char* GERMAN_LOCALE;
  13. extern const char* US_ENGLISH_LOCALE;
  14. /**
  15. * Abstract base class for streamable classes.
  16. */
  17. class Streamable {
  18. public:
  19. /**
  20. * Format (compression method)
  21. */
  22. enum Format {
  23. /**
  24. * Save in ascii format (uncompressed)
  25. */
  26. ASCII=0,
  27. /**
  28. * Compress the ascii format with bzip2
  29. * (only supported if compiled with bzlib, use \#ifdef NICE_USELIB_BZLIB)
  30. */
  31. ASCII_BZ=1,
  32. /**
  33. * Compress the ascii format with gzip
  34. * (only supported if compiled with zlib, use \#ifdef NICE_USELIB_ZLIB)
  35. */
  36. ASCII_GZ=2
  37. };
  38. virtual ~Streamable(){}
  39. /**
  40. * Read this object (class attributes) from \c ostream.
  41. * \param stream istream to read elements from
  42. */
  43. virtual void read(std::istream& stream) = 0;
  44. /**
  45. * Write class atttributes to ostream
  46. * \param stream ostream to write elements to
  47. */
  48. virtual void write(std::ostream& stream) const = 0;
  49. /** Read class attributes from a file.
  50. * \param filename Name of the file
  51. * \param compression file format \see SaveMethod
  52. */
  53. virtual void readFromFile(const char* filename,
  54. Format compression=ASCII);
  55. //! see readFromFile(const char *, Format)
  56. inline void readFromFile(const std::string& filename,
  57. Format compression=ASCII) {
  58. readFromFile(filename.c_str(), compression);
  59. }
  60. /**
  61. * Write class attributes to a file.
  62. * \param filename Name of the file
  63. * \param compression file format \see SaveMethod
  64. */
  65. virtual void writeToFile(const char* filename,
  66. Format compression=ASCII) const;
  67. //! see writeToFile(const char *, Format)
  68. inline void writeToFile(const std::string& filename,
  69. Format compression=ASCII) const {
  70. writeToFile(filename.c_str(), compression);
  71. }
  72. /**
  73. * Return a string representing this object.
  74. * Default implementation streams the object to a string stream.
  75. */
  76. virtual std::string toString() const;
  77. };
  78. inline std::ostream& operator<<(std::ostream &strm, const Streamable &ex) {
  79. ex.write(strm);
  80. return strm;
  81. }
  82. inline std::istream& operator>>(std::istream &strm, Streamable &ex) {
  83. ex.read(strm);
  84. return strm;
  85. }
  86. inline void writeBool(std::ostream& output, bool b) {
  87. int i = (b ? 1 : 0);
  88. output << i;
  89. }
  90. inline bool readBool(std::istream& input) {
  91. int i;
  92. input >> i;
  93. return i != 0;
  94. }
  95. }; // namespace NICE
  96. #endif /* _STREAMABLE_FBASICS_H */