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.

178 lines
4.2 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef COERCIBLEVARIANT_T_H
  8. #define COERCIBLEVARIANT_T_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "ehandle.h"
  13. class CBaseEntity;
  14. //
  15. // A variant class for use in response rule contexts.
  16. // It provides implicit conversion between types.
  17. // So, you can construct it as a string, but fetch it
  18. // as an int, and so on.
  19. // It caches these conversions to make them faster afterwards.
  20. // If you construct from a string, it will make a copy of
  21. // that string, so it's okay to pass in extemporaneous stack strings.
  22. //
  23. class coerciblevariant_t
  24. {
  25. // each of the types we store
  26. bool bVal;
  27. int iVal;
  28. float flVal;
  29. char * szVal; // generally this one is only computed on demand (because it eats memory)
  30. CHandle<CBaseEntity> eVal; // this can't be in the union because it has a constructor.
  31. // my native type -- but I can be implicitly converted to others.
  32. fieldtype_t fieldType;
  33. // store which types have been initialized
  34. enum
  35. {
  36. kINIT_BOOLEAN = ( 1 << 0 ),
  37. kINIT_INT = ( 1 << 1 ),
  38. kINIT_FLOAT = ( 1 << 2 ),
  39. kINIT_STRING = ( 1 << 3 ),
  40. kINIT_EHANDLE = ( 1 << 4 ),
  41. };
  42. unsigned int m_bvInitFields;
  43. enum
  44. {
  45. DEFAULT_VARIANT_STRING_SIZE = 64 // how big my allocated strings are, when I make them.
  46. };
  47. public:
  48. // constructor
  49. coerciblevariant_t() : fieldType(FIELD_VOID), iVal(0), bVal(false), szVal(NULL), flVal(0), m_bvInitFields(0) {}
  50. ~coerciblevariant_t();
  51. // assignment ctors (private because not used yet, please don't use them
  52. private:
  53. coerciblevariant_t(const coerciblevariant_t &src);
  54. coerciblevariant_t& operator=(const coerciblevariant_t &src);
  55. public:
  56. // convenience constructors
  57. coerciblevariant_t( bool b );
  58. coerciblevariant_t( const char * str );
  59. coerciblevariant_t( int i );
  60. coerciblevariant_t( float f );
  61. coerciblevariant_t( const EHANDLE &handle );
  62. coerciblevariant_t( CBaseEntity *ent );
  63. inline bool Bool( void ) ;// const { return( fieldType == FIELD_BOOLEAN ) ? bVal : false; }
  64. const char *String( void ) ;// const { return( fieldType == FIELD_STRING ) ? STRING(iszVal) : ToString(); }
  65. inline int Int( void ) ;// const { return( fieldType == FIELD_INTEGER ) ? iVal : 0; }
  66. inline float Float( void ) ;// const { return( fieldType == FIELD_FLOAT ) ? flVal : 0; }
  67. inline const CHandle<CBaseEntity> &Entity(void) ;// const;
  68. fieldtype_t FieldType( void ) { return fieldType; }
  69. void SetBool( bool b );
  70. void SetString( const char * str );
  71. void SetInt( int val );
  72. void SetFloat( float val );
  73. void SetEntity( CBaseEntity *val );
  74. protected:
  75. // from my native type, make a float value if possible
  76. float ConvertFloat() const;
  77. // from my native type, make an int value if possible
  78. int ConvertInt() const;
  79. // from my native type, make a bool value if possible
  80. bool ConvertBool() const;
  81. // from my native type, make an entity if possible, NULL otherwise
  82. CBaseEntity * ConvertEntity() const;
  83. private:
  84. // zero out my contents -- called at top of each Set
  85. inline void Void( void );
  86. };
  87. // mark me as having no conversions, dump the string
  88. // if there is one
  89. void coerciblevariant_t::Void( void )
  90. {
  91. m_bvInitFields = 0;
  92. if (szVal)
  93. {
  94. delete[] szVal;
  95. szVal = NULL;
  96. }
  97. }
  98. // get my bool contents
  99. bool coerciblevariant_t::Bool( void )
  100. {
  101. if ( (m_bvInitFields & kINIT_BOOLEAN) == 0 )
  102. {
  103. // we need to convert
  104. bVal = ConvertBool();
  105. m_bvInitFields |= kINIT_BOOLEAN ;
  106. }
  107. return bVal;
  108. }
  109. // get my int contents
  110. int coerciblevariant_t::Int( void )
  111. {
  112. if ( (m_bvInitFields & kINIT_INT) == 0 )
  113. {
  114. // we need to convert
  115. iVal = ConvertInt();
  116. m_bvInitFields |= kINIT_INT ;
  117. }
  118. return iVal;
  119. }
  120. // get my float contents
  121. float coerciblevariant_t::Float( void )
  122. {
  123. if ( (m_bvInitFields & kINIT_FLOAT) == 0 )
  124. {
  125. // we need to convert
  126. flVal = ConvertFloat();
  127. m_bvInitFields |= kINIT_FLOAT ;
  128. }
  129. return flVal;
  130. }
  131. // get me as an entity
  132. const CHandle<CBaseEntity> &coerciblevariant_t::Entity( void )
  133. {
  134. if ( (m_bvInitFields & kINIT_EHANDLE) == 0 )
  135. {
  136. // we need to convert
  137. eVal = ConvertEntity();
  138. m_bvInitFields |= kINIT_EHANDLE ;
  139. }
  140. return eVal;
  141. }
  142. typedef coerciblevariant_t cvariant_t; // easier typing!
  143. #endif // VARIANT_T_H