diff --git a/cbp/ModSample.cbp b/cbp/ModSample.cbp index 4b6bd938..3f411134 100644 --- a/cbp/ModSample.cbp +++ b/cbp/ModSample.cbp @@ -362,7 +362,10 @@ + + + diff --git a/modules/ini/Common.hpp b/modules/ini/Common.hpp index 06ba2b12..fb6cedba 100644 --- a/modules/ini/Common.hpp +++ b/modules/ini/Common.hpp @@ -10,12 +10,6 @@ // ------------------------------------------------------------------------------------------------ #include -// ------------------------------------------------------------------------------------------------ -extern "C" { - struct SQVM; - typedef struct SQVM* HSQUIRRELVM; -} /*extern "C"*/ - // ------------------------------------------------------------------------------------------------ namespace SqMod { diff --git a/modules/irc/Common.hpp b/modules/irc/Common.hpp index 715257be..c0a3c554 100644 --- a/modules/irc/Common.hpp +++ b/modules/irc/Common.hpp @@ -4,12 +4,6 @@ // ------------------------------------------------------------------------------------------------ #include "ModBase.hpp" -// ------------------------------------------------------------------------------------------------ -extern "C" { - struct SQVM; - typedef struct SQVM* HSQUIRRELVM; -} /*extern "C"*/ - // ------------------------------------------------------------------------------------------------ namespace SqMod { diff --git a/modules/sample/Common.cpp b/modules/sample/Common.cpp index c669be1f..5deed336 100644 --- a/modules/sample/Common.cpp +++ b/modules/sample/Common.cpp @@ -4,10 +4,72 @@ // ------------------------------------------------------------------------------------------------ #include +#include + +// ------------------------------------------------------------------------------------------------ +#include // ------------------------------------------------------------------------------------------------ namespace SqMod { +// ------------------------------------------------------------------------------------------------ +static SQChar g_Buffer[4096]; // Common buffer to reduce memory allocations. + +// ------------------------------------------------------------------------------------------------ +SStr GetTempBuff() +{ + 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); +} + // ------------------------------------------------------------------------------------------------ int SampleFunction() { diff --git a/modules/sample/Common.hpp b/modules/sample/Common.hpp index 8e1b6d53..d09634c2 100644 --- a/modules/sample/Common.hpp +++ b/modules/sample/Common.hpp @@ -20,6 +20,70 @@ namespace SqMod { #define SQSAMPLE_VERSION_MINOR 0 #define SQSAMPLE_VERSION_PATCH 1 +/* ------------------------------------------------------------------------------------------------ + * 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. */ +}; + /* -------------------------------------------------------------------------------------------- * Sample Plugin API */ diff --git a/modules/sqlite/Common.hpp b/modules/sqlite/Common.hpp index 9ab6447b..a22f334f 100644 --- a/modules/sqlite/Common.hpp +++ b/modules/sqlite/Common.hpp @@ -14,12 +14,6 @@ // ------------------------------------------------------------------------------------------------ #include -// ------------------------------------------------------------------------------------------------ -extern "C" { - struct SQVM; - typedef struct SQVM* HSQUIRRELVM; -} /*extern "C"*/ - // ------------------------------------------------------------------------------------------------ namespace SqMod {