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.

241 lines
4.5 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: A class to wrap data for transport over a boundary like a thread
  4. // or window.
  5. //
  6. //=============================================================================
  7. #include "tier1/utlstring.h"
  8. #include "tier0/basetypes.h"
  9. #ifndef UTLENVELOPE_H
  10. #define UTLENVELOPE_H
  11. #if defined( _WIN32 )
  12. #pragma once
  13. #endif
  14. //-----------------------------------------------------------------------------
  15. class CUtlDataEnvelope
  16. {
  17. public:
  18. CUtlDataEnvelope( const void *pData, int nBytes );
  19. CUtlDataEnvelope( const CUtlDataEnvelope &from );
  20. ~CUtlDataEnvelope();
  21. CUtlDataEnvelope &operator=( const CUtlDataEnvelope &from );
  22. operator void *();
  23. operator void *() const;
  24. private:
  25. void Assign( const void *pData, int nBytes );
  26. void Assign( const CUtlDataEnvelope &from );
  27. void Purge();
  28. // TODO: switch to a reference counted array?
  29. union
  30. {
  31. byte *m_pData;
  32. byte m_data[4];
  33. };
  34. int m_nBytes;
  35. };
  36. //-----------------------------------------------------------------------------
  37. template <typename T>
  38. class CUtlEnvelope : protected CUtlDataEnvelope
  39. {
  40. public:
  41. CUtlEnvelope( const T *pData, int nElems = 1 );
  42. CUtlEnvelope( const CUtlEnvelope<T> &from );
  43. CUtlEnvelope<T> &operator=( const CUtlEnvelope<T> &from );
  44. operator T *();
  45. operator T *() const;
  46. operator void *();
  47. operator void *() const;
  48. };
  49. //-----------------------------------------------------------------------------
  50. template <>
  51. class CUtlEnvelope<const char *>
  52. {
  53. public:
  54. CUtlEnvelope( const char *pData )
  55. {
  56. m_string = pData;
  57. }
  58. CUtlEnvelope( const CUtlEnvelope<const char *> &from )
  59. {
  60. m_string = from.m_string;
  61. }
  62. CUtlEnvelope<const char *> &operator=( const CUtlEnvelope<const char *> &from )
  63. {
  64. m_string = from.m_string;
  65. return *this;
  66. }
  67. operator char *()
  68. {
  69. return (char *) m_string.Get();
  70. }
  71. operator char *() const
  72. {
  73. return (char *) m_string.Get();
  74. }
  75. operator void *()
  76. {
  77. return (void *) m_string.Get();
  78. }
  79. operator void *() const
  80. {
  81. return (void *) m_string.Get();
  82. }
  83. private:
  84. CUtlString m_string;
  85. };
  86. //-----------------------------------------------------------------------------
  87. #include "tier0/memdbgon.h"
  88. inline void CUtlDataEnvelope::Assign( const void *pData, int nBytes )
  89. {
  90. if ( pData )
  91. {
  92. m_nBytes = nBytes;
  93. if ( m_nBytes > 4 )
  94. {
  95. m_pData = new byte[nBytes];
  96. memcpy( m_pData, pData, nBytes );
  97. }
  98. else
  99. {
  100. memcpy( m_data, pData, nBytes );
  101. }
  102. }
  103. else
  104. {
  105. m_pData = NULL;
  106. m_nBytes = 0;
  107. }
  108. }
  109. inline void CUtlDataEnvelope::Assign( const CUtlDataEnvelope &from )
  110. {
  111. Assign( from.operator void *(), from.m_nBytes );
  112. }
  113. inline void CUtlDataEnvelope::Purge()
  114. {
  115. if (m_nBytes > 4)
  116. delete [] m_pData;
  117. m_nBytes = 0;
  118. }
  119. inline CUtlDataEnvelope::CUtlDataEnvelope( const void *pData, int nBytes )
  120. {
  121. Assign( pData, nBytes );
  122. }
  123. inline CUtlDataEnvelope::CUtlDataEnvelope( const CUtlDataEnvelope &from )
  124. {
  125. Assign( from );
  126. }
  127. inline CUtlDataEnvelope::~CUtlDataEnvelope()
  128. {
  129. Purge();
  130. }
  131. inline CUtlDataEnvelope &CUtlDataEnvelope::operator=( const CUtlDataEnvelope &from )
  132. {
  133. Purge();
  134. Assign( from );
  135. return *this;
  136. }
  137. inline CUtlDataEnvelope::operator void *()
  138. {
  139. if ( !m_nBytes )
  140. {
  141. return NULL;
  142. }
  143. return ( m_nBytes > 4) ? m_pData : m_data;
  144. }
  145. inline CUtlDataEnvelope::operator void *() const
  146. {
  147. if ( !m_nBytes )
  148. {
  149. return NULL;
  150. }
  151. return ( m_nBytes > 4) ? (void *)m_pData : (void *)m_data;
  152. }
  153. //-----------------------------------------------------------------------------
  154. template <typename T>
  155. inline CUtlEnvelope<T>::CUtlEnvelope( const T *pData, int nElems )
  156. : CUtlDataEnvelope( pData, sizeof(T) * nElems )
  157. {
  158. }
  159. template <typename T>
  160. inline CUtlEnvelope<T>::CUtlEnvelope( const CUtlEnvelope<T> &from )
  161. : CUtlDataEnvelope( from )
  162. {
  163. }
  164. template <typename T>
  165. inline CUtlEnvelope<T> &CUtlEnvelope<T>::operator=( const CUtlEnvelope<T> &from )
  166. {
  167. CUtlDataEnvelope::operator=( from );
  168. return *this;
  169. }
  170. template <typename T>
  171. inline CUtlEnvelope<T>::operator T *()
  172. {
  173. return (T *)CUtlDataEnvelope::operator void *();
  174. }
  175. template <typename T>
  176. inline CUtlEnvelope<T>::operator T *() const
  177. {
  178. return (T *)( (const_cast<CUtlEnvelope<T> *>(this))->operator T *() );
  179. }
  180. template <typename T>
  181. inline CUtlEnvelope<T>::operator void *()
  182. {
  183. return CUtlDataEnvelope::operator void *();
  184. }
  185. template <typename T>
  186. inline CUtlEnvelope<T>::operator void *() const
  187. {
  188. return ( (const_cast<CUtlEnvelope<T> *>(this))->operator void *() );
  189. }
  190. //-----------------------------------------------------------------------------
  191. #include "tier0/memdbgoff.h"
  192. #endif // UTLENVELOPE_H