BinStreamable.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 _BINSTREAMABLE_FBASICS_H
  8. #define _BINSTREAMABLE_FBASICS_H
  9. #include <iostream>
  10. #include <core/basics/binstream.h>
  11. namespace NICE {
  12. /**
  13. * Abstract base class for binarily streamable classes.
  14. */
  15. class BinStreamable {
  16. public:
  17. /**
  18. * Format (compression method)
  19. */
  20. enum Format {
  21. /**
  22. * Save in binary format which is compressed with gzip
  23. * (only supported if compiled with zlib, use \#ifdef NICE_USELIB_ZLIB)
  24. */
  25. BIN_GZ=3,
  26. /**
  27. * Save in binary format (uncompressed)
  28. */
  29. BIN=4
  30. };
  31. virtual ~BinStreamable(){}
  32. /**
  33. * Read class attributes from ibinstream
  34. * \param stream ibinstream to read elements from
  35. */
  36. virtual void read(ibinstream& stream) = 0;
  37. /**
  38. * Write class attributes to obinstream.
  39. * \param stream obinstream to write elements to
  40. */
  41. virtual void write(obinstream& stream) const = 0;
  42. /**
  43. * Read class attributes from ibinstream
  44. * \param stream ibinstream to read elements from
  45. */
  46. inline void readBin(ibinstream& stream) {
  47. read(stream);
  48. }
  49. /**
  50. * Write class attributes to obinstream.
  51. * \param stream obinstream to write elements to
  52. */
  53. inline void writeBin(obinstream& stream) const {
  54. write(stream);
  55. }
  56. /** Read class attributes from a file.
  57. * \param filename Name of the file
  58. * \param compression file format \see SaveMethod
  59. */
  60. virtual void readFromBinFile(const char *filename,
  61. Format compression=BIN);
  62. /**
  63. * Write class attributes to a file.
  64. * \param filename Name of the file
  65. * \param compression file format \see SaveMethod
  66. */
  67. virtual void writeToBinFile(const char *filename,
  68. Format compression=BIN) const;
  69. };
  70. inline obinstream& operator<<(obinstream &strm, const BinStreamable &ex) {
  71. ex.writeBin(strm);
  72. return strm;
  73. }
  74. inline ibinstream& operator>>(ibinstream &strm, BinStreamable &ex) {
  75. ex.readBin(strm);
  76. return strm;
  77. }
  78. }; // namespace NICE
  79. #endif /* _BINSTREAMABLE_FBASICS_H */