1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 08:47:17 +01:00

Initial revision of the buffer implementation.

This commit is contained in:
Sandu Liviu Catalin 2016-07-08 00:56:54 +03:00
parent e5f9fffd8e
commit 4e93e58397
13 changed files with 1001 additions and 1201 deletions

View File

@ -527,10 +527,8 @@
<Unit filename="../source/Library/System/Path.hpp" />
<Unit filename="../source/Library/Utils.cpp" />
<Unit filename="../source/Library/Utils.hpp" />
<Unit filename="../source/Library/Utils/BufferInterpreter.cpp" />
<Unit filename="../source/Library/Utils/BufferInterpreter.hpp" />
<Unit filename="../source/Library/Utils/BufferWrapper.cpp" />
<Unit filename="../source/Library/Utils/BufferWrapper.hpp" />
<Unit filename="../source/Library/Utils/Buffer.cpp" />
<Unit filename="../source/Library/Utils/Buffer.hpp" />
<Unit filename="../source/Logger.cpp" />
<Unit filename="../source/Logger.hpp" />
<Unit filename="../source/Main.cpp" />

View File

@ -1,5 +1,6 @@
// ------------------------------------------------------------------------------------------------
#include "Base/Buffer.hpp"
#include "sqrat/sqratUtil.h"
// ------------------------------------------------------------------------------------------------
#include <cstdlib>
@ -30,7 +31,7 @@ inline unsigned int NextPow2(unsigned int num)
void ThrowMemExcept(const char * msg, ...)
{
// Exception messages should be concise
char buffer[128];
char buffer[256];
// Variable arguments structure
va_list args;
// Get the specified arguments
@ -40,10 +41,10 @@ void ThrowMemExcept(const char * msg, ...)
// Check for formatting errors
if (ret < 0)
{
throw std::runtime_error("Unknown memory error");
throw Sqrat::Exception("Unknown memory error");
}
// Throw the actual exception
throw std::runtime_error(buffer);
throw Sqrat::Exception(buffer);
}
/* ------------------------------------------------------------------------------------------------

View File

@ -2,6 +2,7 @@
#define _BASE_BUFFER_HPP_
// ------------------------------------------------------------------------------------------------
#include <cmath>
#include <cassert>
#include <cstdarg>
@ -214,6 +215,14 @@ private:
cur = 0;
}
/* --------------------------------------------------------------------------------------------
* Round a size value.
*/
template < typename T > static inline SzType RoundSz(T n)
{
return static_cast< SzType >(floor(n));
}
public:
/* --------------------------------------------------------------------------------------------
@ -380,7 +389,15 @@ public:
*/
operator bool () const
{
return m_Ptr;
return (m_Ptr != nullptr);
}
/* --------------------------------------------------------------------------------------------
* Negation operator.
*/
operator ! () const
{
return (!m_Ptr);
}
/* --------------------------------------------------------------------------------------------
@ -420,8 +437,20 @@ public:
*/
template < typename T = Value > T & At(SzType n)
{
assert(n < static_cast< SzType >(m_Cap / sizeof(T)));
return reinterpret_cast< T * >(m_Ptr)[n];
// Make sure that the buffer can host at least one element of this type
if (m_Cap < sizeof(T))
{
ThrowMemExcept("Buffer capacity of (%u) is unable to host an element of size (%u)",
m_Cap, sizeof(T));
}
// Make sure that the specified element is withing buffer range
else if (n > (m_Cap - sizeof(T)))
{
ThrowMemExcept("Element of size (%d) at index (%u) is out of buffer capacity (%u)",
sizeof(T), n, m_Cap);
}
// Return the requested element
return *reinterpret_cast< T * >(m_Ptr + n);
}
/* --------------------------------------------------------------------------------------------
@ -429,8 +458,20 @@ public:
*/
template < typename T = Value > const T & At(SzType n) const
{
assert(n < static_cast< SzType >(m_Cap / sizeof(T)));
return reinterpret_cast< const T * >(m_Ptr)[n];
// Make sure that the buffer can host at least one element of this type
if (m_Cap < sizeof(T))
{
ThrowMemExcept("Buffer capacity of (%u) is unable to host an element of size (%u)",
m_Cap, sizeof(T));
}
// Make sure that the specified element is withing buffer range
else if (n > (m_Cap - sizeof(T)))
{
ThrowMemExcept("Element of size (%d) at index (%u) is out of buffer capacity (%u)",
sizeof(T), n, m_Cap);
}
// Return the requested element
return *reinterpret_cast< const T * >(m_Ptr + n);
}
/* --------------------------------------------------------------------------------------------
@ -470,8 +511,14 @@ public:
*/
template < typename T = Value > T & Front()
{
assert(m_Cap >= sizeof(T));
return reinterpret_cast< T * >(m_Ptr)[0];
// Make sure that the buffer can host at least one element of this type
if (m_Cap < sizeof(T))
{
ThrowMemExcept("Buffer capacity of (%u) is unable to host an element of size (%u)",
m_Cap, sizeof(T));
}
// Return the requested element
return *reinterpret_cast< T * >(m_Ptr);
}
/* --------------------------------------------------------------------------------------------
@ -479,8 +526,14 @@ public:
*/
template < typename T = Value > const T & Front() const
{
assert(m_Cap >= sizeof(T));
return reinterpret_cast< const T * >(m_Ptr)[0];
// Make sure that the buffer can host at least one element of this type
if (m_Cap < sizeof(T))
{
ThrowMemExcept("Buffer capacity of (%u) is unable to host an element of size (%u)",
m_Cap, sizeof(T));
}
// Return the requested element
return *reinterpret_cast< T * >(m_Ptr);
}
/* --------------------------------------------------------------------------------------------
@ -488,8 +541,14 @@ public:
*/
template < typename T = Value > T & Next()
{
assert(m_Cap >= (sizeof(T) * 2));
return reinterpret_cast< T * >(m_Ptr)[1];
// Make sure that the buffer can host at least two elements of this type
if (m_Cap < (sizeof(T) * 2))
{
ThrowMemExcept("Buffer capacity of (%u) is unable to host two elements of size (%u)",
m_Cap, sizeof(T));
}
// Return the requested element
return *reinterpret_cast< T * >(m_Ptr + sizeof(T));
}
/* --------------------------------------------------------------------------------------------
@ -497,8 +556,14 @@ public:
*/
template < typename T = Value > const T & Next() const
{
assert(m_Cap >= (sizeof(T) * 2));
return reinterpret_cast< const T * >(m_Ptr)[1];
// Make sure that the buffer can host at least two elements of this type
if (m_Cap < (sizeof(T) * 2))
{
ThrowMemExcept("Buffer capacity of (%u) is unable to host two elements of size (%u)",
m_Cap, sizeof(T));
}
// Return the requested element
return *reinterpret_cast< const T * >(m_Ptr + sizeof(T));
}
/* --------------------------------------------------------------------------------------------
@ -506,8 +571,14 @@ public:
*/
template < typename T = Value > T & Back()
{
assert(m_Cap >= sizeof(T));
return reinterpret_cast< T * >(m_Ptr)[(m_Cap / sizeof(T))-1];
// Make sure that the buffer can host at least one element of this type
if (m_Cap < sizeof(T))
{
ThrowMemExcept("Buffer capacity of (%u) is unable to host an element of size (%u)",
m_Cap, sizeof(T));
}
// Return the requested element
return *reinterpret_cast< T * >(m_Ptr + (m_Cap - sizeof(T)));
}
/* --------------------------------------------------------------------------------------------
@ -515,8 +586,14 @@ public:
*/
template < typename T = Value > const T & Back() const
{
assert(m_Cap >= sizeof(T));
return reinterpret_cast< const T * >(m_Ptr)[(m_Cap / sizeof(T))-1];
// Make sure that the buffer can host at least one element of this type
if (m_Cap < sizeof(T))
{
ThrowMemExcept("Buffer capacity of (%u) is unable to host an element of size (%u)",
m_Cap, sizeof(T));
}
// Return the requested element
return *reinterpret_cast< const T * >(m_Ptr + (m_Cap - sizeof(T)));
}
/* --------------------------------------------------------------------------------------------
@ -524,8 +601,14 @@ public:
*/
template < typename T = Value > T & Prev()
{
assert(m_Cap >= (sizeof(T) * 2));
return reinterpret_cast< T * >(m_Ptr)[(m_Cap / sizeof(T))-2];
// Make sure that the buffer can host at least two elements of this type
if (m_Cap < (sizeof(T) * 2))
{
ThrowMemExcept("Buffer capacity of (%u) is unable to host two elements of size (%u)",
m_Cap, sizeof(T));
}
// Return the requested element
return *reinterpret_cast< T * >(m_Ptr + (m_Cap - (sizeof(T) * 2)));
}
/* --------------------------------------------------------------------------------------------
@ -533,8 +616,14 @@ public:
*/
template < typename T = Value > const T & Prev() const
{
assert(m_Cap >= (sizeof(T) * 2));
return reinterpret_cast< const T * >(m_Ptr)[(m_Cap / sizeof(T))-2];
// Make sure that the buffer can host at least two elements of this type
if (m_Cap < (sizeof(T) * 2))
{
ThrowMemExcept("Buffer capacity of (%u) is unable to host two elements of size (%u)",
m_Cap, sizeof(T));
}
// Return the requested element
return *reinterpret_cast< const T * >(m_Ptr + (m_Cap - (sizeof(T) * 2)));
}
/* --------------------------------------------------------------------------------------------
@ -543,7 +632,7 @@ public:
template < typename T = Value > void Advance(SzType n)
{
// Do we need to scale the buffer?
if ((m_Cur + (n * sizeof(T))) >= m_Cap)
if ((m_Cur + (n * sizeof(T))) > m_Cap)
{
Grow(m_Cur + (n * sizeof(T)));
}
@ -574,7 +663,7 @@ public:
template < typename T = Value > void Move(SzType n)
{
// Do we need to scale the buffer?
if ((n * sizeof(T)) >= m_Cap)
if ((n * sizeof(T)) > m_Cap)
{
Grow(n * sizeof(T));
}
@ -588,12 +677,12 @@ public:
template < typename T = Value > void Push(T v)
{
// Do we need to scale the buffer?
if ((m_Cur + sizeof(T)) >= m_Cap)
if ((m_Cur + sizeof(T)) > m_Cap)
{
Grow(m_Cap + sizeof(T));
}
// Assign the specified value
reinterpret_cast< T * >(m_Ptr)[m_Cur] = v;
*reinterpret_cast< T * >(m_Ptr + m_Cur) = v;
// Move to the next element
m_Cur += sizeof(T);
}
@ -603,8 +692,14 @@ public:
*/
template < typename T = Value > T & Cursor()
{
assert((m_Cur / sizeof(T)) < (m_Cap / sizeof(T)));
return reinterpret_cast< T * >(m_Ptr)[m_Cur];
// Make sure that at least one element of this type exists after the cursor
if ((m_Cur + sizeof(T)) > m_Cap)
{
ThrowMemExcept("Element of size (%u) starting at (%u) exceeds buffer capacity (%u)",
sizeof(T), m_Cur, m_Cap);
}
// Return the requested element
return *reinterpret_cast< T * >(m_Ptr + m_Cur);
}
/* --------------------------------------------------------------------------------------------
@ -612,8 +707,14 @@ public:
*/
template < typename T = Value > const T & Cursor() const
{
assert((m_Cur / sizeof(T)) < (m_Cap / sizeof(T)));
return reinterpret_cast< const T * >(m_Ptr)[m_Cur];
// Make sure that at least one element of this type exists after the cursor
if ((m_Cur + sizeof(T)) > m_Cap)
{
ThrowMemExcept("Element of size (%u) starting at (%u) exceeds buffer capacity (%u)",
sizeof(T), m_Cur, m_Cap);
}
// Return the requested element
return *reinterpret_cast< const T * >(m_Ptr + m_Cur);
}
/* --------------------------------------------------------------------------------------------
@ -621,8 +722,14 @@ public:
*/
template < typename T = Value > T & Before()
{
assert(m_Cur >= sizeof(T));
return reinterpret_cast< T * >(m_Ptr)[(m_Cur / sizeof(T))-1];
// The cursor must have at least one element of this type behind
if (m_Cur < sizeof(T))
{
ThrowMemExcept("Cannot read an element of size (%u) before the cursor at (%u)",
sizeof(T), m_Cur);
}
// Return the requested element
return *reinterpret_cast< T * >(m_Ptr + (m_Cur - sizeof(T)));
}
/* --------------------------------------------------------------------------------------------
@ -630,8 +737,14 @@ public:
*/
template < typename T = Value > const T & Before() const
{
assert(m_Cur >= sizeof(T));
return reinterpret_cast< const T * >(m_Ptr)[(m_Cur / sizeof(T))-1];
// The cursor must have at least one element of this type behind
if (m_Cur < sizeof(T))
{
ThrowMemExcept("Cannot read an element of size (%u) before the cursor at (%u)",
sizeof(T), m_Cur);
}
// Return the requested element
return *reinterpret_cast< const T * >(m_Ptr + (m_Cur - sizeof(T)));
}
/* --------------------------------------------------------------------------------------------
@ -639,8 +752,20 @@ public:
*/
template < typename T = Value > T & After()
{
assert(m_Cap >= sizeof(T) && (m_Cur + sizeof(T)) <= (m_Cap - sizeof(T)));
return reinterpret_cast< T * >(m_Ptr)[(m_Cur / sizeof(T))+1];
// Make sure that the buffer can host at least one element of this type
if (m_Cap < sizeof(T))
{
ThrowMemExcept("Buffer capacity of (%u) is unable to host an element of size (%u)",
m_Cap, sizeof(T));
}
// There must be buffer left for at least two elements of this type after the cursor
else if ((m_Cur + (sizeof(T) * 2)) > m_Cap)
{
ThrowMemExcept("Element of size (%u) starting at (%u) exceeds buffer capacity (%u)",
sizeof(T), m_Cur + sizeof(T), m_Cap);
}
// Return the requested element
return *reinterpret_cast< T * >(m_Ptr + m_Cur + sizeof(T));
}
/* --------------------------------------------------------------------------------------------
@ -648,8 +773,20 @@ public:
*/
template < typename T = Value > const T & After() const
{
assert(m_Cap >= sizeof(T) && (m_Cur + sizeof(T)) <= (m_Cap - sizeof(T)));
return reinterpret_cast< const T * >(m_Ptr)[(m_Cur / sizeof(T))+1];
// Make sure that the buffer can host at least one element of this type
if (m_Cap < sizeof(T))
{
ThrowMemExcept("Buffer capacity of (%u) is unable to host an element of size (%u)",
m_Cap, sizeof(T));
}
// There must be buffer left for at least two elements of this type after the cursor
else if ((m_Cur + (sizeof(T) * 2)) > m_Cap)
{
ThrowMemExcept("Element of size (%u) starting at (%u) exceeds buffer capacity (%u)",
sizeof(T), m_Cur + sizeof(T), m_Cap);
}
// Return the requested element
return *reinterpret_cast< const T * >(m_Ptr + m_Cur + sizeof(T));
}
/* --------------------------------------------------------------------------------------------
@ -676,10 +813,26 @@ public:
return m_Cap;
}
/* --------------------------------------------------------------------------------------------
* Retrieve the current buffer capacity in byte count.
*/
template < typename T = Value > SzType CapacityAs() const
{
return static_cast< SzType >(m_Cap / sizeof(T));
}
/* --------------------------------------------------------------------------------------------
* Retrieve the current position of the cursor in the buffer.
*/
template < typename T = Value > SzType Position() const
SzType Position() const
{
return m_Cur;
}
/* --------------------------------------------------------------------------------------------
* Retrieve the current position of the cursor in the buffer.
*/
template < typename T = Value > SzType PositionAs() const
{
return static_cast< SzType >(m_Cur / sizeof(T));
}
@ -687,9 +840,9 @@ public:
/* --------------------------------------------------------------------------------------------
* Retrieve the amount of unused buffer after the edit cursor.
*/
template < typename T = Value > SzType Remaining() const
SzType Remaining() const
{
return static_cast< SzType >((m_Cap - m_Cur) / sizeof(T));
return m_Cap - m_Cur;
}
/* --------------------------------------------------------------------------------------------

View File

@ -2,7 +2,7 @@
#include "Core.hpp"
#include "Base/Shared.hpp"
#include "Base/Buffer.hpp"
#include "Library/Utils/BufferWrapper.hpp"
#include "Library/Utils/Buffer.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
@ -1079,9 +1079,9 @@ void Core::EmitClientScriptData(Int32 player_id, const uint8_t * data, size_t si
// Remember the current stack size
const StackGuard sg;
// Create a protected instance of a buffer wrapper
AutoDelete< BufferWrapper > ad(new BufferWrapper(std::move(b)));
AutoDelete< SqBuffer > ad(new SqBuffer(std::move(b)));
// Transform the pointer into a script object
PushVar< BufferWrapper * >(DefaultVM::Get(), ad.Get());
PushVar< SqBuffer * >(DefaultVM::Get(), ad.Get());
// The script took over the instance now
ad.Release();
// Get the object from the stack and store it

View File

@ -4,7 +4,7 @@
#include "Base/Color3.hpp"
#include "Base/Color4.hpp"
#include "Base/Vector3.hpp"
#include "Library/Utils/BufferWrapper.hpp"
#include "Library/Utils/Buffer.hpp"
#include "Core.hpp"
// ------------------------------------------------------------------------------------------------
@ -1497,7 +1497,7 @@ Uint32 CPlayer::GetBufferCapacity() const
}
// ------------------------------------------------------------------------------------------------
void CPlayer::SendBuffer(const BufferWrapper & buffer) const
void CPlayer::SendBuffer(const SqBuffer & buffer) const
{
// Validate the managed identifier
Validate();

View File

@ -853,7 +853,7 @@ public:
/* --------------------------------------------------------------------------------------------
* Send the specified buffer contents to the managed player entity.
*/
void SendBuffer(const BufferWrapper & buffer) const;
void SendBuffer(const SqBuffer & buffer) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the position on the x axis of the managed player entity.

