mirror of
https://github.com/VCMP-SqMod/SqMod.git
synced 2024-11-08 00:37:15 +01:00
Migrated the XML module to C++ exceptions as well.
Also enabled the latest C++ revision in the project. Various other fixes and improvements.
This commit is contained in:
parent
47eec5cb10
commit
3162221e7f
@ -361,7 +361,10 @@
|
||||
</Build>
|
||||
<Compiler>
|
||||
<Add option="-Wall" />
|
||||
<Add option="-std=c++14" />
|
||||
<Add option="-DSQMOD_PLUGIN_API" />
|
||||
<Add option="-DSCRAT_USE_EXCEPTIONS" />
|
||||
<Add option="-DSCRAT_USE_CXX11_OPTIMIZATIONS" />
|
||||
<Add directory="../modules/xml" />
|
||||
<Add directory="../shared" />
|
||||
<Add directory="../include" />
|
||||
|
@ -9,185 +9,141 @@
|
||||
namespace SqMod {
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
bool Attribute::Validate() const
|
||||
SQInteger Attribute::Typename(HSQUIRRELVM vm)
|
||||
{
|
||||
if (m_Doc)
|
||||
return true;
|
||||
// Invalid document reference
|
||||
_SqMod->SqThrow("Invalid XML document reference");
|
||||
return false;
|
||||
static SQChar name[] = _SC("SqXmlAttribute");
|
||||
sq_pushstring(vm, name, sizeof(name));
|
||||
return 1;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void Attribute::SetName(CSStr name)
|
||||
void Attribute::Validate() const
|
||||
{
|
||||
if (!m_Attr.set_name(name))
|
||||
_SqMod->SqThrow("Unable to set xml attribute name");
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void Attribute::SetValue(CSStr name)
|
||||
{
|
||||
if (!m_Attr.set_value(name))
|
||||
_SqMod->SqThrow("Unable to set xml attribute value");
|
||||
// Validate the document handle
|
||||
if (!m_Doc)
|
||||
SqThrowF("Invalid XML document reference");
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Object Attribute::AsLong(Object & def) const
|
||||
{
|
||||
// Obtain the initial stack size
|
||||
const Int32 top = sq_gettop(_SqVM);
|
||||
const StackGuard sg(_SqVM);
|
||||
// Push the specified object onto the stack
|
||||
Var< Object >::push(_SqVM, def);
|
||||
// The resulted long integer value
|
||||
Int64 longint = 0;
|
||||
// Attempt to get the numeric value inside the specified object
|
||||
if (SQ_FAILED(_SqMod->GetSLongValue(_SqVM, -1, &longint)))
|
||||
_SqMod->SqThrow("Invalid long integer specified");
|
||||
SqThrowF("Invalid long integer specified");
|
||||
// Push a long integer instance with the requested value on the stack
|
||||
_SqMod->PushSLongObject(_SqVM, m_Attr.as_llong(longint));
|
||||
// Obtain the object from the stack
|
||||
Var< Object > inst(_SqVM, -1);
|
||||
// Remove an pushed values (if any) to restore the stack
|
||||
sq_pop(_SqVM, sq_gettop(_SqVM) - top);
|
||||
// Return the long integer instance
|
||||
return inst.value;
|
||||
// Obtain the object from the stack and return it
|
||||
return Var< Object >(_SqVM, -1).value;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Object Attribute::AsUlong(Object & def) const
|
||||
{
|
||||
// Obtain the initial stack size
|
||||
const Int32 top = sq_gettop(_SqVM);
|
||||
const StackGuard sg(_SqVM);
|
||||
// Push the specified object onto the stack
|
||||
Var< Object >::push(_SqVM, def);
|
||||
// The resulted long integer value
|
||||
Uint64 longint = 0;
|
||||
// Attempt to get the numeric value inside the specified object
|
||||
if (SQ_FAILED(_SqMod->GetULongValue(_SqVM, -1, &longint)))
|
||||
_SqMod->SqThrow("Invalid long integer specified");
|
||||
SqThrowF("Invalid long integer specified");
|
||||
// Push a long integer instance with the requested value on the stack
|
||||
_SqMod->PushULongObject(_SqVM, m_Attr.as_ullong(longint));
|
||||
// Obtain the object from the stack
|
||||
Var< Object > inst(_SqVM, -1);
|
||||
// Remove an pushed values (if any) to restore the stack
|
||||
sq_pop(_SqVM, sq_gettop(_SqVM) - top);
|
||||
// Return the long integer instance
|
||||
return inst.value;
|
||||
// Obtain the object from the stack and return it
|
||||
return Var< Object >(_SqVM, -1).value;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
bool Attribute::ApplyLong(Object & value)
|
||||
{
|
||||
// Obtain the initial stack size
|
||||
const Int32 top = sq_gettop(_SqVM);
|
||||
const StackGuard sg(_SqVM);
|
||||
// Push the specified object onto the stack
|
||||
Var< Object & >::push(_SqVM, value);
|
||||
// The resulted long integer value
|
||||
Int64 longint = 0;
|
||||
// Whether the operation succeeded
|
||||
bool res = false;
|
||||
// Attempt to get the numeric value inside the specified object
|
||||
if (SQ_FAILED(_SqMod->GetSLongValue(_SqVM, -1, &longint)))
|
||||
_SqMod->SqThrow("Invalid long integer specified");
|
||||
// Assign the obtained value
|
||||
else
|
||||
res = m_Attr.set_value(longint);
|
||||
// Remove an pushed values (if any) to restore the stack
|
||||
sq_pop(_SqVM, sq_gettop(_SqVM) - top);
|
||||
// Return the result
|
||||
return res;
|
||||
SqThrowF("Invalid long integer specified");
|
||||
// Assign the obtained value and return the result
|
||||
return m_Attr.set_value(longint);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
bool Attribute::ApplyUlong(Object & value)
|
||||
{
|
||||
// Obtain the initial stack size
|
||||
const Int32 top = sq_gettop(_SqVM);
|
||||
const StackGuard sg(_SqVM);
|
||||
// Push the specified object onto the stack
|
||||
Var< Object & >::push(_SqVM, value);
|
||||
// The resulted long integer value
|
||||
Uint64 longint = 0;
|
||||
// Whether the operation succeeded
|
||||
bool res = false;
|
||||
// Attempt to get the numeric value inside the specified object
|
||||
if (SQ_FAILED(_SqMod->GetULongValue(_SqVM, -1, &longint)))
|
||||
_SqMod->SqThrow("Invalid long integer specified");
|
||||
// Assign the obtained value
|
||||
else
|
||||
res = m_Attr.set_value(longint);
|
||||
// Remove an pushed values (if any) to restore the stack
|
||||
sq_pop(_SqVM, sq_gettop(_SqVM) - top);
|
||||
// Return the result
|
||||
return res;
|
||||
SqThrowF("Invalid long integer specified");
|
||||
// Assign the obtained value and return the result
|
||||
return m_Attr.set_value(longint);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Object Attribute::GetLong() const
|
||||
{
|
||||
// Obtain the initial stack size
|
||||
const Int32 top = sq_gettop(_SqVM);
|
||||
const StackGuard sg(_SqVM);
|
||||
// Push a long integer instance with the requested value on the stack
|
||||
_SqMod->PushSLongObject(_SqVM, m_Attr.as_llong());
|
||||
// Obtain the object from the stack
|
||||
Var< Object > inst(_SqVM, -1);
|
||||
// Remove an pushed values (if any) to restore the stack
|
||||
sq_pop(_SqVM, sq_gettop(_SqVM) - top);
|
||||
// Return the long integer instance
|
||||
return inst.value;
|
||||
// Obtain the object from the stack and return it
|
||||
return Var< Object >(_SqVM, -1).value;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void Attribute::SetLong(Object & value)
|
||||
{
|
||||
// Obtain the initial stack size
|
||||
const Int32 top = sq_gettop(_SqVM);
|
||||
const StackGuard sg(_SqVM);
|
||||
// Push the specified object onto the stack
|
||||
Var< Object & >::push(_SqVM, value);
|
||||
// The resulted long integer value
|
||||
Int64 longint = 0;
|
||||
// Attempt to get the numeric value inside the specified object
|
||||
if (SQ_FAILED(_SqMod->GetSLongValue(_SqVM, -1, &longint)))
|
||||
_SqMod->SqThrow("Invalid long integer specified");
|
||||
SqThrowF("Invalid long integer specified");
|
||||
// Assign the obtained value
|
||||
else
|
||||
m_Attr = longint;
|
||||
// Remove an pushed values (if any) to restore the stack
|
||||
sq_pop(_SqVM, sq_gettop(_SqVM) - top);
|
||||
m_Attr = longint;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Object Attribute::GetUlong() const
|
||||
{
|
||||
// Obtain the initial stack size
|
||||
const Int32 top = sq_gettop(_SqVM);
|
||||
const StackGuard sg(_SqVM);
|
||||
// Push a long integer instance with the requested value on the stack
|
||||
_SqMod->PushULongObject(_SqVM, m_Attr.as_ullong());
|
||||
// Obtain the object from the stack
|
||||
Var< Object > inst(_SqVM, -1);
|
||||
// Remove an pushed values (if any) to restore the stack
|
||||
sq_pop(_SqVM, sq_gettop(_SqVM) - top);
|
||||
// Return the long integer instance
|
||||
return inst.value;
|
||||
// Obtain the object from the stack and return it
|
||||
return Var< Object >(_SqVM, -1).value;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void Attribute::SetUlong(Object & value)
|
||||
{
|
||||
// Obtain the initial stack size
|
||||
const Int32 top = sq_gettop(_SqVM);
|
||||
const StackGuard sg(_SqVM);
|
||||
// Push the specified object onto the stack
|
||||
Var< Object & >::push(_SqVM, value);
|
||||
// The resulted long integer value
|
||||
Uint64 longint = 0;
|
||||
// Attempt to get the numeric value inside the specified object
|
||||
if (SQ_FAILED(_SqMod->GetULongValue(_SqVM, -1, &longint)))
|
||||
_SqMod->SqThrow("Invalid long integer specified");
|
||||
SqThrowF("Invalid long integer specified");
|
||||
// Assign the obtained value
|
||||
else
|
||||
m_Attr = longint;
|
||||
// Remove an pushed values (if any) to restore the stack
|
||||
sq_pop(_SqVM, sq_gettop(_SqVM) - top);
|
||||
m_Attr = longint;
|
||||
}
|
||||
|
||||
} // Namespace:: SqMod
|
||||
|
@ -32,7 +32,7 @@ protected:
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Validate the document reference and throw an error if invalid.
|
||||
*/
|
||||
bool Validate() const;
|
||||
void Validate() const;
|
||||
|
||||
private:
|
||||
|
||||
@ -99,6 +99,11 @@ public:
|
||||
return m_Attr.value();
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Used by the script engine to retrieve the name from instances of this type.
|
||||
*/
|
||||
static SQInteger Typename(HSQUIRRELVM vm);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* See whether this instance references a valid xml document.
|
||||
*/
|
||||
@ -142,7 +147,11 @@ public:
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve attribute name.
|
||||
*/
|
||||
void SetName(CSStr name);
|
||||
void SetName(CSStr name)
|
||||
{
|
||||
if (!m_Attr.set_name(name))
|
||||
SqThrowF("Unable to set xml attribute name");
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Modify the attribute name.
|
||||
@ -163,7 +172,11 @@ public:
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve attribute value.
|
||||
*/
|
||||
void SetValue(CSStr name);
|
||||
void SetValue(CSStr name)
|
||||
{
|
||||
if (!m_Attr.set_value(name))
|
||||
SqThrowF("Unable to set xml attribute value");
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Modify the attribute value.
|
||||
|
@ -2,24 +2,94 @@
|
||||
#include "Common.hpp"
|
||||
#include "Module.hpp"
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include <stdarg.h>
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include <sqrat.h>
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
namespace SqMod {
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
bool ParseResult::Validate() const
|
||||
static SQChar g_Buffer[4096]; // Common buffer to reduce memory allocations.
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
SStr GetTempBuff()
|
||||
{
|
||||
if (m_Doc)
|
||||
return true;
|
||||
// Invalid document reference
|
||||
_SqMod->SqThrow("Invalid XML document reference");
|
||||
return false;
|
||||
return g_Buffer;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Uint32 GetTempBuffSize()
|
||||
{
|
||||
return sizeof(g_Buffer);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void SqThrowF(CSStr str, ...)
|
||||
{
|
||||
// Initialize the argument list
|
||||
va_list args;
|
||||
va_start (args, str);
|
||||
// Write the requested contents
|
||||
if (snprintf(g_Buffer, sizeof(g_Buffer), str, args) < 0)
|
||||
strcpy(g_Buffer, "Unknown error has occurred");
|
||||
// Release the argument list
|
||||
va_end(args);
|
||||
// Throw the exception with the resulted message
|
||||
throw Sqrat::Exception(g_Buffer);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
CSStr FmtStr(CSStr str, ...)
|
||||
{
|
||||
// Initialize the argument list
|
||||
va_list args;
|
||||
va_start (args, str);
|
||||
// Write the requested contents
|
||||
if (snprintf(g_Buffer, sizeof(g_Buffer), str, args) < 0)
|
||||
g_Buffer[0] = 0; /* make sure the string is terminated */
|
||||
// Release the argument list
|
||||
va_end(args);
|
||||
// Return the data from the buffer
|
||||
return g_Buffer;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
StackGuard::StackGuard(HSQUIRRELVM vm)
|
||||
: m_Top(sq_gettop(vm)), m_VM(vm)
|
||||
{
|
||||
/* ... */
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
StackGuard::~StackGuard()
|
||||
{
|
||||
sq_pop(m_VM, sq_gettop(m_VM) - m_Top);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
SQInteger ParseResult::Typename(HSQUIRRELVM vm)
|
||||
{
|
||||
static SQChar name[] = _SC("SqXmlParseResult");
|
||||
sq_pushstring(vm, name, sizeof(name));
|
||||
return 1;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void ParseResult::Validate() const
|
||||
{
|
||||
// Is the documen handle valid?
|
||||
if (!m_Doc)
|
||||
SqThrowF("Invalid XML document reference");
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void ParseResult::Check() const
|
||||
{
|
||||
if (m_Result.status != status_ok)
|
||||
_SqMod->SqThrow("XML parse error [%s]", m_Result.description());
|
||||
SqThrowF("XML parse error [%s]", m_Result.description());
|
||||
}
|
||||
|
||||
} // Namespace:: SqMod
|
||||
|
@ -10,6 +10,12 @@
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include <pugixml.hpp>
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
extern "C" {
|
||||
struct SQVM;
|
||||
typedef struct SQVM* HSQUIRRELVM;
|
||||
} /*extern "C"*/
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
namespace SqMod {
|
||||
|
||||
@ -40,6 +46,70 @@ class XPathVariable;
|
||||
class XPathVariableSet;
|
||||
class XPathVariableQuery;
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Retrieve the temporary buffer.
|
||||
*/
|
||||
SStr GetTempBuff();
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Retrieve the size of the temporary buffer.
|
||||
*/
|
||||
Uint32 GetTempBuffSize();
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Throw a formatted exception.
|
||||
*/
|
||||
void SqThrowF(CSStr str, ...);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Generate a formatted string.
|
||||
*/
|
||||
CSStr FmtStr(CSStr str, ...);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Implements RAII to restore the VM stack to it's initial size on function exit.
|
||||
*/
|
||||
struct StackGuard
|
||||
{
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Base constructor.
|
||||
*/
|
||||
StackGuard(HSQUIRRELVM vm);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Destructor.
|
||||
*/
|
||||
~StackGuard();
|
||||
|
||||
private:
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Copy constructor.
|
||||
*/
|
||||
StackGuard(const StackGuard &);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Move constructor.
|
||||
*/
|
||||
StackGuard(StackGuard &&);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Copy assignment operator.
|
||||
*/
|
||||
StackGuard & operator = (const StackGuard &);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Move assignment operator.
|
||||
*/
|
||||
StackGuard & operator = (StackGuard &&);
|
||||
|
||||
private:
|
||||
|
||||
// --------------------------------------------------------------------------------------------
|
||||
Int32 m_Top; /* The top of the stack when this instance was created. */
|
||||
HSQUIRRELVM m_VM; /* The VM where the stack should be restored. */
|
||||
};
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Manages a reference counted xml document instance.
|
||||
*/
|
||||
@ -124,6 +194,16 @@ public:
|
||||
Grab();
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Move constructor.
|
||||
*/
|
||||
DocumentRef(DocumentRef && o)
|
||||
: m_Ptr(o.m_Ptr), m_Ref(o.m_Ref)
|
||||
{
|
||||
o.m_Ptr = NULL;
|
||||
o.m_Ref = NULL;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Destructor.
|
||||
*/
|
||||
@ -147,6 +227,21 @@ public:
|
||||
return *this;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Move assignment operator.
|
||||
*/
|
||||
DocumentRef & operator = (DocumentRef && o)
|
||||
{
|
||||
if (m_Ptr != o.m_Ptr)
|
||||
{
|
||||
m_Ptr = o.m_Ptr;
|
||||
m_Ref = o.m_Ref;
|
||||
o.m_Ptr = NULL;
|
||||
o.m_Ref = NULL;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Perform an equality comparison between two document instances.
|
||||
*/
|
||||
@ -210,7 +305,7 @@ public:
|
||||
*/
|
||||
Pointer operator -> () const
|
||||
{
|
||||
assert(m_Ptr != NULL);
|
||||
assert(m_Ptr);
|
||||
return m_Ptr;
|
||||
}
|
||||
|
||||
@ -219,7 +314,7 @@ public:
|
||||
*/
|
||||
Reference operator * () const
|
||||
{
|
||||
assert(m_Ptr != NULL);
|
||||
assert(m_Ptr);
|
||||
return *m_Ptr;
|
||||
}
|
||||
|
||||
@ -258,13 +353,13 @@ protected:
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Validate the document reference and throw an error if invalid.
|
||||
*/
|
||||
bool Validate() const;
|
||||
void Validate() const;
|
||||
|
||||
private:
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
DocumentRef m_Doc; /* The main xml document instance. */
|
||||
Result m_Result; /* The managed parse result. */
|
||||
DocumentRef m_Doc; /* The main xml document instance. */
|
||||
Result m_Result; /* The managed parse result. */
|
||||
|
||||
public:
|
||||
|
||||
@ -325,6 +420,11 @@ public:
|
||||
return m_Result.description();
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Used by the script engine to retrieve the name from instances of this type.
|
||||
*/
|
||||
static SQInteger Typename(HSQUIRRELVM vm);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* See whether this instance references a valid xml document.
|
||||
*/
|
||||
|
@ -7,27 +7,31 @@
|
||||
namespace SqMod {
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
bool Document::Validate() const
|
||||
SQInteger Document::Typename(HSQUIRRELVM vm)
|
||||
{
|
||||
if (m_Doc)
|
||||
return true;
|
||||
// Invalid document reference
|
||||
_SqMod->SqThrow("Invalid XML document reference");
|
||||
return false;
|
||||
static SQChar name[] = _SC("SqXmlDocument");
|
||||
sq_pushstring(vm, name, sizeof(name));
|
||||
return 1;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
bool Document::CanLoad() const
|
||||
void Document::Validate() const
|
||||
{
|
||||
// Validate the document handle
|
||||
if (!m_Doc)
|
||||
SqThrowF("Invalid XML document reference");
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void Document::CanLoad() const
|
||||
{
|
||||
// Is the document even valid?
|
||||
if (!Validate())
|
||||
return false;
|
||||
if (!m_Doc)
|
||||
SqThrowF("Invalid XML document reference");
|
||||
// Are there any other references?
|
||||
else if (m_Doc.Count() == 1)
|
||||
return true;
|
||||
// To load new values now, would mean to cause undefined behavior in existing references
|
||||
_SqMod->SqThrow("Loading is disabled while document is referenced");
|
||||
return false;
|
||||
else if (m_Doc.Count() > 1)
|
||||
// To load new values now, would mean to cause undefined behavior in existing references
|
||||
SqThrowF("Loading is disabled while document is referenced");
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
|
@ -30,12 +30,12 @@ protected:
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Validate the document reference and throw an error if invalid.
|
||||
*/
|
||||
bool Validate() const;
|
||||
void Validate() const;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* See if the document is allowed to overwrite its contents and throw an error if not.
|
||||
*/
|
||||
bool CanLoad() const;
|
||||
void CanLoad() const;
|
||||
|
||||
private:
|
||||
|
||||
@ -90,6 +90,11 @@ public:
|
||||
return _SC("");
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Used by the script engine to retrieve the name from instances of this type.
|
||||
*/
|
||||
static SQInteger Typename(HSQUIRRELVM vm);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* See whether this instance references a valid xml document.
|
||||
*/
|
||||
@ -103,7 +108,7 @@ public:
|
||||
*/
|
||||
Uint32 GetRefCount() const
|
||||
{
|
||||
return m_Doc ? *(m_Doc.m_Ref) : 0;
|
||||
return m_Doc.Count();
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
@ -111,8 +116,10 @@ public:
|
||||
*/
|
||||
void Reset()
|
||||
{
|
||||
if (Validate())
|
||||
m_Doc->reset();
|
||||
// Validate the document handle
|
||||
Validate();
|
||||
// Perform the requested operation
|
||||
m_Doc->reset();
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
@ -120,8 +127,11 @@ public:
|
||||
*/
|
||||
void Reset(const Document & doc)
|
||||
{
|
||||
if (Validate() && doc.Validate())
|
||||
m_Doc->reset(*doc.m_Doc);
|
||||
// Validate the document handles
|
||||
Validate();
|
||||
doc.Validate();
|
||||
// Perform the requested operation
|
||||
m_Doc->reset(*doc.m_Doc);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
@ -129,9 +139,10 @@ public:
|
||||
*/
|
||||
ParseResult LoadData(CSStr source)
|
||||
{
|
||||
if (CanLoad())
|
||||
return ParseResult(m_Doc, m_Doc->load_string(source));
|
||||
return ParseResult();
|
||||
// Make sure that we are allowed to load in data
|
||||
CanLoad();
|
||||
// Perform the requested operation and return the result
|
||||
return ParseResult(m_Doc, m_Doc->load_string(source));
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
@ -139,9 +150,10 @@ public:
|
||||
*/
|
||||
ParseResult LoadData(CSStr source, Uint32 options)
|
||||
{
|
||||
if (CanLoad())
|
||||
return ParseResult(m_Doc, m_Doc->load_string(source, options));
|
||||
return ParseResult();
|
||||
// Make sure that we are allowed to load in data
|
||||
CanLoad();
|
||||
// Perform the requested operation and return the result
|
||||
return ParseResult(m_Doc, m_Doc->load_string(source, options));
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
@ -149,9 +161,10 @@ public:
|
||||
*/
|
||||
ParseResult LoadFile(CSStr filepath)
|
||||
{
|
||||
if (CanLoad())
|
||||
return ParseResult(m_Doc, m_Doc->load_file(filepath));
|
||||
return ParseResult();
|
||||
// Make sure that we are allowed to load in data
|
||||
CanLoad();
|
||||
// Perform the requested operation and return the result
|
||||
return ParseResult(m_Doc, m_Doc->load_file(filepath));
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
@ -159,9 +172,10 @@ public:
|
||||
*/
|
||||
ParseResult LoadFile(CSStr filepath, Uint32 options)
|
||||
{
|
||||
if (CanLoad())
|
||||
return ParseResult(m_Doc, m_Doc->load_file(filepath, options));
|
||||
return ParseResult();
|
||||
// Make sure that we are allowed to load in data
|
||||
CanLoad();
|
||||
// Perform the requested operation and return the result
|
||||
return ParseResult(m_Doc, m_Doc->load_file(filepath, options));
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
@ -169,9 +183,10 @@ public:
|
||||
*/
|
||||
ParseResult LoadFile(CSStr filepath, Uint32 options, Int32 encoding)
|
||||
{
|
||||
if (CanLoad())
|
||||
return ParseResult(m_Doc, m_Doc->load_file(filepath, options, (xml_encoding)encoding));
|
||||
return ParseResult();
|
||||
// Make sure that we are allowed to load in data
|
||||
CanLoad();
|
||||
// Perform the requested operation and return the result
|
||||
return ParseResult(m_Doc, m_Doc->load_file(filepath, options, (xml_encoding)encoding));
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
@ -179,8 +194,10 @@ public:
|
||||
*/
|
||||
void SaveFile(CSStr filepath)
|
||||
{
|
||||
if (Validate())
|
||||
m_Doc->save_file(filepath);
|
||||
// Validate the document handle
|
||||
Validate();
|
||||
// Perform the requested operation
|
||||
m_Doc->save_file(filepath);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
@ -188,8 +205,10 @@ public:
|
||||
*/
|
||||
void SaveFile(CSStr filepath, CSStr indent)
|
||||
{
|
||||
if (Validate())
|
||||
m_Doc->save_file(filepath, indent);
|
||||
// Validate the document handle
|
||||
Validate();
|
||||
// Perform the requested operation
|
||||
m_Doc->save_file(filepath, indent);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
@ -197,8 +216,10 @@ public:
|
||||
*/
|
||||
void SaveFile(CSStr filepath, CSStr indent, Uint32 format)
|
||||
{
|
||||
if (Validate())
|
||||
m_Doc->save_file(filepath, indent, format);
|
||||
// Validate the document handle
|
||||
Validate();
|
||||
// Perform the requested operation
|
||||
m_Doc->save_file(filepath, indent, format);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
@ -206,8 +227,10 @@ public:
|
||||
*/
|
||||
void SaveFile(CSStr filepath, CSStr indent, Uint32 format, Int32 encoding)
|
||||
{
|
||||
if (Validate())
|
||||
m_Doc->save_file(filepath, indent, format, (xml_encoding)encoding);
|
||||
// Validate the document handle
|
||||
Validate();
|
||||
// Perform the requested operation
|
||||
m_Doc->save_file(filepath, indent, format, (xml_encoding)encoding);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
|
@ -176,6 +176,7 @@ void RegisterAPI(HSQUIRRELVM vm)
|
||||
.Ctor< const ParseResult & >()
|
||||
/* Core Metamethods */
|
||||
.Func(_SC("_cmp"), &ParseResult::Cmp)
|
||||
.SquirrelFunc(_SC("_typename"), &ParseResult::Typename)
|
||||
.Func(_SC("_tostring"), &ParseResult::ToString)
|
||||
/* Properties */
|
||||
.Prop(_SC("Valid"), &ParseResult::IsValid)
|
||||
@ -195,6 +196,7 @@ void RegisterAPI(HSQUIRRELVM vm)
|
||||
.Ctor< const Attribute & >()
|
||||
/* Core Metamethods */
|
||||
.Func(_SC("_cmp"), &Attribute::Cmp)
|
||||
.SquirrelFunc(_SC("_typename"), &Attribute::Typename)
|
||||
.Func(_SC("_tostring"), &Attribute::ToString)
|
||||
/* Properties */
|
||||
.Prop(_SC("Valid"), &Attribute::IsValid)
|
||||
@ -239,6 +241,7 @@ void RegisterAPI(HSQUIRRELVM vm)
|
||||
.Ctor< const Text & >()
|
||||
/* Core Metamethods */
|
||||
.Func(_SC("_cmp"), &Text::Cmp)
|
||||
.SquirrelFunc(_SC("_typename"), &Text::Typename)
|
||||
.Func(_SC("_tostring"), &Text::ToString)
|
||||
/* Properties */
|
||||
.Prop(_SC("Valid"), &Text::IsValid)
|
||||
@ -278,6 +281,7 @@ void RegisterAPI(HSQUIRRELVM vm)
|
||||
.Ctor< const Node & >()
|
||||
/* Core Metamethods */
|
||||
.Func(_SC("_cmp"), &Node::Cmp)
|
||||
.SquirrelFunc(_SC("_typename"), &Node::Typename)
|
||||
.Func(_SC("_tostring"), &Node::ToString)
|
||||
/* Properties */
|
||||
.Prop(_SC("Valid"), &Node::IsValid)
|
||||
@ -352,6 +356,7 @@ void RegisterAPI(HSQUIRRELVM vm)
|
||||
.Ctor()
|
||||
/* Core Metamethods */
|
||||
.Func(_SC("_cmp"), &Document::Cmp)
|
||||
.SquirrelFunc(_SC("_typename"), &Document::Typename)
|
||||
.Func(_SC("_tostring"), &Document::ToString)
|
||||
/* Properties */
|
||||
.Prop(_SC("Valid"), &Document::IsValid)
|
||||
|
@ -8,27 +8,19 @@
|
||||
namespace SqMod {
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
bool Node::Validate() const
|
||||
SQInteger Node::Typename(HSQUIRRELVM vm)
|
||||
{
|
||||
if (m_Doc)
|
||||
return true;
|
||||
// Invalid document reference
|
||||
_SqMod->SqThrow("Invalid XML document reference");
|
||||
return false;
|
||||
static SQChar name[] = _SC("SqXmlNode");
|
||||
sq_pushstring(vm, name, sizeof(name));
|
||||
return 1;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void Node::SetName(CSStr name)
|
||||
void Node::Validate() const
|
||||
{
|
||||
if (!m_Node.set_name(name))
|
||||
_SqMod->SqThrow("Unable to set XML node name");
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void Node::SetValue(CSStr name)
|
||||
{
|
||||
if (!m_Node.set_value(name))
|
||||
_SqMod->SqThrow("Unable to set XML node value");
|
||||
// Validate the document handle
|
||||
if (!m_Doc)
|
||||
SqThrowF("Invalid XML document reference");
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
|
@ -33,7 +33,7 @@ protected:
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Validate the document reference and throw an error if invalid.
|
||||
*/
|
||||
bool Validate() const;
|
||||
void Validate() const;
|
||||
|
||||
private:
|
||||
|
||||
@ -100,6 +100,11 @@ public:
|
||||
return m_Node.value();
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Used by the script engine to retrieve the name from instances of this type.
|
||||
*/
|
||||
static SQInteger Typename(HSQUIRRELVM vm);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* See whether this instance references a valid xml document.
|
||||
*/
|
||||
@ -159,7 +164,11 @@ public:
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve node name.
|
||||
*/
|
||||
void SetName(CSStr name);
|
||||
void SetName(CSStr name)
|
||||
{
|
||||
if (!m_Node.set_name(name))
|
||||
SqThrowF("Unable to set XML node name");
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Modify the node name.
|
||||
@ -180,7 +189,11 @@ public:
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve node value.
|
||||
*/
|
||||
void SetValue(CSStr name);
|
||||
void SetValue(CSStr name)
|
||||
{
|
||||
if (!m_Node.set_value(name))
|
||||
SqThrowF("Unable to set XML node value");
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Modify the node value.
|
||||
|
@ -11,13 +11,19 @@
|
||||
namespace SqMod {
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
bool Text::Validate() const
|
||||
SQInteger Text::Typename(HSQUIRRELVM vm)
|
||||
{
|
||||
if (m_Doc)
|
||||
return true;
|
||||
// Invalid document reference
|
||||
_SqMod->SqThrow("Invalid XML document reference");
|
||||
return false;
|
||||
static SQChar name[] = _SC("SqXmlText");
|
||||
sq_pushstring(vm, name, sizeof(name));
|
||||
return 1;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void Text::Validate() const
|
||||
{
|
||||
// Validate the document handle
|
||||
if (!m_Doc)
|
||||
SqThrowF("Invalid XML document reference");
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
@ -35,158 +41,122 @@ Int32 Text::Cmp(const Text & o)
|
||||
Object Text::AsLong(Object & def) const
|
||||
{
|
||||
// Obtain the initial stack size
|
||||
const Int32 top = sq_gettop(_SqVM);
|
||||
const StackGuard sg(_SqVM);
|
||||
// Push the specified object onto the stack
|
||||
Var< Object >::push(_SqVM, def);
|
||||
// The resulted long integer value
|
||||
Int64 longint = 0;
|
||||
// Attempt to get the numeric value inside the specified object
|
||||
if (SQ_FAILED(_SqMod->GetSLongValue(_SqVM, -1, &longint)))
|
||||
_SqMod->SqThrow("Invalid long integer specified");
|
||||
SqThrowF("Invalid long integer specified");
|
||||
// Push a long integer instance with the requested value on the stack
|
||||
_SqMod->PushSLongObject(_SqVM, m_Text.as_llong(longint));
|
||||
// Obtain the object from the stack
|
||||
Var< Object > inst(_SqVM, -1);
|
||||
// Remove an pushed values (if any) to restore the stack
|
||||
sq_pop(_SqVM, sq_gettop(_SqVM) - top);
|
||||
// Return the long integer instance
|
||||
return inst.value;
|
||||
// Obtain the object from the stack and return it
|
||||
return Var< Object >(_SqVM, -1).value;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Object Text::AsUlong(Object & def) const
|
||||
{
|
||||
// Obtain the initial stack size
|
||||
const Int32 top = sq_gettop(_SqVM);
|
||||
const StackGuard sg(_SqVM);
|
||||
// Push the specified object onto the stack
|
||||
Var< Object >::push(_SqVM, def);
|
||||
// The resulted long integer value
|
||||
Uint64 longint = 0;
|
||||
// Attempt to get the numeric value inside the specified object
|
||||
if (SQ_FAILED(_SqMod->GetULongValue(_SqVM, -1, &longint)))
|
||||
_SqMod->SqThrow("Invalid long integer specified");
|
||||
SqThrowF("Invalid long integer specified");
|
||||
// Push a long integer instance with the requested value on the stack
|
||||
_SqMod->PushULongObject(_SqVM, m_Text.as_ullong(longint));
|
||||
// Obtain the object from the stack
|
||||
Var< Object > inst(_SqVM, -1);
|
||||
// Remove an pushed values (if any) to restore the stack
|
||||
sq_pop(_SqVM, sq_gettop(_SqVM) - top);
|
||||
// Return the long integer instance
|
||||
return inst.value;
|
||||
// Obtain the object from the stack and return it
|
||||
return Var< Object >(_SqVM, -1).value;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
bool Text::ApplyLong(Object & value)
|
||||
{
|
||||
// Obtain the initial stack size
|
||||
const Int32 top = sq_gettop(_SqVM);
|
||||
const StackGuard sg(_SqVM);
|
||||
// Push the specified object onto the stack
|
||||
Var< Object & >::push(_SqVM, value);
|
||||
// The resulted long integer value
|
||||
Int64 longint = 0;
|
||||
// Whether the operation succeeded
|
||||
bool res = false;
|
||||
// Attempt to get the numeric value inside the specified object
|
||||
if (SQ_FAILED(_SqMod->GetSLongValue(_SqVM, -1, &longint)))
|
||||
_SqMod->SqThrow("Invalid long integer specified");
|
||||
// Assign the obtained value
|
||||
else
|
||||
res = m_Text.set(longint);
|
||||
// Remove an pushed values (if any) to restore the stack
|
||||
sq_pop(_SqVM, sq_gettop(_SqVM) - top);
|
||||
// Return the result
|
||||
return res;
|
||||
SqThrowF("Invalid long integer specified");
|
||||
// Assign the obtained value and return the result
|
||||
return m_Text.set(longint);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
bool Text::ApplyUlong(Object & value)
|
||||
{
|
||||
// Obtain the initial stack size
|
||||
const Int32 top = sq_gettop(_SqVM);
|
||||
const StackGuard sg(_SqVM);
|
||||
// Push the specified object onto the stack
|
||||
Var< Object & >::push(_SqVM, value);
|
||||
// The resulted long integer value
|
||||
Uint64 longint = 0;
|
||||
// Whether the operation succeeded
|
||||
bool res = false;
|
||||
// Attempt to get the numeric value inside the specified object
|
||||
if (SQ_FAILED(_SqMod->GetULongValue(_SqVM, -1, &longint)))
|
||||
_SqMod->SqThrow("Invalid long integer specified");
|
||||
// Assign the obtained value
|
||||
else
|
||||
res = m_Text.set(longint);
|
||||
// Remove an pushed values (if any) to restore the stack
|
||||
sq_pop(_SqVM, sq_gettop(_SqVM) - top);
|
||||
// Return the result
|
||||
return res;
|
||||
SqThrowF("Invalid long integer specified");
|
||||
// Assign the obtained value and return the result
|
||||
return m_Text.set(longint);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Object Text::GetLong() const
|
||||
{
|
||||
// Obtain the initial stack size
|
||||
const Int32 top = sq_gettop(_SqVM);
|
||||
const StackGuard sg(_SqVM);
|
||||
// Push a long integer instance with the requested value on the stack
|
||||
_SqMod->PushSLongObject(_SqVM, m_Text.as_llong());
|
||||
// Obtain the object from the stack
|
||||
Var< Object > inst(_SqVM, -1);
|
||||
// Remove an pushed values (if any) to restore the stack
|
||||
sq_pop(_SqVM, sq_gettop(_SqVM) - top);
|
||||
// Return the long integer instance
|
||||
return inst.value;
|
||||
// Obtain the object from the stack and return it
|
||||
return Var< Object >(_SqVM, -1).value;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void Text::SetLong(Object & value)
|
||||
{
|
||||
// Obtain the initial stack size
|
||||
const Int32 top = sq_gettop(_SqVM);
|
||||
const StackGuard sg(_SqVM);
|
||||
// Push the specified object onto the stack
|
||||
Var< Object & >::push(_SqVM, value);
|
||||
// The resulted long integer value
|
||||
Int64 longint = 0;
|
||||
// Attempt to get the numeric value inside the specified object
|
||||
if (SQ_FAILED(_SqMod->GetSLongValue(_SqVM, -1, &longint)))
|
||||
_SqMod->SqThrow("Invalid long integer specified");
|
||||
SqThrowF("Invalid long integer specified");
|
||||
// Assign the obtained value
|
||||
else
|
||||
m_Text = longint;
|
||||
// Remove an pushed values (if any) to restore the stack
|
||||
sq_pop(_SqVM, sq_gettop(_SqVM) - top);
|
||||
m_Text = longint;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Object Text::GetUlong() const
|
||||
{
|
||||
// Obtain the initial stack size
|
||||
const Int32 top = sq_gettop(_SqVM);
|
||||
const StackGuard sg(_SqVM);
|
||||
// Push a long integer instance with the requested value on the stack
|
||||
_SqMod->PushULongObject(_SqVM, m_Text.as_ullong());
|
||||
// Obtain the object from the stack
|
||||
Var< Object > inst(_SqVM, -1);
|
||||
// Remove an pushed values (if any) to restore the stack
|
||||
sq_pop(_SqVM, sq_gettop(_SqVM) - top);
|
||||
// Return the long integer instance
|
||||
return inst.value;
|
||||
// Obtain the object from the stack and return it
|
||||
return Var< Object >(_SqVM, -1).value;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void Text::SetUlong(Object & value)
|
||||
{
|
||||
// Obtain the initial stack size
|
||||
const Int32 top = sq_gettop(_SqVM);
|
||||
const StackGuard sg(_SqVM);
|
||||
// Push the specified object onto the stack
|
||||
Var< Object & >::push(_SqVM, value);
|
||||
// The resulted long integer value
|
||||
Uint64 longint = 0;
|
||||
// Attempt to get the numeric value inside the specified object
|
||||
if (SQ_FAILED(_SqMod->GetULongValue(_SqVM, -1, &longint)))
|
||||
_SqMod->SqThrow("Invalid long integer specified");
|
||||
SqThrowF("Invalid long integer specified");
|
||||
// Assign the obtained value
|
||||
else
|
||||
m_Text = longint;
|
||||
// Remove an pushed values (if any) to restore the stack
|
||||
sq_pop(_SqVM, sq_gettop(_SqVM) - top);
|
||||
m_Text = longint;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
|
@ -32,7 +32,7 @@ protected:
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Validate the document reference and throw an error if invalid.
|
||||
*/
|
||||
bool Validate() const;
|
||||
void Validate() const;
|
||||
|
||||
private:
|
||||
|
||||
@ -90,6 +90,11 @@ public:
|
||||
return m_Text.get();
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Used by the script engine to retrieve the name from instances of this type.
|
||||
*/
|
||||
static SQInteger Typename(HSQUIRRELVM vm);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* See whether this instance references a valid xml document.
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user