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.

277 lines
6.7 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "coerciblevariant_t.h"
  8. #include "entitylist.h"
  9. // memdbgon must be the last include file in a .cpp file!!!
  10. #include "tier0/memdbgon.h"
  11. #pragma warning(disable: 4800) // yes, we really do want to coerce ints to bools,
  12. // that's the whole point of this class.
  13. coerciblevariant_t::~coerciblevariant_t()
  14. {
  15. if (szVal)
  16. delete[] szVal;
  17. }
  18. // Note: the constructor and SetXXX() functions are
  19. // almost identical, except that the Sets() have a Void()
  20. // step, because they may need to wipe out a string that's
  21. // been allocated.
  22. coerciblevariant_t::coerciblevariant_t( bool b ) : szVal(NULL)
  23. {
  24. // do the bool, int, and float conversions right now, set the flags
  25. bVal = b;
  26. iVal = b ? 1 : 0;
  27. flVal = b ? 1.0f : 0.0f;
  28. m_bvInitFields = kINIT_BOOLEAN | kINIT_INT | kINIT_FLOAT ;
  29. fieldType = FIELD_BOOLEAN;
  30. }
  31. void coerciblevariant_t::SetBool( bool b )
  32. {
  33. Void();
  34. // do the bool, int, and float conversions right now, set the flags
  35. bVal = b;
  36. iVal = b ? 1 : 0;
  37. flVal = b ? 1.0f : 0.0f;
  38. m_bvInitFields = kINIT_BOOLEAN | kINIT_INT | kINIT_FLOAT ;
  39. fieldType = FIELD_BOOLEAN;
  40. }
  41. coerciblevariant_t::coerciblevariant_t( int i ) : szVal(NULL)
  42. {
  43. // do the bool, int, and float conversions right now, set the flags
  44. bVal = i;
  45. iVal = i;
  46. flVal = i;
  47. m_bvInitFields = kINIT_BOOLEAN | kINIT_INT | kINIT_FLOAT ;
  48. fieldType = FIELD_INTEGER;
  49. }
  50. void coerciblevariant_t::SetInt( int i )
  51. {
  52. Void();
  53. // do the bool, int, and float conversions right now, set the flags
  54. bVal = i;
  55. iVal = i;
  56. flVal = i;
  57. m_bvInitFields = kINIT_BOOLEAN | kINIT_INT | kINIT_FLOAT ;
  58. fieldType = FIELD_INTEGER;
  59. }
  60. coerciblevariant_t::coerciblevariant_t( float f ) : szVal(NULL)
  61. {
  62. // do the bool, int, and float conversions right now, set the flags
  63. bVal = f;
  64. iVal = f;
  65. flVal = f;
  66. m_bvInitFields = kINIT_BOOLEAN | kINIT_INT | kINIT_FLOAT ;
  67. fieldType = FIELD_FLOAT;
  68. }
  69. void coerciblevariant_t::SetFloat( float f )
  70. {
  71. Void();
  72. // do the bool, int, and float conversions right now, set the flags
  73. bVal = f;
  74. iVal = f;
  75. flVal = f;
  76. m_bvInitFields = kINIT_BOOLEAN | kINIT_INT | kINIT_FLOAT ;
  77. fieldType = FIELD_FLOAT;
  78. }
  79. coerciblevariant_t::coerciblevariant_t( const char * str )
  80. {
  81. SetString(str);
  82. }
  83. void coerciblevariant_t::SetString( const char * str )
  84. {
  85. Void();
  86. // make a copy of the string for me.
  87. szVal = new char[DEFAULT_VARIANT_STRING_SIZE];
  88. Q_strncpy( szVal, str, DEFAULT_VARIANT_STRING_SIZE );
  89. m_bvInitFields = kINIT_STRING;
  90. fieldType = FIELD_STRING;
  91. }
  92. coerciblevariant_t::coerciblevariant_t( const EHANDLE &handle )
  93. : eVal(handle), fieldType(FIELD_EHANDLE), m_bvInitFields(kINIT_EHANDLE), szVal(NULL)
  94. {};
  95. coerciblevariant_t::coerciblevariant_t( CBaseEntity *ent )
  96. : eVal( ent ), fieldType(FIELD_EHANDLE), m_bvInitFields(kINIT_EHANDLE), szVal(NULL)
  97. {};
  98. void coerciblevariant_t::SetEntity( CBaseEntity *val )
  99. {
  100. Void();
  101. eVal = val;
  102. m_bvInitFields = kINIT_EHANDLE;
  103. fieldType = FIELD_EHANDLE;
  104. }
  105. // Get me as a string
  106. const char * coerciblevariant_t::String( void )
  107. {
  108. if ( (m_bvInitFields & kINIT_STRING) == 0 )
  109. {
  110. // need to initialize the string.
  111. AssertMsg(szVal == NULL, "A variant tried to convert itself to a string when it already was one.");
  112. // convert from the appropriate native type
  113. switch (fieldType)
  114. {
  115. case FIELD_BOOLEAN:
  116. szVal = new char[2];
  117. szVal[0] = bVal ? '1' : '0';
  118. szVal[1] = '\0';
  119. break;
  120. case FIELD_INTEGER:
  121. szVal = new char[24];
  122. Q_snprintf( szVal, 24, "%d", iVal );
  123. break;
  124. case FIELD_FLOAT:
  125. szVal = new char[32];
  126. Q_snprintf( szVal, 32, "%.3f", flVal );
  127. break;
  128. case FIELD_EHANDLE: // entity name, or ""
  129. {
  130. CBaseEntity *ent = eVal.Get();
  131. if (ent)
  132. {
  133. szVal = new char[DEFAULT_VARIANT_STRING_SIZE];
  134. Q_strncpy( szVal, ent->GetEntityName().ToCStr(), DEFAULT_VARIANT_STRING_SIZE );
  135. }
  136. else
  137. {
  138. szVal = new char[1];
  139. szVal[0] = 0;
  140. }
  141. break;
  142. }
  143. case FIELD_STRING:
  144. AssertMsg( false, "Tried to convert a variant from string to string?!" );
  145. break;
  146. default:
  147. AssertMsg1( false, "Tried to convert a variant from unknown field type %d", fieldType );
  148. szVal = new char[1];
  149. szVal[0] = 0;
  150. break;
  151. }
  152. m_bvInitFields |= kINIT_STRING;
  153. }
  154. return szVal;
  155. }
  156. float coerciblevariant_t::ConvertFloat() const
  157. {
  158. // convert from salient native type to a float.
  159. switch (fieldType)
  160. {
  161. case FIELD_STRING:
  162. // try to turn the string into a float
  163. return atof(szVal);
  164. case FIELD_EHANDLE:
  165. AssertMsg(false, "Variant tried to convert from EHANDLE to float?!" );
  166. return eVal.Get() ? 1 : 0;
  167. case FIELD_FLOAT:
  168. case FIELD_INTEGER:
  169. case FIELD_BOOLEAN:
  170. AssertMsg( false, "Can't-happen: coercible variant tried to convert from float to a number, which should already have been done" );
  171. return flVal;
  172. default:
  173. AssertMsg1( false, "Tried to convert a variant from unknown field type %d", fieldType );
  174. return 0;
  175. }
  176. }
  177. int coerciblevariant_t::ConvertInt() const
  178. {
  179. // convert from salient native type to an int.
  180. switch (fieldType)
  181. {
  182. case FIELD_STRING:
  183. // try to turn the string into an int
  184. return atoi(szVal);
  185. case FIELD_EHANDLE:
  186. AssertMsg(false, "Variant tried to convert from EHANDLE to int?!" );
  187. return eVal.Get() ? 1 : 0;
  188. case FIELD_FLOAT:
  189. case FIELD_INTEGER:
  190. case FIELD_BOOLEAN:
  191. AssertMsg( false, "Can't-happen: coercible variant tried to convert from int to a number, which should already have been done" );
  192. return iVal;
  193. default:
  194. AssertMsg1( false, "Tried to convert a variant from unknown field type %d", fieldType );
  195. return 0;
  196. }
  197. }
  198. bool coerciblevariant_t::ConvertBool() const
  199. {
  200. // convert from salient native type to an bool.
  201. switch (fieldType)
  202. {
  203. case FIELD_STRING:
  204. // try to turn the string into an int
  205. return atoi(szVal);
  206. case FIELD_EHANDLE:
  207. return eVal.Get();
  208. case FIELD_FLOAT:
  209. case FIELD_INTEGER:
  210. case FIELD_BOOLEAN:
  211. AssertMsg( false, "Can't-happen: coercible variant tried to convert from int to a number, which should already have been done" );
  212. return iVal;
  213. default:
  214. AssertMsg1( false, "Tried to convert a variant from unknown field type %d", fieldType );
  215. return 0;
  216. }
  217. }
  218. CBaseEntity *coerciblevariant_t::ConvertEntity() const
  219. {
  220. // convert from salient native type to an entity pointer.
  221. switch (fieldType)
  222. {
  223. case FIELD_STRING:
  224. // try to look up the entity by name
  225. return gEntList.FindEntityByName(NULL, szVal);
  226. case FIELD_EHANDLE:
  227. return eVal.Get();
  228. case FIELD_FLOAT:
  229. case FIELD_INTEGER:
  230. case FIELD_BOOLEAN:
  231. AssertMsg( false, "Coercible variant tried to turn a number into an entity, which is just plain wierd." );
  232. return NULL;
  233. default:
  234. AssertMsg1( false, "Tried to convert a variant from unknown field type %d", fieldType );
  235. return NULL;
  236. }
  237. }