Deserializer.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "BoundingBox.h"
  25. #include "Rect.h"
  26. #include "Variant.h"
  27. /// Abstract stream for reading
  28. class Deserializer
  29. {
  30. public:
  31. /// Construct with zero size
  32. Deserializer();
  33. /// Construct with defined size
  34. Deserializer(unsigned size);
  35. /// Destruct
  36. virtual ~Deserializer();
  37. /// Read bytes from the stream. Return number of bytes actually read
  38. virtual unsigned Read(void* dest, unsigned size) = 0;
  39. /// Set position from the beginning of the stream
  40. virtual unsigned Seek(unsigned position) = 0;
  41. /// Return name of the stream
  42. virtual const std::string& GetName() const;
  43. /// Return a checksum if applicable
  44. virtual unsigned GetChecksum();
  45. /// Return current position
  46. unsigned GetPosition() const { return position_; }
  47. /// Return size
  48. unsigned GetSize() const { return size_; }
  49. /// Return whether the end of stream has been reached
  50. bool IsEof() const { return position_ >= size_; }
  51. /// Read a 32-bit integer
  52. int ReadInt();
  53. /// Read a 16-bit integer
  54. short ReadShort();
  55. /// Read an 8-bit integer
  56. signed char ReadByte();
  57. /// Read a 32-bit unsigned integer
  58. unsigned ReadUInt();
  59. /// Read a 16-bit unsigned integer
  60. unsigned short ReadUShort();
  61. /// Read an 8-bit unsigned integer
  62. unsigned char ReadUByte();
  63. /// Read a bool
  64. bool ReadBool();
  65. /// Read a float
  66. float ReadFloat();
  67. /// Read an IntRect
  68. IntRect ReadIntRect();
  69. /// Read an IntVector2
  70. IntVector2 ReadIntVector2();
  71. /// Read a Rect
  72. Rect ReadRect();
  73. /// Read a Vector2
  74. Vector2 ReadVector2();
  75. /// Read a Vector3
  76. Vector3 ReadVector3();
  77. /// Read a Vector3 packed into 3 x 16 bits with the specified maximum absolute range
  78. Vector3 ReadPackedVector3(float maxAbsCoord);
  79. /// Read a Vector4
  80. Vector4 ReadVector4();
  81. /// Read a Quaternion
  82. Quaternion ReadQuaternion();
  83. /// Read a Quaternion with each component packed in 16 bits
  84. Quaternion ReadPackedQuaternion();
  85. /// Read a Color
  86. Color ReadColor();
  87. /// Read a BoundingBox
  88. BoundingBox ReadBoundingBox();
  89. /// Read a null-terminated string
  90. std::string ReadString();
  91. /// Read a four-letter ID
  92. std::string ReadID();
  93. /// Read a 32-bit StringHash
  94. StringHash ReadStringHash();
  95. /// Read a 16-bit ShortStringHash
  96. ShortStringHash ReadShortStringHash();
  97. /// Read a buffer with size encoded as VLE
  98. std::vector<unsigned char> ReadBuffer();
  99. /// Read a resource reference
  100. ResourceRef ReadResourceRef();
  101. /// Read a resource reference list
  102. ResourceRefList ReadResourceRefList();
  103. /// Read a Variant
  104. Variant ReadVariant();
  105. /// Read a Variant whose type is already known
  106. Variant ReadVariant(VariantType type);
  107. /// Read a vector of Variants
  108. VariantVector ReadVariantVector();
  109. /// Read a map of Variants
  110. VariantMap ReadVariantMap();
  111. /// Read a variable-length encoded unsigned integer, which can use 29 bits maximum
  112. unsigned ReadVLE();
  113. /// Read a text line
  114. std::string ReadLine();
  115. protected:
  116. /// Stream position
  117. unsigned position_;
  118. /// Stream size
  119. unsigned size_;
  120. };