ConcurrentStream.h
上传用户:kx_jwh
上传日期:2021-09-03
资源大小:76k
文件大小:3k
源码类别:

STL

开发平台:

Visual C++

  1. /* vim: set tabstop=4 : */
  2. #ifndef __ConcurrentStream_h__
  3. #define __ConcurrentStream_h__
  4. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  5. # pragma once
  6. #endif
  7. #include "../stdtypes.h"
  8. #include "../thread/lockable.h"
  9. #include "../thread/LockSentry.h"
  10. namespace febird {
  11. template<class StreamClass>
  12. class ConcurrentStreamWrapper : public StreamClass
  13. {
  14. ConcurrentStreamWrapper(const ConcurrentStreamWrapper&);
  15. ConcurrentStreamWrapper& operator=(const ConcurrentStreamWrapper&);
  16. protected:
  17. thread::MutexLock lock;
  18. public:
  19. ConcurrentStreamWrapper() {}
  20. template<class T1>
  21. ConcurrentStreamWrapper(T1 p1) : StreamClass(p1) {}
  22. template<class T1, class T2>
  23. ConcurrentStreamWrapper(T1 p1, T2 p2) : StreamClass(p1, p2) {}
  24. template<class T1, class T2, class T3>
  25. ConcurrentStreamWrapper(T1 p1, T2 p2, T3 p3) : StreamClass(p1, p2, p3) {}
  26. int getByte()
  27. {
  28. thread::MutexLock::Sentry sentry(lock);
  29. return StreamClass::getByte();
  30. }
  31. byte readByte()
  32. {
  33. thread::MutexLock::Sentry sentry(lock);
  34. return StreamClass::readByte();
  35. }
  36. void writeByte(byte b) 
  37. {
  38. thread::MutexLock::Sentry sentry(lock);
  39. return StreamClass::writeByte(b);
  40. }
  41. size_t read(void* data, size_t length)
  42. {
  43. thread::MutexLock::Sentry sentry(lock);
  44. return StreamClass::read(data, length);
  45. }
  46. size_t write(const void* data, size_t length)
  47. {
  48. thread::MutexLock::Sentry sentry(lock);
  49. return StreamClass::write(data, length);
  50. }
  51. void flush()
  52. {
  53. thread::MutexLock::Sentry sentry(lock);
  54. StreamClass::flush();
  55. }
  56. bool eof()
  57. {
  58. thread::MutexLock::Sentry sentry(lock);
  59. return StreamClass::eof();
  60. }
  61. bool seek(size_t newPos)
  62. {
  63. thread::MutexLock::Sentry sentry(lock);
  64. return StreamClass::seek(newPos);
  65. }
  66. bool seek(long offset, int origin)
  67. {
  68. thread::MutexLock::Sentry sentry(lock);
  69. return StreamClass::seek(offset, origin);
  70. }
  71. size_t tell()
  72. {
  73. thread::MutexLock::Sentry sentry(lock);
  74. return StreamClass::tell();
  75. }
  76. // for divided dual IO
  77. bool seekp(size_t newPos)
  78. {
  79. thread::MutexLock::Sentry sentry(lock);
  80. return StreamClass::seekp(newPos);
  81. }
  82. bool seekp(long offset, int origin)
  83. {
  84. thread::MutexLock::Sentry sentry(lock);
  85. return StreamClass::seekp(offset, origin);
  86. }
  87. size_t tellp()
  88. {
  89. thread::MutexLock::Sentry sentry(lock);
  90. return StreamClass::tellp();
  91. }
  92. bool seekg(size_t newPos)
  93. {
  94. thread::MutexLock::Sentry sentry(lock);
  95. return StreamClass::seekg(newPos);
  96. }
  97. bool seekg(long offset, int origin)
  98. {
  99. thread::MutexLock::Sentry sentry(lock);
  100. return StreamClass::seekg(offset, origin);
  101. }
  102. size_t tellg()
  103. {
  104. thread::MutexLock::Sentry sentry(lock);
  105. return StreamClass::tellg();
  106. }
  107. };
  108. } // namespace febird
  109. #endif // __ConcurrentStream_h__