llmemorystream.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**
  2. * @file llmemorystream.h
  3. * @author Phoenix
  4. * @date 2005-06-03
  5. * @brief Implementation of a simple fixed memory stream
  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_LLMEMORYSTREAM_H
  35. #define LL_LLMEMORYSTREAM_H
  36. // This is a simple but effective optimization when you want to treat a chunk
  37. // of memory as an istream. I wrote this to avoid turning a buffer into a
  38. // string, and then throwing the string into an iostringstream just to parse it
  39. // into another datatype, eg, LLSD.
  40. // The memory passed in is NOT owned by an instance. The caller must be careful
  41. // to always pass in a valid memory location that exists for at least as long
  42. // as this streambuf.
  43. #include <iostream>
  44. #include "llpreprocessor.h"
  45. #include "stdtypes.h"
  46. // LLMemoryStreamBuf class
  47. class LLMemoryStreamBuf : public std::streambuf
  48. {
  49. public:
  50. LL_INLINE LLMemoryStreamBuf(const U8* start, S32 length)
  51. {
  52. reset(start, length);
  53. }
  54. LL_INLINE void reset(const U8* start, S32 length)
  55. {
  56. setg((char*)start, (char*)start, (char*)start + length);
  57. }
  58. protected:
  59. LL_INLINE int underflow()
  60. {
  61. if (gptr() >= egptr())
  62. {
  63. return EOF;
  64. }
  65. return *gptr();
  66. }
  67. };
  68. // LLMemoryStream class
  69. class LLMemoryStream : public std::istream
  70. {
  71. public:
  72. LL_INLINE LLMemoryStream(const U8* start, S32 length)
  73. : std::istream(&mStreamBuf),
  74. mStreamBuf(start, length)
  75. {
  76. }
  77. protected:
  78. LLMemoryStreamBuf mStreamBuf;
  79. };
  80. #endif // LL_LLMEMORYSTREAM_H