ScummVM API documentation
as_scriptobject.h
1 /*
2  AngelCode Scripting Library
3  Copyright (c) 2003-2019 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
55 public:
57  int AddRef() const;
58  int Release() const;
59 
60  bool Get() const;
61  void Set(bool);
62 
63  void Lock() const;
64  void Unlock() const;
65 
66 protected:
67  mutable asCAtomic refCount;
68  bool value;
69  DECLARECRITICALSECTION(mutable lock)
70 };
71 
73 public:
74 //===================================
75 // From asIScriptObject
76 //===================================
77 
78  // Memory management
79  int AddRef() const;
80  int Release() const;
81  asILockableSharedBool *GetWeakRefFlag() const;
82 
83  // Type info
84  int GetTypeId() const;
85  asITypeInfo *GetObjectType() const;
86 
87  // Class properties
88  asUINT GetPropertyCount() const;
89  int GetPropertyTypeId(asUINT prop) const;
90  const char *GetPropertyName(asUINT prop) const;
91  void *GetAddressOfProperty(asUINT prop);
92 
93  // Miscellaneous
94  asIScriptEngine *GetEngine() const;
95  int CopyFrom(const asIScriptObject *other);
96 
97  // User data
98  void *SetUserData(void *data, asPWORD type = 0);
99  void *GetUserData(asPWORD type = 0) const;
100 
101 //====================================
102 // Internal
103 //====================================
104  asCScriptObject(asCObjectType *objType, bool doInitialize = true);
105  virtual ~asCScriptObject();
106 
107  asCScriptObject &operator=(const asCScriptObject &other);
108 
109  // GC methods
110  void Destruct();
111  int GetRefCount();
112  void SetFlag();
113  bool GetFlag();
114  void EnumReferences(asIScriptEngine *engine);
115  void ReleaseAllHandles(asIScriptEngine *engine);
116 
117  // Used for properties
118  void *AllocateUninitializedObject(asCObjectType *objType, asCScriptEngine *engine);
119  void FreeObject(void *ptr, asCObjectType *objType, asCScriptEngine *engine);
120  void CopyObject(const void *src, void *dst, asCObjectType *objType, asCScriptEngine *engine);
121  void CopyHandle(const asPWORD *src, asPWORD *dst, asCObjectType *objType, asCScriptEngine *engine);
122  int CopyFromAs(const asCScriptObject *other, asCObjectType *objType);
123 
124  void CallDestructor();
125 
126 //=============================================
127 // Properties
128 //=============================================
129 protected:
130  friend class asCContext;
131  asCObjectType *objType;
132 
133  mutable asCAtomic refCount;
134  mutable asBYTE gcFlag: 1;
135  mutable asBYTE hasRefCountReachedZero: 1;
136  bool isDestructCalled;
137 
138  // Most script classes instances won't have neither the weakRefFlags nor
139  // userData so we only allocate this if requested. Even when used it is
140  // not something that will be accessed all the time so having the extra
141  // indirection will not affect the performance significantly.
142  struct SExtra {
143  SExtra() : weakRefFlag(0) {};
144  asCLockableSharedBool *weakRefFlag;
145  asCArray<asPWORD> userData;
146  };
147  mutable SExtra *extra;
148 };
149 
150 void ScriptObject_Construct(asCObjectType *objType, asCScriptObject *self);
151 asCScriptObject &ScriptObject_Assignment(asCScriptObject *other, asCScriptObject *self);
152 
153 void ScriptObject_ConstructUnitialized(asCObjectType *objType, asCScriptObject *self);
154 
155 void RegisterScriptObject(asCScriptEngine *engine);
156 
157 asIScriptObject *ScriptObjectFactory(const asCObjectType *objType, asCScriptEngine *engine);
158 asIScriptObject *ScriptObjectCopyFactory(const asCObjectType *objType, void *origObj, asCScriptEngine *engine);
159 
160 END_AS_NAMESPACE
161 
162 #endif
Definition: as_objecttype.h:100
Definition: angelscript.h:1154
Definition: as_atomic.h:49
Definition: as_scriptobject.h:142
Definition: as_scriptobject.h:54
Definition: angelscript.h:982
Definition: as_scriptobject.h:72
Definition: as_context.h:54
Definition: as_scriptengine.h:64
Definition: angelscript.h:639
Definition: angelscript.h:1011