llbufferstream.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**
  2. * @file llbufferstream.h
  3. * @author Phoenix
  4. * @date 2005-10-10
  5. * @brief Classes to treat an LLBufferArray as a c++ iostream.
  6. *
  7. * $LicenseInfo:firstyear=2005&license=viewergpl$
  8. *
  9. * Copyright (c) 2005-2009, Linden Research, Inc.
  10. *
  11. * Second Life Viewer Source Code
  12. * The source code in this file ("Source Code") is provided by Linden Lab
  13. * to you under the terms of the GNU General Public License, version 2.0
  14. * ("GPL"), unless you have obtained a separate licensing agreement
  15. * ("Other License"), formally executed by you and Linden Lab. Terms of
  16. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  17. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  18. *
  19. * There are special exceptions to the terms and conditions of the GPL as
  20. * it is applied to this Source Code. View the full text of the exception
  21. * in the file doc/FLOSS-exception.txt in this software distribution, or
  22. * online at
  23. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  24. *
  25. * By copying, modifying or distributing this software, you acknowledge
  26. * that you have read and understood your obligations described above,
  27. * and agree to abide by those obligations.
  28. *
  29. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  30. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  31. * COMPLETENESS OR PERFORMANCE.
  32. * $/LicenseInfo$
  33. */
  34. #ifndef LL_LLBUFFERSTREAM_H
  35. #define LL_LLBUFFERSTREAM_H
  36. #include <iosfwd>
  37. #include <iostream>
  38. #include "llbuffer.h"
  39. /**
  40. * @class LLBufferStreamBuf
  41. * @brief This implements the buffer wrapper for an istream
  42. *
  43. * The buffer array passed in is not owned by the stream buf object.
  44. */
  45. class LLBufferStreamBuf final : public std::streambuf
  46. {
  47. public:
  48. LLBufferStreamBuf(const LLChannelDescriptors& channels,
  49. LLBufferArray* buffer);
  50. virtual ~LLBufferStreamBuf();
  51. protected:
  52. typedef std::streambuf::pos_type pos_type;
  53. typedef std::streambuf::off_type off_type;
  54. // streambuf vrtual implementations
  55. // Called when we hit the end of input. Returns the character at the
  56. // current position or EOF.
  57. int underflow() override;
  58. // Called when we hit the end of output
  59. // - c: the character to store at the current put position
  60. // Returns EOF if the function failed. Any other value on success.
  61. int overflow(int c) override;
  62. // Synchronizes the buffer.Returns 0 on success or -1 on failure.
  63. int sync() override;
  64. // Seeks to an offset position in a stream.
  65. // - off: offset value relative to way paramter
  66. // - way: the seek direction. One of ios::beg, ios::cur, and ios::end.
  67. // - which: which pointer to modify. One of ios::in, ios::out, or both
  68. // masked together.
  69. // Returns the new position or an invalid position on failure.
  70. pos_type seekoff(off_type off, std::ios::seekdir way,
  71. std::ios::openmode which) override;
  72. protected:
  73. // This channels we are working on.
  74. LLChannelDescriptors mChannels;
  75. // The buffer we work on
  76. LLBufferArray* mBuffer;
  77. };
  78. /**
  79. * @class LLBufferStream
  80. * @brief This implements an istream based wrapper around an LLBufferArray.
  81. *
  82. * This class does not own the buffer array, and does not hold a shared pointer
  83. * to it. Since the class itself is fairly ligthweight, just make one on the
  84. * stack when needed and let it fall out of scope.
  85. */
  86. class LLBufferStream final : public std::iostream
  87. {
  88. public:
  89. LLBufferStream(const LLChannelDescriptors& channels,
  90. LLBufferArray* buffer);
  91. protected:
  92. LLBufferStreamBuf mStreamBuf;
  93. };
  94. #endif // LL_LLBUFFERSTREAM_H