Serializer.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 "StringHash.h"
  25. #include "Map.h"
  26. #include "Vector.h"
  27. class Color;
  28. class IntRect;
  29. class IntVector2;
  30. class Quaternion;
  31. class Rect;
  32. class Variant;
  33. class Vector2;
  34. class Vector3;
  35. class Vector4;
  36. struct ResourceRef;
  37. struct ResourceRefList;
  38. typedef Vector<Variant> VariantVector;
  39. typedef Map<ShortStringHash, Variant> VariantMap;
  40. /// Abstract stream for writing
  41. class Serializer
  42. {
  43. public:
  44. /// Destruct
  45. virtual ~Serializer();
  46. /// Write bytes to the stream. Return number of bytes actually written
  47. virtual unsigned Write(const void* data, unsigned size) = 0;
  48. /// Write a 32-bit integer
  49. bool WriteInt(int value);
  50. /// Write a 16-bit integer
  51. bool WriteShort(short value);
  52. /// Write an 8-bit integer
  53. bool WriteByte(signed char value);
  54. /// Write a 32-bit unsigned integer
  55. bool WriteUInt(unsigned value);
  56. /// Write a 16-bit unsigned integer
  57. bool WriteUShort(unsigned short value);
  58. /// Write an 8-bit unsigned integer
  59. bool WriteUByte(unsigned char value);
  60. /// Write a bool
  61. bool WriteBool(bool value);
  62. /// Write a float
  63. bool WriteFloat(float value);
  64. /// Write an IntRect
  65. bool WriteIntRect(const IntRect& value);
  66. /// Write an IntVector2
  67. bool WriteIntVector2(const IntVector2& value);
  68. /// Write a Rect
  69. bool WriteRect(const Rect& value);
  70. /// Write a Vector2
  71. bool WriteVector2(const Vector2& value);
  72. /// Write a Vector3
  73. bool WriteVector3(const Vector3& value);
  74. /// Write a Vector3 packed into 3 x 16 bits with the specified maximum absolute range
  75. bool WritePackedVector3(const Vector3& value, float maxAbsCoord);
  76. /// Write a Vector4
  77. bool WriteVector4(const Vector4& value);
  78. /// Write a Quaternion
  79. bool WriteQuaternion(const Quaternion& value);
  80. /// Write a Quaternion with each component packed in 16 bits
  81. bool WritePackedQuaternion(const Quaternion& value);
  82. /// Write a Color
  83. bool WriteColor(const Color& value);
  84. /// Write a BoundingBox
  85. bool WriteBoundingBox(const BoundingBox& value);
  86. /// Write a null-terminated string
  87. bool WriteString(const String& value);
  88. /// Write a four-letter ID. If the string is not long enough, spaces will be appended
  89. bool WriteID(const String& value);
  90. /// Write a 32-bit StringHash
  91. bool WriteStringHash(const StringHash& value);
  92. /// Write a 16-bit ShortStringHash
  93. bool WriteShortStringHash(const ShortStringHash& value);
  94. /// Write a buffer, with size encoded as VLE
  95. bool WriteBuffer(const Vector<unsigned char>& buffer);
  96. /// Write a resource reference
  97. bool WriteResourceRef(const ResourceRef& value);
  98. /// Write a resource reference list
  99. bool WriteResourceRefList(const ResourceRefList& value);
  100. /// Write a Variant
  101. bool WriteVariant(const Variant& value);
  102. /// Write a Variant without the type information
  103. bool WriteVariantData(const Variant& value);
  104. /// Write a variant vector
  105. bool WriteVariantVector(const VariantVector& value);
  106. /// Write a variant map
  107. bool WriteVariantMap(const VariantMap& value);
  108. /// Write a variable-length encoded unsigned integer, which can use 29 bits maximum
  109. bool WriteVLE(unsigned value);
  110. /// Write a text line. Char codes 13 & 10 will be automatically appended.
  111. bool WriteLine(const String& value);
  112. };