View File

@ -1198,7 +1198,7 @@ static CSStr StrImplode(Array & arr, SQChar chr)
static CSStr FromArray(Array & arr)
{
// Determine array size
const Int32 length = static_cast< Int32 >(arr.Length());
const Int32 length = ConvTo< Int32 >::From(arr.Length());
// Obtain a temporary buffer
Buffer b(length * sizeof(Int32));
// Get array elements as integers
@ -1206,7 +1206,7 @@ static CSStr FromArray(Array & arr)
// Overwrite integers with characters
for (Int32 n = 0; n < length; ++n)
{
b.At(n) = static_cast< SQChar >(b.At< Int32 >(n));
b.At(n) = ConvTo< SQChar >::From(b.At< Int32 >(n * sizeof(Int32)));
}
// Terminate the resulted string
b.At(length) = '\0';

View File

@ -0,0 +1,453 @@
// ------------------------------------------------------------------------------------------------
#include "Library/Utils/Buffer.hpp"
#include "Library/Numeric/LongInt.hpp"
#include "Base/AABB.hpp"
#include "Base/Circle.hpp"
#include "Base/Color3.hpp"
#include "Base/Color4.hpp"
#include "Base/Quaternion.hpp"
#include "Base/Sphere.hpp"
#include "Base/Vector2.hpp"
#include "Base/Vector2i.hpp"
#include "Base/Vector3.hpp"
#include "Base/Vector4.hpp"
// ------------------------------------------------------------------------------------------------
#include <cstring>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
void SqBuffer::WriteInt64(const SLongInt & val)
{
// Validate the managed buffer reference
Validate();
// Perform the requested operation
m_Buffer->Push< Int64 >(val.GetNum());
}
// ------------------------------------------------------------------------------------------------
void SqBuffer::WriteUint64(const ULongInt & val)
{
// Validate the managed buffer reference
Validate();
// Perform the requested operation
m_Buffer->Push< Uint64 >(val.GetNum());
}
// ------------------------------------------------------------------------------------------------
void SqBuffer::WriteString(CSStr val)
{
// Validate the managed buffer reference
Validate();
// Is the given string value even valid?
if (!val)
{
STHROWF("Invalid string argument: null");
}
// Calculate the string length
Uint16 length = ConvTo< Uint16 >::From(std::strlen(val));
// Change the size endianness to big endian
Uint16 size = ((length >> 8) & 0xFF) | ((length & 0xFF) << 8);
// Write the size and then the string contents
m_Buffer->Push< Uint16 >(size);
m_Buffer->AppendS(val, length);
}
// ------------------------------------------------------------------------------------------------
void SqBuffer::WriteRawString(CSStr val)
{
// Validate the managed buffer reference
Validate();
// Is the given string value even valid?
if (!val)
{
STHROWF("Invalid string argument: null");
}
// Calculate the string length
Uint16 length = ConvTo< Uint16 >::From(std::strlen(val));
// Write the the string contents
m_Buffer->AppendS(val, length);
}
// ------------------------------------------------------------------------------------------------
void SqBuffer::WriteAABB(const AABB & val)
{
// Validate the managed buffer reference
Validate();
// Perform the requested operation
m_Buffer->Push< AABB >(val);
}
// ------------------------------------------------------------------------------------------------
void SqBuffer::WriteCircle(const Circle & val)
{
// Validate the managed buffer reference
Validate();
// Perform the requested operation
m_Buffer->Push< Circle >(val);
}
// ------------------------------------------------------------------------------------------------
void SqBuffer::WriteColor3(const Color3 & val)
{
// Validate the managed buffer reference
Validate();
// Perform the requested operation
m_Buffer->Push< Color3 >(val);
}
// ------------------------------------------------------------------------------------------------
void SqBuffer::WriteColor4(const Color4 & val)
{
// Validate the managed buffer reference
Validate();
// Perform the requested operation
m_Buffer->Push< Color4 >(val);
}
// ------------------------------------------------------------------------------------------------
void SqBuffer::WriteQuaternion(const Quaternion & val)
{
// Validate the managed buffer reference
Validate();
// Perform the requested operation
m_Buffer->Push< Quaternion >(val);
}
// ------------------------------------------------------------------------------------------------
void SqBuffer::WriteSphere(const Sphere &val)
{
// Validate the managed buffer reference
Validate();
// Perform the requested operation
m_Buffer->Push< Sphere >(val);
}
// ------------------------------------------------------------------------------------------------
void SqBuffer::WriteVector2(const Vector2 & val)
{
// Validate the managed buffer reference
Validate();
// Perform the requested operation
m_Buffer->Push< Vector2 >(val);
}
// ------------------------------------------------------------------------------------------------
void SqBuffer::WriteVector2i(const Vector2i & val)
{
// Validate the managed buffer reference
Validate();
// Perform the requested operation
m_Buffer->Push< Vector2i >(val);
}
// ------------------------------------------------------------------------------------------------
void SqBuffer::WriteVector3(const Vector3 & val)
{
// Validate the managed buffer reference
Validate();
// Perform the requested operation
m_Buffer->Push< Vector3 >(val);
}
// ------------------------------------------------------------------------------------------------
void SqBuffer::WriteVector4(const Vector4 & val)
{
// Validate the managed buffer reference
Validate();
// Perform the requested operation
m_Buffer->Push< Vector4 >(val);
}
// ------------------------------------------------------------------------------------------------
SLongInt SqBuffer::ReadInt64()
{
// Validate the managed buffer reference
ValidateDeeper();
// Read one element from the buffer
const Int64 value = m_Buffer->Cursor< Int64 >();
// Advance the buffer cursor
m_Buffer->Advance< Int64 >(1);
// Return the requested information
return SLongInt(value);
}
// ------------------------------------------------------------------------------------------------
ULongInt SqBuffer::ReadUint64()
{
// Validate the managed buffer reference
ValidateDeeper();
// Read one element from the buffer
const Uint64 value = m_Buffer->Cursor< Uint64 >();
// Advance the buffer cursor
m_Buffer->Advance< Uint64 >(1);
// Return the requested information
return ULongInt(value);
}
// ------------------------------------------------------------------------------------------------
Object SqBuffer::ReadString()
{
// Validate the managed buffer reference
ValidateDeeper();
// Read one element from the buffer
Uint16 length = m_Buffer->Cursor< Uint16 >();
// Convert the length to little endian
length = ((length >> 8) & 0xFF) | ((length & 0xFF) << 8);
// Validate the obtained length
if ((m_Buffer->Position() + sizeof(Uint16) + length) > m_Buffer->Capacity())
{
STHROWF("String of size (%u) starting at (%u) is out of buffer capacity (%u)",
length, m_Buffer->Position() + sizeof(Uint16), m_Buffer->Capacity());
}
// Advance the buffer to the actual string
m_Buffer->Advance< Uint16 >(1);
// Remember the current stack size
const StackGuard sg;
// Attempt to create the string as an object
sq_pushstring(DefaultVM::Get(), &m_Buffer->Cursor(), length);
// Advance the cursor after the string
m_Buffer->Advance(length);
// Return the resulted object
return Var< Object >(DefaultVM::Get(), -1).value;
}
// ------------------------------------------------------------------------------------------------
Object SqBuffer::ReadRawString(Uint32 len)
{
// Validate the managed buffer reference
ValidateDeeper();
// Validate the obtained length
if ((m_Buffer->Position() + len) > m_Buffer->Capacity())
{
STHROWF("String of size (%u) starting at (%u) is out of buffer capacity (%u)",
len, m_Buffer->Position(), m_Buffer->Capacity());
}
// Remember the current stack size
const StackGuard sg;
// Attempt to create the string as an object
sq_pushstring(DefaultVM::Get(), &m_Buffer->Cursor(), len);
// Advance the cursor after the string
m_Buffer->Advance(len);
// Return the resulted object
return Var< Object >(DefaultVM::Get(), -1).value;
}
// ------------------------------------------------------------------------------------------------
AABB SqBuffer::ReadAABB()
{
// Validate the managed buffer reference
ValidateDeeper();
// Read one element from the buffer
const AABB & value = m_Buffer->Cursor< AABB >();
// Advance the buffer cursor
m_Buffer->Advance< AABB >(1);
// Return the requested information
return AABB(value);
}
// ------------------------------------------------------------------------------------------------
Circle SqBuffer::ReadCircle()
{
// Validate the managed buffer reference
ValidateDeeper();
// Read one element from the buffer
const Circle & value = m_Buffer->Cursor< Circle >();
// Advance the buffer cursor
m_Buffer->Advance< Circle >(1);
// Return the requested information
return Circle(value);
}
// ------------------------------------------------------------------------------------------------
Color3 SqBuffer::ReadColor3()
{
// Validate the managed buffer reference
ValidateDeeper();
// Read one element from the buffer
const Color3 & value = m_Buffer->Cursor< Color3 >();
// Advance the buffer cursor
m_Buffer->Advance< Color3 >(1);
// Return the requested information
return Color3(value);
}
// ------------------------------------------------------------------------------------------------
Color4 SqBuffer::ReadColor4()
{
// Validate the managed buffer reference
ValidateDeeper();
// Read one element from the buffer
const Color4 & value = m_Buffer->Cursor< Color4 >();
// Advance the buffer cursor
m_Buffer->Advance< Color4 >(1);
// Return the requested information
return Color4(value);
}
// ------------------------------------------------------------------------------------------------
Quaternion SqBuffer::ReadQuaternion()
{
// Validate the managed buffer reference
ValidateDeeper();
// Read one element from the buffer
const Quaternion & value = m_Buffer->Cursor< Quaternion >();
// Advance the buffer cursor
m_Buffer->Advance< Quaternion >(1);
// Return the requested information
return Quaternion(value);
}
// ------------------------------------------------------------------------------------------------
Sphere SqBuffer::ReadSphere()
{
// Validate the managed buffer reference
ValidateDeeper();
// Read one element from the buffer
const Sphere & value = m_Buffer->Cursor< Sphere >();
// Advance the buffer cursor
m_Buffer->Advance< Sphere >(1);
// Return the requested information
return Sphere(value);
}
// ------------------------------------------------------------------------------------------------
Vector2 SqBuffer::ReadVector2()
{
// Validate the managed buffer reference
ValidateDeeper();
// Read one element from the buffer
const Vector2 & value = m_Buffer->Cursor< Vector2 >();
// Advance the buffer cursor
m_Buffer->Advance< Vector2 >(1);
// Return the requested information
return Vector2(value);
}
// ------------------------------------------------------------------------------------------------
Vector2i SqBuffer::ReadVector2i()
{
// Validate the managed buffer reference
ValidateDeeper();
// Read one element from the buffer
const Vector2i & value = m_Buffer->Cursor< Vector2i >();
// Advance the buffer cursor
m_Buffer->Advance< Vector2i >(1);
// Return the requested information
return Vector2i(value);
}
// ------------------------------------------------------------------------------------------------
Vector3 SqBuffer::ReadVector3()
{
// Validate the managed buffer reference
ValidateDeeper();
// Read one element from the buffer
const Vector3 & value = m_Buffer->Cursor< Vector3 >();
// Advance the buffer cursor
m_Buffer->Advance< Vector3 >(1);
// Return the requested information
return Vector3(value);
}
// ------------------------------------------------------------------------------------------------
Vector4 SqBuffer::ReadVector4()
{
// Validate the managed buffer reference
ValidateDeeper();
// Read one element from the buffer
const Vector4 & value = m_Buffer->Cursor< Vector4 >();
// Advance the buffer cursor
m_Buffer->Advance< Vector4 >(1);
// Return the requested information
return Vector4(value);
}
// ================================================================================================
void Register_Buffer(HSQUIRRELVM vm)
{
RootTable(vm).Bind(_SC("SqBuffer"),
Class< SqBuffer >(vm, _SC("SqBuffer"))
// Constructors
.Ctor()
.Ctor< SQInteger >()
// Properties
.Prop(_SC("Front"), &SqBuffer::GetFront, &SqBuffer::SetFront)
.Prop(_SC("Next"), &SqBuffer::GetNext, &SqBuffer::SetNext)
.Prop(_SC("Back"), &SqBuffer::GetBack, &SqBuffer::SetBack)
.Prop(_SC("Prev"), &SqBuffer::GetPrev, &SqBuffer::SetPrev)
.Prop(_SC("Cursor"), &SqBuffer::GetCursor, &SqBuffer::SetCursor)
.Prop(_SC("Before"), &SqBuffer::GetBefore, &SqBuffer::SetBefore)
.Prop(_SC("After"), &SqBuffer::GetAfter, &SqBuffer::SetAfter)
.Prop(_SC("Max"), &SqBuffer::GetMax)
.Prop(_SC("Size"), &SqBuffer::GetSize, &SqBuffer::Adjust)
.Prop(_SC("Capacity"), &SqBuffer::GetCapacity, &SqBuffer::Adjust)
.Prop(_SC("Position"), &SqBuffer::GetPosition, &SqBuffer::Move)
.Prop(_SC("Remaining"), &SqBuffer::GetRemaining)
// Member Methods
.Func(_SC("Get"), &SqBuffer::Get)
.Func(_SC("Set"), &SqBuffer::Set)
.Func(_SC("Move"), &SqBuffer::Move)
.Func(_SC("Advance"), &SqBuffer::Advance)
.Func(_SC("Retreat"), &SqBuffer::Retreat)
.Func(_SC("Push"), &SqBuffer::Push)
.Func(_SC("Grow"), &SqBuffer::Grow)
.Func(_SC("Adjust"), &SqBuffer::Adjust)
.Func(_SC("WriteByte"), &SqBuffer::WriteUint8)
.Func(_SC("WriteShort"), &SqBuffer::WriteInt16)
.Func(_SC("WriteInt"), &SqBuffer::WriteInt32)
.Func(_SC("WriteFloat"), &SqBuffer::WriteFloat32)
.Func(_SC("WriteInt8"), &SqBuffer::WriteInt8)
.Func(_SC("WriteUint8"), &SqBuffer::WriteUint8)
.Func(_SC("WriteInt16"), &SqBuffer::WriteInt16)
.Func(_SC("WriteUint16"), &SqBuffer::WriteUint16)
.Func(_SC("WriteInt32"), &SqBuffer::WriteInt32)
.Func(_SC("WriteUint32"), &SqBuffer::WriteUint32)
.Func(_SC("WriteInt64"), &SqBuffer::WriteInt64)
.Func(_SC("WriteUint64"), &SqBuffer::WriteUint64)
.Func(_SC("WriteFloat32"), &SqBuffer::WriteFloat32)
.Func(_SC("WriteFloat64"), &SqBuffer::WriteFloat64)
.Func(_SC("WriteString"), &SqBuffer::WriteString)
.Func(_SC("WriteRawString"), &SqBuffer::WriteRawString)
.Func(_SC("WriteAABB"), &SqBuffer::WriteAABB)
.Func(_SC("WriteCircle"), &SqBuffer::WriteCircle)
.Func(_SC("WriteColor3"), &SqBuffer::WriteColor3)
.Func(_SC("WriteColor4"), &SqBuffer::WriteColor4)
.Func(_SC("WriteQuaternion"), &SqBuffer::WriteQuaternion)
.Func(_SC("WriteSphere"), &SqBuffer::WriteSphere)
.Func(_SC("WriteVector2"), &SqBuffer::WriteVector2)
.Func(_SC("WriteVector2i"), &SqBuffer::WriteVector2i)
.Func(_SC("WriteVector3"), &SqBuffer::WriteVector3)
.Func(_SC("WriteVector4"), &SqBuffer::WriteVector4)
.Func(_SC("ReadByte"), &SqBuffer::ReadUint8)
.Func(_SC("ReadShort"), &SqBuffer::ReadInt16)
.Func(_SC("ReadInt"), &SqBuffer::ReadInt32)
.Func(_SC("ReadFloat"), &SqBuffer::ReadFloat32)
.Func(_SC("ReadInt8"), &SqBuffer::ReadInt8)
.Func(_SC("ReadUint8"), &SqBuffer::ReadUint8)
.Func(_SC("ReadInt16"), &SqBuffer::ReadInt16)
.Func(_SC("ReadUint16"), &SqBuffer::ReadUint16)
.Func(_SC("ReadInt32"), &SqBuffer::ReadInt32)
.Func(_SC("ReadUint32"), &SqBuffer::ReadUint32)
.Func(_SC("ReadInt64"), &SqBuffer::ReadInt64)
.Func(_SC("ReadUint64"), &SqBuffer::ReadUint64)
.Func(_SC("ReadFloat32"), &SqBuffer::ReadFloat32)
.Func(_SC("ReadFloat64"), &SqBuffer::ReadFloat64)
.Func(_SC("ReadString"), &SqBuffer::ReadString)
.Func(_SC("ReadRawString"), &SqBuffer::ReadRawString)
.Func(_SC("ReadAABB"), &SqBuffer::ReadAABB)
.Func(_SC("ReadCircle"), &SqBuffer::ReadCircle)
.Func(_SC("ReadColor3"), &SqBuffer::ReadColor3)
.Func(_SC("ReadColor4"), &SqBuffer::ReadColor4)
.Func(_SC("ReadQuaternion"), &SqBuffer::ReadQuaternion)
.Func(_SC("ReadSphere"), &SqBuffer::ReadSphere)
.Func(_SC("ReadVector2"), &SqBuffer::ReadVector2)
.Func(_SC("ReadVector2i"), &SqBuffer::ReadVector2i)
.Func(_SC("ReadVector3"), &SqBuffer::ReadVector3)
.Func(_SC("ReadVector4"), &SqBuffer::ReadVector4)
);
}
} // Namespace:: SqMod

