Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

197 lines
4.8 KiB

  1. //====== Copyright � 1996-2005, Valve Corporation, All rights reserved. =======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. // Utilities for serialization/unserialization buffer
  8. //=============================================================================//
  9. #ifndef UTLBUFFERUTIL_H
  10. #define UTLBUFFERUTIL_H
  11. #ifdef _WIN32
  12. #pragma once
  13. #endif
  14. #include "tier1/utlvector.h"
  15. #include "tier1/utlbuffer.h"
  16. //-----------------------------------------------------------------------------
  17. // Forward declarations
  18. //-----------------------------------------------------------------------------
  19. class Vector2D;
  20. class Vector;
  21. class Vector4D;
  22. class QAngle;
  23. class Quaternion;
  24. class VMatrix;
  25. class Color;
  26. class CUtlBinaryBlock;
  27. class CUtlString;
  28. class CUtlCharConversion;
  29. class CUtlSymbolLarge;
  30. //-----------------------------------------------------------------------------
  31. // For string serialization, set the delimiter rules
  32. //-----------------------------------------------------------------------------
  33. void SetSerializationDelimiter( CUtlCharConversion *pConv );
  34. void SetSerializationArrayDelimiter( const char *pDelimiter );
  35. //-----------------------------------------------------------------------------
  36. // Standard serialization methods for basic types
  37. //-----------------------------------------------------------------------------
  38. bool Serialize( CUtlBuffer &buf, const bool &src );
  39. bool Unserialize( CUtlBuffer &buf, bool &dest );
  40. bool Serialize( CUtlBuffer &buf, const int &src );
  41. bool Unserialize( CUtlBuffer &buf, int &dest );
  42. bool Serialize( CUtlBuffer &buf, const float &src );
  43. bool Unserialize( CUtlBuffer &buf, float &dest );
  44. bool Serialize( CUtlBuffer &buf, const Vector2D &src );
  45. bool Unserialize( CUtlBuffer &buf, Vector2D &dest );
  46. bool Serialize( CUtlBuffer &buf, const Vector &src );
  47. bool Unserialize( CUtlBuffer &buf, Vector &dest );
  48. bool Serialize( CUtlBuffer &buf, const Vector4D &src );
  49. bool Unserialize( CUtlBuffer &buf, Vector4D &dest );
  50. bool Serialize( CUtlBuffer &buf, const QAngle &src );
  51. bool Unserialize( CUtlBuffer &buf, QAngle &dest );
  52. bool Serialize( CUtlBuffer &buf, const Quaternion &src );
  53. bool Unserialize( CUtlBuffer &buf, Quaternion &dest );
  54. bool Serialize( CUtlBuffer &buf, const VMatrix &src );
  55. bool Unserialize( CUtlBuffer &buf, VMatrix &dest );
  56. bool Serialize( CUtlBuffer &buf, const Color &src );
  57. bool Unserialize( CUtlBuffer &buf, Color &dest );
  58. bool Serialize( CUtlBuffer &buf, const CUtlBinaryBlock &src );
  59. bool Unserialize( CUtlBuffer &buf, CUtlBinaryBlock &dest );
  60. bool Serialize( CUtlBuffer &buf, const CUtlString &src );
  61. bool Unserialize( CUtlBuffer &buf, CUtlString &dest );
  62. bool Serialize( CUtlBuffer &buf, const CUtlSymbolLarge &src );
  63. // There is explicitly no unserialize of CUtlSymbolLarge,
  64. // it requires adding the a string to a specific symbol table.
  65. //-----------------------------------------------------------------------------
  66. // You can use this to check if a type serializes on multiple lines
  67. //-----------------------------------------------------------------------------
  68. template< class T >
  69. inline bool SerializesOnMultipleLines()
  70. {
  71. return false;
  72. }
  73. template< >
  74. inline bool SerializesOnMultipleLines<VMatrix>()
  75. {
  76. return true;
  77. }
  78. template< >
  79. inline bool SerializesOnMultipleLines<CUtlBinaryBlock>()
  80. {
  81. return true;
  82. }
  83. //-----------------------------------------------------------------------------
  84. // Vector serialization
  85. //-----------------------------------------------------------------------------
  86. template< class T >
  87. bool Serialize( CUtlBuffer &buf, const CUtlVector<T> &src )
  88. {
  89. extern const char *s_pUtlBufferUtilArrayDelim;
  90. int nCount = src.Count();
  91. if ( !buf.IsText() )
  92. {
  93. buf.PutInt( nCount );
  94. for ( int i = 0; i < nCount; ++i )
  95. {
  96. ::Serialize( buf, src[i] );
  97. }
  98. return buf.IsValid();
  99. }
  100. if ( !SerializesOnMultipleLines<T>() )
  101. {
  102. buf.PutChar('\n');
  103. for ( int i = 0; i < nCount; ++i )
  104. {
  105. ::Serialize( buf, src[i] );
  106. if ( s_pUtlBufferUtilArrayDelim && (i != nCount-1) )
  107. {
  108. buf.PutString( s_pUtlBufferUtilArrayDelim );
  109. }
  110. buf.PutChar('\n');
  111. }
  112. }
  113. else
  114. {
  115. for ( int i = 0; i < nCount; ++i )
  116. {
  117. ::Serialize( buf, src[i] );
  118. if ( s_pUtlBufferUtilArrayDelim && (i != nCount-1) )
  119. {
  120. buf.PutString( s_pUtlBufferUtilArrayDelim );
  121. }
  122. buf.PutChar(' ');
  123. }
  124. }
  125. return buf.IsValid();
  126. }
  127. template< class T >
  128. bool Unserialize( CUtlBuffer &buf, CUtlVector<T> &dest )
  129. {
  130. dest.RemoveAll();
  131. MEM_ALLOC_CREDIT_FUNCTION();
  132. if ( !buf.IsText() )
  133. {
  134. int nCount = buf.GetInt();
  135. if ( nCount )
  136. {
  137. dest.EnsureCapacity( nCount );
  138. for ( int i = 0; i < nCount; ++i )
  139. {
  140. VerifyEquals( dest.AddToTail(), i );
  141. if ( !::Unserialize( buf, dest[i] ) )
  142. return false;
  143. }
  144. }
  145. return buf.IsValid();
  146. }
  147. while ( true )
  148. {
  149. buf.EatWhiteSpace();
  150. if ( !buf.IsValid() )
  151. break;
  152. int i = dest.AddToTail( );
  153. if ( ! ::Unserialize( buf, dest[i] ) )
  154. return false;
  155. }
  156. return true;
  157. }
  158. #endif // UTLBUFFERUTIL_H