as_scriptobject.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2013 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. [email protected]
  22. */
  23. // Modified by Lasse Oorni for Urho3D
  24. //
  25. // as_scriptobject.h
  26. //
  27. // A generic class for handling script declared structures
  28. //
  29. #ifndef AS_SCRIPTOBJECT_H
  30. #define AS_SCRIPTOBJECT_H
  31. #include "as_config.h"
  32. #include "as_atomic.h"
  33. BEGIN_AS_NAMESPACE
  34. class asCObjectType;
  35. // TODO: Add const overload for GetAddressOfProperty
  36. // TODO: weak: Should move to its own file
  37. class asCLockableSharedBool : public asILockableSharedBool
  38. {
  39. public:
  40. asCLockableSharedBool();
  41. int AddRef() const;
  42. int Release() const;
  43. bool Get() const;
  44. void Set(bool);
  45. void Lock() const;
  46. void Unlock() const;
  47. protected:
  48. mutable asCAtomic refCount;
  49. bool value;
  50. DECLARECRITICALSECTION(mutable lock);
  51. };
  52. class asCScriptObject : public asIScriptObject
  53. {
  54. public:
  55. //===================================
  56. // From asIScriptObject
  57. //===================================
  58. asIScriptEngine *GetEngine() const;
  59. // Memory management
  60. int AddRef() const;
  61. int Release() const;
  62. // Type info
  63. int GetTypeId() const;
  64. asIObjectType *GetObjectType() const;
  65. // Class properties
  66. asUINT GetPropertyCount() const;
  67. int GetPropertyTypeId(asUINT prop) const;
  68. const char *GetPropertyName(asUINT prop) const;
  69. void *GetAddressOfProperty(asUINT prop);
  70. int CopyFrom(asIScriptObject *other);
  71. // Urho3D: added userdata
  72. void *SetUserData(void *data);
  73. void *GetUserData() const;
  74. //====================================
  75. // Internal
  76. //====================================
  77. asCScriptObject(asCObjectType *objType, bool doInitialize = true);
  78. virtual ~asCScriptObject();
  79. asCScriptObject &operator=(const asCScriptObject &other);
  80. // GC methods
  81. void Destruct();
  82. int GetRefCount();
  83. void SetFlag();
  84. bool GetFlag();
  85. void EnumReferences(asIScriptEngine *engine);
  86. void ReleaseAllHandles(asIScriptEngine *engine);
  87. // Weakref methods
  88. asILockableSharedBool *GetWeakRefFlag() const;
  89. // Used for properties
  90. void *AllocateUninitializedObject(asCObjectType *objType, asCScriptEngine *engine);
  91. void FreeObject(void *ptr, asCObjectType *objType, asCScriptEngine *engine);
  92. void CopyObject(void *src, void *dst, asCObjectType *objType, asCScriptEngine *engine);
  93. void CopyHandle(asPWORD *src, asPWORD *dst, asCObjectType *objType, asCScriptEngine *engine);
  94. void CallDestructor();
  95. //=============================================
  96. // Properties
  97. //=============================================
  98. public:
  99. asCObjectType *objType;
  100. protected:
  101. mutable asCAtomic refCount;
  102. mutable asBYTE gcFlag:1;
  103. mutable asBYTE hasRefCountReachedZero:1;
  104. bool isDestructCalled;
  105. mutable asCLockableSharedBool *weakRefFlag;
  106. // Urho3D: added userdata
  107. void* userData;
  108. };
  109. void ScriptObject_Construct(asCObjectType *objType, asCScriptObject *self);
  110. asCScriptObject &ScriptObject_Assignment(asCScriptObject *other, asCScriptObject *self);
  111. void ScriptObject_ConstructUnitialized(asCObjectType *objType, asCScriptObject *self);
  112. void ScriptObject_Construct_Generic(asIScriptGeneric *gen);
  113. void ScriptObject_Assignment_Generic(asIScriptGeneric *gen);
  114. void RegisterScriptObject(asCScriptEngine *engine);
  115. asIScriptObject *ScriptObjectFactory(const asCObjectType *objType, asCScriptEngine *engine);
  116. END_AS_NAMESPACE
  117. #endif