View File

@ -1,5 +1,5 @@
#ifndef _LIBRARY_UTILS_BUFFERWRAPPER_HPP_
#define _LIBRARY_UTILS_BUFFERWRAPPER_HPP_
#ifndef _LIBRARY_UTILS_BUFFER_HPP_
#define _LIBRARY_UTILS_BUFFER_HPP_
// ------------------------------------------------------------------------------------------------
#include "Base/Shared.hpp"
@ -8,13 +8,10 @@
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
template < typename T > class BufferInterpreter;
/* ------------------------------------------------------------------------------------------------
* Squirrel wrapper for the shared buffer class.
*/
class BufferWrapper
class SqBuffer
{
private:
@ -49,9 +46,27 @@ public:
static Object Create(SzType n);
/* --------------------------------------------------------------------------------------------
* Base constructor.
* Default constructor.
*/
BufferWrapper(const SRef & ref)
SqBuffer()
: m_Buffer(new Buffer())
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
* Allocate constructor.
*/
SqBuffer(SQInteger n)
: m_Buffer(new Buffer(ConvTo< SzType >::From(n)))
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
* Reference constructor.
*/
SqBuffer(const SRef & ref)
: m_Buffer(ref)
{
/* ... */
@ -60,7 +75,7 @@ public:
/* --------------------------------------------------------------------------------------------
* Buffer constructor.
*/
BufferWrapper(const Buffer & b)
SqBuffer(const Buffer & b)
: m_Buffer(new Buffer(b))
{
/* ... */
@ -69,7 +84,7 @@ public:
/* --------------------------------------------------------------------------------------------
* Buffer constructor.
*/
BufferWrapper(Buffer && b)
SqBuffer(Buffer && b)
: m_Buffer(new Buffer(std::move(b)))
{
/* ... */
@ -78,27 +93,27 @@ public:
/* --------------------------------------------------------------------------------------------
* Copy constructor.
*/
BufferWrapper(const BufferWrapper & o) = default;
SqBuffer(const SqBuffer & o) = default;
/* --------------------------------------------------------------------------------------------
* Move constructor.
*/
BufferWrapper(BufferWrapper && o) = default;
SqBuffer(SqBuffer && o) = default;
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~BufferWrapper() = default;
~SqBuffer() = default;
/* --------------------------------------------------------------------------------------------
* Copy assignment operator.
*/
BufferWrapper & operator = (const BufferWrapper & o) = default;
SqBuffer & operator = (const SqBuffer & o) = default;
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
BufferWrapper & operator = (BufferWrapper && o) = default;
SqBuffer & operator = (SqBuffer && o) = default;
/* --------------------------------------------------------------------------------------------
* Retrieve a reference to the managed memory buffer.
@ -140,33 +155,23 @@ public:
/* --------------------------------------------------------------------------------------------
* Retrieve a certain element type at the specified position.
*/
Value Get(SzType n) const
Value Get(SQInteger n) const
{
// Validate the managed buffer reference
Validate();
// Are we out of the memory buffer range?
if (n >= m_Buffer->Size())
{
STHROWF("Index (%u) is out of bounds (%u)", n, m_Buffer->Size());
}
// Return the requested element
return m_Buffer->At(n);
return m_Buffer->At(ConvTo< SzType >::From(n));
}
/* --------------------------------------------------------------------------------------------
* Modify a certain element type at the specified position.
*/
void Set(SzType n, Value v)
void Set(SQInteger n, SQInteger v)
{
// Validate the managed buffer reference
Validate();
// Are we out of the memory buffer range?
if (n >= m_Buffer->Size())
{
STHROWF("Index (%u) is out of bounds (%u)", n, m_Buffer->Size());
}
// Return the requested element
m_Buffer->At(n) = v;
m_Buffer->At(ConvTo< SzType >::From(n)) = ConvTo< Value >::From(v);
}
/* --------------------------------------------------------------------------------------------
@ -176,12 +181,6 @@ public:
{
// Validate the managed buffer reference
Validate();
// Are we out of the memory buffer range?
if (m_Buffer->Capacity() < sizeof(Value))
{
STHROWF("Value size (%u starting at 0) is out of bounds (%u)",
sizeof(Value), m_Buffer->Capacity());
}
// Return the requested element
return m_Buffer->Front();
}
@ -189,18 +188,12 @@ public:
/* --------------------------------------------------------------------------------------------
* Modify the element at the front of the buffer.
*/
void SetFront(Value v)
void SetFront(SQInteger v)
{
// Validate the managed buffer reference
Validate();
// Are we out of the memory buffer range?
if (m_Buffer->Capacity() < sizeof(Value))
{
STHROWF("Value size (%u starting at 0) is out of bounds (%u)",
sizeof(Value), m_Buffer->Capacity());
}
// Return the requested element
m_Buffer->Front() = v;
m_Buffer->Front() = ConvTo< Value >::From(v);
}
/* --------------------------------------------------------------------------------------------
@ -210,12 +203,6 @@ public:
{
// Validate the managed buffer reference
Validate();
// Are we out of the memory buffer range?
if (m_Buffer->Capacity() < (sizeof(Value) * 2))
{
STHROWF("Value size (%u starting at %u) is out of bounds (%u)",
sizeof(Value), sizeof(Value), m_Buffer->Capacity());
}
// Return the requested element
return m_Buffer->Next();
}
@ -223,18 +210,12 @@ public:
/* --------------------------------------------------------------------------------------------
* Modify the element after the first element in the buffer.
*/
void SetNext(Value v)
void SetNext(SQInteger v)
{
// Validate the managed buffer reference
Validate();
// Are we out of the memory buffer range?
if (m_Buffer->Capacity() < (sizeof(Value) * 2))
{
STHROWF("Value size (%u starting at %u) is out of bounds (%u)",
sizeof(Value), sizeof(Value), m_Buffer->Capacity());
}
// Return the requested element
m_Buffer->Next() = v;
m_Buffer->Next() = ConvTo< Value >::From(v);
}
/* --------------------------------------------------------------------------------------------
@ -244,12 +225,6 @@ public:
{
// Validate the managed buffer reference
Validate();
// Are we out of the memory buffer range?
if (m_Buffer->Capacity() < sizeof(Value))
{
STHROWF("Value size (%u starting at 0) is out of bounds (%u)",
sizeof(Value), m_Buffer->Capacity());
}
// Return the requested element
return m_Buffer->Back();
}
@ -257,18 +232,12 @@ public:
/* --------------------------------------------------------------------------------------------
* Modify the element at the back of the buffer.
*/
void SetBack(Value v)
void SetBack(SQInteger v)
{
// Validate the managed buffer reference
Validate();
// Are we out of the memory buffer range?
if (m_Buffer->Capacity() < sizeof(Value))
{
STHROWF("Value size (%u starting at 0) is out of bounds (%u)",
sizeof(Value), m_Buffer->Capacity());
}
// Return the requested element
m_Buffer->Back() = v;
m_Buffer->Back() = ConvTo< Value >::From(v);
}
/* --------------------------------------------------------------------------------------------
@ -278,12 +247,6 @@ public:
{
// Validate the managed buffer reference
Validate();
// Are we out of the memory buffer range?
if (m_Buffer->Capacity() < (sizeof(Value) * 2))
{
STHROWF("Value size (%u starting at %u) is out of bounds (%u)",
sizeof(Value), sizeof(Value), m_Buffer->Capacity());
}
// Return the requested element
return m_Buffer->Prev();
}
@ -291,62 +254,56 @@ public:
/* --------------------------------------------------------------------------------------------
* Modify the element before the last element in the buffer.
*/
void SetPrev(Value v)
void SetPrev(SQInteger v)
{
// Validate the managed buffer reference
Validate();
// Are we out of the memory buffer range?
if (m_Buffer->Capacity() < (sizeof(Value) * 2))
{
STHROWF("Value size (%u starting at %u) is out of bounds (%u)",
sizeof(Value), sizeof(Value), m_Buffer->Capacity());
}
// Return the requested element
m_Buffer->Prev() = v;
m_Buffer->Prev() = ConvTo< Value >::From(v);
}
/* --------------------------------------------------------------------------------------------
* Reposition the edit cursor to the specified number of elements ahead.
*/
void Advance(SzType n)
void Advance(SQInteger n)
{
// Validate the managed buffer reference
Validate();
// Perform the requested operation
m_Buffer->Advance(n);
m_Buffer->Advance(ConvTo< SzType >::From(n));
}
/* --------------------------------------------------------------------------------------------
* Reposition the edit cursor to the specified number of elements behind.
*/
void Retreat(SzType n)
void Retreat(SQInteger n)
{
// Validate the managed buffer reference
Validate();
// Perform the requested operation
m_Buffer->Retreat(n);
m_Buffer->Retreat(ConvTo< SzType >::From(n));
}
/* --------------------------------------------------------------------------------------------
* Reposition the edit cursor to a fixed position within the buffer.
*/
void Move(SzType n)
void Move(SQInteger n)
{
// Validate the managed buffer reference
Validate();
// Perform the requested operation
m_Buffer->Move(n);
m_Buffer->Move(ConvTo< SzType >::From(n));
}
/* --------------------------------------------------------------------------------------------
* Append a value to the current cursor location and advance the cursor.
*/
void Push(Value v)
void Push(SQInteger v)
{
// Validate the managed buffer reference
Validate();
// Perform the requested operation
m_Buffer->Push(v);
m_Buffer->Push(ConvTo< Value >::From(v));
}
/* --------------------------------------------------------------------------------------------
@ -356,12 +313,6 @@ public:
{
// Validate the managed buffer reference
Validate();
// Are we out of the memory buffer range?
if (m_Buffer->Position() >= m_Buffer->Size())
{
STHROWF("Value size (%u starting at %u) is out of bounds (%u)",
sizeof(Value), m_Buffer->Position(), m_Buffer->Capacity());
}
// Return the requested element
return m_Buffer->Cursor();
}
@ -369,18 +320,12 @@ public:
/* --------------------------------------------------------------------------------------------
* Modify the element at the cursor position.
*/
void SetCursor(Value v)
void SetCursor(SQInteger v)
{
// Validate the managed buffer reference
Validate();
// Are we out of the memory buffer range?
if (m_Buffer->Position() >= m_Buffer->Size())
{
STHROWF("Value size (%u starting at %u) is out of bounds (%u)",
sizeof(Value), m_Buffer->Position(), m_Buffer->Capacity());
}
// Return the requested element
m_Buffer->Cursor() = v;
m_Buffer->Cursor() = ConvTo< Value >::From(v);
}
/* --------------------------------------------------------------------------------------------
@ -390,12 +335,6 @@ public:
{
// Validate the managed buffer reference
Validate();
// Are we out of the memory buffer range?
if (m_Buffer->Position() < sizeof(Value))
{
STHROWF("Value size (%u starting at 0) is out of bounds (%u)",
sizeof(Value), m_Buffer->Capacity());
}
// Return the requested element
return m_Buffer->Before();
}
@ -403,18 +342,12 @@ public:
/* --------------------------------------------------------------------------------------------
* Modify the element before the cursor position.
*/
void SetBefore(Value v)
void SetBefore(SQInteger v)
{
// Validate the managed buffer reference
Validate();
// Are we out of the memory buffer range?
if (m_Buffer->Position() < sizeof(Value))
{
STHROWF("Value size (%u starting at 0) is out of bounds (%u)",
sizeof(Value), m_Buffer->Capacity());
}
// Return the requested element
m_Buffer->Before() = v;
m_Buffer->Before() = ConvTo< Value >::From(v);
}
/* --------------------------------------------------------------------------------------------
@ -424,13 +357,6 @@ public:
{
// Validate the managed buffer reference
Validate();
// Are we out of the memory buffer range?
if (m_Buffer->Capacity() < sizeof(Value) ||
(m_Buffer->Position() + sizeof(Value)) > (m_Buffer->Capacity() - sizeof(Value)))
{
STHROWF("Value size (%u starting at %u) is out of bounds (%u)",
sizeof(Value), m_Buffer->Position(), m_Buffer->Capacity());
}
// Return the requested element
return m_Buffer->After();
}
@ -438,19 +364,12 @@ public:
/* --------------------------------------------------------------------------------------------
* Modify the element after the cursor position.
*/
void SetAfter(Value v)
void SetAfter(SQInteger v)
{
// Validate the managed buffer reference
Validate();
// Are we out of the memory buffer range?
if (m_Buffer->Capacity() < sizeof(Value) ||
(m_Buffer->Position() + sizeof(Value)) > (m_Buffer->Capacity() - sizeof(Value)))
{
STHROWF("Value size (%u starting at %u) is out of bounds (%u)",
sizeof(Value), m_Buffer->Position(), m_Buffer->Capacity());
}
// Return the requested element
m_Buffer->After() = v;
m_Buffer->After() = ConvTo< Value >::From(v);
}
/* --------------------------------------------------------------------------------------------
@ -469,7 +388,7 @@ public:
// Validate the managed buffer reference
Validate();
// Return the requested information
return m_Buffer->Size();
return m_Buffer->CapacityAs< Value >();
}
/* --------------------------------------------------------------------------------------------
@ -508,25 +427,25 @@ public:
/* --------------------------------------------------------------------------------------------
* Grow the size of the internal buffer by the specified amount of bytes.
*/
void Grow(SzType n)
void Grow(SQInteger n)
{
// Validate the managed buffer reference
Validate();
// Perform the requested operation
return m_Buffer->Grow(n * sizeof(Value));
return m_Buffer->Grow(ConvTo< SzType >::From(n) * sizeof(Value));
}
/* --------------------------------------------------------------------------------------------
* Makes sure there is enough capacity to hold the specified element count.
*/
void Adjust(SzType n)
void Adjust(SQInteger n)
{
// Validate the managed buffer reference
Validate();
// Attempt to perform the requested operation
try
{
Buffer bkp(m_Buffer->Adjust(n * sizeof(Value)));
Buffer bkp(m_Buffer->Adjust(ConvTo< SzType >::From(n) * sizeof(Value)));
// Copy the data into the new buffer
m_Buffer->Write(0, bkp.Data(), bkp.Capacity());
m_Buffer->Move(bkp.Position());
@ -538,116 +457,354 @@ public:
}
/* --------------------------------------------------------------------------------------------
* Write a 8 bit byte to the stream buffer.
* Write a signed 8 bit integer to the buffer.
*/
void WriteByte(SQInteger val);
void WriteInt8(SQInteger val)
{
// Validate the managed buffer reference
Validate();
// Perform the requested operation
m_Buffer->Push< Int8 >(ConvTo< Int8 >::From(val));
}
/* --------------------------------------------------------------------------------------------
* Write a 16 bit short to the stream buffer.
* Write an unsigned 8 bit integer to the buffer.
*/
void WriteShort(SQInteger val);
void WriteUint8(SQInteger val)
{
// Validate the managed buffer reference
Validate();
// Perform the requested operation
m_Buffer->Push< Uint8 >(ConvTo< Uint8 >::From(val));
}
/* --------------------------------------------------------------------------------------------
* Write a 32 bit integer to the stream buffer.
* Write a signed 16 bit integer to the buffer.
*/
void WriteInt(SQInteger val);
void WriteInt16(SQInteger val)
{
// Validate the managed buffer reference
Validate();
// Perform the requested operation
m_Buffer->Push< Int16 >(ConvTo< Int16 >::From(val));
}
/* --------------------------------------------------------------------------------------------
* Write a 32 bit float to the stream buffer.
* Write an unsigned 16 bit integer to the buffer.
*/
void WriteFloat(SQFloat val);
void WriteUint16(SQInteger val)
{
// Validate the managed buffer reference
Validate();
// Perform the requested operation
m_Buffer->Push< Uint16 >(ConvTo< Uint16 >::From(val));
}
/* --------------------------------------------------------------------------------------------
* Write a string to the stream buffer.
* Write a signed 32 bit integer to the buffer.
*/
void WriteInt32(SQInteger val)
{
// Validate the managed buffer reference
Validate();
// Perform the requested operation
m_Buffer->Push< Int32 >(ConvTo< Int32 >::From(val));
}
/* --------------------------------------------------------------------------------------------
* Write an unsigned 32 bit integer to the buffer.
*/
void WriteUint32(SQInteger val)
{
// Validate the managed buffer reference
Validate();
// Perform the requested operation
m_Buffer->Push< Uint32 >(ConvTo< Uint32 >::From(val));
}
/* --------------------------------------------------------------------------------------------
* Write a signed 64 bit integer to the buffer.
*/
void WriteInt64(const SLongInt & val);
/* --------------------------------------------------------------------------------------------
* Write an unsigned 64 bit integer to the buffer.
*/
void WriteUint64(const ULongInt & val);
/* --------------------------------------------------------------------------------------------
* Write a 32 bit float to the buffer.
*/
void WriteFloat32(SQFloat val)
{
// Validate the managed buffer reference
Validate();
// Perform the requested operation
m_Buffer->Push< Float32 >(ConvTo< Float32 >::From(val));
}
/* --------------------------------------------------------------------------------------------
* Write a 64 bit float to the buffer.
*/
void WriteFloat64(SQFloat val)
{
// Validate the managed buffer reference
Validate();
// Perform the requested operation
m_Buffer->Push< Float64 >(ConvTo< Float64 >::From(val));
}
/* --------------------------------------------------------------------------------------------
* Write a string to the buffer.
*/
void WriteString(CSStr val);
/* --------------------------------------------------------------------------------------------
* Write a raw string to the stream buffer.
* Write a raw string to the buffer.
*/
void WriteRawString(CSStr val);
/* --------------------------------------------------------------------------------------------
* Read a 8 bit byte to the stream buffer.
* Write a AABB to the buffer.
*/
SQInteger ReadByte();
void WriteAABB(const AABB & val);
/* --------------------------------------------------------------------------------------------
* Read a 16 bit short to the stream buffer.
* Write a Circle to the buffer.
*/
SQInteger ReadShort();
void WriteCircle(const Circle & val);
/* --------------------------------------------------------------------------------------------
* Read a 32 bit integer to the stream buffer.
* Write a Color3 to the buffer.
*/
SQInteger ReadInt();
void WriteColor3(const Color3 & val);
/* --------------------------------------------------------------------------------------------
* Read a 32 bit float to the stream buffer.
* Write a Color4 to the buffer.
*/
SQFloat ReadFloat();
void WriteColor4(const Color4 & val);
/* --------------------------------------------------------------------------------------------
* Read a string to the stream buffer.
* Write a Quaternion to the buffer.
*/
void WriteQuaternion(const Quaternion & val);
/* --------------------------------------------------------------------------------------------
* Write a Sphere to the buffer.
*/
void WriteSphere(const Sphere &val);
/* --------------------------------------------------------------------------------------------
* Write a Vector2 to the buffer.
*/
void WriteVector2(const Vector2 & val);
/* --------------------------------------------------------------------------------------------
* Write a Vector2i to the buffer.
*/
void WriteVector2i(const Vector2i & val);
/* --------------------------------------------------------------------------------------------
* Write a Vector3 to the buffer.
*/
void WriteVector3(const Vector3 & val);
/* --------------------------------------------------------------------------------------------
* Write a Vector4 to the buffer.
*/
void WriteVector4(const Vector4 & val);
/* --------------------------------------------------------------------------------------------
* Write a signed 8 bit integer from the buffer.
*/
SQInteger ReadInt8()
{
// Validate the managed buffer reference
ValidateDeeper();
// Read one element from the buffer
const Int8 value = m_Buffer->Cursor< Int8 >();
// Advance the buffer cursor
m_Buffer->Advance< Int8 >(1);
// Return the requested information
return ConvTo< SQInteger >::From(value);
}
/* --------------------------------------------------------------------------------------------
* Read an unsigned 8 bit integer from the buffer.
*/
SQInteger ReadUint8()
{
// Validate the managed buffer reference
ValidateDeeper();
// Read one element from the buffer
const Uint8 value = m_Buffer->Cursor< Uint8 >();
// Advance the buffer cursor
m_Buffer->Advance< Uint8 >(1);
// Return the requested information
return ConvTo< SQInteger >::From(value);
}
/* --------------------------------------------------------------------------------------------
* Read a signed 16 bit integer from the buffer.
*/
SQInteger ReadInt16()
{
// Validate the managed buffer reference
ValidateDeeper();
// Read one element from the buffer
const Int16 value = m_Buffer->Cursor< Int16 >();
// Advance the buffer cursor
m_Buffer->Advance< Int16 >(1);
// Return the requested information
return ConvTo< SQInteger >::From(value);
}
/* --------------------------------------------------------------------------------------------
* Read an unsigned 16 bit integer from the buffer.
*/
SQInteger ReadUint16()
{
// Validate the managed buffer reference
ValidateDeeper();
// Read one element from the buffer
const Uint16 value = m_Buffer->Cursor< Uint16 >();
// Advance the buffer cursor
m_Buffer->Advance< Uint16 >(1);
// Return the requested information
return ConvTo< SQInteger >::From(value);
}
/* --------------------------------------------------------------------------------------------
* Read a signed 32 bit integer from the buffer.
*/
SQInteger ReadInt32()
{
// Validate the managed buffer reference
ValidateDeeper();
// Read one element from the buffer
const Int32 value = m_Buffer->Cursor< Int32 >();
// Advance the buffer cursor
m_Buffer->Advance< Int32 >(1);
// Return the requested information
return ConvTo< SQInteger >::From(value);
}
/* --------------------------------------------------------------------------------------------
* Read an unsigned 32 bit integer from the buffer.
*/
SQInteger ReadUint32()
{
// Validate the managed buffer reference
ValidateDeeper();
// Read one element from the buffer
const Uint32 value = m_Buffer->Cursor< Uint32 >();
// Advance the buffer cursor
m_Buffer->Advance< Uint32 >(1);
// Return the requested information
return ConvTo< SQInteger >::From(value);
}
/* --------------------------------------------------------------------------------------------
* Read a signed 64 bit integer from the buffer.
*/
SLongInt ReadInt64();
/* --------------------------------------------------------------------------------------------
* Read an unsigned 64 bit integer from the buffer.
*/
ULongInt ReadUint64();
/* --------------------------------------------------------------------------------------------
* Read a 32 bit float from the buffer.
*/
SQFloat ReadFloat32()
{
// Validate the managed buffer reference
ValidateDeeper();
// Read one element from the buffer
const Float32 value = m_Buffer->Cursor< Float32 >();
// Advance the buffer cursor
m_Buffer->Advance< Float32 >(1);
// Return the requested information
return ConvTo< SQFloat >::From(value);
}
/* --------------------------------------------------------------------------------------------
* Read a 64 bit float from the buffer.
*/
SQFloat ReadFloat64()
{
// Validate the managed buffer reference
ValidateDeeper();
// Read one element from the buffer
const Float64 value = m_Buffer->Cursor< Float64 >();
// Advance the buffer cursor
m_Buffer->Advance< Float64 >(1);
// Return the requested information
return ConvTo< SQFloat >::From(value);
}
/* --------------------------------------------------------------------------------------------
* Read a string from the buffer.
*/
Object ReadString();
/* --------------------------------------------------------------------------------------------
* Read a raw string to the stream buffer.
* Read a raw string from the buffer.
*/
Object ReadRawString(Uint32 len);
/* --------------------------------------------------------------------------------------------
* Retrieve a signed 8 bit interpreter to this buffer.
* Read a AABB from the buffer.
*/
BufferInterpreter< Int8 > GetInt8Interpreter() const;
AABB ReadAABB();
/* --------------------------------------------------------------------------------------------
* Retrieve an unsigned 8 bit interpreter to this buffer.
* Read a Circle from the buffer.
*/
BufferInterpreter< Uint8 > GetUint8Interpreter() const;
Circle ReadCircle();
/* --------------------------------------------------------------------------------------------
* Retrieve a signed 16 bit interpreter to this buffer.
* Read a Color3 from the buffer.
*/
BufferInterpreter< Int16 > GetInt16Interpreter() const;
Color3 ReadColor3();
/* --------------------------------------------------------------------------------------------
* Retrieve an unsigned 16 bit interpreter to this buffer.
* Read a Color4 from the buffer.
*/
BufferInterpreter< Uint16 > GetUint16Interpreter() const;
Color4 ReadColor4();
/* --------------------------------------------------------------------------------------------
* Retrieve a signed 32 bit interpreter to this buffer.
* Read a Quaternion from the buffer.
*/
BufferInterpreter< Int32 > GetInt32Interpreter() const;
Quaternion ReadQuaternion();
/* --------------------------------------------------------------------------------------------
* Retrieve an unsigned 32 bit interpreter to this buffer.
* Read a Sphere from the buffer.
*/
BufferInterpreter< Uint32 > GetUint32Interpreter() const;
Sphere ReadSphere();
/* --------------------------------------------------------------------------------------------
* Retrieve a signed 64 bit interpreter to this buffer.
* Read a Vector2 from the buffer.
*/
BufferInterpreter< Int64 > GetInt64Interpreter() const;
Vector2 ReadVector2();
/* --------------------------------------------------------------------------------------------
* Retrieve an unsigned 64 bit interpreter to this buffer.
* Read a Vector2i from the buffer.
*/
BufferInterpreter< Uint64 > GetUint64Interpreter() const;
Vector2i ReadVector2i();
/* --------------------------------------------------------------------------------------------
* Retrieve a 32 bit floating point interpreter to this buffer.
* Read a Vector3 from the buffer.
*/
BufferInterpreter< Float32 > GetFloat32Interpreter() const;
Vector3 ReadVector3();
/* --------------------------------------------------------------------------------------------
* Retrieve a 64 bit floating point interpreter to this buffer.
* Read a Vector4 from the buffer.
*/
BufferInterpreter< Float64 > GetFloat64Interpreter() const;
Vector4 ReadVector4();
};
} // Namespace:: SqMod
#endif // _LIBRARY_UTILS_BUFFERWRAPPER_HPP_
#endif // _LIBRARY_UTILS_BUFFER_HPP_

View File

@ -1,63 +0,0 @@
// ------------------------------------------------------------------------------------------------
#include "Library/Utils/BufferInterpreter.hpp"
#include "Library/Utils/BufferWrapper.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
const SharedPtr< Buffer > & GetBufferBufferRef(const BufferWrapper & buffer)
{
return buffer.GetRef();
}
// ------------------------------------------------------------------------------------------------
template < typename T > static void RegisterInterpreter(Table & bns, CSStr cname, CSStr bname)
{
typedef BufferInterpreter< T > Interpreter;
bns.Bind(bname,
Class< Interpreter >(bns.GetVM(), cname)
// Constructors
.Ctor()
// Properties
.Prop(_SC("Front"), &Interpreter::GetFront, &Interpreter::SetFront)
.Prop(_SC("Next"), &Interpreter::GetNext, &Interpreter::SetNext)
.Prop(_SC("Back"), &Interpreter::GetBack, &Interpreter::SetBack)
.Prop(_SC("Prev"), &Interpreter::GetPrev, &Interpreter::SetPrev)
.Prop(_SC("Cursor"), &Interpreter::GetCursor, &Interpreter::SetCursor)
.Prop(_SC("Before"), &Interpreter::GetBefore, &Interpreter::SetBefore)
.Prop(_SC("After"), &Interpreter::GetAfter, &Interpreter::SetAfter)
.Prop(_SC("Max"), &Interpreter::GetMax)
.Prop(_SC("Size"), &Interpreter::GetSize)
.Prop(_SC("Capacity"), &Interpreter::GetCapacity)
.Prop(_SC("Position"), &Interpreter::GetPosition)
.Prop(_SC("Remaining"), &Interpreter::GetRemaining)
// Member Methods
.Func(_SC("Use"), &Interpreter::UseBuffer)
.Func(_SC("Get"), &Interpreter::Get)
.Func(_SC("Set"), &Interpreter::Set)
.Func(_SC("Advance"), &Interpreter::Advance)
.Func(_SC("Retreat"), &Interpreter::Retreat)
.Func(_SC("Push"), &Interpreter::Push)
.Func(_SC("Grow"), &Interpreter::Grow)
.Func(_SC("Adjust"), &Interpreter::Adjust)
);
}
// ================================================================================================
void Register_BufferInterpreter(Table & bns)
{
RegisterInterpreter< Int8 >(bns, _SC("SqBufferInterpreterS8"), _SC("S8Interpreter"));
RegisterInterpreter< Uint8 >(bns, _SC("SqBufferInterpreterU8"), _SC("U8Interpreter"));
RegisterInterpreter< Int16 >(bns, _SC("SqBufferInterpreterS16"), _SC("S16Interpreter"));
RegisterInterpreter< Uint16 >(bns, _SC("SqBufferInterpreterU16"), _SC("U16Interpreter"));
RegisterInterpreter< Int32 >(bns, _SC("SqBufferInterpreterS32"), _SC("S32Interpreter"));
RegisterInterpreter< Uint32 >(bns, _SC("SqBufferInterpreterU32"), _SC("U32Interpreter"));
RegisterInterpreter< Int64 >(bns, _SC("SqBufferInterpreterS64"), _SC("S64Interpreter"));
RegisterInterpreter< Uint64 >(bns, _SC("SqBufferInterpreterU64"), _SC("U64Interpreter"));
RegisterInterpreter< Float32 >(bns, _SC("SqBufferInterpreterF32"), _SC("F32Interpreter"));
RegisterInterpreter< Float64 >(bns, _SC("SqBufferInterpreterF64"), _SC("F64Interpreter"));
}
} // Namespace:: SqMod

View File

@ -1,542 +0,0 @@
#ifndef _LIBRARY_UTILS_BUFFERINTERPRETER_HPP_
#define _LIBRARY_UTILS_BUFFERINTERPRETER_HPP_
// ------------------------------------------------------------------------------------------------
#include "Base/Shared.hpp"
#include "Base/Buffer.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
class BufferWrapper;
/* ------------------------------------------------------------------------------------------------
* Used internally to obtain a reference to the memory buffer without including the wrapper header.
*/
const SharedPtr< Buffer > & GetBufferBufferRef(const BufferWrapper & buffer);
/* ------------------------------------------------------------------------------------------------
* Utility class used to interpret a memory buffer in different ways.
*/
template < typename T > class BufferInterpreter
{
private:
// --------------------------------------------------------------------------------------------
typedef SharedPtr< Buffer > SRef; // Strong reference type to the interpreted memory buffer.
typedef WeakPtr< Buffer > WRef; // Weak reference type to the interpreted memory buffer.
// --------------------------------------------------------------------------------------------
WRef m_Buffer; // The interpreted memory buffer.
public:
// --------------------------------------------------------------------------------------------
typedef T Value; // The type of value used to represent a byte.
// --------------------------------------------------------------------------------------------
typedef Value & Reference; // A reference to the stored value type.
typedef const Value & ConstRef; // A const reference to the stored value type.
// --------------------------------------------------------------------------------------------
typedef Value * Pointer; // A pointer to the stored value type.
typedef const Value * ConstPtr; // A const pointer to the stored value type.
// --------------------------------------------------------------------------------------------
typedef Buffer::SzType SzType; // The type used to represent size in general.
protected:
/* --------------------------------------------------------------------------------------------
* Attempt to obtain a strong reference to the memory buffer at all costs.
*/
SRef Validate() const
{
// Did the buffer that we reference expired?
if (m_Buffer.Expired())
{
STHROWF("Invalid memory buffer reference");
}
// Obtain a strong reference to it
return m_Buffer.Lock();
}
/* --------------------------------------------------------------------------------------------
* Attempt to obtain a strong reference to a valid memory buffer at all costs.
*/
SRef ValidateDeeper() const
{
// Did the buffer that we reference expired?
if (m_Buffer.Expired())
{
STHROWF("Invalid memory buffer reference");
}
// Obtain a strong reference to it
SRef ref = m_Buffer.Lock();
// Validate the buffer itself
if (!(*ref))
{
STHROWF("Invalid memory buffer");
}
// Return the reference
return ref;
}
public:
/* --------------------------------------------------------------------------------------------
* Default constructor. (null)
*/
BufferInterpreter()
: m_Buffer()
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
* Base constructor.
*/
BufferInterpreter(const WRef & ref)
: m_Buffer(ref)
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
* Base constructor.
*/
BufferInterpreter(const SRef & ref)
: m_Buffer(ref)
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
* Copy constructor.
*/
BufferInterpreter(const BufferInterpreter & o) = default;
/* --------------------------------------------------------------------------------------------
* Move constructor.
*/
BufferInterpreter(BufferInterpreter && o) = default;
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~BufferInterpreter() = default;
/* --------------------------------------------------------------------------------------------
* Copy assignment operator.
*/
BufferInterpreter & operator = (const BufferInterpreter & o) = default;
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
BufferInterpreter & operator = (BufferInterpreter && o) = default;
/* --------------------------------------------------------------------------------------------
* Retrieve a reference to the managed memory buffer.
*/
const WRef & GetRef() const
{
return m_Buffer;
}
/* --------------------------------------------------------------------------------------------
* Assign a different memory buffer to interpret from.
*/
void UseBuffer(const BufferWrapper & buffer)
{
m_Buffer = GetBufferBufferRef(buffer);
}
/* --------------------------------------------------------------------------------------------
* Retrieve a certain element type at the specified position.
*/
T Get(SzType n) const
{
// Acquire a reference to the memory buffer
SRef b(Validate());
// Are we out of the memory buffer range?
if (n >= b->Size< T >())
{
STHROWF("Index (%u) is out of bounds (%u)", n, b->Size< T >());
}
// Return the requested element
return b->At< T >(n);
}
/* --------------------------------------------------------------------------------------------
* Modify a certain element type at the specified position.
*/
void Set(SzType n, T v)
{
// Acquire a reference to the memory buffer
SRef b(Validate());
// Are we out of the memory buffer range?
if (n >= b->Size< T >())
{
STHROWF("Index (%u) is out of bounds (%u)", n, b->Size< T >());
}
// Return the requested element
b->At< T >(n) = v;
}
/* --------------------------------------------------------------------------------------------
* Retrieve the element at the front of the buffer.
*/
T GetFront() const
{
// Acquire a reference to the memory buffer
SRef b(Validate());
// Are we out of the memory buffer range?
if (b->Capacity() < sizeof(T))
{
STHROWF("Value size (%u starting at 0) is out of bounds (%u)",
sizeof(T), b->Capacity());
}
// Return the requested element
return b->Front< T >();
}
/* --------------------------------------------------------------------------------------------
* Modify the element at the front of the buffer.
*/
void SetFront(T v)
{
// Acquire a reference to the memory buffer
SRef b(Validate());
// Are we out of the memory buffer range?
if (b->Capacity() < sizeof(T))
{
STHROWF("Value size (%u starting at 0) is out of bounds (%u)",
sizeof(T), b->Capacity());
}
// Return the requested element
b->Front< T >() = v;
}
/* --------------------------------------------------------------------------------------------
* Retrieve the element after the first element in the buffer.
*/
T GetNext() const
{
// Acquire a reference to the memory buffer
SRef b(Validate());
// Are we out of the memory buffer range?
if (b->Capacity() < (sizeof(T) * 2))
{
STHROWF("Value size (%u starting at %u) is out of bounds (%u)",
sizeof(T), sizeof(T), b->Capacity());
}
// Return the requested element
return b->Next< T >();
}
/* --------------------------------------------------------------------------------------------
* Modify the element after the first element in the buffer.
*/
void SetNext(T v)
{
// Acquire a reference to the memory buffer
SRef b(Validate());
// Are we out of the memory buffer range?
if (b->Capacity() < (sizeof(T) * 2))
{
STHROWF("Value size (%u starting at %u) is out of bounds (%u)",
sizeof(T), sizeof(T), b->Capacity());
}
// Return the requested element
b->Next< T >() = v;
}
/* --------------------------------------------------------------------------------------------
* Retrieve the element at the back of the buffer.
*/
T GetBack() const
{
// Acquire a reference to the memory buffer
SRef b(Validate());
// Are we out of the memory buffer range?
if (b->Capacity() < sizeof(T))
{
STHROWF("Value size (%u starting at 0) is out of bounds (%u)",
sizeof(T), b->Capacity());
}
// Return the requested element
return b->Back< T >();
}
/* --------------------------------------------------------------------------------------------
* Modify the element at the back of the buffer.
*/
void SetBack(T v)
{
// Acquire a reference to the memory buffer
SRef b(Validate());
// Are we out of the memory buffer range?
if (b->Capacity() < sizeof(T))
{
STHROWF("Value size (%u starting at 0) is out of bounds (%u)",
sizeof(T), b->Capacity());
}
// Return the requested element
b->Back< T >() = v;
}
/* --------------------------------------------------------------------------------------------
* Retrieve the element before the last element in the buffer.
*/
T GetPrev() const
{
// Acquire a reference to the memory buffer
SRef b(Validate());
// Are we out of the memory buffer range?
if (b->Capacity() < (sizeof(T) * 2))
{
STHROWF("Value size (%u starting at %u) is out of bounds (%u)",
sizeof(T), sizeof(T), b->Capacity());
}
// Return the requested element
return b->Prev< T >();
}
/* --------------------------------------------------------------------------------------------
* Modify the element before the last element in the buffer.
*/
void SetPrev(T v)
{
// Acquire a reference to the memory buffer
SRef b(Validate());
// Are we out of the memory buffer range?
if (b->Capacity() < (sizeof(T) * 2))
{
STHROWF("Value size (%u starting at %u) is out of bounds (%u)",
sizeof(T), sizeof(T), b->Capacity());
}
// Return the requested element
b->Prev< T >() = v;
}
/* --------------------------------------------------------------------------------------------
* Reposition the edit cursor to the specified number of elements ahead.
*/
void Advance(SzType n)
{
// Acquire a reference to the memory buffer
SRef b(Validate());
// Perform the requested operation
b->Advance< T >(n);
}
/* --------------------------------------------------------------------------------------------
* Reposition the edit cursor to the specified number of elements behind.
*/
void Retreat(SzType n)
{
// Acquire a reference to the memory buffer
SRef b(Validate());
// Perform the requested operation
b->Retreat< T >(n);
}
/* --------------------------------------------------------------------------------------------
* Reposition the edit cursor to a fixed position within the buffer.
*/
void Move(SzType n)
{
// Acquire a reference to the memory buffer
SRef b(Validate());
// Perform the requested operation
b->Move< T >(n);
}
/* --------------------------------------------------------------------------------------------
* Append a value to the current cursor location and advance the cursor.
*/
void Push(T v)
{
// Acquire a reference to the memory buffer
SRef b(Validate());
// Perform the requested operation
b->Push< T >(v);
}
/* --------------------------------------------------------------------------------------------
* Retrieve the element at the cursor position.
*/
T GetCursor() const
{
// Acquire a reference to the memory buffer
SRef b(Validate());
// Are we out of the memory buffer range?
if (b->Position< T >() >= b->Size< T >())
{
STHROWF("Value size (%u starting at %u) is out of bounds (%u)",
sizeof(T), b->Position(), b->Capacity());
}
// Return the requested element
return b->Cursor< T >();
}
/* --------------------------------------------------------------------------------------------
* Modify the element at the cursor position.
*/
void SetCursor(T v)
{
// Acquire a reference to the memory buffer
SRef b(Validate());
// Are we out of the memory buffer range?
if (b->Position< T >() >= b->Size< T >())
{
STHROWF("Value size (%u starting at %u) is out of bounds (%u)",
sizeof(T), b->Position(), b->Capacity());
}
// Return the requested element
b->Cursor< T >() = v;
}
/* --------------------------------------------------------------------------------------------
* Retrieve the element before the cursor position.
*/
T GetBefore() const
{
// Acquire a reference to the memory buffer
SRef b(Validate());
// Are we out of the memory buffer range?
if (b->Position() < sizeof(T))
{
STHROWF("Value size (%u starting at 0) is out of bounds (%u)",
sizeof(T), b->Capacity());
}
// Return the requested element
return b->Before< T >();
}
/* --------------------------------------------------------------------------------------------
* Modify the element before the cursor position.
*/
void SetBefore(T v)
{
// Acquire a reference to the memory buffer
SRef b(Validate());
// Are we out of the memory buffer range?
if (b->Position() < sizeof(T))
{
STHROWF("Value size (%u starting at 0) is out of bounds (%u)",
sizeof(T), b->Capacity());
}
// Return the requested element
b->Before< T >() = v;
}
/* --------------------------------------------------------------------------------------------
* Retrieve the element after the cursor position.
*/
T GetAfter() const
{
// Acquire a reference to the memory buffer
SRef b(Validate());
// Are we out of the memory buffer range?
if (b->Capacity() < sizeof(T) || (b->Position() + sizeof(T)) > (b->Capacity() - sizeof(T)))
{
STHROWF("Value size (%u starting at %u) is out of bounds (%u)",
sizeof(T), b->Position(), b->Capacity());
}
// Return the requested element
return b->After< T >();
}
/* --------------------------------------------------------------------------------------------
* Modify the element after the cursor position.
*/
void SetAfter(T v)
{
// Acquire a reference to the memory buffer
SRef b(Validate());
// Are we out of the memory buffer range?
if (b->Capacity() < sizeof(T) || (b->Position() + sizeof(T)) > (b->Capacity() - sizeof(T)))
{
STHROWF("Value size (%u starting at %u) is out of bounds (%u)",
sizeof(T), b->Position(), b->Capacity());
}
// Return the requested element
b->After< T >() = v;
}
/* --------------------------------------------------------------------------------------------
* Retrieve maximum elements it can hold for a certain type.
*/
SzType GetMax() const
{
return Buffer::Max< T >();
}
/* --------------------------------------------------------------------------------------------
* Retrieve the current buffer capacity in element count.
*/
SzType GetSize() const
{
return Validate()->template Size< T >();
}
/* --------------------------------------------------------------------------------------------
* Retrieve the current buffer capacity in byte count.
*/
SzType GetCapacity() const
{
return Validate()->Capacity();
}
/* --------------------------------------------------------------------------------------------
* Retrieve the current position of the cursor in the buffer.
*/
SzType GetPosition() const
{
return Validate()->template Position< T >();
}
/* --------------------------------------------------------------------------------------------
* Retrieve the amount of unused buffer after the edit cursor.
*/
SzType GetRemaining() const
{
return Validate()->template Remaining< T >();
}
/* --------------------------------------------------------------------------------------------
* Grow the size of the internal buffer by the specified amount of bytes.
*/
void Grow(SzType n)
{
return Validate()->Grow(n * sizeof(T));
}
/* --------------------------------------------------------------------------------------------
* Makes sure there is enough capacity to hold the specified element count.
*/
void Adjust(SzType n)
{
// Acquire a reference to the memory buffer
SRef b(Validate());
// Attempt to perform the requested operation
try
{
Buffer bkp(b->Adjust(n * sizeof(T)));
// Copy the data into the new buffer
b->Write(0, bkp.Data(), bkp.Capacity());
b->Move(bkp.Position());
}
catch (const std::exception & e)
{
STHROWF("%s", e.what()); // Re-package
}
}
};
} // Namespace:: SqMod
#endif // _LIBRARY_UTILS_BUFFERINTERPRETER_HPP_

View File

@ -1,357 +0,0 @@
// ------------------------------------------------------------------------------------------------
#include "Library/Utils/BufferWrapper.hpp"
#include "Library/Utils/BufferInterpreter.hpp"
// ------------------------------------------------------------------------------------------------
#include <cstring>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
extern void Register_BufferInterpreter(Table & bns);
// ------------------------------------------------------------------------------------------------
Object BufferWrapper::Create(SzType n)
{
// Attempt to create the requested buffer
try
{
return MakeObject(BufferWrapper(SRef(new Buffer(n))));
}
catch (const Sqrat::Exception & e)
{
throw e; // Re-throw
}
catch (const std::exception & e)
{
STHROWF("%s", e.what()); // Re-package
}
// Shouldn't really reach this point
return NullObject();
}
// ------------------------------------------------------------------------------------------------
void BufferWrapper::WriteByte(SQInteger val)
{
// Validate the managed buffer reference
Validate();
// Perform the requested operation
m_Buffer->Push< Uint8 >(ConvTo< Uint8 >::From(val));
}
// ------------------------------------------------------------------------------------------------
void BufferWrapper::WriteShort(SQInteger val)
{
// Validate the managed buffer reference
Validate();
// Perform the requested operation
m_Buffer->Push< Int16 >(ConvTo< Int16 >::From(val));
}
// ------------------------------------------------------------------------------------------------
void BufferWrapper::WriteInt(SQInteger val)
{
// Validate the managed buffer reference
Validate();
// Perform the requested operation
m_Buffer->Push< Int32 >(ConvTo< Int32 >::From(val));
}
// ------------------------------------------------------------------------------------------------
void BufferWrapper::WriteFloat(SQFloat val)
{
// Validate the managed buffer reference
Validate();
// Perform the requested operation
m_Buffer->Push< Float32 >(ConvTo< Float32 >::From(val));
}
// ------------------------------------------------------------------------------------------------
void BufferWrapper::WriteString(CSStr val)
{
// Validate the managed buffer reference
Validate();
// Is the given string value even valid?
if (!val)
{
STHROWF("Invalid string argument: null");
}
// Calculate the string length
Uint16 length = ConvTo< Uint16 >::From(std::strlen(val));
// Change the size endianness to big endian
Uint16 size = ((length >> 8) & 0xFF) | ((length & 0xFF) << 8);
// Write the size and then the string contents
m_Buffer->Push< Uint16 >(size);
m_Buffer->AppendS(val, length);
}
// ------------------------------------------------------------------------------------------------
void BufferWrapper::WriteRawString(CSStr val)
{
// Validate the managed buffer reference
Validate();
// Is the given string value even valid?
if (!val)
{
STHROWF("Invalid string argument: null");
}
// Calculate the string length
Uint16 length = ConvTo< Uint16 >::From(std::strlen(val));
// Write the the string contents
m_Buffer->AppendS(val, length);
}
// ------------------------------------------------------------------------------------------------
SQInteger BufferWrapper::ReadByte()
{
// Validate the managed buffer reference
ValidateDeeper();
// Are we out of the memory buffer range?
if (m_Buffer->Position< Int8 >() >= m_Buffer->Size< Int8 >())
{
STHROWF("Value size (%u starting at %u) is out of bounds (%u)",
sizeof(Int8), m_Buffer->Position(), m_Buffer->Capacity());
}
// Read one element from the buffer
const Int8 value = m_Buffer->Cursor< Int8 >();
// Advance the buffer cursor
m_Buffer->Advance< Int8 >(1);
// Return the requested information
return value;
}
// ------------------------------------------------------------------------------------------------
SQInteger BufferWrapper::ReadShort()
{
// Validate the managed buffer reference
ValidateDeeper();
// Are we out of the memory buffer range?
if (m_Buffer->Position< Int16 >() >= m_Buffer->Size< Int16 >())
{
STHROWF("Value size (%u starting at %u) is out of bounds (%u)",
sizeof(Int16), m_Buffer->Position(), m_Buffer->Capacity());
}
// Read one element from the buffer
const Int16 value = *reinterpret_cast< Int16 * >(&m_Buffer->Cursor());
// Advance the buffer cursor
m_Buffer->Advance< Int16 >(1);
// Return the requested information
return value;
}
// ------------------------------------------------------------------------------------------------
SQInteger BufferWrapper::ReadInt()
{
// Validate the managed buffer reference
ValidateDeeper();
// Are we out of the memory buffer range?
if (m_Buffer->Position< Int32 >() >= m_Buffer->Size< Int32 >())
{
STHROWF("Value size (%u starting at %u) is out of bounds (%u)",
sizeof(Int32), m_Buffer->Position(), m_Buffer->Capacity());
}
// Read one element from the buffer
const Int32 value = *reinterpret_cast< Int32 * >(&m_Buffer->Cursor());
// Advance the buffer cursor
m_Buffer->Advance< Int32 >(1);
// Return the requested information
return value;
}
// ------------------------------------------------------------------------------------------------
SQFloat BufferWrapper::ReadFloat()
{
// Validate the managed buffer reference
ValidateDeeper();
// Are we out of the memory buffer range?
if (m_Buffer->Position< Float32 >() >= m_Buffer->Size< Float32 >())
{
STHROWF("Value size (%u starting at %u) is out of bounds (%u)",
sizeof(Float32), m_Buffer->Position(), m_Buffer->Capacity());
}
// Read one element from the buffer
const Float32 value = *reinterpret_cast< Float32 * >(&m_Buffer->Cursor());
// Advance the buffer cursor
m_Buffer->Advance< Float32 >(1);
// Return the requested information
return value;
}
// ------------------------------------------------------------------------------------------------
Object BufferWrapper::ReadString()
{
// Validate the managed buffer reference
ValidateDeeper();
// Are we out of the memory buffer range?
if (m_Buffer->Position< Int16 >() >= m_Buffer->Size< Int16 >())
{
STHROWF("Value size (%u starting at %u) is out of bounds (%u)",
sizeof(Int16), m_Buffer->Position(), m_Buffer->Capacity());
}
// Read one element from the buffer
Int16 length = *reinterpret_cast< Int16 * >(&m_Buffer->Cursor());
// Convert the length to little endian
length = ((length >> 8) & 0xFF) | ((length & 0xFF) << 8);
// Validate the obtained length
if ((m_Buffer->Position() + sizeof(Int16) + length) >= m_Buffer->Size())
{
STHROWF("String size (%u starting at %u) is out of bounds (%u)",
length, m_Buffer->Position() + sizeof(Int16), m_Buffer->Capacity());
}
// Advance the buffer to the actual string
m_Buffer->Advance< Int16 >(1);
// Remember the current stack size
const StackGuard sg;
// Attempt to create the string as an object
sq_pushstring(DefaultVM::Get(), &m_Buffer->Cursor(), length);
// Advance the cursor after the string
m_Buffer->Advance(length);
// Return the resulted object
return Var< Object >(DefaultVM::Get(), -1).value;
}
// ------------------------------------------------------------------------------------------------
Object BufferWrapper::ReadRawString(Uint32 len)
{
// Validate the managed buffer reference
ValidateDeeper();
// Validate the obtained length
if ((m_Buffer->Position() + len) >= m_Buffer->Size())
{
STHROWF("String size (%u starting at %u) is out of bounds (%u)",
len, m_Buffer->Position(), m_Buffer->Capacity());
}
// Remember the current stack size
const StackGuard sg;
// Attempt to create the string as an object
sq_pushstring(DefaultVM::Get(), &m_Buffer->Cursor(), len);
// Advance the cursor after the string
m_Buffer->Advance(len);
// Return the resulted object
return Var< Object >(DefaultVM::Get(), -1).value;
}
// ------------------------------------------------------------------------------------------------
BufferInterpreter< Int8 > BufferWrapper::GetInt8Interpreter() const
{
return BufferInterpreter< Int8 >(m_Buffer);
}
// ------------------------------------------------------------------------------------------------
BufferInterpreter< Uint8 > BufferWrapper::GetUint8Interpreter() const
{
return BufferInterpreter< Uint8 >(m_Buffer);
}
// ------------------------------------------------------------------------------------------------
BufferInterpreter< Int16 > BufferWrapper::GetInt16Interpreter() const
{
return BufferInterpreter< Int16 >(m_Buffer);
}
// ------------------------------------------------------------------------------------------------
BufferInterpreter< Uint16 > BufferWrapper::GetUint16Interpreter() const
{
return BufferInterpreter< Uint16 >(m_Buffer);
}
// ------------------------------------------------------------------------------------------------
BufferInterpreter< Int32 > BufferWrapper::GetInt32Interpreter() const
{
return BufferInterpreter< Int32 >(m_Buffer);
}
// ------------------------------------------------------------------------------------------------
BufferInterpreter< Uint32 > BufferWrapper::GetUint32Interpreter() const
{
return BufferInterpreter< Uint32 >(m_Buffer);
}
// ------------------------------------------------------------------------------------------------
BufferInterpreter< Int64 > BufferWrapper::GetInt64Interpreter() const
{
return BufferInterpreter< Int64 >(m_Buffer);
}
// ------------------------------------------------------------------------------------------------
BufferInterpreter< Uint64 > BufferWrapper::GetUint64Interpreter() const
{
return BufferInterpreter< Uint64 >(m_Buffer);
}
// ------------------------------------------------------------------------------------------------
BufferInterpreter< Float32 > BufferWrapper::GetFloat32Interpreter() const
{
return BufferInterpreter< Float32 >(m_Buffer);
}
// ------------------------------------------------------------------------------------------------
BufferInterpreter< Float64 > BufferWrapper::GetFloat64Interpreter() const
{
return BufferInterpreter< Float64 >(m_Buffer);
}
// ================================================================================================
void Register_Buffer(HSQUIRRELVM vm)
{
Table bns(vm);
bns.Bind(_SC("Wrapper"),
Class< BufferWrapper >(vm, _SC("SqBufferWrapper"))
// Constructors
.Ctor()
.Ctor< const BufferWrapper & >()
// Properties
.Prop(_SC("Front"), &BufferWrapper::GetFront, &BufferWrapper::SetFront)
.Prop(_SC("Next"), &BufferWrapper::GetNext, &BufferWrapper::SetNext)
.Prop(_SC("Back"), &BufferWrapper::GetBack, &BufferWrapper::SetBack)
.Prop(_SC("Prev"), &BufferWrapper::GetPrev, &BufferWrapper::SetPrev)
.Prop(_SC("Cursor"), &BufferWrapper::GetCursor, &BufferWrapper::SetCursor)
.Prop(_SC("Before"), &BufferWrapper::GetBefore, &BufferWrapper::SetBefore)
.Prop(_SC("After"), &BufferWrapper::GetAfter, &BufferWrapper::SetAfter)
.Prop(_SC("Max"), &BufferWrapper::GetMax)
.Prop(_SC("Size"), &BufferWrapper::GetSize)
.Prop(_SC("Capacity"), &BufferWrapper::GetCapacity)
.Prop(_SC("Position"), &BufferWrapper::GetPosition)
.Prop(_SC("Remaining"), &BufferWrapper::GetRemaining)
.Prop(_SC("Int8"), &BufferWrapper::GetInt8Interpreter)
.Prop(_SC("Uint8"), &BufferWrapper::GetUint8Interpreter)
.Prop(_SC("Int16"), &BufferWrapper::GetInt16Interpreter)
.Prop(_SC("Uint16"), &BufferWrapper::GetUint16Interpreter)
.Prop(_SC("Int32"), &BufferWrapper::GetInt32Interpreter)
.Prop(_SC("Uint32"), &BufferWrapper::GetUint32Interpreter)
.Prop(_SC("Int64"), &BufferWrapper::GetInt64Interpreter)
.Prop(_SC("Uint64"), &BufferWrapper::GetUint64Interpreter)
.Prop(_SC("Float32"), &BufferWrapper::GetFloat32Interpreter)
.Prop(_SC("Float64"), &BufferWrapper::GetFloat64Interpreter)
// Member Methods
.Func(_SC("Get"), &BufferWrapper::Get)
.Func(_SC("Set"), &BufferWrapper::Set)
.Func(_SC("Advance"), &BufferWrapper::Advance)
.Func(_SC("Retreat"), &BufferWrapper::Retreat)
.Func(_SC("Push"), &BufferWrapper::Push)
.Func(_SC("Grow"), &BufferWrapper::Grow)
.Func(_SC("Adjust"), &BufferWrapper::Adjust)
.Func(_SC("WriteByte"), &BufferWrapper::WriteByte)
.Func(_SC("WriteShort"), &BufferWrapper::WriteShort)
.Func(_SC("WriteInt"), &BufferWrapper::WriteInt)
.Func(_SC("WriteFloat"), &BufferWrapper::WriteFloat)
.Func(_SC("WriteString"), &BufferWrapper::WriteString)
.Func(_SC("WriteRawString"), &BufferWrapper::WriteRawString)
.Func(_SC("ReadByte"), &BufferWrapper::ReadByte)
.Func(_SC("ReadShort"), &BufferWrapper::ReadShort)
.Func(_SC("ReadInt"), &BufferWrapper::ReadInt)
.Func(_SC("ReadFloat"), &BufferWrapper::ReadFloat)
.Func(_SC("ReadString"), &BufferWrapper::ReadString)
.Func(_SC("ReadRawString"), &BufferWrapper::ReadRawString)
);
Register_BufferInterpreter(bns);
bns.Func(_SC("Create"), &BufferWrapper::Create);
RootTable(vm).Bind(_SC("SqBuffer"), bns);
}
} // Namespace:: SqMod

View File

@ -276,7 +276,7 @@ typedef LongInt< Int64 > SLongInt;
typedef LongInt< Uint64 > ULongInt;
// ------------------------------------------------------------------------------------------------
class BufferWrapper;
class SqBuffer;
/* ------------------------------------------------------------------------------------------------
* FORWARD DECLARATIONS