SWF_ScriptVar.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #ifndef __SWF_SCRIPTVAR_H__
  21. #define __SWF_SCRIPTVAR_H__
  22. class idSWFScriptObject;
  23. class idSWFScriptFunction;
  24. /*
  25. ========================
  26. A reference counted string
  27. ========================
  28. */
  29. class idSWFScriptString : public idStr {
  30. public:
  31. idSWFScriptString( const idStr & s ) : idStr( s ), refCount( 1 ) { }
  32. static idSWFScriptString * Alloc( const idStr & s ) { return new (TAG_SWF) idSWFScriptString( s ); }
  33. ID_INLINE void AddRef() { refCount++; }
  34. ID_INLINE void Release() { if ( --refCount == 0 ) { delete this; } }
  35. private:
  36. int refCount;
  37. };
  38. /*
  39. ========================
  40. A variable in an action script
  41. these can be on the stack, in a script object, passed around as parameters, etc
  42. they can contain raw data (int, float), strings, functions, or objects
  43. ========================
  44. */
  45. class idSWFScriptVar {
  46. public:
  47. idSWFScriptVar() : type( SWF_VAR_UNDEF ) { }
  48. idSWFScriptVar( const idSWFScriptVar & other );
  49. idSWFScriptVar( idSWFScriptObject * o ) : type( SWF_VAR_UNDEF ) { SetObject( o ); }
  50. idSWFScriptVar( idStrId s ) : type( SWF_VAR_UNDEF ) { SetString( s ); }
  51. idSWFScriptVar( const idStr & s ) : type( SWF_VAR_UNDEF ) { SetString( s ); }
  52. idSWFScriptVar( const char * s ) : type( SWF_VAR_UNDEF ) { SetString( idStr( s ) ); }
  53. idSWFScriptVar( float f ) : type( SWF_VAR_UNDEF ) { SetFloat( f ); }
  54. idSWFScriptVar( bool b ) : type( SWF_VAR_UNDEF ) { SetBool( b ); }
  55. idSWFScriptVar( int32 i ) : type( SWF_VAR_UNDEF ) { SetInteger( i ); }
  56. idSWFScriptVar( idSWFScriptFunction * nf ) : type( SWF_VAR_UNDEF ) { SetFunction( nf ); }
  57. ~idSWFScriptVar();
  58. idSWFScriptVar & operator=( const idSWFScriptVar & other );
  59. // implements ECMA 262 11.9.3
  60. bool AbstractEquals( const idSWFScriptVar & other );
  61. bool StrictEquals( const idSWFScriptVar & other );
  62. void SetString( idStrId s ) { Free(); type = SWF_VAR_STRINGID; value.i = s.GetIndex(); }
  63. void SetString( const idStr & s ) { Free(); type = SWF_VAR_STRING; value.string = idSWFScriptString::Alloc( s ); }
  64. void SetString( const char * s ) { Free(); type = SWF_VAR_STRING; value.string = idSWFScriptString::Alloc( s ); }
  65. void SetString( idSWFScriptString * s ) { Free(); type = SWF_VAR_STRING; value.string = s; s->AddRef(); }
  66. void SetFloat( float f ) { Free(); type = SWF_VAR_FLOAT; value.f = f; }
  67. void SetNULL() { Free(); type = SWF_VAR_NULL; }
  68. void SetUndefined() { Free(); type = SWF_VAR_UNDEF; }
  69. void SetBool( bool b ) { Free(); type = SWF_VAR_BOOL; value.b = b; }
  70. void SetInteger( int32 i ) { Free(); type = SWF_VAR_INTEGER; value.i = i; }
  71. void SetObject( idSWFScriptObject * o );
  72. void SetFunction( idSWFScriptFunction * f );
  73. idStr ToString() const;
  74. float ToFloat() const;
  75. bool ToBool() const;
  76. int32 ToInteger() const;
  77. idSWFScriptObject * GetObject() { assert( type == SWF_VAR_OBJECT ); return value.object; }
  78. idSWFScriptObject * GetObject() const { assert( type == SWF_VAR_OBJECT ); return value.object; }
  79. idSWFScriptFunction * GetFunction() { assert( type == SWF_VAR_FUNCTION ); return value.function; }
  80. idSWFSpriteInstance * ToSprite();
  81. idSWFTextInstance * ToText();
  82. idSWFScriptVar GetNestedVar( const char * arg1, const char * arg2 = NULL, const char * arg3 = NULL, const char * arg4 = NULL, const char * arg5 = NULL, const char * arg6 = NULL );
  83. idSWFScriptObject * GetNestedObj( const char * arg1, const char * arg2 = NULL, const char * arg3 = NULL, const char * arg4 = NULL, const char * arg5 = NULL, const char * arg6 = NULL );
  84. idSWFSpriteInstance * GetNestedSprite( const char * arg1, const char * arg2 = NULL, const char * arg3 = NULL, const char * arg4 = NULL, const char * arg5 = NULL, const char * arg6 = NULL );
  85. idSWFTextInstance * GetNestedText( const char * arg1, const char * arg2 = NULL, const char * arg3 = NULL, const char * arg4 = NULL, const char * arg5 = NULL, const char * arg6 = NULL );
  86. const char * TypeOf() const;
  87. // debug print of this variable to the console
  88. void PrintToConsole() const;
  89. bool IsString() const { return ( type == SWF_VAR_STRING ) || ( type == SWF_VAR_STRINGID ); }
  90. bool IsNULL() const { return ( type == SWF_VAR_NULL ); }
  91. bool IsUndefined() const { return ( type == SWF_VAR_UNDEF ); }
  92. bool IsValid() const { return ( type != SWF_VAR_UNDEF ) && ( type != SWF_VAR_NULL ); }
  93. bool IsFunction() const { return ( type == SWF_VAR_FUNCTION ); }
  94. bool IsObject() const { return ( type == SWF_VAR_OBJECT ); }
  95. bool IsNumeric() const { return ( type == SWF_VAR_FLOAT ) || ( type == SWF_VAR_INTEGER ) || ( type == SWF_VAR_BOOL ); }
  96. enum swfScriptVarType {
  97. SWF_VAR_STRINGID,
  98. SWF_VAR_STRING,
  99. SWF_VAR_FLOAT,
  100. SWF_VAR_NULL,
  101. SWF_VAR_UNDEF,
  102. SWF_VAR_BOOL,
  103. SWF_VAR_INTEGER,
  104. SWF_VAR_FUNCTION,
  105. SWF_VAR_OBJECT
  106. };
  107. swfScriptVarType GetType() const { return type; }
  108. private:
  109. void Free();
  110. swfScriptVarType type;
  111. union swfScriptVarValue_t {
  112. float f;
  113. int32 i;
  114. bool b;
  115. idSWFScriptObject * object;
  116. idSWFScriptString * string;
  117. idSWFScriptFunction * function;
  118. } value;
  119. };
  120. #endif // !__SWF_SCRIPTVAR_H__