ChipMaster's trial hacks on C++CMS starting with v1.2.1. Not sure I'll follow on with the v2 since it looks to be breaking and mostly frivolous.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

141 lines
3.2 KiB

  1. #ifndef BOOSTER_STREAMBUF_H
  2. #define BOOSTER_STREAMBUF_H
  3. #include <booster/config.h>
  4. #include <streambuf>
  5. #include <stdio.h>
  6. #include <booster/hold_ptr.h>
  7. #include <booster/auto_ptr_inc.h>
  8. #include <vector>
  9. namespace booster {
  10. ///
  11. /// \brief This class is a base class of generic I/O device that can be
  12. /// used in very simple manner with booster::streambuf allowing to create
  13. /// iostreams easily
  14. ///
  15. class io_device {
  16. public:
  17. ///
  18. /// Seek reference
  19. ///
  20. typedef enum {
  21. set, //!< Set actual position (i.e. SEEK_CUR)
  22. cur, //!< Set relatively to current position (i.e. SEEK_CUR)
  23. end //!< Set relatively to end of file (i.e. SEEK_END)
  24. } pos_type;
  25. ///
  26. /// Read \a length bytes from the stream to buffer \a pos, return number of bytes
  27. /// actually read. If return value is less then length, it is considered end of file
  28. ///
  29. /// If the stream is write only, do not implement (returns EOF by default)
  30. ///
  31. virtual size_t read(char * /*pos*/,size_t /*length*/)
  32. {
  33. return 0;
  34. }
  35. ///
  36. /// Write \a length bytes to the devise from buffer \a pos, return number of bytes
  37. /// actually written, if the result is less then \a length, considered as EOF.
  38. ///
  39. /// If the stream is read only, do not implement (returns EOF by default)
  40. ///
  41. virtual size_t write(char const * /*pos*/,size_t /*length*/)
  42. {
  43. return 0;
  44. }
  45. ///
  46. /// Seek the device to \a position relatively to \a pos. Return current position
  47. /// in file.
  48. ///
  49. /// If error occurs return -1.
  50. ///
  51. /// If the stream is not seekable do not reimplement, returns -1 by default.
  52. ///
  53. virtual long long seek(long long /*position*/,pos_type /*pos*/ = set)
  54. {
  55. return -1;
  56. }
  57. virtual ~io_device()
  58. {
  59. }
  60. };
  61. ///
  62. /// \brief this is an implementation of generic streambuffer
  63. ///
  64. class BOOSTER_API streambuf : public std::streambuf {
  65. public:
  66. ///
  67. /// Create a new stream buffer - without a stream
  68. ///
  69. streambuf();
  70. ~streambuf();
  71. ///
  72. /// Assign an io_device to the streambuf transferring an ownership on it
  73. ///
  74. void device(std::auto_ptr<io_device> d);
  75. ///
  76. /// Assign an existing io_device to the streambuf.
  77. ///
  78. void device(io_device &d);
  79. ///
  80. /// Detach currently attached io_device from the streambuf. If it is owned, it is destroyed.
  81. ///
  82. void reset_device();
  83. ///
  84. /// Get the io_device that is in use
  85. ///
  86. io_device &device();
  87. ///
  88. /// Set the size of the internal buffer that is used for read and write operations. Default
  89. /// is 1024 bytes for each direction
  90. ///
  91. void set_buffer_size(size_t n);
  92. protected:
  93. // Seek
  94. virtual std::streampos seekoff( std::streamoff off,
  95. std::ios_base::seekdir way,
  96. std::ios_base::openmode m = std::ios_base::in | std::ios_base::out);
  97. virtual std::streampos seekpos( std::streampos pos,
  98. std::ios_base::openmode m = std::ios_base::in | std::ios_base::out);
  99. // Get
  100. virtual int underflow();
  101. virtual int pbackfail(int c = EOF);
  102. // Put
  103. virtual int overflow(int c = EOF);
  104. virtual int sync();
  105. private:
  106. std::vector<char> buffer_out_;
  107. std::vector<char> buffer_in_;
  108. size_t buffer_size_;
  109. struct _data;
  110. hold_ptr<_data> d; // for future use
  111. std::auto_ptr<io_device> device_auto_ptr_;
  112. io_device *device_;
  113. };
  114. } // booster
  115. #endif