as_scriptobject.h 4.7 KB

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