Team Fortress 2 Source Code as on 22/4/2020
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.

192 lines
4.6 KiB

  1. //========= Copyright 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. //-----------------------------------------------------------------------------
  30. // For string serialization, set the delimiter rules
  31. //-----------------------------------------------------------------------------
  32. void SetSerializationDelimiter( CUtlCharConversion *pConv );
  33. void SetSerializationArrayDelimiter( const char *pDelimiter );
  34. //-----------------------------------------------------------------------------
  35. // Standard serialization methods for basic types
  36. //-----------------------------------------------------------------------------
  37. bool Serialize( CUtlBuffer &buf, const bool &src );
  38. bool Unserialize( CUtlBuffer &buf, bool &dest );
  39. bool Serialize( CUtlBuffer &buf, const int &src );
  40. bool Unserialize( CUtlBuffer &buf, int &dest );
  41. bool Serialize( CUtlBuffer &buf, const float &src );
  42. bool Unserialize( CUtlBuffer &buf, float &dest );
  43. bool Serialize( CUtlBuffer &buf, const Vector2D &src );
  44. bool Unserialize( CUtlBuffer &buf, Vector2D &dest );
  45. bool Serialize( CUtlBuffer &buf, const Vector &src );
  46. bool Unserialize( CUtlBuffer &buf, Vector &dest );
  47. bool Serialize( CUtlBuffer &buf, const Vector4D &src );
  48. bool Unserialize( CUtlBuffer &buf, Vector4D &dest );
  49. bool Serialize( CUtlBuffer &buf, const QAngle &src );
  50. bool Unserialize( CUtlBuffer &buf, QAngle &dest );
  51. bool Serialize( CUtlBuffer &buf, const Quaternion &src );
  52. bool Unserialize( CUtlBuffer &buf, Quaternion &dest );
  53. bool Serialize( CUtlBuffer &buf, const VMatrix &src );
  54. bool Unserialize( CUtlBuffer &buf, VMatrix &dest );
  55. bool Serialize( CUtlBuffer &buf, const Color &src );
  56. bool Unserialize( CUtlBuffer &buf, Color &dest );
  57. bool Serialize( CUtlBuffer &buf, const CUtlBinaryBlock &src );
  58. bool Unserialize( CUtlBuffer &buf, CUtlBinaryBlock &dest );
  59. bool Serialize( CUtlBuffer &buf, const CUtlString &src );
  60. bool Unserialize( CUtlBuffer &buf, CUtlString &dest );
  61. //-----------------------------------------------------------------------------
  62. // You can use this to check if a type serializes on multiple lines
  63. //-----------------------------------------------------------------------------
  64. template< class T >
  65. inline bool SerializesOnMultipleLines()
  66. {
  67. return false;
  68. }
  69. template< >
  70. inline bool SerializesOnMultipleLines<VMatrix>()
  71. {
  72. return true;
  73. }
  74. template< >
  75. inline bool SerializesOnMultipleLines<CUtlBinaryBlock>()
  76. {
  77. return true;
  78. }
  79. //-----------------------------------------------------------------------------
  80. // Vector serialization
  81. //-----------------------------------------------------------------------------
  82. template< class T >
  83. bool Serialize( CUtlBuffer &buf, const CUtlVector<T> &src )
  84. {
  85. extern const char *s_pUtlBufferUtilArrayDelim;
  86. int nCount = src.Count();
  87. if ( !buf.IsText() )
  88. {
  89. buf.PutInt( nCount );
  90. for ( int i = 0; i < nCount; ++i )
  91. {
  92. ::Serialize( buf, src[i] );
  93. }
  94. return buf.IsValid();
  95. }
  96. if ( !SerializesOnMultipleLines<T>() )
  97. {
  98. buf.PutChar('\n');
  99. for ( int i = 0; i < nCount; ++i )
  100. {
  101. ::Serialize( buf, src[i] );
  102. if ( s_pUtlBufferUtilArrayDelim && (i != nCount-1) )
  103. {
  104. buf.PutString( s_pUtlBufferUtilArrayDelim );
  105. }
  106. buf.PutChar('\n');
  107. }
  108. }
  109. else
  110. {
  111. for ( int i = 0; i < nCount; ++i )
  112. {
  113. ::Serialize( buf, src[i] );
  114. if ( s_pUtlBufferUtilArrayDelim && (i != nCount-1) )
  115. {
  116. buf.PutString( s_pUtlBufferUtilArrayDelim );
  117. }
  118. buf.PutChar(' ');
  119. }
  120. }
  121. return buf.IsValid();
  122. }
  123. template< class T >
  124. bool Unserialize( CUtlBuffer &buf, CUtlVector<T> &dest )
  125. {
  126. dest.RemoveAll();
  127. MEM_ALLOC_CREDIT_FUNCTION();
  128. if ( !buf.IsText() )
  129. {
  130. int nCount = buf.GetInt();
  131. if ( nCount )
  132. {
  133. dest.EnsureCapacity( nCount );
  134. for ( int i = 0; i < nCount; ++i )
  135. {
  136. VerifyEquals( dest.AddToTail(), i );
  137. if ( !::Unserialize( buf, dest[i] ) )
  138. return false;
  139. }
  140. }
  141. return buf.IsValid();
  142. }
  143. while ( true )
  144. {
  145. buf.EatWhiteSpace();
  146. if ( !buf.IsValid() )
  147. break;
  148. int i = dest.AddToTail( );
  149. if ( ! ::Unserialize( buf, dest[i] ) )
  150. return false;
  151. }
  152. return true;
  153. }
  154. #endif // UTLBUFFERUTIL_H