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