llcorebufferstream.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * @file llcorebufferstream.h
  3. * @brief Public-facing declaration for the BufferStream adapter class
  4. *
  5. * $LicenseInfo:firstyear=2012&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2012, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #ifndef _LLCORE_BUFFER_STREAM_H_
  27. #define _LLCORE_BUFFER_STREAM_H_
  28. #include <sstream>
  29. #include <cstdlib>
  30. #include "llcorebufferarray.h"
  31. // std::streambuf and std::iostream adapters for BufferArray objects.
  32. //
  33. // BufferArrayStreamBuf inherits std::streambuf and implements an unbuffered
  34. // interface for streambuf. This may or may not be the most time efficient
  35. // implementation and it is a little challenging.
  36. //
  37. // BufferArrayStream inherits std::iostream and will be the adapter object most
  38. // callers will be interested in (though it uses BufferArrayStreamBuf
  39. // internally). Instances allow for the usual streaming operators ('<<', '>>')
  40. // and serialization methods.
  41. //
  42. // Example of LLSD serialization to a BufferArray:
  43. //
  44. // BufferArray * ba = new BufferArray;
  45. // BufferArrayStream bas(ba);
  46. // LLSDSerialize::toXML(llsd, bas);
  47. // operationOnBufferArray(ba);
  48. // ba->release();
  49. // ba = NULL;
  50. // // operationOnBufferArray and bas are each holding
  51. // // references to the ba instance at this point.
  52. namespace LLCore
  53. {
  54. // =====================================================
  55. // BufferArrayStreamBuf
  56. // =====================================================
  57. // Adapter class to put a std::streambuf interface on a BufferArray
  58. //
  59. // Application developers will rarely be interested in anything other than the
  60. // constructor and even that will rarely be used except indirectly via the
  61. // @BufferArrayStream class. The choice of interfaces implemented yields a
  62. // bufferless adapter that doesn't used either the input or output pointer
  63. // triplets of the more common buffered implementations. This may or may not be
  64. // faster and that question could stand to be looked at sometime.
  65. class BufferArrayStreamBuf : public std::streambuf
  66. {
  67. public:
  68. // Constructor increments the reference count on the
  69. // BufferArray argument and calls release() on destruction.
  70. BufferArrayStreamBuf(BufferArray* array);
  71. ~BufferArrayStreamBuf() override;
  72. BufferArrayStreamBuf(const BufferArrayStreamBuf&) = delete;
  73. void operator=(const BufferArrayStreamBuf&) = delete;
  74. // Input interfaces from std::streambuf
  75. int_type underflow() override;
  76. int_type uflow() override;
  77. int_type pbackfail(int_type ch) override;
  78. std::streamsize showmanyc() override;
  79. // Output interfaces from std::streambuf
  80. int_type overflow(int c) override;
  81. std::streamsize xsputn(const char* src, std::streamsize count) override;
  82. // Common/misc interfaces from std::streambuf
  83. std::streampos seekoff(std::streamoff off, std::ios_base::seekdir way,
  84. std::ios_base::openmode which) override;
  85. protected:
  86. BufferArray* mBufferArray; // Ref counted
  87. const char* mReadBegin;
  88. const char* mReadCur;
  89. const char* mReadEnd;
  90. size_t mReadCurPos;
  91. S32 mReadCurBlock;
  92. size_t mWriteCurPos;
  93. };
  94. // =====================================================
  95. // BufferArrayStream
  96. // =====================================================
  97. // Adapter class that supplies streaming operators to BufferArray
  98. //
  99. // Provides a streaming adapter to an existing BufferArray instance so that the
  100. // convenient '<<' and '>>' conversions can be applied to a BufferArray. Very
  101. // convenient for LLSD serialization and parsing as well.
  102. class BufferArrayStream : public std::iostream
  103. {
  104. public:
  105. // Constructor increments the reference count on the BufferArray argument
  106. // and calls release() on destruction.
  107. BufferArrayStream(BufferArray* ba);
  108. protected:
  109. BufferArrayStream(const BufferArrayStream&);
  110. void operator=(const BufferArrayStream&);
  111. protected:
  112. BufferArrayStreamBuf mStreamBuf;
  113. };
  114. } // End namespace LLCore
  115. #endif // _LLCORE_BUFFER_STREAM_H_