1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-01-31 09:57:14 +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:
Sandu Liviu Catalin 2016-02-28 17:39:26 +02:00
parent 47eec5cb10
commit 3162221e7f
12 changed files with 383 additions and 229 deletions

View File

@ -361,7 +361,10 @@
</Build> </Build>
<Compiler> <Compiler>
<Add option="-Wall" /> <Add option="-Wall" />
<Add option="-std=c++14" />
<Add option="-DSQMOD_PLUGIN_API" /> <Add option="-DSQMOD_PLUGIN_API" />
<Add option="-DSCRAT_USE_EXCEPTIONS" />
<Add option="-DSCRAT_USE_CXX11_OPTIMIZATIONS" />
<Add directory="../modules/xml" /> <Add directory="../modules/xml" />
<Add directory="../shared" /> <Add directory="../shared" />
<Add directory="../include" /> <Add directory="../include" />

View File

@ -9,185 +9,141 @@
namespace SqMod { namespace SqMod {
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
bool Attribute::Validate() const SQInteger Attribute::Typename(HSQUIRRELVM vm)
{ {
if (m_Doc) static SQChar name[] = _SC("SqXmlAttribute");
return true; sq_pushstring(vm, name, sizeof(name));
// Invalid document reference return 1;
_SqMod->SqThrow("Invalid XML document reference");
return false;
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void Attribute::SetName(CSStr name) void Attribute::Validate() const
{ {
if (!m_Attr.set_name(name)) // Validate the document handle
_SqMod->SqThrow("Unable to set xml attribute name"); if (!m_Doc)
} SqThrowF("Invalid XML document reference");
// ------------------------------------------------------------------------------------------------
void Attribute::SetValue(CSStr name)
{
if (!m_Attr.set_value(name))
_SqMod->SqThrow("Unable to set xml attribute value");
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
Object Attribute::AsLong(Object & def) const Object Attribute::AsLong(Object & def) const
{ {
// Obtain the initial stack size // Obtain the initial stack size
const Int32 top = sq_gettop(_SqVM); const StackGuard sg(_SqVM);
// Push the specified object onto the stack // Push the specified object onto the stack
Var< Object >::push(_SqVM, def); Var< Object >::push(_SqVM, def);
// The resulted long integer value // The resulted long integer value
Int64 longint = 0; Int64 longint = 0;
// Attempt to get the numeric value inside the specified object // Attempt to get the numeric value inside the specified object
if (SQ_FAILED(_SqMod->GetSLongValue(_SqVM, -1, &longint))) 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 // Push a long integer instance with the requested value on the stack
_SqMod->PushSLongObject(_SqVM, m_Attr.as_llong(longint)); _SqMod->PushSLongObject(_SqVM, m_Attr.as_llong(longint));
// Obtain the object from the stack // Obtain the object from the stack and return it
Var< Object > inst(_SqVM, -1); return Var< Object >(_SqVM, -1).value;
// 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;
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
Object Attribute::AsUlong(Object & def) const Object Attribute::AsUlong(Object & def) const
{ {
// Obtain the initial stack size // Obtain the initial stack size
const Int32 top = sq_gettop(_SqVM); const StackGuard sg(_SqVM);
// Push the specified object onto the stack // Push the specified object onto the stack
Var< Object >::push(_SqVM, def); Var< Object >::push(_SqVM, def);
// The resulted long integer value // The resulted long integer value
Uint64 longint = 0; Uint64 longint = 0;
// Attempt to get the numeric value inside the specified object // Attempt to get the numeric value inside the specified object
if (SQ_FAILED(_SqMod->GetULongValue(_SqVM, -1, &longint))) 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 // Push a long integer instance with the requested value on the stack
_SqMod->PushULongObject(_SqVM, m_Attr.as_ullong(longint)); _SqMod->PushULongObject(_SqVM, m_Attr.as_ullong(longint));
// Obtain the object from the stack // Obtain the object from the stack and return it
Var< Object > inst(_SqVM, -1); return Var< Object >(_SqVM, -1).value;
// 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;
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
bool Attribute::ApplyLong(Object & value) bool Attribute::ApplyLong(Object & value)
{ {
// Obtain the initial stack size // Obtain the initial stack size
const Int32 top = sq_gettop(_SqVM); const StackGuard sg(_SqVM);
// Push the specified object onto the stack // Push the specified object onto the stack
Var< Object & >::push(_SqVM, value); Var< Object & >::push(_SqVM, value);
// The resulted long integer value // The resulted long integer value
Int64 longint = 0; Int64 longint = 0;
// Whether the operation succeeded
bool res = false;
// Attempt to get the numeric value inside the specified object // Attempt to get the numeric value inside the specified object
if (SQ_FAILED(_SqMod->GetSLongValue(_SqVM, -1, &longint))) if (SQ_FAILED(_SqMod->GetSLongValue(_SqVM, -1, &longint)))
_SqMod->SqThrow("Invalid long integer specified"); SqThrowF("Invalid long integer specified");
// Assign the obtained value // Assign the obtained value and return the result
else return m_Attr.set_value(longint);
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;
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
bool Attribute::ApplyUlong(Object & value) bool Attribute::ApplyUlong(Object & value)
{ {
// Obtain the initial stack size // Obtain the initial stack size
const Int32 top = sq_gettop(_SqVM); const StackGuard sg(_SqVM);
// Push the specified object onto the stack // Push the specified object onto the stack
Var< Object & >::push(_SqVM, value); Var< Object & >::push(_SqVM, value);
// The resulted long integer value // The resulted long integer value
Uint64 longint = 0; Uint64 longint = 0;
// Whether the operation succeeded
bool res = false;
// Attempt to get the numeric value inside the specified object // Attempt to get the numeric value inside the specified object
if (SQ_FAILED(_SqMod->GetULongValue(_SqVM, -1, &longint))) if (SQ_FAILED(_SqMod->GetULongValue(_SqVM, -1, &longint)))
_SqMod->SqThrow("Invalid long integer specified"); SqThrowF("Invalid long integer specified");
// Assign the obtained value // Assign the obtained value and return the result
else return m_Attr.set_value(longint);
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;
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
Object Attribute::GetLong() const Object Attribute::GetLong() const
{ {
// Obtain the initial stack size // 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 // Push a long integer instance with the requested value on the stack
_SqMod->PushSLongObject(_SqVM, m_Attr.as_llong()); _SqMod->PushSLongObject(_SqVM, m_Attr.as_llong());
// Obtain the object from the stack // Obtain the object from the stack and return it
Var< Object > inst(_SqVM, -1); return Var< Object >(_SqVM, -1).value;
// 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;
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void Attribute::SetLong(Object & value) void Attribute::SetLong(Object & value)
{ {
// Obtain the initial stack size // Obtain the initial stack size
const Int32 top = sq_gettop(_SqVM); const StackGuard sg(_SqVM);
// Push the specified object onto the stack // Push the specified object onto the stack
Var< Object & >::push(_SqVM, value); Var< Object & >::push(_SqVM, value);
// The resulted long integer value // The resulted long integer value
Int64 longint = 0; Int64 longint = 0;
// Attempt to get the numeric value inside the specified object // Attempt to get the numeric value inside the specified object
if (SQ_FAILED(_SqMod->GetSLongValue(_SqVM, -1, &longint))) if (SQ_FAILED(_SqMod->GetSLongValue(_SqVM, -1, &longint)))
_SqMod->SqThrow("Invalid long integer specified"); SqThrowF("Invalid long integer specified");
// Assign the obtained value // Assign the obtained value
else m_Attr = longint;
m_Attr = longint;
// Remove an pushed values (if any) to restore the stack
sq_pop(_SqVM, sq_gettop(_SqVM) - top);
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
Object Attribute::GetUlong() const Object Attribute::GetUlong() const
{ {
// Obtain the initial stack size // 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 // Push a long integer instance with the requested value on the stack
_SqMod->PushULongObject(_SqVM, m_Attr.as_ullong()); _SqMod->PushULongObject(_SqVM, m_Attr.as_ullong());
// Obtain the object from the stack // Obtain the object from the stack and return it
Var< Object > inst(_SqVM, -1); return Var< Object >(_SqVM, -1).value;
// 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;
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void Attribute::SetUlong(Object & value) void Attribute::SetUlong(Object & value)
{ {
// Obtain the initial stack size // Obtain the initial stack size
const Int32 top = sq_gettop(_SqVM); const StackGuard sg(_SqVM);
// Push the specified object onto the stack // Push the specified object onto the stack
Var< Object & >::push(_SqVM, value); Var< Object & >::push(_SqVM, value);
// The resulted long integer value // The resulted long integer value
Uint64 longint = 0; Uint64 longint = 0;
// Attempt to get the numeric value inside the specified object // Attempt to get the numeric value inside the specified object
if (SQ_FAILED(_SqMod->GetULongValue(_SqVM, -1, &longint))) if (SQ_FAILED(_SqMod->GetULongValue(_SqVM, -1, &longint)))
_SqMod->SqThrow("Invalid long integer specified"); SqThrowF("Invalid long integer specified");
// Assign the obtained value // Assign the obtained value
else m_Attr = longint;
m_Attr = longint;
// Remove an pushed values (if any) to restore the stack
sq_pop(_SqVM, sq_gettop(_SqVM) - top);
} }
} // Namespace:: SqMod } // Namespace:: SqMod

View File

@ -32,7 +32,7 @@ protected:
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Validate the document reference and throw an error if invalid. * Validate the document reference and throw an error if invalid.
*/ */
bool Validate() const; void Validate() const;
private: private:
@ -99,6 +99,11 @@ public:
return m_Attr.value(); 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. * See whether this instance references a valid xml document.
*/ */
@ -142,7 +147,11 @@ public:
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Retrieve attribute name. * 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. * Modify the attribute name.
@ -163,7 +172,11 @@ public:
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Retrieve attribute value. * 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. * Modify the attribute value.

View File

@ -2,24 +2,94 @@
#include "Common.hpp" #include "Common.hpp"
#include "Module.hpp" #include "Module.hpp"
// ------------------------------------------------------------------------------------------------
#include <stdarg.h>
// ------------------------------------------------------------------------------------------------
#include <sqrat.h>
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
namespace SqMod { namespace SqMod {
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
bool ParseResult::Validate() const static SQChar g_Buffer[4096]; // Common buffer to reduce memory allocations.
// ------------------------------------------------------------------------------------------------
SStr GetTempBuff()
{ {
if (m_Doc) return g_Buffer;
return true; }
// Invalid document reference
_SqMod->SqThrow("Invalid XML document reference"); // ------------------------------------------------------------------------------------------------
return false; 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 void ParseResult::Check() const
{ {
if (m_Result.status != status_ok) 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 } // Namespace:: SqMod

View File

@ -10,6 +10,12 @@
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
#include <pugixml.hpp> #include <pugixml.hpp>
// ------------------------------------------------------------------------------------------------
extern "C" {
struct SQVM;
typedef struct SQVM* HSQUIRRELVM;
} /*extern "C"*/
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
namespace SqMod { namespace SqMod {
@ -40,6 +46,70 @@ class XPathVariable;
class XPathVariableSet; class XPathVariableSet;
class XPathVariableQuery; 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. * Manages a reference counted xml document instance.
*/ */
@ -124,6 +194,16 @@ public:
Grab(); 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. * Destructor.
*/ */
@ -147,6 +227,21 @@ public:
return *this; 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. * Perform an equality comparison between two document instances.
*/ */
@ -210,7 +305,7 @@ public:
*/ */
Pointer operator -> () const Pointer operator -> () const
{ {
assert(m_Ptr != NULL); assert(m_Ptr);
return m_Ptr; return m_Ptr;
} }
@ -219,7 +314,7 @@ public:
*/ */
Reference operator * () const Reference operator * () const
{ {
assert(m_Ptr != NULL); assert(m_Ptr);
return *m_Ptr; return *m_Ptr;
} }
@ -258,13 +353,13 @@ protected:
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Validate the document reference and throw an error if invalid. * Validate the document reference and throw an error if invalid.
*/ */
bool Validate() const; void Validate() const;
private: private:
// --------------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------------
DocumentRef m_Doc; /* The main xml document instance. */ DocumentRef m_Doc; /* The main xml document instance. */
Result m_Result; /* The managed parse result. */ Result m_Result; /* The managed parse result. */
public: public:
@ -325,6 +420,11 @@ public:
return m_Result.description(); 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. * See whether this instance references a valid xml document.
*/ */

View File

@ -7,27 +7,31 @@
namespace SqMod { namespace SqMod {
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
bool Document::Validate() const SQInteger Document::Typename(HSQUIRRELVM vm)
{ {
if (m_Doc) static SQChar name[] = _SC("SqXmlDocument");
return true; sq_pushstring(vm, name, sizeof(name));
// Invalid document reference return 1;
_SqMod->SqThrow("Invalid XML document reference");
return false;
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
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? // Is the document even valid?
if (!Validate()) if (!m_Doc)
return false; SqThrowF("Invalid XML document reference");
// Are there any other references? // Are there any other references?
else if (m_Doc.Count() == 1) else if (m_Doc.Count() > 1)
return true; // To load new values now, would mean to cause undefined behavior in existing references
// To load new values now, would mean to cause undefined behavior in existing references SqThrowF("Loading is disabled while document is referenced");
_SqMod->SqThrow("Loading is disabled while document is referenced");
return false;
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------

View File

@ -30,12 +30,12 @@ protected:
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Validate the document reference and throw an error if invalid. * 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. * See if the document is allowed to overwrite its contents and throw an error if not.
*/ */
bool CanLoad() const; void CanLoad() const;
private: private:
@ -90,6 +90,11 @@ public:
return _SC(""); 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. * See whether this instance references a valid xml document.
*/ */
@ -103,7 +108,7 @@ public:
*/ */
Uint32 GetRefCount() const Uint32 GetRefCount() const
{ {
return m_Doc ? *(m_Doc.m_Ref) : 0; return m_Doc.Count();
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -111,8 +116,10 @@ public:
*/ */
void Reset() void Reset()
{ {
if (Validate()) // Validate the document handle
m_Doc->reset(); Validate();
// Perform the requested operation
m_Doc->reset();
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -120,8 +127,11 @@ public:
*/ */
void Reset(const Document & doc) void Reset(const Document & doc)
{ {
if (Validate() && doc.Validate()) // Validate the document handles
m_Doc->reset(*doc.m_Doc); Validate();
doc.Validate();
// Perform the requested operation
m_Doc->reset(*doc.m_Doc);
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -129,9 +139,10 @@ public:
*/ */
ParseResult LoadData(CSStr source) ParseResult LoadData(CSStr source)
{ {
if (CanLoad()) // Make sure that we are allowed to load in data
return ParseResult(m_Doc, m_Doc->load_string(source)); CanLoad();
return ParseResult(); // 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) ParseResult LoadData(CSStr source, Uint32 options)
{ {
if (CanLoad()) // Make sure that we are allowed to load in data
return ParseResult(m_Doc, m_Doc->load_string(source, options)); CanLoad();
return ParseResult(); // 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) ParseResult LoadFile(CSStr filepath)
{ {
if (CanLoad()) // Make sure that we are allowed to load in data
return ParseResult(m_Doc, m_Doc->load_file(filepath)); CanLoad();
return ParseResult(); // 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) ParseResult LoadFile(CSStr filepath, Uint32 options)
{ {
if (CanLoad()) // Make sure that we are allowed to load in data
return ParseResult(m_Doc, m_Doc->load_file(filepath, options)); CanLoad();
return ParseResult(); // 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) ParseResult LoadFile(CSStr filepath, Uint32 options, Int32 encoding)
{ {
if (CanLoad()) // Make sure that we are allowed to load in data
return ParseResult(m_Doc, m_Doc->load_file(filepath, options, (xml_encoding)encoding)); CanLoad();
return ParseResult(); // 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) void SaveFile(CSStr filepath)
{ {
if (Validate()) // Validate the document handle
m_Doc->save_file(filepath); Validate();
// Perform the requested operation
m_Doc->save_file(filepath);
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -188,8 +205,10 @@ public:
*/ */
void SaveFile(CSStr filepath, CSStr indent) void SaveFile(CSStr filepath, CSStr indent)
{ {
if (Validate()) // Validate the document handle
m_Doc->save_file(filepath, indent); Validate();
// Perform the requested operation
m_Doc->save_file(filepath, indent);
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -197,8 +216,10 @@ public:
*/ */
void SaveFile(CSStr filepath, CSStr indent, Uint32 format) void SaveFile(CSStr filepath, CSStr indent, Uint32 format)
{ {
if (Validate()) // Validate the document handle
m_Doc->save_file(filepath, indent, format); 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) void SaveFile(CSStr filepath, CSStr indent, Uint32 format, Int32 encoding)
{ {
if (Validate()) // Validate the document handle
m_Doc->save_file(filepath, indent, format, (xml_encoding)encoding); Validate();
// Perform the requested operation
m_Doc->save_file(filepath, indent, format, (xml_encoding)encoding);
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------

View File

@ -176,6 +176,7 @@ void RegisterAPI(HSQUIRRELVM vm)
.Ctor< const ParseResult & >() .Ctor< const ParseResult & >()
/* Core Metamethods */ /* Core Metamethods */
.Func(_SC("_cmp"), &ParseResult::Cmp) .Func(_SC("_cmp"), &ParseResult::Cmp)
.SquirrelFunc(_SC("_typename"), &ParseResult::Typename)
.Func(_SC("_tostring"), &ParseResult::ToString) .Func(_SC("_tostring"), &ParseResult::ToString)
/* Properties */ /* Properties */
.Prop(_SC("Valid"), &ParseResult::IsValid) .Prop(_SC("Valid"), &ParseResult::IsValid)
@ -195,6 +196,7 @@ void RegisterAPI(HSQUIRRELVM vm)
.Ctor< const Attribute & >() .Ctor< const Attribute & >()
/* Core Metamethods */ /* Core Metamethods */
.Func(_SC("_cmp"), &Attribute::Cmp) .Func(_SC("_cmp"), &Attribute::Cmp)
.SquirrelFunc(_SC("_typename"), &Attribute::Typename)
.Func(_SC("_tostring"), &Attribute::ToString) .Func(_SC("_tostring"), &Attribute::ToString)
/* Properties */ /* Properties */
.Prop(_SC("Valid"), &Attribute::IsValid) .Prop(_SC("Valid"), &Attribute::IsValid)
@ -239,6 +241,7 @@ void RegisterAPI(HSQUIRRELVM vm)
.Ctor< const Text & >() .Ctor< const Text & >()
/* Core Metamethods */ /* Core Metamethods */
.Func(_SC("_cmp"), &Text::Cmp) .Func(_SC("_cmp"), &Text::Cmp)
.SquirrelFunc(_SC("_typename"), &Text::Typename)
.Func(_SC("_tostring"), &Text::ToString) .Func(_SC("_tostring"), &Text::ToString)
/* Properties */ /* Properties */
.Prop(_SC("Valid"), &Text::IsValid) .Prop(_SC("Valid"), &Text::IsValid)
@ -278,6 +281,7 @@ void RegisterAPI(HSQUIRRELVM vm)
.Ctor< const Node & >() .Ctor< const Node & >()
/* Core Metamethods */ /* Core Metamethods */
.Func(_SC("_cmp"), &Node::Cmp) .Func(_SC("_cmp"), &Node::Cmp)
.SquirrelFunc(_SC("_typename"), &Node::Typename)
.Func(_SC("_tostring"), &Node::ToString) .Func(_SC("_tostring"), &Node::ToString)
/* Properties */ /* Properties */
.Prop(_SC("Valid"), &Node::IsValid) .Prop(_SC("Valid"), &Node::IsValid)
@ -352,6 +356,7 @@ void RegisterAPI(HSQUIRRELVM vm)
.Ctor() .Ctor()
/* Core Metamethods */ /* Core Metamethods */
.Func(_SC("_cmp"), &Document::Cmp) .Func(_SC("_cmp"), &Document::Cmp)
.SquirrelFunc(_SC("_typename"), &Document::Typename)
.Func(_SC("_tostring"), &Document::ToString) .Func(_SC("_tostring"), &Document::ToString)
/* Properties */ /* Properties */
.Prop(_SC("Valid"), &Document::IsValid) .Prop(_SC("Valid"), &Document::IsValid)

View File

@ -8,27 +8,19 @@
namespace SqMod { namespace SqMod {
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
bool Node::Validate() const SQInteger Node::Typename(HSQUIRRELVM vm)
{ {
if (m_Doc) static SQChar name[] = _SC("SqXmlNode");
return true; sq_pushstring(vm, name, sizeof(name));
// Invalid document reference return 1;
_SqMod->SqThrow("Invalid XML document reference");
return false;
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void Node::SetName(CSStr name) void Node::Validate() const
{ {
if (!m_Node.set_name(name)) // Validate the document handle
_SqMod->SqThrow("Unable to set XML node name"); if (!m_Doc)
} SqThrowF("Invalid XML document reference");
// ------------------------------------------------------------------------------------------------
void Node::SetValue(CSStr name)
{
if (!m_Node.set_value(name))
_SqMod->SqThrow("Unable to set XML node value");
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------

View File

@ -33,7 +33,7 @@ protected:
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Validate the document reference and throw an error if invalid. * Validate the document reference and throw an error if invalid.
*/ */
bool Validate() const; void Validate() const;
private: private:
@ -100,6 +100,11 @@ public:
return m_Node.value(); 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. * See whether this instance references a valid xml document.
*/ */
@ -159,7 +164,11 @@ public:
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Retrieve node name. * 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. * Modify the node name.
@ -180,7 +189,11 @@ public:
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Retrieve node value. * 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. * Modify the node value.

View File

@ -11,13 +11,19 @@
namespace SqMod { namespace SqMod {
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
bool Text::Validate() const SQInteger Text::Typename(HSQUIRRELVM vm)
{ {
if (m_Doc) static SQChar name[] = _SC("SqXmlText");
return true; sq_pushstring(vm, name, sizeof(name));
// Invalid document reference return 1;
_SqMod->SqThrow("Invalid XML document reference"); }
return false;
// ------------------------------------------------------------------------------------------------
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 Object Text::AsLong(Object & def) const
{ {
// Obtain the initial stack size // Obtain the initial stack size
const Int32 top = sq_gettop(_SqVM); const StackGuard sg(_SqVM);
// Push the specified object onto the stack // Push the specified object onto the stack
Var< Object >::push(_SqVM, def); Var< Object >::push(_SqVM, def);
// The resulted long integer value // The resulted long integer value
Int64 longint = 0; Int64 longint = 0;
// Attempt to get the numeric value inside the specified object // Attempt to get the numeric value inside the specified object
if (SQ_FAILED(_SqMod->GetSLongValue(_SqVM, -1, &longint))) 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 // Push a long integer instance with the requested value on the stack
_SqMod->PushSLongObject(_SqVM, m_Text.as_llong(longint)); _SqMod->PushSLongObject(_SqVM, m_Text.as_llong(longint));
// Obtain the object from the stack // Obtain the object from the stack and return it
Var< Object > inst(_SqVM, -1); return Var< Object >(_SqVM, -1).value;
// 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;
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
Object Text::AsUlong(Object & def) const Object Text::AsUlong(Object & def) const
{ {
// Obtain the initial stack size // Obtain the initial stack size
const Int32 top = sq_gettop(_SqVM); const StackGuard sg(_SqVM);
// Push the specified object onto the stack // Push the specified object onto the stack
Var< Object >::push(_SqVM, def); Var< Object >::push(_SqVM, def);
// The resulted long integer value // The resulted long integer value
Uint64 longint = 0; Uint64 longint = 0;
// Attempt to get the numeric value inside the specified object // Attempt to get the numeric value inside the specified object
if (SQ_FAILED(_SqMod->GetULongValue(_SqVM, -1, &longint))) 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 // Push a long integer instance with the requested value on the stack
_SqMod->PushULongObject(_SqVM, m_Text.as_ullong(longint)); _SqMod->PushULongObject(_SqVM, m_Text.as_ullong(longint));
// Obtain the object from the stack // Obtain the object from the stack and return it
Var< Object > inst(_SqVM, -1); return Var< Object >(_SqVM, -1).value;
// 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;
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
bool Text::ApplyLong(Object & value) bool Text::ApplyLong(Object & value)
{ {
// Obtain the initial stack size // Obtain the initial stack size
const Int32 top = sq_gettop(_SqVM); const StackGuard sg(_SqVM);
// Push the specified object onto the stack // Push the specified object onto the stack
Var< Object & >::push(_SqVM, value); Var< Object & >::push(_SqVM, value);
// The resulted long integer value // The resulted long integer value
Int64 longint = 0; Int64 longint = 0;
// Whether the operation succeeded
bool res = false;
// Attempt to get the numeric value inside the specified object // Attempt to get the numeric value inside the specified object
if (SQ_FAILED(_SqMod->GetSLongValue(_SqVM, -1, &longint))) if (SQ_FAILED(_SqMod->GetSLongValue(_SqVM, -1, &longint)))
_SqMod->SqThrow("Invalid long integer specified"); SqThrowF("Invalid long integer specified");
// Assign the obtained value // Assign the obtained value and return the result
else return m_Text.set(longint);
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;
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
bool Text::ApplyUlong(Object & value) bool Text::ApplyUlong(Object & value)
{ {
// Obtain the initial stack size // Obtain the initial stack size
const Int32 top = sq_gettop(_SqVM); const StackGuard sg(_SqVM);
// Push the specified object onto the stack // Push the specified object onto the stack
Var< Object & >::push(_SqVM, value); Var< Object & >::push(_SqVM, value);
// The resulted long integer value // The resulted long integer value
Uint64 longint = 0; Uint64 longint = 0;
// Whether the operation succeeded
bool res = false;
// Attempt to get the numeric value inside the specified object // Attempt to get the numeric value inside the specified object
if (SQ_FAILED(_SqMod->GetULongValue(_SqVM, -1, &longint))) if (SQ_FAILED(_SqMod->GetULongValue(_SqVM, -1, &longint)))
_SqMod->SqThrow("Invalid long integer specified"); SqThrowF("Invalid long integer specified");
// Assign the obtained value // Assign the obtained value and return the result
else return m_Text.set(longint);
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;
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
Object Text::GetLong() const Object Text::GetLong() const
{ {
// Obtain the initial stack size // 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 // Push a long integer instance with the requested value on the stack
_SqMod->PushSLongObject(_SqVM, m_Text.as_llong()); _SqMod->PushSLongObject(_SqVM, m_Text.as_llong());
// Obtain the object from the stack // Obtain the object from the stack and return it
Var< Object > inst(_SqVM, -1); return Var< Object >(_SqVM, -1).value;
// 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;
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void Text::SetLong(Object & value) void Text::SetLong(Object & value)
{ {
// Obtain the initial stack size // Obtain the initial stack size
const Int32 top = sq_gettop(_SqVM); const StackGuard sg(_SqVM);
// Push the specified object onto the stack // Push the specified object onto the stack
Var< Object & >::push(_SqVM, value); Var< Object & >::push(_SqVM, value);
// The resulted long integer value // The resulted long integer value
Int64 longint = 0; Int64 longint = 0;
// Attempt to get the numeric value inside the specified object // Attempt to get the numeric value inside the specified object
if (SQ_FAILED(_SqMod->GetSLongValue(_SqVM, -1, &longint))) if (SQ_FAILED(_SqMod->GetSLongValue(_SqVM, -1, &longint)))
_SqMod->SqThrow("Invalid long integer specified"); SqThrowF("Invalid long integer specified");
// Assign the obtained value // Assign the obtained value
else m_Text = longint;
m_Text = longint;
// Remove an pushed values (if any) to restore the stack
sq_pop(_SqVM, sq_gettop(_SqVM) - top);
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
Object Text::GetUlong() const Object Text::GetUlong() const
{ {
// Obtain the initial stack size // 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 // Push a long integer instance with the requested value on the stack
_SqMod->PushULongObject(_SqVM, m_Text.as_ullong()); _SqMod->PushULongObject(_SqVM, m_Text.as_ullong());
// Obtain the object from the stack // Obtain the object from the stack and return it
Var< Object > inst(_SqVM, -1); return Var< Object >(_SqVM, -1).value;
// 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;
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void Text::SetUlong(Object & value) void Text::SetUlong(Object & value)
{ {
// Obtain the initial stack size // Obtain the initial stack size
const Int32 top = sq_gettop(_SqVM); const StackGuard sg(_SqVM);
// Push the specified object onto the stack // Push the specified object onto the stack
Var< Object & >::push(_SqVM, value); Var< Object & >::push(_SqVM, value);
// The resulted long integer value // The resulted long integer value
Uint64 longint = 0; Uint64 longint = 0;
// Attempt to get the numeric value inside the specified object // Attempt to get the numeric value inside the specified object
if (SQ_FAILED(_SqMod->GetULongValue(_SqVM, -1, &longint))) if (SQ_FAILED(_SqMod->GetULongValue(_SqVM, -1, &longint)))
_SqMod->SqThrow("Invalid long integer specified"); SqThrowF("Invalid long integer specified");
// Assign the obtained value // Assign the obtained value
else m_Text = longint;
m_Text = longint;
// Remove an pushed values (if any) to restore the stack
sq_pop(_SqVM, sq_gettop(_SqVM) - top);
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------

View File

@ -32,7 +32,7 @@ protected:
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Validate the document reference and throw an error if invalid. * Validate the document reference and throw an error if invalid.
*/ */
bool Validate() const; void Validate() const;
private: private:
@ -90,6 +90,11 @@ public:
return m_Text.get(); 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. * See whether this instance references a valid xml document.
*/ */