1 /* 2 AngelCode Scripting Library 3 Copyright (c) 2003-2018 Andreas Jonsson 4 5 This software is provided 'as-is', without any express or implied 6 warranty. In no event will the authors be held liable for any 7 damages arising from the use of this software. 8 9 Permission is granted to anyone to use this software for any 10 purpose, including commercial applications, and to alter it and 11 redistribute it freely, subject to the following restrictions: 12 13 1. The origin of this software must not be misrepresented; you 14 must not claim that you wrote the original software. If you use 15 this software in a product, an acknowledgment in the product 16 documentation would be appreciated but is not required. 17 18 2. Altered source versions must be plainly marked as such, and 19 must not be misrepresented as being the original software. 20 21 3. This notice may not be removed or altered from any source 22 distribution. 23 24 The original version of this library can be located at: 25 http://www.angelcode.com/angelscript/ 26 27 Andreas Jonsson 28 andreas@angelcode.com 29 */ 30 31 32 33 // 34 // as_scriptobject.h 35 // 36 // A generic class for handling script declared structures 37 // 38 39 40 41 #ifndef AS_SCRIPTOBJECT_H 42 #define AS_SCRIPTOBJECT_H 43 44 #include "as_config.h" 45 #include "as_atomic.h" 46 47 BEGIN_AS_NAMESPACE 48 49 class asCObjectType; 50 51 // TODO: Add const overload for GetAddressOfProperty 52 53 // TODO: weak: Should move to its own file 54 class asCLockableSharedBool : public asILockableSharedBool 55 { 56 public: 57 asCLockableSharedBool(); 58 int AddRef() const; 59 int Release() const; 60 61 bool Get() const; 62 void Set(bool); 63 64 void Lock() const; 65 void Unlock() const; 66 67 protected: 68 mutable asCAtomic refCount; 69 bool value; 70 DECLARECRITICALSECTION(mutable lock) 71 }; 72 73 class asCScriptObject : public asIScriptObject 74 { 75 public: 76 //=================================== 77 // From asIScriptObject 78 //=================================== 79 80 // Memory management 81 int AddRef() const; 82 int Release() const; 83 asILockableSharedBool *GetWeakRefFlag() const; 84 85 // Type info 86 int GetTypeId() const; 87 asITypeInfo *GetObjectType() const; 88 89 // Class properties 90 asUINT GetPropertyCount() const; 91 int GetPropertyTypeId(asUINT prop) const; 92 const char *GetPropertyName(asUINT prop) const; 93 void *GetAddressOfProperty(asUINT prop); 94 95 // Miscellaneous 96 asIScriptEngine *GetEngine() const; 97 int CopyFrom(asIScriptObject *other); 98 99 // User data 100 void *SetUserData(void *data, asPWORD type = 0); 101 void *GetUserData(asPWORD type = 0) const; 102 103 //==================================== 104 // Internal 105 //==================================== 106 asCScriptObject(asCObjectType *objType, bool doInitialize = true); 107 virtual ~asCScriptObject(); 108 109 asCScriptObject &operator=(const asCScriptObject &other); 110 111 // GC methods 112 void Destruct(); 113 int GetRefCount(); 114 void SetFlag(); 115 bool GetFlag(); 116 void EnumReferences(asIScriptEngine *engine); 117 void ReleaseAllHandles(asIScriptEngine *engine); 118 119 // Used for properties 120 void *AllocateUninitializedObject(asCObjectType *objType, asCScriptEngine *engine); 121 void FreeObject(void *ptr, asCObjectType *objType, asCScriptEngine *engine); 122 void CopyObject(void *src, void *dst, asCObjectType *objType, asCScriptEngine *engine); 123 void CopyHandle(asPWORD *src, asPWORD *dst, asCObjectType *objType, asCScriptEngine *engine); 124 125 void CallDestructor(); 126 127 //============================================= 128 // Properties 129 //============================================= 130 protected: 131 friend class asCContext; 132 asCObjectType *objType; 133 134 mutable asCAtomic refCount; 135 mutable asBYTE gcFlag:1; 136 mutable asBYTE hasRefCountReachedZero:1; 137 bool isDestructCalled; 138 139 // Most script classes instances won't have neither the weakRefFlags nor 140 // userData so we only allocate this if requested. Even when used it is 141 // not something that will be accessed all the time so having the extra 142 // indirection will not affect the performance significantly. 143 struct SExtra 144 { SExtraSExtra145 SExtra() : weakRefFlag(0) {}; 146 asCLockableSharedBool *weakRefFlag; 147 asCArray<asPWORD> userData; 148 }; 149 mutable SExtra *extra; 150 }; 151 152 void ScriptObject_Construct(asCObjectType *objType, asCScriptObject *self); 153 asCScriptObject &ScriptObject_Assignment(asCScriptObject *other, asCScriptObject *self); 154 155 void ScriptObject_ConstructUnitialized(asCObjectType *objType, asCScriptObject *self); 156 157 void RegisterScriptObject(asCScriptEngine *engine); 158 159 asIScriptObject *ScriptObjectFactory(const asCObjectType *objType, asCScriptEngine *engine); 160 asIScriptObject *ScriptObjectCopyFactory(const asCObjectType *objType, void *origObj, asCScriptEngine *engine); 161 162 END_AS_NAMESPACE 163 164 #endif 165