parent
e5b2ff5c59
commit
e748f6e96f
extern/angelscript_addons
scriptarray
scriptbuilder
scriptdictionary
scripthandle
scripthelper
scriptstdstring
src/ScriptResolving/AngelScript
|
@ -4,6 +4,7 @@
|
|||
#include <assert.h>
|
||||
#include <stdio.h> // sprintf
|
||||
#include <string>
|
||||
#include <algorithm> // std::sort
|
||||
|
||||
#include "scriptarray.h"
|
||||
|
||||
|
@ -998,7 +999,7 @@ void CScriptArray::Destruct(SArrayBuffer *buf, asUINT start, asUINT end)
|
|||
|
||||
|
||||
// internal
|
||||
bool CScriptArray::Less(const void *a, const void *b, bool asc, asIScriptContext *ctx, SArrayCache *cache)
|
||||
bool CScriptArray::Less(const void *a, const void *b, bool asc)
|
||||
{
|
||||
if( !asc )
|
||||
{
|
||||
|
@ -1027,42 +1028,6 @@ bool CScriptArray::Less(const void *a, const void *b, bool asc, asIScriptContext
|
|||
#undef COMPARE
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
int r = 0;
|
||||
|
||||
if( subTypeId & asTYPEID_OBJHANDLE )
|
||||
{
|
||||
// Allow sort to work even if the array contains null handles
|
||||
if( *(void**)a == 0 ) return true;
|
||||
if( *(void**)b == 0 ) return false;
|
||||
}
|
||||
|
||||
// Execute object opCmp
|
||||
if( cache && cache->cmpFunc )
|
||||
{
|
||||
// TODO: Add proper error handling
|
||||
r = ctx->Prepare(cache->cmpFunc); assert(r >= 0);
|
||||
|
||||
if( subTypeId & asTYPEID_OBJHANDLE )
|
||||
{
|
||||
r = ctx->SetObject(*((void**)a)); assert(r >= 0);
|
||||
r = ctx->SetArgObject(0, *((void**)b)); assert(r >= 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
r = ctx->SetObject((void*)a); assert(r >= 0);
|
||||
r = ctx->SetArgObject(0, (void*)b); assert(r >= 0);
|
||||
}
|
||||
|
||||
r = ctx->Execute();
|
||||
|
||||
if( r == asEXECUTION_FINISHED )
|
||||
{
|
||||
return (int)ctx->GetReturnDWord() < 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
@ -1475,12 +1440,11 @@ void CScriptArray::Sort(asUINT startAt, asUINT count, bool asc)
|
|||
return;
|
||||
}
|
||||
|
||||
asBYTE tmp[16];
|
||||
asIScriptContext *cmpContext = 0;
|
||||
bool isNested = false;
|
||||
|
||||
if( subTypeId & ~asTYPEID_MASK_SEQNBR )
|
||||
{
|
||||
asIScriptContext *cmpContext = 0;
|
||||
bool isNested = false;
|
||||
|
||||
// Try to reuse the active context
|
||||
cmpContext = asGetActiveContext();
|
||||
if( cmpContext )
|
||||
|
@ -1491,38 +1455,83 @@ void CScriptArray::Sort(asUINT startAt, asUINT count, bool asc)
|
|||
cmpContext = 0;
|
||||
}
|
||||
if( cmpContext == 0 )
|
||||
{
|
||||
cmpContext = objType->GetEngine()->RequestContext();
|
||||
|
||||
// Do the sorting
|
||||
struct {
|
||||
bool asc;
|
||||
asIScriptContext *cmpContext;
|
||||
asIScriptFunction *cmpFunc;
|
||||
bool operator()(void *a, void *b) const
|
||||
{
|
||||
if( !asc )
|
||||
{
|
||||
// Swap items
|
||||
void *TEMP = a;
|
||||
a = b;
|
||||
b = TEMP;
|
||||
}
|
||||
|
||||
int r = 0;
|
||||
|
||||
// Allow sort to work even if the array contains null handles
|
||||
if( a == 0 ) return true;
|
||||
if( b == 0 ) return false;
|
||||
|
||||
// Execute object opCmp
|
||||
if( cmpFunc )
|
||||
{
|
||||
// TODO: Add proper error handling
|
||||
r = cmpContext->Prepare(cmpFunc); assert(r >= 0);
|
||||
r = cmpContext->SetObject(a); assert(r >= 0);
|
||||
r = cmpContext->SetArgObject(0, b); assert(r >= 0);
|
||||
r = cmpContext->Execute();
|
||||
|
||||
if( r == asEXECUTION_FINISHED )
|
||||
{
|
||||
return (int)cmpContext->GetReturnDWord() < 0;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
} customLess = {asc, cmpContext, cache ? cache->cmpFunc : 0};
|
||||
std::sort((void**)GetArrayItemPointer(start), (void**)GetArrayItemPointer(end), customLess);
|
||||
|
||||
// Clean up
|
||||
if( cmpContext )
|
||||
{
|
||||
if( isNested )
|
||||
{
|
||||
asEContextState state = cmpContext->GetState();
|
||||
cmpContext->PopState();
|
||||
if( state == asEXECUTION_ABORTED )
|
||||
cmpContext->Abort();
|
||||
}
|
||||
else
|
||||
objType->GetEngine()->ReturnContext(cmpContext);
|
||||
}
|
||||
}
|
||||
|
||||
// Insertion sort
|
||||
for( int i = start + 1; i < end; i++ )
|
||||
else
|
||||
{
|
||||
Copy(tmp, GetArrayItemPointer(i));
|
||||
|
||||
int j = i - 1;
|
||||
|
||||
while( j >= start && Less(GetDataPointer(tmp), At(j), asc, cmpContext, cache) )
|
||||
// TODO: Use std::sort for primitive types too
|
||||
|
||||
// Insertion sort
|
||||
asBYTE tmp[16];
|
||||
for( int i = start + 1; i < end; i++ )
|
||||
{
|
||||
Copy(GetArrayItemPointer(j + 1), GetArrayItemPointer(j));
|
||||
j--;
|
||||
}
|
||||
Copy(tmp, GetArrayItemPointer(i));
|
||||
|
||||
Copy(GetArrayItemPointer(j + 1), tmp);
|
||||
}
|
||||
int j = i - 1;
|
||||
|
||||
if( cmpContext )
|
||||
{
|
||||
if( isNested )
|
||||
{
|
||||
asEContextState state = cmpContext->GetState();
|
||||
cmpContext->PopState();
|
||||
if( state == asEXECUTION_ABORTED )
|
||||
cmpContext->Abort();
|
||||
while( j >= start && Less(GetDataPointer(tmp), At(j), asc) )
|
||||
{
|
||||
Copy(GetArrayItemPointer(j + 1), GetArrayItemPointer(j));
|
||||
j--;
|
||||
}
|
||||
|
||||
Copy(GetArrayItemPointer(j + 1), tmp);
|
||||
}
|
||||
else
|
||||
objType->GetEngine()->ReturnContext(cmpContext);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1550,7 +1559,6 @@ void CScriptArray::Sort(asIScriptFunction *func, asUINT startAt, asUINT count)
|
|||
return;
|
||||
}
|
||||
|
||||
asBYTE tmp[16];
|
||||
asIScriptContext *cmpContext = 0;
|
||||
bool isNested = false;
|
||||
|
||||
|
@ -1567,6 +1575,7 @@ void CScriptArray::Sort(asIScriptFunction *func, asUINT startAt, asUINT count)
|
|||
cmpContext = objType->GetEngine()->RequestContext();
|
||||
|
||||
// Insertion sort
|
||||
asBYTE tmp[16];
|
||||
for (asUINT i = start + 1; i < end; i++)
|
||||
{
|
||||
Copy(tmp, GetArrayItemPointer(i));
|
||||
|
|
|
@ -120,7 +120,7 @@ protected:
|
|||
CScriptArray(const CScriptArray &other);
|
||||
virtual ~CScriptArray();
|
||||
|
||||
bool Less(const void *a, const void *b, bool asc, asIScriptContext *ctx, SArrayCache *cache);
|
||||
bool Less(const void *a, const void *b, bool asc);
|
||||
void *GetArrayItemPointer(int index);
|
||||
void *GetDataPointer(void *buffer);
|
||||
void Copy(void *dst, void *src);
|
||||
|
|
|
@ -14,6 +14,8 @@
|
|||
|
||||
// TODO: Implement flags for turning on/off include directives and conditional programming
|
||||
|
||||
|
||||
|
||||
//---------------------------
|
||||
// Declaration
|
||||
//
|
||||
|
@ -23,9 +25,10 @@
|
|||
#include <angelscript.h>
|
||||
#endif
|
||||
|
||||
|
||||
#if defined(_MSC_VER) && _MSC_VER <= 1200
|
||||
// disable the annoying warnings on MSVC 6
|
||||
#pragma warning(disable : 4786)
|
||||
#pragma warning (disable:4786)
|
||||
#endif
|
||||
|
||||
#include <string>
|
||||
|
@ -197,7 +200,7 @@ protected:
|
|||
{
|
||||
bool operator()(const std::string &a, const std::string &b) const
|
||||
{
|
||||
return _strcmpi(a.c_str(), b.c_str()) < 0;
|
||||
return _stricmp(a.c_str(), b.c_str()) < 0;
|
||||
}
|
||||
};
|
||||
std::set<std::string, ci_less> includedScripts;
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,240 @@
|
|||
#ifndef SCRIPTDICTIONARY_H
|
||||
#define SCRIPTDICTIONARY_H
|
||||
|
||||
// The dictionary class relies on the script string object, thus the script
|
||||
// string type must be registered with the engine before registering the
|
||||
// dictionary type
|
||||
|
||||
#ifndef ANGELSCRIPT_H
|
||||
// Avoid having to inform include path if header is already include before
|
||||
#include <angelscript.h>
|
||||
#endif
|
||||
|
||||
// By default the CScriptDictionary use the std::string for the keys.
|
||||
// If the application uses a custom string type, then this typedef
|
||||
// can be changed accordingly.
|
||||
#include <string>
|
||||
typedef std::string dictKey_t;
|
||||
|
||||
// Forward declare CScriptDictValue so we can typedef the internal map type
|
||||
BEGIN_AS_NAMESPACE
|
||||
class CScriptDictValue;
|
||||
END_AS_NAMESPACE
|
||||
|
||||
// C++11 introduced the std::unordered_map which is a hash map which is
|
||||
// is generally more performatic for lookups than the std::map which is a
|
||||
// binary tree.
|
||||
// TODO: memory: The map allocator should use the asAllocMem and asFreeMem
|
||||
#if AS_CAN_USE_CPP11
|
||||
#include <unordered_map>
|
||||
typedef std::unordered_map<dictKey_t, AS_NAMESPACE_QUALIFIER CScriptDictValue> dictMap_t;
|
||||
#else
|
||||
#include <map>
|
||||
typedef std::map<dictKey_t, AS_NAMESPACE_QUALIFIER CScriptDictValue> dictMap_t;
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef _MSC_VER
|
||||
// Turn off annoying warnings about truncated symbol names
|
||||
#pragma warning (disable:4786)
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
// Sometimes it may be desired to use the same method names as used by C++ STL.
|
||||
// This may for example reduce time when converting code from script to C++ or
|
||||
// back.
|
||||
//
|
||||
// 0 = off
|
||||
// 1 = on
|
||||
|
||||
#ifndef AS_USE_STLNAMES
|
||||
#define AS_USE_STLNAMES 0
|
||||
#endif
|
||||
|
||||
|
||||
BEGIN_AS_NAMESPACE
|
||||
|
||||
class CScriptArray;
|
||||
class CScriptDictionary;
|
||||
|
||||
class CScriptDictValue
|
||||
{
|
||||
public:
|
||||
// This class must not be declared as local variable in C++, because it needs
|
||||
// to receive the script engine pointer in all operations. The engine pointer
|
||||
// is not kept as member in order to keep the size down
|
||||
CScriptDictValue();
|
||||
CScriptDictValue(asIScriptEngine *engine, void *value, int typeId);
|
||||
|
||||
// Destructor must not be called without first calling FreeValue, otherwise a memory leak will occur
|
||||
~CScriptDictValue();
|
||||
|
||||
// Replace the stored value
|
||||
void Set(asIScriptEngine *engine, void *value, int typeId);
|
||||
void Set(asIScriptEngine *engine, const asINT64 &value);
|
||||
void Set(asIScriptEngine *engine, const double &value);
|
||||
void Set(asIScriptEngine *engine, CScriptDictValue &value);
|
||||
|
||||
// Gets the stored value. Returns false if the value isn't compatible with the informed typeId
|
||||
bool Get(asIScriptEngine *engine, void *value, int typeId) const;
|
||||
bool Get(asIScriptEngine *engine, asINT64 &value) const;
|
||||
bool Get(asIScriptEngine *engine, double &value) const;
|
||||
|
||||
// Returns the address of the stored value for inspection
|
||||
const void *GetAddressOfValue() const;
|
||||
|
||||
// Returns the type id of the stored value
|
||||
int GetTypeId() const;
|
||||
|
||||
// Free the stored value
|
||||
void FreeValue(asIScriptEngine *engine);
|
||||
|
||||
// GC callback
|
||||
void EnumReferences(asIScriptEngine *engine);
|
||||
|
||||
protected:
|
||||
friend class CScriptDictionary;
|
||||
|
||||
union
|
||||
{
|
||||
asINT64 m_valueInt;
|
||||
double m_valueFlt;
|
||||
void *m_valueObj;
|
||||
};
|
||||
int m_typeId;
|
||||
};
|
||||
|
||||
class CScriptDictionary
|
||||
{
|
||||
public:
|
||||
// Factory functions
|
||||
static CScriptDictionary *Create(asIScriptEngine *engine);
|
||||
|
||||
// Called from the script to instantiate a dictionary from an initialization list
|
||||
static CScriptDictionary *Create(asBYTE *buffer);
|
||||
|
||||
// Reference counting
|
||||
void AddRef() const;
|
||||
void Release() const;
|
||||
|
||||
// Reassign the dictionary
|
||||
CScriptDictionary &operator =(const CScriptDictionary &other);
|
||||
|
||||
// Sets a key/value pair
|
||||
void Set(const dictKey_t &key, void *value, int typeId);
|
||||
void Set(const dictKey_t &key, const asINT64 &value);
|
||||
void Set(const dictKey_t &key, const double &value);
|
||||
|
||||
// Gets the stored value. Returns false if the value isn't compatible with the informed typeId
|
||||
bool Get(const dictKey_t &key, void *value, int typeId) const;
|
||||
bool Get(const dictKey_t &key, asINT64 &value) const;
|
||||
bool Get(const dictKey_t &key, double &value) const;
|
||||
|
||||
// Index accessors. If the dictionary is not const it inserts the value if it doesn't already exist
|
||||
// If the dictionary is const then a script exception is set if it doesn't exist and a null pointer is returned
|
||||
CScriptDictValue *operator[](const dictKey_t &key);
|
||||
const CScriptDictValue *operator[](const dictKey_t &key) const;
|
||||
|
||||
// Returns the type id of the stored value, or negative if it doesn't exist
|
||||
int GetTypeId(const dictKey_t &key) const;
|
||||
|
||||
// Returns true if the key is set
|
||||
bool Exists(const dictKey_t &key) const;
|
||||
|
||||
// Returns true if there are no key/value pairs in the dictionary
|
||||
bool IsEmpty() const;
|
||||
|
||||
// Returns the number of key/value pairs in the dictionary
|
||||
asUINT GetSize() const;
|
||||
|
||||
// Deletes the key
|
||||
bool Delete(const dictKey_t &key);
|
||||
|
||||
// Deletes all keys
|
||||
void DeleteAll();
|
||||
|
||||
// Get an array of all keys
|
||||
CScriptArray *GetKeys() const;
|
||||
|
||||
// STL style iterator
|
||||
class CIterator
|
||||
{
|
||||
public:
|
||||
void operator++(); // Pre-increment
|
||||
void operator++(int); // Post-increment
|
||||
|
||||
// This is needed to support C++11 range-for
|
||||
CIterator &operator*();
|
||||
|
||||
bool operator==(const CIterator &other) const;
|
||||
bool operator!=(const CIterator &other) const;
|
||||
|
||||
// Accessors
|
||||
const dictKey_t &GetKey() const;
|
||||
int GetTypeId() const;
|
||||
bool GetValue(asINT64 &value) const;
|
||||
bool GetValue(double &value) const;
|
||||
bool GetValue(void *value, int typeId) const;
|
||||
const void * GetAddressOfValue() const;
|
||||
|
||||
protected:
|
||||
friend class CScriptDictionary;
|
||||
|
||||
CIterator();
|
||||
CIterator(const CScriptDictionary &dict,
|
||||
dictMap_t::const_iterator it);
|
||||
|
||||
CIterator &operator=(const CIterator &) {return *this;} // Not used
|
||||
|
||||
dictMap_t::const_iterator m_it;
|
||||
const CScriptDictionary &m_dict;
|
||||
};
|
||||
|
||||
CIterator begin() const;
|
||||
CIterator end() const;
|
||||
CIterator find(const dictKey_t &key) const;
|
||||
|
||||
// Garbage collections behaviours
|
||||
int GetRefCount();
|
||||
void SetGCFlag();
|
||||
bool GetGCFlag();
|
||||
void EnumReferences(asIScriptEngine *engine);
|
||||
void ReleaseAllReferences(asIScriptEngine *engine);
|
||||
|
||||
protected:
|
||||
// Since the dictionary uses the asAllocMem and asFreeMem functions to allocate memory
|
||||
// the constructors are made protected so that the application cannot allocate it
|
||||
// manually in a different way
|
||||
CScriptDictionary(asIScriptEngine *engine);
|
||||
CScriptDictionary(asBYTE *buffer);
|
||||
|
||||
// We don't want anyone to call the destructor directly, it should be called through the Release method
|
||||
virtual ~CScriptDictionary();
|
||||
|
||||
// Cache the object types needed
|
||||
void Init(asIScriptEngine *engine);
|
||||
|
||||
// Our properties
|
||||
asIScriptEngine *engine;
|
||||
mutable int refCount;
|
||||
mutable bool gcFlag;
|
||||
dictMap_t dict;
|
||||
};
|
||||
|
||||
// This function will determine the configuration of the engine
|
||||
// and use one of the two functions below to register the dictionary object
|
||||
void RegisterScriptDictionary(asIScriptEngine *engine);
|
||||
|
||||
// Call this function to register the math functions
|
||||
// using native calling conventions
|
||||
void RegisterScriptDictionary_Native(asIScriptEngine *engine);
|
||||
|
||||
// Use this one instead if native calling conventions
|
||||
// are not supported on the target platform
|
||||
void RegisterScriptDictionary_Generic(asIScriptEngine *engine);
|
||||
|
||||
END_AS_NAMESPACE
|
||||
|
||||
#endif
|
|
@ -1,367 +1,360 @@
|
|||
#include "scripthandle.h"
|
||||
#include <assert.h>
|
||||
#include <new>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
BEGIN_AS_NAMESPACE
|
||||
|
||||
static void Construct(CScriptHandle* self) { new (self) CScriptHandle(); }
|
||||
static void Construct(CScriptHandle* self, const CScriptHandle& o) { new (self) CScriptHandle(o); }
|
||||
static void Construct(CScriptHandle *self) { new(self) CScriptHandle(); }
|
||||
static void Construct(CScriptHandle *self, const CScriptHandle &o) { new(self) CScriptHandle(o); }
|
||||
// This one is not static because it needs to be friend with the CScriptHandle class
|
||||
void Construct(CScriptHandle* self, void* ref, int typeId) { new (self) CScriptHandle(ref, typeId); }
|
||||
static void Destruct(CScriptHandle* self) { self->~CScriptHandle(); }
|
||||
void Construct(CScriptHandle *self, void *ref, int typeId) { new(self) CScriptHandle(ref, typeId); }
|
||||
static void Destruct(CScriptHandle *self) { self->~CScriptHandle(); }
|
||||
|
||||
CScriptHandle::CScriptHandle() {
|
||||
m_ref = 0;
|
||||
m_type = 0;
|
||||
CScriptHandle::CScriptHandle()
|
||||
{
|
||||
m_ref = 0;
|
||||
m_type = 0;
|
||||
}
|
||||
|
||||
CScriptHandle::CScriptHandle(const CScriptHandle& other) {
|
||||
m_ref = other.m_ref;
|
||||
m_type = other.m_type;
|
||||
CScriptHandle::CScriptHandle(const CScriptHandle &other)
|
||||
{
|
||||
m_ref = other.m_ref;
|
||||
m_type = other.m_type;
|
||||
|
||||
AddRefHandle();
|
||||
AddRefHandle();
|
||||
}
|
||||
|
||||
CScriptHandle::CScriptHandle(void* ref, asITypeInfo* type) {
|
||||
m_ref = ref;
|
||||
m_type = type;
|
||||
CScriptHandle::CScriptHandle(void *ref, asITypeInfo *type)
|
||||
{
|
||||
m_ref = ref;
|
||||
m_type = type;
|
||||
|
||||
AddRefHandle();
|
||||
AddRefHandle();
|
||||
}
|
||||
|
||||
// This constructor shouldn't be called from the application
|
||||
// This constructor shouldn't be called from the application
|
||||
// directly as it requires an active script context
|
||||
CScriptHandle::CScriptHandle(void* ref, int typeId) {
|
||||
m_ref = 0;
|
||||
m_type = 0;
|
||||
CScriptHandle::CScriptHandle(void *ref, int typeId)
|
||||
{
|
||||
m_ref = 0;
|
||||
m_type = 0;
|
||||
|
||||
Assign(ref, typeId);
|
||||
Assign(ref, typeId);
|
||||
}
|
||||
|
||||
CScriptHandle::~CScriptHandle() { ReleaseHandle(); }
|
||||
|
||||
void CScriptHandle::ReleaseHandle() {
|
||||
if (m_ref && m_type) {
|
||||
asIScriptEngine* engine = m_type->GetEngine();
|
||||
engine->ReleaseScriptObject(m_ref, m_type);
|
||||
|
||||
engine->Release();
|
||||
|
||||
m_ref = 0;
|
||||
m_type = 0;
|
||||
}
|
||||
CScriptHandle::~CScriptHandle()
|
||||
{
|
||||
ReleaseHandle();
|
||||
}
|
||||
|
||||
void CScriptHandle::AddRefHandle() {
|
||||
if (m_ref && m_type) {
|
||||
asIScriptEngine* engine = m_type->GetEngine();
|
||||
engine->AddRefScriptObject(m_ref, m_type);
|
||||
void CScriptHandle::ReleaseHandle()
|
||||
{
|
||||
if( m_ref && m_type )
|
||||
{
|
||||
asIScriptEngine *engine = m_type->GetEngine();
|
||||
engine->ReleaseScriptObject(m_ref, m_type);
|
||||
|
||||
// Hold on to the engine so it isn't destroyed while
|
||||
// a reference to a script object is still held
|
||||
engine->AddRef();
|
||||
}
|
||||
engine->Release();
|
||||
|
||||
m_ref = 0;
|
||||
m_type = 0;
|
||||
}
|
||||
}
|
||||
|
||||
CScriptHandle& CScriptHandle::operator=(const CScriptHandle& other) {
|
||||
Set(other.m_ref, other.m_type);
|
||||
void CScriptHandle::AddRefHandle()
|
||||
{
|
||||
if( m_ref && m_type )
|
||||
{
|
||||
asIScriptEngine *engine = m_type->GetEngine();
|
||||
engine->AddRefScriptObject(m_ref, m_type);
|
||||
|
||||
return *this;
|
||||
// Hold on to the engine so it isn't destroyed while
|
||||
// a reference to a script object is still held
|
||||
engine->AddRef();
|
||||
}
|
||||
}
|
||||
|
||||
void CScriptHandle::Set(void* ref, asITypeInfo* type) {
|
||||
if (m_ref == ref)
|
||||
return;
|
||||
CScriptHandle &CScriptHandle::operator =(const CScriptHandle &other)
|
||||
{
|
||||
Set(other.m_ref, other.m_type);
|
||||
|
||||
ReleaseHandle();
|
||||
|
||||
m_ref = ref;
|
||||
m_type = type;
|
||||
|
||||
AddRefHandle();
|
||||
return *this;
|
||||
}
|
||||
|
||||
void* CScriptHandle::GetRef() { return m_ref; }
|
||||
void CScriptHandle::Set(void *ref, asITypeInfo *type)
|
||||
{
|
||||
if( m_ref == ref ) return;
|
||||
|
||||
asITypeInfo* CScriptHandle::GetType() const { return m_type; }
|
||||
ReleaseHandle();
|
||||
|
||||
int CScriptHandle::GetTypeId() const {
|
||||
if (m_type == 0)
|
||||
return 0;
|
||||
m_ref = ref;
|
||||
m_type = type;
|
||||
|
||||
return m_type->GetTypeId() | asTYPEID_OBJHANDLE;
|
||||
AddRefHandle();
|
||||
}
|
||||
|
||||
// This method shouldn't be called from the application
|
||||
void *CScriptHandle::GetRef()
|
||||
{
|
||||
return m_ref;
|
||||
}
|
||||
|
||||
asITypeInfo *CScriptHandle::GetType() const
|
||||
{
|
||||
return m_type;
|
||||
}
|
||||
|
||||
int CScriptHandle::GetTypeId() const
|
||||
{
|
||||
if( m_type == 0 ) return 0;
|
||||
|
||||
return m_type->GetTypeId() | asTYPEID_OBJHANDLE;
|
||||
}
|
||||
|
||||
// This method shouldn't be called from the application
|
||||
// directly as it requires an active script context
|
||||
CScriptHandle& CScriptHandle::Assign(void* ref, int typeId) {
|
||||
// When receiving a null handle we just clear our memory
|
||||
if (typeId == 0) {
|
||||
Set(0, 0);
|
||||
return *this;
|
||||
}
|
||||
CScriptHandle &CScriptHandle::Assign(void *ref, int typeId)
|
||||
{
|
||||
// When receiving a null handle we just clear our memory
|
||||
if( typeId == 0 )
|
||||
{
|
||||
Set(0, 0);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Dereference received handles to get the object
|
||||
if (typeId & asTYPEID_OBJHANDLE) {
|
||||
// Store the actual reference
|
||||
ref = *(void**)ref;
|
||||
typeId &= ~asTYPEID_OBJHANDLE;
|
||||
}
|
||||
// Dereference received handles to get the object
|
||||
if( typeId & asTYPEID_OBJHANDLE )
|
||||
{
|
||||
// Store the actual reference
|
||||
ref = *(void**)ref;
|
||||
typeId &= ~asTYPEID_OBJHANDLE;
|
||||
}
|
||||
|
||||
// Get the object type
|
||||
asIScriptContext* ctx = asGetActiveContext();
|
||||
asIScriptEngine* engine = ctx->GetEngine();
|
||||
asITypeInfo* type = engine->GetTypeInfoById(typeId);
|
||||
// Get the object type
|
||||
asIScriptContext *ctx = asGetActiveContext();
|
||||
asIScriptEngine *engine = ctx->GetEngine();
|
||||
asITypeInfo *type = engine->GetTypeInfoById(typeId);
|
||||
|
||||
// If the argument is another CScriptHandle, we should copy the content instead
|
||||
if (type && strcmp(type->GetName(), "ref") == 0) {
|
||||
CScriptHandle* r = (CScriptHandle*)ref;
|
||||
ref = r->m_ref;
|
||||
type = r->m_type;
|
||||
}
|
||||
// If the argument is another CScriptHandle, we should copy the content instead
|
||||
if( type && strcmp(type->GetName(), "ref") == 0 )
|
||||
{
|
||||
CScriptHandle *r = (CScriptHandle*)ref;
|
||||
ref = r->m_ref;
|
||||
type = r->m_type;
|
||||
}
|
||||
|
||||
Set(ref, type);
|
||||
Set(ref, type);
|
||||
|
||||
return *this;
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool CScriptHandle::operator==(const CScriptHandle& o) const {
|
||||
if (m_ref == o.m_ref && m_type == o.m_type)
|
||||
return true;
|
||||
bool CScriptHandle::operator==(const CScriptHandle &o) const
|
||||
{
|
||||
if( m_ref == o.m_ref &&
|
||||
m_type == o.m_type )
|
||||
return true;
|
||||
|
||||
// TODO: If type is not the same, we should attempt to do a dynamic cast,
|
||||
// which may change the pointer for application registered classes
|
||||
// TODO: If type is not the same, we should attempt to do a dynamic cast,
|
||||
// which may change the pointer for application registered classes
|
||||
|
||||
return false;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CScriptHandle::operator!=(const CScriptHandle& o) const { return !(*this == o); }
|
||||
bool CScriptHandle::operator!=(const CScriptHandle &o) const
|
||||
{
|
||||
return !(*this == o);
|
||||
}
|
||||
|
||||
bool CScriptHandle::Equals(void* ref, int typeId) const {
|
||||
// Null handles are received as reference to a null handle
|
||||
if (typeId == 0)
|
||||
ref = 0;
|
||||
bool CScriptHandle::Equals(void *ref, int typeId) const
|
||||
{
|
||||
// Null handles are received as reference to a null handle
|
||||
if( typeId == 0 )
|
||||
ref = 0;
|
||||
|
||||
// Dereference handles to get the object
|
||||
if (typeId & asTYPEID_OBJHANDLE) {
|
||||
// Compare the actual reference
|
||||
ref = *(void**)ref;
|
||||
typeId &= ~asTYPEID_OBJHANDLE;
|
||||
}
|
||||
// Dereference handles to get the object
|
||||
if( typeId & asTYPEID_OBJHANDLE )
|
||||
{
|
||||
// Compare the actual reference
|
||||
ref = *(void**)ref;
|
||||
typeId &= ~asTYPEID_OBJHANDLE;
|
||||
}
|
||||
|
||||
// TODO: If typeId is not the same, we should attempt to do a dynamic cast,
|
||||
// which may change the pointer for application registered classes
|
||||
// TODO: If typeId is not the same, we should attempt to do a dynamic cast,
|
||||
// which may change the pointer for application registered classes
|
||||
|
||||
if (ref == m_ref)
|
||||
return true;
|
||||
if( ref == m_ref ) return true;
|
||||
|
||||
return false;
|
||||
return false;
|
||||
}
|
||||
|
||||
// AngelScript: used as '@obj = cast<obj>(ref);'
|
||||
void CScriptHandle::Cast(void** outRef, int typeId) {
|
||||
// If we hold a null handle, then just return null
|
||||
if (m_type == 0) {
|
||||
*outRef = 0;
|
||||
return;
|
||||
}
|
||||
void CScriptHandle::Cast(void **outRef, int typeId)
|
||||
{
|
||||
// If we hold a null handle, then just return null
|
||||
if( m_type == 0 )
|
||||
{
|
||||
*outRef = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
// It is expected that the outRef is always a handle
|
||||
assert( typeId & asTYPEID_OBJHANDLE );
|
||||
|
||||
// It is expected that the outRef is always a handle
|
||||
assert(typeId & asTYPEID_OBJHANDLE);
|
||||
// Compare the type id of the actual object
|
||||
typeId &= ~asTYPEID_OBJHANDLE;
|
||||
asIScriptEngine *engine = m_type->GetEngine();
|
||||
asITypeInfo *type = engine->GetTypeInfoById(typeId);
|
||||
|
||||
// Compare the type id of the actual object
|
||||
typeId &= ~asTYPEID_OBJHANDLE;
|
||||
asIScriptEngine* engine = m_type->GetEngine();
|
||||
asITypeInfo* type = engine->GetTypeInfoById(typeId);
|
||||
*outRef = 0;
|
||||
|
||||
*outRef = 0;
|
||||
|
||||
// RefCastObject will increment the refCount of the returned object if successful
|
||||
engine->RefCastObject(m_ref, m_type, type, outRef);
|
||||
// RefCastObject will increment the refCount of the returned object if successful
|
||||
engine->RefCastObject(m_ref, m_type, type, outRef);
|
||||
}
|
||||
|
||||
void CScriptHandle::EnumReferences(asIScriptEngine* inEngine) {
|
||||
// If we're holding a reference, we'll notify the garbage collector of it
|
||||
if (m_ref)
|
||||
inEngine->GCEnumCallback(m_ref);
|
||||
void CScriptHandle::EnumReferences(asIScriptEngine *inEngine)
|
||||
{
|
||||
// If we're holding a reference, we'll notify the garbage collector of it
|
||||
if (m_ref)
|
||||
inEngine->GCEnumCallback(m_ref);
|
||||
|
||||
// The object type itself is also garbage collected
|
||||
if (m_type)
|
||||
inEngine->GCEnumCallback(m_type);
|
||||
// The object type itself is also garbage collected
|
||||
if( m_type)
|
||||
inEngine->GCEnumCallback(m_type);
|
||||
}
|
||||
|
||||
void CScriptHandle::ReleaseReferences(asIScriptEngine*) {
|
||||
// Simply clear the content to release the references
|
||||
Set(0, 0);
|
||||
void CScriptHandle::ReleaseReferences(asIScriptEngine *inEngine)
|
||||
{
|
||||
// Simply clear the content to release the references
|
||||
Set(0, 0);
|
||||
}
|
||||
|
||||
void RegisterScriptHandle_Native(asIScriptEngine* engine) {
|
||||
[[maybe_unused]] int r;
|
||||
void RegisterScriptHandle_Native(asIScriptEngine *engine)
|
||||
{
|
||||
int r;
|
||||
|
||||
#if AS_CAN_USE_CPP11
|
||||
// With C++11 it is possible to use asGetTypeTraits to automatically determine the flags that represent the C++
|
||||
// class
|
||||
r = engine->RegisterObjectType("ref", sizeof(CScriptHandle),
|
||||
asOBJ_VALUE | asOBJ_ASHANDLE | asOBJ_GC | asGetTypeTraits<CScriptHandle>());
|
||||
assert(r >= 0);
|
||||
// With C++11 it is possible to use asGetTypeTraits to automatically determine the flags that represent the C++ class
|
||||
r = engine->RegisterObjectType("ref", sizeof(CScriptHandle), asOBJ_VALUE | asOBJ_ASHANDLE | asOBJ_GC | asGetTypeTraits<CScriptHandle>()); assert( r >= 0 );
|
||||
#else
|
||||
r = engine->RegisterObjectType("ref", sizeof(CScriptHandle),
|
||||
asOBJ_VALUE | asOBJ_ASHANDLE | asOBJ_GC | asOBJ_APP_CLASS_CDAK);
|
||||
assert(r >= 0);
|
||||
r = engine->RegisterObjectType("ref", sizeof(CScriptHandle), asOBJ_VALUE | asOBJ_ASHANDLE | asOBJ_GC | asOBJ_APP_CLASS_CDAK); assert( r >= 0 );
|
||||
#endif
|
||||
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_CONSTRUCT, "void f()",
|
||||
asFUNCTIONPR(Construct, (CScriptHandle*), void), asCALL_CDECL_OBJFIRST);
|
||||
assert(r >= 0);
|
||||
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_CONSTRUCT, "void f(const ref &in)",
|
||||
asFUNCTIONPR(Construct, (CScriptHandle*, const CScriptHandle&), void),
|
||||
asCALL_CDECL_OBJFIRST);
|
||||
assert(r >= 0);
|
||||
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_CONSTRUCT, "void f(const ?&in)",
|
||||
asFUNCTIONPR(Construct, (CScriptHandle*, void*, int), void),
|
||||
asCALL_CDECL_OBJFIRST);
|
||||
assert(r >= 0);
|
||||
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_DESTRUCT, "void f()",
|
||||
asFUNCTIONPR(Destruct, (CScriptHandle*), void), asCALL_CDECL_OBJFIRST);
|
||||
assert(r >= 0);
|
||||
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_ENUMREFS, "void f(int&in)",
|
||||
asMETHOD(CScriptHandle, EnumReferences), asCALL_THISCALL);
|
||||
assert(r >= 0);
|
||||
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_RELEASEREFS, "void f(int&in)",
|
||||
asMETHOD(CScriptHandle, ReleaseReferences), asCALL_THISCALL);
|
||||
assert(r >= 0);
|
||||
r = engine->RegisterObjectMethod("ref", "void opCast(?&out)", asMETHODPR(CScriptHandle, Cast, (void**, int), void),
|
||||
asCALL_THISCALL);
|
||||
assert(r >= 0);
|
||||
r = engine->RegisterObjectMethod("ref", "ref &opHndlAssign(const ref &in)", asMETHOD(CScriptHandle, operator=),
|
||||
asCALL_THISCALL);
|
||||
assert(r >= 0);
|
||||
r = engine->RegisterObjectMethod("ref", "ref &opHndlAssign(const ?&in)", asMETHOD(CScriptHandle, Assign),
|
||||
asCALL_THISCALL);
|
||||
assert(r >= 0);
|
||||
r = engine->RegisterObjectMethod("ref", "bool opEquals(const ref &in) const",
|
||||
asMETHODPR(CScriptHandle, operator==,(const CScriptHandle&) const, bool),
|
||||
asCALL_THISCALL);
|
||||
assert(r >= 0);
|
||||
r = engine->RegisterObjectMethod("ref", "bool opEquals(const ?&in) const",
|
||||
asMETHODPR(CScriptHandle, Equals, (void*, int)const, bool), asCALL_THISCALL);
|
||||
assert(r >= 0);
|
||||
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_CONSTRUCT, "void f()", asFUNCTIONPR(Construct, (CScriptHandle *), void), asCALL_CDECL_OBJFIRST); assert( r >= 0 );
|
||||
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_CONSTRUCT, "void f(const ref &in)", asFUNCTIONPR(Construct, (CScriptHandle *, const CScriptHandle &), void), asCALL_CDECL_OBJFIRST); assert( r >= 0 );
|
||||
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_CONSTRUCT, "void f(const ?&in)", asFUNCTIONPR(Construct, (CScriptHandle *, void *, int), void), asCALL_CDECL_OBJFIRST); assert( r >= 0 );
|
||||
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_DESTRUCT, "void f()", asFUNCTIONPR(Destruct, (CScriptHandle *), void), asCALL_CDECL_OBJFIRST); assert( r >= 0 );
|
||||
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_ENUMREFS, "void f(int&in)", asMETHOD(CScriptHandle,EnumReferences), asCALL_THISCALL); assert(r >= 0);
|
||||
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_RELEASEREFS, "void f(int&in)", asMETHOD(CScriptHandle, ReleaseReferences), asCALL_THISCALL); assert(r >= 0);
|
||||
r = engine->RegisterObjectMethod("ref", "void opCast(?&out)", asMETHODPR(CScriptHandle, Cast, (void **, int), void), asCALL_THISCALL); assert( r >= 0 );
|
||||
r = engine->RegisterObjectMethod("ref", "ref &opHndlAssign(const ref &in)", asMETHOD(CScriptHandle, operator=), asCALL_THISCALL); assert( r >= 0 );
|
||||
r = engine->RegisterObjectMethod("ref", "ref &opHndlAssign(const ?&in)", asMETHOD(CScriptHandle, Assign), asCALL_THISCALL); assert( r >= 0 );
|
||||
r = engine->RegisterObjectMethod("ref", "bool opEquals(const ref &in) const", asMETHODPR(CScriptHandle, operator==, (const CScriptHandle &) const, bool), asCALL_THISCALL); assert( r >= 0 );
|
||||
r = engine->RegisterObjectMethod("ref", "bool opEquals(const ?&in) const", asMETHODPR(CScriptHandle, Equals, (void*, int) const, bool), asCALL_THISCALL); assert( r >= 0 );
|
||||
}
|
||||
|
||||
void CScriptHandle_Construct_Generic(asIScriptGeneric* gen) {
|
||||
CScriptHandle* self = reinterpret_cast<CScriptHandle*>(gen->GetObject());
|
||||
new (self) CScriptHandle();
|
||||
void CScriptHandle_Construct_Generic(asIScriptGeneric *gen)
|
||||
{
|
||||
CScriptHandle *self = reinterpret_cast<CScriptHandle*>(gen->GetObject());
|
||||
new(self) CScriptHandle();
|
||||
}
|
||||
|
||||
void CScriptHandle_ConstructCopy_Generic(asIScriptGeneric* gen) {
|
||||
CScriptHandle* other = reinterpret_cast<CScriptHandle*>(gen->GetArgAddress(0));
|
||||
CScriptHandle* self = reinterpret_cast<CScriptHandle*>(gen->GetObject());
|
||||
new (self) CScriptHandle(*other);
|
||||
void CScriptHandle_ConstructCopy_Generic(asIScriptGeneric *gen)
|
||||
{
|
||||
CScriptHandle *other = reinterpret_cast<CScriptHandle*>(gen->GetArgAddress(0));
|
||||
CScriptHandle *self = reinterpret_cast<CScriptHandle*>(gen->GetObject());
|
||||
new(self) CScriptHandle(*other);
|
||||
}
|
||||
|
||||
void CScriptHandle_ConstructVar_Generic(asIScriptGeneric* gen) {
|
||||
void* ref = gen->GetArgAddress(0);
|
||||
int typeId = gen->GetArgTypeId(0);
|
||||
CScriptHandle* self = reinterpret_cast<CScriptHandle*>(gen->GetObject());
|
||||
Construct(self, ref, typeId);
|
||||
void CScriptHandle_ConstructVar_Generic(asIScriptGeneric *gen)
|
||||
{
|
||||
void *ref = gen->GetArgAddress(0);
|
||||
int typeId = gen->GetArgTypeId(0);
|
||||
CScriptHandle *self = reinterpret_cast<CScriptHandle*>(gen->GetObject());
|
||||
Construct(self, ref, typeId);
|
||||
}
|
||||
|
||||
void CScriptHandle_Destruct_Generic(asIScriptGeneric* gen) {
|
||||
CScriptHandle* self = reinterpret_cast<CScriptHandle*>(gen->GetObject());
|
||||
self->~CScriptHandle();
|
||||
void CScriptHandle_Destruct_Generic(asIScriptGeneric *gen)
|
||||
{
|
||||
CScriptHandle *self = reinterpret_cast<CScriptHandle*>(gen->GetObject());
|
||||
self->~CScriptHandle();
|
||||
}
|
||||
|
||||
void CScriptHandle_Cast_Generic(asIScriptGeneric* gen) {
|
||||
void** ref = reinterpret_cast<void**>(gen->GetArgAddress(0));
|
||||
int typeId = gen->GetArgTypeId(0);
|
||||
CScriptHandle* self = reinterpret_cast<CScriptHandle*>(gen->GetObject());
|
||||
self->Cast(ref, typeId);
|
||||
void CScriptHandle_Cast_Generic(asIScriptGeneric *gen)
|
||||
{
|
||||
void **ref = reinterpret_cast<void**>(gen->GetArgAddress(0));
|
||||
int typeId = gen->GetArgTypeId(0);
|
||||
CScriptHandle *self = reinterpret_cast<CScriptHandle*>(gen->GetObject());
|
||||
self->Cast(ref, typeId);
|
||||
}
|
||||
|
||||
void CScriptHandle_Assign_Generic(asIScriptGeneric* gen) {
|
||||
CScriptHandle* other = reinterpret_cast<CScriptHandle*>(gen->GetArgAddress(0));
|
||||
CScriptHandle* self = reinterpret_cast<CScriptHandle*>(gen->GetObject());
|
||||
*self = *other;
|
||||
gen->SetReturnAddress(self);
|
||||
void CScriptHandle_Assign_Generic(asIScriptGeneric *gen)
|
||||
{
|
||||
CScriptHandle *other = reinterpret_cast<CScriptHandle*>(gen->GetArgAddress(0));
|
||||
CScriptHandle *self = reinterpret_cast<CScriptHandle*>(gen->GetObject());
|
||||
*self = *other;
|
||||
gen->SetReturnAddress(self);
|
||||
}
|
||||
|
||||
void CScriptHandle_AssignVar_Generic(asIScriptGeneric* gen) {
|
||||
void* ref = gen->GetArgAddress(0);
|
||||
int typeId = gen->GetArgTypeId(0);
|
||||
CScriptHandle* self = reinterpret_cast<CScriptHandle*>(gen->GetObject());
|
||||
self->Assign(ref, typeId);
|
||||
gen->SetReturnAddress(self);
|
||||
void CScriptHandle_AssignVar_Generic(asIScriptGeneric *gen)
|
||||
{
|
||||
void *ref = gen->GetArgAddress(0);
|
||||
int typeId = gen->GetArgTypeId(0);
|
||||
CScriptHandle *self = reinterpret_cast<CScriptHandle*>(gen->GetObject());
|
||||
self->Assign(ref, typeId);
|
||||
gen->SetReturnAddress(self);
|
||||
}
|
||||
|
||||
void CScriptHandle_Equals_Generic(asIScriptGeneric* gen) {
|
||||
CScriptHandle* other = reinterpret_cast<CScriptHandle*>(gen->GetArgAddress(0));
|
||||
CScriptHandle* self = reinterpret_cast<CScriptHandle*>(gen->GetObject());
|
||||
gen->SetReturnByte(*self == *other);
|
||||
void CScriptHandle_Equals_Generic(asIScriptGeneric *gen)
|
||||
{
|
||||
CScriptHandle *other = reinterpret_cast<CScriptHandle*>(gen->GetArgAddress(0));
|
||||
CScriptHandle *self = reinterpret_cast<CScriptHandle*>(gen->GetObject());
|
||||
gen->SetReturnByte(*self == *other);
|
||||
}
|
||||
|
||||
void CScriptHandle_EqualsVar_Generic(asIScriptGeneric* gen) {
|
||||
void* ref = gen->GetArgAddress(0);
|
||||
int typeId = gen->GetArgTypeId(0);
|
||||
CScriptHandle* self = reinterpret_cast<CScriptHandle*>(gen->GetObject());
|
||||
gen->SetReturnByte(self->Equals(ref, typeId));
|
||||
void CScriptHandle_EqualsVar_Generic(asIScriptGeneric *gen)
|
||||
{
|
||||
void *ref = gen->GetArgAddress(0);
|
||||
int typeId = gen->GetArgTypeId(0);
|
||||
CScriptHandle *self = reinterpret_cast<CScriptHandle*>(gen->GetObject());
|
||||
gen->SetReturnByte(self->Equals(ref, typeId));
|
||||
}
|
||||
|
||||
void CScriptHandle_EnumReferences_Generic(asIScriptGeneric* gen) {
|
||||
CScriptHandle* self = reinterpret_cast<CScriptHandle*>(gen->GetObject());
|
||||
self->EnumReferences(gen->GetEngine());
|
||||
void CScriptHandle_EnumReferences_Generic(asIScriptGeneric *gen)
|
||||
{
|
||||
CScriptHandle *self = reinterpret_cast<CScriptHandle*>(gen->GetObject());
|
||||
self->EnumReferences(gen->GetEngine());
|
||||
}
|
||||
|
||||
void CScriptHandle_ReleaseReferences_Generic(asIScriptGeneric* gen) {
|
||||
CScriptHandle* self = reinterpret_cast<CScriptHandle*>(gen->GetObject());
|
||||
self->ReleaseReferences(gen->GetEngine());
|
||||
void CScriptHandle_ReleaseReferences_Generic(asIScriptGeneric *gen)
|
||||
{
|
||||
CScriptHandle *self = reinterpret_cast<CScriptHandle*>(gen->GetObject());
|
||||
self->ReleaseReferences(gen->GetEngine());
|
||||
}
|
||||
|
||||
void RegisterScriptHandle_Generic(asIScriptEngine* engine) {
|
||||
[[maybe_unused]] int r;
|
||||
void RegisterScriptHandle_Generic(asIScriptEngine *engine)
|
||||
{
|
||||
int r;
|
||||
|
||||
r = engine->RegisterObjectType("ref", sizeof(CScriptHandle),
|
||||
asOBJ_VALUE | asOBJ_ASHANDLE | asOBJ_GC | asOBJ_APP_CLASS_CDAK);
|
||||
assert(r >= 0);
|
||||
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_CONSTRUCT, "void f()",
|
||||
asFUNCTION(CScriptHandle_Construct_Generic), asCALL_GENERIC);
|
||||
assert(r >= 0);
|
||||
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_CONSTRUCT, "void f(const ref &in)",
|
||||
asFUNCTION(CScriptHandle_ConstructCopy_Generic), asCALL_GENERIC);
|
||||
assert(r >= 0);
|
||||
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_CONSTRUCT, "void f(const ?&in)",
|
||||
asFUNCTION(CScriptHandle_ConstructVar_Generic), asCALL_GENERIC);
|
||||
assert(r >= 0);
|
||||
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_DESTRUCT, "void f()",
|
||||
asFUNCTION(CScriptHandle_Destruct_Generic), asCALL_GENERIC);
|
||||
assert(r >= 0);
|
||||
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_ENUMREFS, "void f(int&in)",
|
||||
asFUNCTION(CScriptHandle_EnumReferences_Generic), asCALL_GENERIC);
|
||||
assert(r >= 0);
|
||||
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_RELEASEREFS, "void f(int&in)",
|
||||
asFUNCTION(CScriptHandle_ReleaseReferences_Generic), asCALL_GENERIC);
|
||||
assert(r >= 0);
|
||||
r = engine->RegisterObjectMethod("ref", "void opCast(?&out)", asFUNCTION(CScriptHandle_Cast_Generic),
|
||||
asCALL_GENERIC);
|
||||
assert(r >= 0);
|
||||
r = engine->RegisterObjectMethod("ref", "ref &opHndlAssign(const ref &in)",
|
||||
asFUNCTION(CScriptHandle_Assign_Generic), asCALL_GENERIC);
|
||||
assert(r >= 0);
|
||||
r = engine->RegisterObjectMethod("ref", "ref &opHndlAssign(const ?&in)",
|
||||
asFUNCTION(CScriptHandle_AssignVar_Generic), asCALL_GENERIC);
|
||||
assert(r >= 0);
|
||||
r = engine->RegisterObjectMethod("ref", "bool opEquals(const ref &in) const",
|
||||
asFUNCTION(CScriptHandle_Equals_Generic), asCALL_GENERIC);
|
||||
assert(r >= 0);
|
||||
r = engine->RegisterObjectMethod("ref", "bool opEquals(const ?&in) const",
|
||||
asFUNCTION(CScriptHandle_EqualsVar_Generic), asCALL_GENERIC);
|
||||
assert(r >= 0);
|
||||
r = engine->RegisterObjectType("ref", sizeof(CScriptHandle), asOBJ_VALUE | asOBJ_ASHANDLE | asOBJ_GC | asOBJ_APP_CLASS_CDAK); assert( r >= 0 );
|
||||
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(CScriptHandle_Construct_Generic), asCALL_GENERIC); assert( r >= 0 );
|
||||
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_CONSTRUCT, "void f(const ref &in)", asFUNCTION(CScriptHandle_ConstructCopy_Generic), asCALL_GENERIC); assert( r >= 0 );
|
||||
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_CONSTRUCT, "void f(const ?&in)", asFUNCTION(CScriptHandle_ConstructVar_Generic), asCALL_GENERIC); assert( r >= 0 );
|
||||
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(CScriptHandle_Destruct_Generic), asCALL_GENERIC); assert( r >= 0 );
|
||||
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_ENUMREFS, "void f(int&in)", asFUNCTION(CScriptHandle_EnumReferences_Generic), asCALL_GENERIC); assert(r >= 0);
|
||||
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_RELEASEREFS, "void f(int&in)", asFUNCTION(CScriptHandle_ReleaseReferences_Generic), asCALL_GENERIC); assert(r >= 0);
|
||||
r = engine->RegisterObjectMethod("ref", "void opCast(?&out)", asFUNCTION(CScriptHandle_Cast_Generic), asCALL_GENERIC); assert( r >= 0 );
|
||||
r = engine->RegisterObjectMethod("ref", "ref &opHndlAssign(const ref &in)", asFUNCTION(CScriptHandle_Assign_Generic), asCALL_GENERIC); assert( r >= 0 );
|
||||
r = engine->RegisterObjectMethod("ref", "ref &opHndlAssign(const ?&in)", asFUNCTION(CScriptHandle_AssignVar_Generic), asCALL_GENERIC); assert( r >= 0 );
|
||||
r = engine->RegisterObjectMethod("ref", "bool opEquals(const ref &in) const", asFUNCTION(CScriptHandle_Equals_Generic), asCALL_GENERIC); assert( r >= 0 );
|
||||
r = engine->RegisterObjectMethod("ref", "bool opEquals(const ?&in) const", asFUNCTION(CScriptHandle_EqualsVar_Generic), asCALL_GENERIC); assert( r >= 0 );
|
||||
}
|
||||
|
||||
void RegisterScriptHandle(asIScriptEngine* engine) {
|
||||
if (strstr(asGetLibraryOptions(), "AS_MAX_PORTABILITY"))
|
||||
RegisterScriptHandle_Generic(engine);
|
||||
else
|
||||
RegisterScriptHandle_Native(engine);
|
||||
void RegisterScriptHandle(asIScriptEngine *engine)
|
||||
{
|
||||
if( strstr(asGetLibraryOptions(), "AS_MAX_PORTABILITY") )
|
||||
RegisterScriptHandle_Generic(engine);
|
||||
else
|
||||
RegisterScriptHandle_Native(engine);
|
||||
}
|
||||
|
||||
|
||||
END_AS_NAMESPACE
|
||||
|
|
|
@ -975,8 +975,7 @@ string ScriptGetExceptionInfo()
|
|||
|
||||
void RegisterExceptionRoutines(asIScriptEngine *engine)
|
||||
{
|
||||
[[maybe_unused]]
|
||||
int r;
|
||||
int r;
|
||||
|
||||
// The string type must be available
|
||||
assert(engine->GetTypeInfoByDecl("string"));
|
||||
|
|
|
@ -28,25 +28,26 @@ typedef map<string, int> map_t;
|
|||
END_AS_NAMESPACE
|
||||
#endif
|
||||
|
||||
BEGIN_AS_NAMESPACE
|
||||
class CStdStringFactory : public asIStringFactory
|
||||
{
|
||||
public:
|
||||
CStdStringFactory() = default;
|
||||
~CStdStringFactory() override
|
||||
CStdStringFactory() {}
|
||||
~CStdStringFactory()
|
||||
{
|
||||
// The script engine must release each string
|
||||
// constant that it has requested
|
||||
assert(stringCache.empty());
|
||||
assert(stringCache.size() == 0);
|
||||
}
|
||||
|
||||
const void *GetStringConstant(const char *data, asUINT length) override
|
||||
const void *GetStringConstant(const char *data, asUINT length)
|
||||
{
|
||||
// The string factory might be modified from multiple
|
||||
// threads, so it is necessary to use a mutex.
|
||||
asAcquireExclusiveLock();
|
||||
|
||||
string str(data, length);
|
||||
auto it = stringCache.find(str);
|
||||
map_t::iterator it = stringCache.find(str);
|
||||
if (it != stringCache.end())
|
||||
it->second++;
|
||||
else
|
||||
|
@ -57,9 +58,9 @@ public:
|
|||
return reinterpret_cast<const void*>(&it->first);
|
||||
}
|
||||
|
||||
int ReleaseStringConstant(const void *str) override
|
||||
int ReleaseStringConstant(const void *str)
|
||||
{
|
||||
if (str == nullptr)
|
||||
if (str == 0)
|
||||
return asERROR;
|
||||
|
||||
int ret = asSUCCESS;
|
||||
|
@ -68,7 +69,7 @@ public:
|
|||
// threads, so it is necessary to use a mutex.
|
||||
asAcquireExclusiveLock();
|
||||
|
||||
auto it = stringCache.find(*reinterpret_cast<const string*>(str));
|
||||
map_t::iterator it = stringCache.find(*reinterpret_cast<const string*>(str));
|
||||
if (it == stringCache.end())
|
||||
ret = asERROR;
|
||||
else
|
||||
|
@ -83,9 +84,9 @@ public:
|
|||
return ret;
|
||||
}
|
||||
|
||||
int GetRawStringData(const void *str, char *data, asUINT *length) const override
|
||||
int GetRawStringData(const void *str, char *data, asUINT *length) const
|
||||
{
|
||||
if (str == nullptr)
|
||||
if (str == 0)
|
||||
return asERROR;
|
||||
|
||||
if (length)
|
||||
|
@ -98,17 +99,17 @@ public:
|
|||
}
|
||||
|
||||
// THe access to the string cache is protected with the common mutex provided by AngelScript
|
||||
map_t stringCache = {};
|
||||
map_t stringCache;
|
||||
};
|
||||
|
||||
static CStdStringFactory *stringFactory = nullptr;
|
||||
static CStdStringFactory *stringFactory = 0;
|
||||
|
||||
// TODO: Make this public so the application can also use the string
|
||||
// factory and share the string constants if so desired, or to
|
||||
// monitor the size of the string factory cache.
|
||||
CStdStringFactory *GetStdStringFactorySingleton()
|
||||
{
|
||||
if( stringFactory == nullptr )
|
||||
if( stringFactory == 0 )
|
||||
{
|
||||
// The following instance will be destroyed by the global
|
||||
// CStdStringFactoryCleaner instance upon application shutdown
|
||||
|
@ -122,7 +123,7 @@ class CStdStringFactoryCleaner
|
|||
public:
|
||||
~CStdStringFactoryCleaner()
|
||||
{
|
||||
if (stringFactory != nullptr)
|
||||
if (stringFactory)
|
||||
{
|
||||
// Only delete the string factory if the stringCache is empty
|
||||
// If it is not empty, it means that someone might still attempt
|
||||
|
@ -133,13 +134,13 @@ public:
|
|||
if (stringFactory->stringCache.empty())
|
||||
{
|
||||
delete stringFactory;
|
||||
stringFactory = nullptr;
|
||||
stringFactory = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static CStdStringFactoryCleaner cleaner = {};
|
||||
static CStdStringFactoryCleaner cleaner;
|
||||
|
||||
|
||||
static void ConstructString(string *thisPointer)
|
||||
|
@ -461,12 +462,12 @@ static void StringResize(asUINT l, string &str)
|
|||
// string formatInt(int64 val, const string &in options, uint width)
|
||||
static string formatInt(asINT64 value, const string &options, asUINT width)
|
||||
{
|
||||
bool leftJustify = options.find('l') != string::npos;
|
||||
bool padWithZero = options.find('0') != string::npos;
|
||||
bool alwaysSign = options.find('+') != string::npos;
|
||||
bool spaceOnSign = options.find(' ') != string::npos;
|
||||
bool hexSmall = options.find('h') != string::npos;
|
||||
bool hexLarge = options.find('H') != string::npos;
|
||||
bool leftJustify = options.find("l") != string::npos;
|
||||
bool padWithZero = options.find("0") != string::npos;
|
||||
bool alwaysSign = options.find("+") != string::npos;
|
||||
bool spaceOnSign = options.find(" ") != string::npos;
|
||||
bool hexSmall = options.find("h") != string::npos;
|
||||
bool hexLarge = options.find("H") != string::npos;
|
||||
|
||||
string fmt = "%";
|
||||
if( leftJustify ) fmt += "-";
|
||||
|
@ -505,12 +506,12 @@ static string formatInt(asINT64 value, const string &options, asUINT width)
|
|||
// string formatUInt(uint64 val, const string &in options, uint width)
|
||||
static string formatUInt(asQWORD value, const string &options, asUINT width)
|
||||
{
|
||||
bool leftJustify = options.find('l') != string::npos;
|
||||
bool padWithZero = options.find('0') != string::npos;
|
||||
bool alwaysSign = options.find('+') != string::npos;
|
||||
bool spaceOnSign = options.find(' ') != string::npos;
|
||||
bool hexSmall = options.find('h') != string::npos;
|
||||
bool hexLarge = options.find('H') != string::npos;
|
||||
bool leftJustify = options.find("l") != string::npos;
|
||||
bool padWithZero = options.find("0") != string::npos;
|
||||
bool alwaysSign = options.find("+") != string::npos;
|
||||
bool spaceOnSign = options.find(" ") != string::npos;
|
||||
bool hexSmall = options.find("h") != string::npos;
|
||||
bool hexLarge = options.find("H") != string::npos;
|
||||
|
||||
string fmt = "%";
|
||||
if( leftJustify ) fmt += "-";
|
||||
|
@ -549,12 +550,12 @@ static string formatUInt(asQWORD value, const string &options, asUINT width)
|
|||
// string formatFloat(double val, const string &in options, uint width, uint precision)
|
||||
static string formatFloat(double value, const string &options, asUINT width, asUINT precision)
|
||||
{
|
||||
bool leftJustify = options.find('l') != string::npos;
|
||||
bool padWithZero = options.find('0') != string::npos;
|
||||
bool alwaysSign = options.find('+') != string::npos;
|
||||
bool spaceOnSign = options.find(' ') != string::npos;
|
||||
bool expSmall = options.find('e') != string::npos;
|
||||
bool expLarge = options.find('E') != string::npos;
|
||||
bool leftJustify = options.find("l") != string::npos;
|
||||
bool padWithZero = options.find("0") != string::npos;
|
||||
bool alwaysSign = options.find("+") != string::npos;
|
||||
bool spaceOnSign = options.find(" ") != string::npos;
|
||||
bool expSmall = options.find("e") != string::npos;
|
||||
bool expLarge = options.find("E") != string::npos;
|
||||
|
||||
string fmt = "%";
|
||||
if( leftJustify ) fmt += "-";
|
||||
|
@ -734,7 +735,7 @@ static string StringSubString(asUINT start, int count, const string &str)
|
|||
// makro, so this wrapper was introduced as work around.
|
||||
static bool StringEquals(const std::string& lhs, const std::string& rhs)
|
||||
{
|
||||
return lhs == rhs;
|
||||
return lhs == rhs;
|
||||
}
|
||||
|
||||
void RegisterStdString_Native(asIScriptEngine *engine)
|
||||
|
|
|
@ -112,8 +112,7 @@ static void StringJoin_Generic(asIScriptGeneric *gen)
|
|||
// The string type must have been registered first.
|
||||
void RegisterStdStringUtils(asIScriptEngine *engine)
|
||||
{
|
||||
[[maybe_unused]]
|
||||
int r;
|
||||
int r;
|
||||
|
||||
if( strstr(asGetLibraryOptions(), "AS_MAX_PORTABILITY") )
|
||||
{
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#include "AngelScriptResolver.hpp"
|
||||
#include <CreatureLib/Battling/Models/Creature.hpp>
|
||||
#include <regex>
|
||||
#include "../../../extern/angelscript_addons/scriptdictionary/scriptdictionary.h"
|
||||
#include "../../../extern/angelscript_addons/scripthandle/scripthandle.h"
|
||||
#include "../../../extern/angelscript_addons/scripthelper/scripthelper.h"
|
||||
#include "../../../extern/angelscript_addons/scriptstdstring/scriptstdstring.h"
|
||||
|
@ -35,7 +36,7 @@ static void TranslateException(asIScriptContext* ctx, void* /*userParam*/) {
|
|||
} catch (ArbUt::Exception& e) {
|
||||
// Tell the VM the type of exception that occurred
|
||||
ctx->SetException(e.what());
|
||||
}catch (std::exception& e) {
|
||||
} catch (std::exception& e) {
|
||||
// Tell the VM the type of exception that occurred
|
||||
ctx->SetException(e.what());
|
||||
} catch (...) {
|
||||
|
@ -76,6 +77,7 @@ void AngelScriptResolver::Initialize(CreatureLib::Battling::BattleLibrary* arg,
|
|||
// Register Script Array type
|
||||
RegisterScriptArray(_engine, true);
|
||||
RegisterScriptHandle(_engine);
|
||||
RegisterScriptDictionary(_engine);
|
||||
r = _engine->RegisterGlobalFunction("void print(const string &in)", asFUNCTION(Print), asCALL_CDECL);
|
||||
if (r < 0)
|
||||
throw ArbUt::Exception("Registering print function failed.");
|
||||
|
|
Loading…
Reference in New Issue