mirror of
				https://github.com/VCMP-SqMod/SqMod.git
				synced 2025-11-04 00:07:19 +01:00 
			
		
		
		
	Move shared code into main source.
Remove extra module API.
This commit is contained in:
		
							
								
								
									
										612
									
								
								source/Base/Buffer.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										612
									
								
								source/Base/Buffer.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,612 @@
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
#include "Base/Buffer.hpp"
 | 
			
		||||
#include "sqrat/sqratUtil.h"
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
#include <cstdlib>
 | 
			
		||||
#include <cstring>
 | 
			
		||||
#include <exception>
 | 
			
		||||
#include <stdexcept>
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
namespace SqMod {
 | 
			
		||||
 | 
			
		||||
/* ------------------------------------------------------------------------------------------------
 | 
			
		||||
 * Compute the next power of two for the specified number.
 | 
			
		||||
*/
 | 
			
		||||
inline unsigned int NextPow2(unsigned int num)
 | 
			
		||||
{
 | 
			
		||||
    --num;
 | 
			
		||||
    num |= num >> 1;
 | 
			
		||||
    num |= num >> 2;
 | 
			
		||||
    num |= num >> 4;
 | 
			
		||||
    num |= num >> 8;
 | 
			
		||||
    num |= num >> 16;
 | 
			
		||||
    return ++num;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* ------------------------------------------------------------------------------------------------
 | 
			
		||||
 * Throw an memory exception.
 | 
			
		||||
*/
 | 
			
		||||
void ThrowMemExcept(const char * msg, ...)
 | 
			
		||||
{
 | 
			
		||||
    // Exception messages should be concise
 | 
			
		||||
    SQChar buffer[256];
 | 
			
		||||
    // Variable arguments structure
 | 
			
		||||
    va_list args;
 | 
			
		||||
    // Get the specified arguments
 | 
			
		||||
    va_start(args, msg);
 | 
			
		||||
    // Run the specified format
 | 
			
		||||
    int ret =  std::vsnprintf(buffer, sizeof(buffer), msg, args);
 | 
			
		||||
    // Check for formatting errors
 | 
			
		||||
    if (ret < 0)
 | 
			
		||||
    {
 | 
			
		||||
        throw Sqrat::Exception(_SC("Unknown memory error"));
 | 
			
		||||
    }
 | 
			
		||||
    // Throw the actual exception
 | 
			
		||||
    throw Sqrat::Exception(buffer);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* ------------------------------------------------------------------------------------------------
 | 
			
		||||
 * Allocate a memory buffer and return it.
 | 
			
		||||
*/
 | 
			
		||||
static Buffer::Pointer AllocMem(Buffer::SzType size)
 | 
			
		||||
{
 | 
			
		||||
    // Attempt to allocate memory directly
 | 
			
		||||
    Buffer::Pointer ptr = reinterpret_cast< Buffer::Pointer >(std::malloc(size));
 | 
			
		||||
    // Validate the allocated memory
 | 
			
		||||
    if (!ptr)
 | 
			
		||||
    {
 | 
			
		||||
        ThrowMemExcept("Unable to allocate (%u) bytes of memory", size);
 | 
			
		||||
    }
 | 
			
		||||
    // Return the allocated memory
 | 
			
		||||
    return ptr;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* ------------------------------------------------------------------------------------------------
 | 
			
		||||
 * ...
 | 
			
		||||
*/
 | 
			
		||||
class MemCat
 | 
			
		||||
{
 | 
			
		||||
    // --------------------------------------------------------------------------------------------
 | 
			
		||||
    friend class Memory;
 | 
			
		||||
    friend class Buffer;
 | 
			
		||||
 | 
			
		||||
public:
 | 
			
		||||
 | 
			
		||||
    // --------------------------------------------------------------------------------------------
 | 
			
		||||
    typedef Buffer::Value       Value; // The type of value used to represent a byte.
 | 
			
		||||
 | 
			
		||||
    // --------------------------------------------------------------------------------------------
 | 
			
		||||
    typedef Buffer::Reference   Reference; // A reference to the stored value type.
 | 
			
		||||
    typedef Buffer::ConstRef    ConstRef; // A const reference to the stored value type.
 | 
			
		||||
 | 
			
		||||
    // --------------------------------------------------------------------------------------------
 | 
			
		||||
    typedef Buffer::Pointer     Pointer; // A pointer to the stored value type.
 | 
			
		||||
    typedef Buffer::ConstPtr    ConstPtr; // A const pointer to the stored value type.
 | 
			
		||||
 | 
			
		||||
    // --------------------------------------------------------------------------------------------
 | 
			
		||||
    typedef Buffer::SzType      SzType; // The type used to represent size in general.
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Structure used to store a memory chunk in the linked list.
 | 
			
		||||
    */
 | 
			
		||||
    struct Node
 | 
			
		||||
    {
 | 
			
		||||
        // ----------------------------------------------------------------------------------------
 | 
			
		||||
        SzType      mCap; // The size of the memory chunk.
 | 
			
		||||
        Pointer     mPtr; // Pointer to the memory chunk.
 | 
			
		||||
        Node*       mNext; // The next node in the list.
 | 
			
		||||
 | 
			
		||||
        /* ----------------------------------------------------------------------------------------
 | 
			
		||||
         * Base constructor.
 | 
			
		||||
        */
 | 
			
		||||
        Node(Node * next)
 | 
			
		||||
            : mCap(0)
 | 
			
		||||
            , mPtr(nullptr)
 | 
			
		||||
            , mNext(next)
 | 
			
		||||
        {
 | 
			
		||||
            /* ... */
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    // --------------------------------------------------------------------------------------------
 | 
			
		||||
    static Node *   s_Nodes; /* List of unused node instances. */
 | 
			
		||||
 | 
			
		||||
    // --------------------------------------------------------------------------------------------
 | 
			
		||||
    Node*   m_Head; /* The head memory node. */
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Default constructor.
 | 
			
		||||
    */
 | 
			
		||||
    MemCat()
 | 
			
		||||
        : m_Head(nullptr)
 | 
			
		||||
    {
 | 
			
		||||
        /* ... */
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Destructor.
 | 
			
		||||
    */
 | 
			
		||||
    ~MemCat()
 | 
			
		||||
    {
 | 
			
		||||
        for (Node * node = m_Head, * next = nullptr; node; node = next)
 | 
			
		||||
        {
 | 
			
		||||
            // Free the memory (if any)
 | 
			
		||||
            if (node->mPtr)
 | 
			
		||||
            {
 | 
			
		||||
                std::free(node->mPtr);
 | 
			
		||||
            }
 | 
			
		||||
            // Save the next node
 | 
			
		||||
            next = node->mNext;
 | 
			
		||||
            // Release the node instance
 | 
			
		||||
            delete node;
 | 
			
		||||
        }
 | 
			
		||||
        // Explicitly set the head node to null
 | 
			
		||||
        m_Head = nullptr;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Clear all memory buffers from the pool.
 | 
			
		||||
    */
 | 
			
		||||
    void Clear()
 | 
			
		||||
    {
 | 
			
		||||
        for (Node * node = m_Head, * next = nullptr; node; node = next)
 | 
			
		||||
        {
 | 
			
		||||
            // Free the memory (if any)
 | 
			
		||||
            if (node->mPtr)
 | 
			
		||||
            {
 | 
			
		||||
                free(node->mPtr);
 | 
			
		||||
            }
 | 
			
		||||
            // Save the next node
 | 
			
		||||
            next = node->mNext;
 | 
			
		||||
            // Release the node instance
 | 
			
		||||
            Push(node);
 | 
			
		||||
        }
 | 
			
		||||
        // Explicitly set the head node to null
 | 
			
		||||
        m_Head = nullptr;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Grab a memory buffer from the pool.
 | 
			
		||||
    */
 | 
			
		||||
    void Grab(Pointer & ptr, SzType & size)
 | 
			
		||||
    {
 | 
			
		||||
        // NOTE: Function assumes (size > 0)
 | 
			
		||||
        // Find a buffer large enough to satisfy the requested size
 | 
			
		||||
        for (Node * node = m_Head, * prev = nullptr; node; prev = node, node = node->mNext)
 | 
			
		||||
        {
 | 
			
		||||
            // Is this buffer large enough?
 | 
			
		||||
            if (node->mCap >= size)
 | 
			
		||||
            {
 | 
			
		||||
                // Was there a previous node?
 | 
			
		||||
                if (prev)
 | 
			
		||||
                {
 | 
			
		||||
                    prev->mNext = node->mNext;
 | 
			
		||||
                }
 | 
			
		||||
                // Probably this was the head
 | 
			
		||||
                else
 | 
			
		||||
                {
 | 
			
		||||
                    m_Head = node->mNext;
 | 
			
		||||
                }
 | 
			
		||||
                // Assign the memory
 | 
			
		||||
                ptr = node->mPtr;
 | 
			
		||||
                // Assign the size
 | 
			
		||||
                size = node->mCap;
 | 
			
		||||
                // Release the node instance
 | 
			
		||||
                Push(node);
 | 
			
		||||
                // Exit the function
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        // Round up the size to a power of two number
 | 
			
		||||
        size = (size & (size - 1)) ? NextPow2(size) : size;
 | 
			
		||||
        // Allocate the memory directly
 | 
			
		||||
        ptr = AllocMem(size);
 | 
			
		||||
        // See if the memory could be allocated
 | 
			
		||||
        // (shouldn't reach this point if allocation failed)
 | 
			
		||||
        if (!ptr)
 | 
			
		||||
        {
 | 
			
		||||
            // Revert the size
 | 
			
		||||
            size = 0;
 | 
			
		||||
            // Throw the exception
 | 
			
		||||
            ThrowMemExcept("Unable to allocate (%u) bytes of memory", size);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Return a memory buffer to the pool.
 | 
			
		||||
    */
 | 
			
		||||
    void Drop(Pointer & ptr, SzType & size)
 | 
			
		||||
    {
 | 
			
		||||
        if (!ptr)
 | 
			
		||||
        {
 | 
			
		||||
            ThrowMemExcept("Cannot store invalid memory buffer");
 | 
			
		||||
        }
 | 
			
		||||
        // Request a node instance
 | 
			
		||||
        Node * node = Pull();
 | 
			
		||||
        // Assign the specified memory
 | 
			
		||||
        node->mPtr = ptr;
 | 
			
		||||
        // Assign the specified size
 | 
			
		||||
        node->mCap = size;
 | 
			
		||||
        // Demote the current head node
 | 
			
		||||
        node->mNext = m_Head;
 | 
			
		||||
        // Promote as the head node
 | 
			
		||||
        m_Head = node;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Allocate a group of nodes and pool them for later use.
 | 
			
		||||
    */
 | 
			
		||||
    static void Make()
 | 
			
		||||
    {
 | 
			
		||||
        for (SzType n = 16; n; --n)
 | 
			
		||||
        {
 | 
			
		||||
            // Create a new node instance
 | 
			
		||||
            s_Nodes = new Node(s_Nodes);
 | 
			
		||||
            // Validate the head node
 | 
			
		||||
            if (!s_Nodes)
 | 
			
		||||
            {
 | 
			
		||||
                ThrowMemExcept("Unable to allocate memory nodes");
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Retrieve an unused node from the free list.
 | 
			
		||||
    */
 | 
			
		||||
    static Node * Pull()
 | 
			
		||||
    {
 | 
			
		||||
        // Are there any nodes available?
 | 
			
		||||
        if (!s_Nodes)
 | 
			
		||||
        {
 | 
			
		||||
            Make(); // Make some!
 | 
			
		||||
        }
 | 
			
		||||
        // Grab the head node
 | 
			
		||||
        Node * node = s_Nodes;
 | 
			
		||||
        // Promote the next node as the head
 | 
			
		||||
        s_Nodes = node->mNext;
 | 
			
		||||
        // Return the node
 | 
			
		||||
        return node;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Return a node to the free list.
 | 
			
		||||
    */
 | 
			
		||||
    static void Push(Node * node)
 | 
			
		||||
    {
 | 
			
		||||
        // See if the node is even valid
 | 
			
		||||
        if (!node)
 | 
			
		||||
        {
 | 
			
		||||
            ThrowMemExcept("Attempting to push invalid node");
 | 
			
		||||
        }
 | 
			
		||||
        // Demote the current head node
 | 
			
		||||
        node->mNext = s_Nodes;
 | 
			
		||||
        // Promote as the head node
 | 
			
		||||
        s_Nodes = node;
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
MemCat::Node *  MemCat::s_Nodes = nullptr;
 | 
			
		||||
 | 
			
		||||
/* ------------------------------------------------------------------------------------------------
 | 
			
		||||
 * Lightweight memory allocator to reduce the overhead of small allocations.
 | 
			
		||||
*/
 | 
			
		||||
class Memory
 | 
			
		||||
{
 | 
			
		||||
    // --------------------------------------------------------------------------------------------
 | 
			
		||||
    friend class Buffer; // Allow the buffer type to access the memory categories.
 | 
			
		||||
    friend class MemRef; // Allow the memory manager reference to create new instances.
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Default constructor.
 | 
			
		||||
    */
 | 
			
		||||
    Memory()
 | 
			
		||||
        : m_Small()
 | 
			
		||||
        , m_Medium()
 | 
			
		||||
        , m_Large()
 | 
			
		||||
    {
 | 
			
		||||
        // Allocate several nodes for when memory starts pooling
 | 
			
		||||
        MemCat::Make();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Destructor.
 | 
			
		||||
    */
 | 
			
		||||
    ~Memory()
 | 
			
		||||
    {
 | 
			
		||||
        for (MemCat::Node * node = MemCat::s_Nodes, * next = nullptr; node; node = next)
 | 
			
		||||
        {
 | 
			
		||||
            // Save the next node
 | 
			
		||||
            next = node->mNext;
 | 
			
		||||
            // Release the node instance
 | 
			
		||||
            delete node;
 | 
			
		||||
        }
 | 
			
		||||
        // Explicitly set the head node to null
 | 
			
		||||
        MemCat::s_Nodes = nullptr;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
 | 
			
		||||
    // --------------------------------------------------------------------------------------------
 | 
			
		||||
    MemCat m_Small; // Small memory allocations of <= 1024 bytes.
 | 
			
		||||
    MemCat m_Medium; // Medium memory allocations of <= 4096 bytes.
 | 
			
		||||
    MemCat m_Large; // Large memory allocations of <= 4096 bytes.
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
MemRef MemRef::s_Mem;
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
void MemRef::Grab()
 | 
			
		||||
{
 | 
			
		||||
    if (m_Ptr)
 | 
			
		||||
    {
 | 
			
		||||
        ++(*m_Ref);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
void MemRef::Drop()
 | 
			
		||||
{
 | 
			
		||||
    if (m_Ptr && --(*m_Ref) == 0)
 | 
			
		||||
    {
 | 
			
		||||
        delete m_Ptr;
 | 
			
		||||
        delete m_Ref;
 | 
			
		||||
        m_Ptr = nullptr;
 | 
			
		||||
        m_Ref = nullptr;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
const MemRef & MemRef::Get()
 | 
			
		||||
{
 | 
			
		||||
    if (!s_Mem.m_Ptr)
 | 
			
		||||
    {
 | 
			
		||||
        s_Mem.m_Ptr = new Memory();
 | 
			
		||||
        s_Mem.m_Ref = new Counter(1);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return s_Mem;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
Buffer::Buffer(const Buffer & o)
 | 
			
		||||
    : m_Ptr(nullptr)
 | 
			
		||||
    , m_Cap(o.m_Cap)
 | 
			
		||||
    , m_Cur(o.m_Cur)
 | 
			
		||||
    , m_Mem(o.m_Mem)
 | 
			
		||||
{
 | 
			
		||||
    if (m_Cap)
 | 
			
		||||
    {
 | 
			
		||||
        Request(o.m_Cap);
 | 
			
		||||
        std::memcpy(m_Ptr, o.m_Ptr, o.m_Cap);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
Buffer::~Buffer()
 | 
			
		||||
{
 | 
			
		||||
    // Do we have a buffer?
 | 
			
		||||
    if (m_Ptr)
 | 
			
		||||
    {
 | 
			
		||||
        Release(); // Release it!
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
Buffer & Buffer::operator = (const Buffer & o)
 | 
			
		||||
{
 | 
			
		||||
    if (m_Ptr != o.m_Ptr)
 | 
			
		||||
    {
 | 
			
		||||
        // Can we work in the current buffer?
 | 
			
		||||
        if (m_Cap && o.m_Cap <= m_Cap)
 | 
			
		||||
        {
 | 
			
		||||
            // It's safe to copy the data
 | 
			
		||||
            std::memcpy(m_Ptr, o.m_Ptr, o.m_Cap);
 | 
			
		||||
        }
 | 
			
		||||
        // Do we even have data to copy?
 | 
			
		||||
        else if (!o.m_Cap)
 | 
			
		||||
        {
 | 
			
		||||
            // Do we have a buffer?
 | 
			
		||||
            if (m_Ptr)
 | 
			
		||||
            {
 | 
			
		||||
                Release(); // Release it!
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        else
 | 
			
		||||
        {
 | 
			
		||||
            // Do we have a buffer?
 | 
			
		||||
            if (m_Ptr)
 | 
			
		||||
            {
 | 
			
		||||
                Release(); // Release it!
 | 
			
		||||
            }
 | 
			
		||||
            // Request a larger buffer
 | 
			
		||||
            Request(o.m_Cap);
 | 
			
		||||
            // Now it's safe to copy the data
 | 
			
		||||
            std::memcpy(m_Ptr, o.m_Ptr, o.m_Cap);
 | 
			
		||||
        }
 | 
			
		||||
        // Also copy the edit cursor
 | 
			
		||||
        m_Cur = o.m_Cur;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return *this;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
void Buffer::Grow(SzType n)
 | 
			
		||||
{
 | 
			
		||||
    // Backup the current memory
 | 
			
		||||
    Buffer bkp(m_Ptr, m_Cap, m_Cur, m_Mem);
 | 
			
		||||
    // Acquire a bigger buffer
 | 
			
		||||
    Request(bkp.m_Cap + n);
 | 
			
		||||
    // Copy the data from the old buffer
 | 
			
		||||
    std::memcpy(m_Ptr, bkp.m_Ptr, bkp.m_Cap);
 | 
			
		||||
    // Copy the previous edit cursor
 | 
			
		||||
    m_Cur = bkp.m_Cur;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
void Buffer::Request(SzType n)
 | 
			
		||||
{
 | 
			
		||||
    // NOTE: Function assumes (n > 0)
 | 
			
		||||
    // Is there a memory manager available?
 | 
			
		||||
    if (!m_Mem)
 | 
			
		||||
    {
 | 
			
		||||
        // Round up the size to a power of two number
 | 
			
		||||
        n = (n & (n - 1)) ? NextPow2(n) : n;
 | 
			
		||||
        // Allocate the memory directly
 | 
			
		||||
        m_Ptr = AllocMem(n);
 | 
			
		||||
    }
 | 
			
		||||
    // Find out in which category does this buffer reside
 | 
			
		||||
    else if (n <= 1024)
 | 
			
		||||
    {
 | 
			
		||||
        m_Mem->m_Small.Grab(m_Ptr, n);
 | 
			
		||||
    }
 | 
			
		||||
    else if (n <= 4096)
 | 
			
		||||
    {
 | 
			
		||||
        m_Mem->m_Medium.Grab(m_Ptr, n);
 | 
			
		||||
    }
 | 
			
		||||
    else
 | 
			
		||||
    {
 | 
			
		||||
        m_Mem->m_Large.Grab(m_Ptr, n);
 | 
			
		||||
    }
 | 
			
		||||
    // If no errors occurred then we can set the size
 | 
			
		||||
    m_Cap = n;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
void Buffer::Release()
 | 
			
		||||
{
 | 
			
		||||
    // TODO: Implement a limit on how much memory can actually be pooled.
 | 
			
		||||
    // Is there a memory manager available?
 | 
			
		||||
    if (!m_Mem)
 | 
			
		||||
    {
 | 
			
		||||
        std::free(m_Ptr); // Deallocate the memory directly
 | 
			
		||||
    }
 | 
			
		||||
    // Find out to which category does this buffer belong
 | 
			
		||||
    else if (m_Cap <= 1024)
 | 
			
		||||
    {
 | 
			
		||||
        m_Mem->m_Small.Drop(m_Ptr, m_Cap);
 | 
			
		||||
    }
 | 
			
		||||
    else if (m_Cap <= 4096)
 | 
			
		||||
    {
 | 
			
		||||
        m_Mem->m_Medium.Drop(m_Ptr, m_Cap);
 | 
			
		||||
    }
 | 
			
		||||
    else
 | 
			
		||||
    {
 | 
			
		||||
        m_Mem->m_Large.Drop(m_Ptr, m_Cap);
 | 
			
		||||
    }
 | 
			
		||||
    // Explicitly reset the buffer
 | 
			
		||||
    m_Ptr = nullptr;
 | 
			
		||||
    m_Cap = 0;
 | 
			
		||||
    m_Cur = 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
Buffer::SzType Buffer::Write(SzType pos, ConstPtr data, SzType size)
 | 
			
		||||
{
 | 
			
		||||
    // Do we have what to write?
 | 
			
		||||
    if (!data || !size)
 | 
			
		||||
    {
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
    // See if the buffer size must be adjusted
 | 
			
		||||
    else if ((pos + size) >= m_Cap)
 | 
			
		||||
    {
 | 
			
		||||
        // Acquire a larger buffer
 | 
			
		||||
        Grow((pos + size) - m_Cap + 32);
 | 
			
		||||
    }
 | 
			
		||||
    // Copy the data into the internal buffer
 | 
			
		||||
    std::memcpy(m_Ptr + pos, data, size);
 | 
			
		||||
    // Return the amount of data written to the buffer
 | 
			
		||||
    return size;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
Buffer::SzType Buffer::WriteF(SzType pos, const char * fmt, ...)
 | 
			
		||||
{
 | 
			
		||||
    // Initialize the variable argument list
 | 
			
		||||
    va_list args;
 | 
			
		||||
    va_start(args, fmt);
 | 
			
		||||
    // Call the function that takes the variable argument list
 | 
			
		||||
    const SzType ret = WriteF(pos, fmt, args);
 | 
			
		||||
    // Finalize the variable argument list
 | 
			
		||||
    va_end(args);
 | 
			
		||||
    // Return the result
 | 
			
		||||
    return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
Buffer::SzType Buffer::WriteF(SzType pos, const char * fmt, va_list args)
 | 
			
		||||
{
 | 
			
		||||
    // Is the specified position within range?
 | 
			
		||||
    if (pos >= m_Cap)
 | 
			
		||||
    {
 | 
			
		||||
        // Acquire a larger buffer
 | 
			
		||||
        Grow(pos - m_Cap + 32);
 | 
			
		||||
    }
 | 
			
		||||
    // Backup the variable argument list
 | 
			
		||||
    va_list args_cpy;
 | 
			
		||||
    va_copy(args_cpy, args);
 | 
			
		||||
    // Attempt to write to the current buffer
 | 
			
		||||
    // (if empty, it should tell us the necessary size)
 | 
			
		||||
    int ret =  std::vsnprintf(m_Ptr + pos, m_Cap, fmt, args);
 | 
			
		||||
    // Do we need a bigger buffer?
 | 
			
		||||
    if ((pos + ret) >= m_Cap)
 | 
			
		||||
    {
 | 
			
		||||
        // Acquire a larger buffer
 | 
			
		||||
        Grow((pos + ret) - m_Cap + 32);
 | 
			
		||||
        // Retry writing the requested information
 | 
			
		||||
        ret =  std::vsnprintf(m_Ptr + pos, m_Cap, fmt, args_cpy);
 | 
			
		||||
    }
 | 
			
		||||
    // Return the value 0 if data could not be written
 | 
			
		||||
    if (ret < 0)
 | 
			
		||||
    {
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
    // Return the number of written characters
 | 
			
		||||
    return static_cast< SzType >(ret);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
Buffer::SzType Buffer::WriteS(SzType pos, ConstPtr str)
 | 
			
		||||
{
 | 
			
		||||
    // Is there any string to write?
 | 
			
		||||
    if (str && *str != '\0')
 | 
			
		||||
    {
 | 
			
		||||
        // Forward this to the regular write function
 | 
			
		||||
        return Write(pos, str, std::strlen(str));
 | 
			
		||||
    }
 | 
			
		||||
    // Nothing to write
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
void Buffer::AppendF(const char * fmt, ...)
 | 
			
		||||
{
 | 
			
		||||
    // Initialize the variable argument list
 | 
			
		||||
    va_list args;
 | 
			
		||||
    va_start(args, fmt);
 | 
			
		||||
    // Forward this to the regular write function
 | 
			
		||||
    m_Cur += WriteF(m_Cur, fmt, args);
 | 
			
		||||
    // Finalize the variable argument list
 | 
			
		||||
    va_end(args);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
void Buffer::AppendS(const char * str)
 | 
			
		||||
{
 | 
			
		||||
    // Is there any string to write?
 | 
			
		||||
    if (str)
 | 
			
		||||
    {
 | 
			
		||||
        m_Cur += Write(m_Cur, str, std::strlen(str));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
} // Namespace:: SqMod
 | 
			
		||||
							
								
								
									
										1026
									
								
								source/Base/Buffer.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1026
									
								
								source/Base/Buffer.hpp
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										809
									
								
								source/Base/DynArg.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										809
									
								
								source/Base/DynArg.hpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,809 @@
 | 
			
		||||
#ifndef _BASE_DYNARG_HPP_
 | 
			
		||||
#define _BASE_DYNARG_HPP_
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
namespace SqMod {
 | 
			
		||||
 | 
			
		||||
/* ------------------------------------------------------------------------------------------------
 | 
			
		||||
 * Helper class used to check if a certain operation is possible on the specified argument.
 | 
			
		||||
*/
 | 
			
		||||
template < typename U > struct SqDynArg
 | 
			
		||||
{
 | 
			
		||||
    static inline bool CanPop(HSQUIRRELVM vm)
 | 
			
		||||
    {
 | 
			
		||||
        return (sq_gettype(vm, 2) == OT_INSTANCE);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    template < typename F > static inline auto Do(F & fn, HSQUIRRELVM vm)
 | 
			
		||||
    {
 | 
			
		||||
        Var< U * > var(vm, 2);
 | 
			
		||||
        // Validate the obtained instance
 | 
			
		||||
        if (!var.value)
 | 
			
		||||
        {
 | 
			
		||||
            STHROWF("No such instance");
 | 
			
		||||
        }
 | 
			
		||||
        // Attempt to perform the requested operation
 | 
			
		||||
        return fn(*var.value);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    template < typename F > static inline auto Do(const F & fn, HSQUIRRELVM vm)
 | 
			
		||||
    {
 | 
			
		||||
        Var< U * > var(vm, 2);
 | 
			
		||||
        // Validate the obtained instance
 | 
			
		||||
        if (!var.value)
 | 
			
		||||
        {
 | 
			
		||||
            STHROWF("No such instance");
 | 
			
		||||
        }
 | 
			
		||||
        // Attempt to perform the requested operation
 | 
			
		||||
        return fn(*var.value);
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
/* ------------------------------------------------------------------------------------------------
 | 
			
		||||
 * Instance pointer specializations of the argument checking structure.
 | 
			
		||||
*/
 | 
			
		||||
template < typename U > struct SqDynArg< U * >
 | 
			
		||||
{
 | 
			
		||||
    static inline bool CanPop(HSQUIRRELVM vm)
 | 
			
		||||
    {
 | 
			
		||||
        return (sq_gettype(vm, 2) == OT_INSTANCE);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    template < typename F > static inline auto Do(F & fn, HSQUIRRELVM vm)
 | 
			
		||||
    {
 | 
			
		||||
        Var< U * > var(vm, 2);
 | 
			
		||||
        // Validate the obtained instance
 | 
			
		||||
        if (!var.value)
 | 
			
		||||
        {
 | 
			
		||||
            STHROWF("No such instance");
 | 
			
		||||
        }
 | 
			
		||||
        // Attempt to perform the requested operation
 | 
			
		||||
        return fn(var.value);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    template < typename F > static inline auto Do(const F & fn, HSQUIRRELVM vm)
 | 
			
		||||
    {
 | 
			
		||||
        Var< U * > var(vm, 2);
 | 
			
		||||
        // Validate the obtained instance
 | 
			
		||||
        if (!var.value)
 | 
			
		||||
        {
 | 
			
		||||
            STHROWF("No such instance");
 | 
			
		||||
        }
 | 
			
		||||
        // Attempt to perform the requested operation
 | 
			
		||||
        return fn(var.value);
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
/* ------------------------------------------------------------------------------------------------
 | 
			
		||||
 * Instance pointer specializations of the argument checking structure.
 | 
			
		||||
*/
 | 
			
		||||
template < typename U > struct SqDynArg< const U * >
 | 
			
		||||
{
 | 
			
		||||
    static inline bool CanPop(HSQUIRRELVM vm)
 | 
			
		||||
    {
 | 
			
		||||
        return (sq_gettype(vm, 2) == OT_INSTANCE);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    template < typename F > static inline auto Do(F & fn, HSQUIRRELVM vm)
 | 
			
		||||
    {
 | 
			
		||||
        Var< U * > var(vm, 2);
 | 
			
		||||
        // Validate the obtained instance
 | 
			
		||||
        if (!var.value)
 | 
			
		||||
        {
 | 
			
		||||
            STHROWF("No such instance");
 | 
			
		||||
        }
 | 
			
		||||
        // Attempt to perform the requested operation
 | 
			
		||||
        return fn(*var.value);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    template < typename F > static inline auto Do(const F & fn, HSQUIRRELVM vm)
 | 
			
		||||
    {
 | 
			
		||||
        Var< U * > var(vm, 2);
 | 
			
		||||
        // Validate the obtained instance
 | 
			
		||||
        if (!var.value)
 | 
			
		||||
        {
 | 
			
		||||
            STHROWF("No such instance");
 | 
			
		||||
        }
 | 
			
		||||
        // Attempt to perform the requested operation
 | 
			
		||||
        return fn(*var.value);
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
/* ------------------------------------------------------------------------------------------------
 | 
			
		||||
 * Inherited by the base specializations of the argument checking structure.
 | 
			
		||||
*/
 | 
			
		||||
template < typename U, SQObjectType SqT > struct SqDynArgBase
 | 
			
		||||
{
 | 
			
		||||
    static inline bool CanPop(HSQUIRRELVM vm)
 | 
			
		||||
    {
 | 
			
		||||
        return (sq_gettype(vm, 2) == SqT);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    template < typename F > static inline auto Do(F & fn, HSQUIRRELVM vm)
 | 
			
		||||
    {
 | 
			
		||||
        Var< U > var(vm, 2);
 | 
			
		||||
        // Attempt to perform the requested operation
 | 
			
		||||
        return fn(var.value);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    template < typename F > static inline auto Do(const F & fn, HSQUIRRELVM vm)
 | 
			
		||||
    {
 | 
			
		||||
        Var< U > var(vm, 2);
 | 
			
		||||
        // Attempt to perform the requested operation
 | 
			
		||||
        return fn(var.value);
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
template < > struct SqDynArg< char >
 | 
			
		||||
    : public SqDynArgBase< char, OT_INTEGER >
 | 
			
		||||
{ /* ... */ };
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
template < > struct SqDynArg< signed char >
 | 
			
		||||
    : public SqDynArgBase< signed char, OT_INTEGER >
 | 
			
		||||
{ /* ... */ };
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
template < > struct SqDynArg< unsigned char >
 | 
			
		||||
    : public SqDynArgBase< unsigned char, OT_INTEGER >
 | 
			
		||||
{ /* ... */ };
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
template < > struct SqDynArg< signed short >
 | 
			
		||||
    : public SqDynArgBase< signed short, OT_INTEGER >
 | 
			
		||||
{ /* ... */ };
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
template < > struct SqDynArg< unsigned short >
 | 
			
		||||
    : public SqDynArgBase< unsigned short, OT_INTEGER >
 | 
			
		||||
{ /* ... */ };
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
template < > struct SqDynArg< signed int >
 | 
			
		||||
    : public SqDynArgBase< signed int, OT_INTEGER >
 | 
			
		||||
{ /* ... */ };
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
template < > struct SqDynArg< unsigned int >
 | 
			
		||||
    : public SqDynArgBase< unsigned int, OT_INTEGER >
 | 
			
		||||
{ /* ... */ };
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
template < > struct SqDynArg< signed long >
 | 
			
		||||
    : public SqDynArgBase< signed long, OT_INTEGER >
 | 
			
		||||
{ /* ... */ };
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
template < > struct SqDynArg< unsigned long >
 | 
			
		||||
    : public SqDynArgBase< unsigned long, OT_INTEGER >
 | 
			
		||||
{ /* ... */ };
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
template < > struct SqDynArg< signed long long >
 | 
			
		||||
    : public SqDynArgBase< signed long long, OT_INTEGER >
 | 
			
		||||
{ /* ... */ };
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
template < > struct SqDynArg< unsigned long long >
 | 
			
		||||
    : public SqDynArgBase< unsigned long long, OT_INTEGER >
 | 
			
		||||
{ /* ... */ };
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
template < > struct SqDynArg< float >
 | 
			
		||||
    : public SqDynArgBase< float, OT_FLOAT >
 | 
			
		||||
{ /* ... */ };
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
template < > struct SqDynArg< double >
 | 
			
		||||
    : public SqDynArgBase< double, OT_FLOAT >
 | 
			
		||||
{ /* ... */ };
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
template < > struct SqDynArg< bool >
 | 
			
		||||
    : public SqDynArgBase< bool, OT_BOOL >
 | 
			
		||||
{ /* ... */ };
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
template < > struct SqDynArg< SQChar * >
 | 
			
		||||
    : public SqDynArgBase< SQChar *, OT_STRING >
 | 
			
		||||
{ /* ... */ };
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
template < > struct SqDynArg< const SQChar * >
 | 
			
		||||
    : public SqDynArgBase< const SQChar *, OT_STRING >
 | 
			
		||||
{ /* ... */ };
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
template < > struct SqDynArg< String >
 | 
			
		||||
    : public SqDynArgBase< String, OT_STRING >
 | 
			
		||||
{ /* ... */ };
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
template < > struct SqDynArg< Table >
 | 
			
		||||
    : public SqDynArgBase< Table, OT_TABLE >
 | 
			
		||||
{ /* ... */ };
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
template < > struct SqDynArg< Array >
 | 
			
		||||
    : public SqDynArgBase< Array, OT_ARRAY >
 | 
			
		||||
{ /* ... */ };
 | 
			
		||||
 | 
			
		||||
/* ------------------------------------------------------------------------------------------------
 | 
			
		||||
 * Specialization of the argument checking structure for functions.
 | 
			
		||||
*/
 | 
			
		||||
template < > struct SqDynArg< Function >
 | 
			
		||||
{
 | 
			
		||||
    static inline bool CanPop(HSQUIRRELVM vm)
 | 
			
		||||
    {
 | 
			
		||||
        const SQObjectType type = sq_gettype(vm, 2);
 | 
			
		||||
        return (type == OT_CLOSURE || type == OT_NATIVECLOSURE);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    template < typename F > static inline auto Do(F & fn, HSQUIRRELVM vm)
 | 
			
		||||
    {
 | 
			
		||||
        Var< Function > var(vm, 2);
 | 
			
		||||
        // Attempt to perform the requested operation
 | 
			
		||||
        return fn(var.value);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    template < typename F > static inline auto Do(const F & fn, HSQUIRRELVM vm)
 | 
			
		||||
    {
 | 
			
		||||
        Var< Function > var(vm, 2);
 | 
			
		||||
        // Attempt to perform the requested operation
 | 
			
		||||
        return fn(var.value);
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
/* ------------------------------------------------------------------------------------------------
 | 
			
		||||
 * Specialization of the argument checking structure for objects.
 | 
			
		||||
*/
 | 
			
		||||
template < > struct SqDynArg< Object >
 | 
			
		||||
{
 | 
			
		||||
    static inline bool CanPop(HSQUIRRELVM /*vm*/)
 | 
			
		||||
    {
 | 
			
		||||
        return true; // Objects can use any type.
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    template < typename F > static inline auto Do(F & fn, HSQUIRRELVM vm)
 | 
			
		||||
    {
 | 
			
		||||
        Var< Object > var(vm, 2);
 | 
			
		||||
        // Attempt to perform the requested operation
 | 
			
		||||
        return fn(var.value);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    template < typename F > static inline auto Do(const F & fn, HSQUIRRELVM vm)
 | 
			
		||||
    {
 | 
			
		||||
        Var< Object > var(vm, 2);
 | 
			
		||||
        // Attempt to perform the requested operation
 | 
			
		||||
        return fn(var.value);
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
/* ------------------------------------------------------------------------------------------------
 | 
			
		||||
 * Specialization of the argument checking structure for null.
 | 
			
		||||
*/
 | 
			
		||||
template < > struct SqDynArg< std::nullptr_t >
 | 
			
		||||
{
 | 
			
		||||
    static inline bool CanPop(HSQUIRRELVM vm)
 | 
			
		||||
    {
 | 
			
		||||
        return (sq_gettype(vm, 2) == OT_NULL);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    template < typename F > static inline auto Do(F & fn, HSQUIRRELVM /*vm*/)
 | 
			
		||||
    {
 | 
			
		||||
        // Attempt to perform the requested operation
 | 
			
		||||
        return fn(nullptr);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    template < typename F > static inline auto Do(const F & fn, HSQUIRRELVM /*vm*/)
 | 
			
		||||
    {
 | 
			
		||||
        // Attempt to perform the requested operation
 | 
			
		||||
        return fn(nullptr);
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
/* ------------------------------------------------------------------------------------------------
 | 
			
		||||
 * Used to perform an operation on multiple types specified at compile time.
 | 
			
		||||
*/
 | 
			
		||||
template < typename... Ts > struct SqDynArgImpl;
 | 
			
		||||
 | 
			
		||||
/* ------------------------------------------------------------------------------------------------
 | 
			
		||||
 * Zeroth case of the operation. No known operation of such pairs of types at this point.
 | 
			
		||||
*/
 | 
			
		||||
template < > struct SqDynArgImpl< >
 | 
			
		||||
{
 | 
			
		||||
    template < typename T > static Int32 Try(const T & /*val*/, HSQUIRRELVM vm)
 | 
			
		||||
    {
 | 
			
		||||
        const String tn1(SqTypeName(vm, 1));
 | 
			
		||||
        const String tn2(SqTypeName(vm, 2));
 | 
			
		||||
        return sq_throwerror(vm, ToStrF("Such operation is not possible between (%s) and (%s)",
 | 
			
		||||
                                        tn1.c_str(), tn2.c_str()));
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
/* ------------------------------------------------------------------------------------------------
 | 
			
		||||
 * Argument pack type pealing. Attempt to perform the operation with a specified type.
 | 
			
		||||
*/
 | 
			
		||||
template < typename U, typename... Ts > struct SqDynArgImpl< U, Ts... >
 | 
			
		||||
{
 | 
			
		||||
    template < typename F > static Int32 Try(F & fn, HSQUIRRELVM vm)
 | 
			
		||||
    {
 | 
			
		||||
        typedef typename std::decay< U >::type ArgType;
 | 
			
		||||
        // Can the stack value be used with the current type?
 | 
			
		||||
        if (SqDynArg< ArgType >::CanPop(vm))
 | 
			
		||||
        {
 | 
			
		||||
            // If not an instance then don't use a try catch block
 | 
			
		||||
            if (sq_gettype(vm, 2) != OT_INSTANCE)
 | 
			
		||||
            {
 | 
			
		||||
                // Attempt to perform the operation and push the result on the stack
 | 
			
		||||
                PushVar(vm, SqDynArg< ArgType >::Do(fn, vm));
 | 
			
		||||
                // Specify that we completed the search and have a result
 | 
			
		||||
                return 1;
 | 
			
		||||
            }
 | 
			
		||||
            // Instances require a try/catch block since we can't differentiate types
 | 
			
		||||
            try
 | 
			
		||||
            {
 | 
			
		||||
                // Attempt to perform the operation and push the result on the stack
 | 
			
		||||
                PushVar(vm, SqDynArg< ArgType >::Do(fn, vm));
 | 
			
		||||
                // Specify that we completed the search and have a result
 | 
			
		||||
                return 1;
 | 
			
		||||
            }
 | 
			
		||||
            catch (const Sqrat::Exception & e)
 | 
			
		||||
            {
 | 
			
		||||
                // Probably the wrong type
 | 
			
		||||
            }
 | 
			
		||||
            catch (...)
 | 
			
		||||
            {
 | 
			
		||||
                // Something very bad happened. At this point, we just don't want to let
 | 
			
		||||
                // exceptions propagate to the virtual machine and we must end here.
 | 
			
		||||
                // Either way, reaching this point is bad and we just shuved it under
 | 
			
		||||
                // the rug. This is bad practice but circumstances forced me.
 | 
			
		||||
                // Don't do this at home kids!
 | 
			
		||||
                return -1;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        // On to the next type
 | 
			
		||||
        return SqDynArgImpl< Ts... >::Try(fn, vm);
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
/* ------------------------------------------------------------------------------------------------
 | 
			
		||||
 * Squirrel function that forwards the call to the actual implementation.
 | 
			
		||||
*/
 | 
			
		||||
template < typename F, typename U, typename... Ts > SQInteger SqDynArgFwd(HSQUIRRELVM vm)
 | 
			
		||||
{
 | 
			
		||||
    // Make sure that there are enough parameters on the stack
 | 
			
		||||
    if (sq_gettop(vm) < 2)
 | 
			
		||||
    {
 | 
			
		||||
        return sq_throwerror(vm, "Insufficient parameters for such operation");
 | 
			
		||||
    }
 | 
			
		||||
    // Attempt to create the functor that performs the operation
 | 
			
		||||
    F fn(vm);
 | 
			
		||||
    // Is this functor in a valid state?
 | 
			
		||||
    if (!fn)
 | 
			
		||||
    {
 | 
			
		||||
        return sq_throwerror(vm, "Functor failed to initialize");
 | 
			
		||||
    }
 | 
			
		||||
    // Attempt to perform the comparison
 | 
			
		||||
    try
 | 
			
		||||
    {
 | 
			
		||||
        return SqDynArgImpl< U, Ts... >::Try(fn, vm);
 | 
			
		||||
    }
 | 
			
		||||
    catch (const Sqrat::Exception & e)
 | 
			
		||||
    {
 | 
			
		||||
        return sq_throwerror(vm, e.what());
 | 
			
		||||
    }
 | 
			
		||||
    catch (...)
 | 
			
		||||
    {
 | 
			
		||||
        return sq_throwerror(vm, "Unknown error occurred during comparison");
 | 
			
		||||
    }
 | 
			
		||||
    // We shouldn't really reach this point but something must be returned
 | 
			
		||||
    return sq_throwerror(vm, "Operation encountered unknown behavior");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* ------------------------------------------------------------------------------------------------
 | 
			
		||||
 * Functor used to perform comparison.
 | 
			
		||||
*/
 | 
			
		||||
template < typename T > struct SqDynArgCmpFn
 | 
			
		||||
{
 | 
			
		||||
    // --------------------------------------------------------------------------------------------
 | 
			
		||||
    HSQUIRRELVM     mVM; // The squirrel virtual machine.
 | 
			
		||||
    Var< T * >      mVar; // The instance of the base type.
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Base constructor. (required)
 | 
			
		||||
    */
 | 
			
		||||
    SqDynArgCmpFn(HSQUIRRELVM vm)
 | 
			
		||||
        : mVM(vm), mVar(vm, 1)
 | 
			
		||||
    {
 | 
			
		||||
        /* ... */
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Implicit conversion to boolean. (required)
 | 
			
		||||
    */
 | 
			
		||||
    operator bool () const
 | 
			
		||||
    {
 | 
			
		||||
        return (mVar.value != nullptr);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Function call operator for references. (required)
 | 
			
		||||
    */
 | 
			
		||||
    template < typename U > SQInteger operator () (U & v) const
 | 
			
		||||
    {
 | 
			
		||||
        return mVar.value->Cmp(v);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Function call operator for const references. (required)
 | 
			
		||||
    */
 | 
			
		||||
    template < typename U > SQInteger operator () (const U & v) const
 | 
			
		||||
    {
 | 
			
		||||
        return mVar.value->Cmp(v);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Function call operator for pointers. (required)
 | 
			
		||||
    */
 | 
			
		||||
    template < typename U > SQInteger operator () (U * v) const
 | 
			
		||||
    {
 | 
			
		||||
        return mVar.value->Cmp(v);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Function call operator for const pointers. (required)
 | 
			
		||||
    */
 | 
			
		||||
    template < typename U > SQInteger operator () (const U * v) const
 | 
			
		||||
    {
 | 
			
		||||
        return mVar.value->Cmp(v);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Function call operator for null pointers. (required)
 | 
			
		||||
    */
 | 
			
		||||
    SQInteger operator () (std::nullptr_t) const
 | 
			
		||||
    {
 | 
			
		||||
        return mVar.value->Cmp(nullptr);
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
/* ------------------------------------------------------------------------------------------------
 | 
			
		||||
 * Functor used to perform addition.
 | 
			
		||||
*/
 | 
			
		||||
template < typename T > struct SqDynArgAddFn
 | 
			
		||||
{
 | 
			
		||||
    // --------------------------------------------------------------------------------------------
 | 
			
		||||
    HSQUIRRELVM     mVM; // The squirrel virtual machine.
 | 
			
		||||
    Var< T * >      mVar; // The instance of the base type.
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Base constructor. (required)
 | 
			
		||||
    */
 | 
			
		||||
    SqDynArgAddFn(HSQUIRRELVM vm)
 | 
			
		||||
        : mVM(vm), mVar(vm, 1)
 | 
			
		||||
    {
 | 
			
		||||
        /* ... */
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Implicit conversion to boolean. (required)
 | 
			
		||||
    */
 | 
			
		||||
    operator bool () const
 | 
			
		||||
    {
 | 
			
		||||
        return (mVar.value != nullptr);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Function call operator for references. (required)
 | 
			
		||||
    */
 | 
			
		||||
    template < typename U > T operator () (U & v) const
 | 
			
		||||
    {
 | 
			
		||||
        return (*mVar.value) + v;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Function call operator for const references. (required)
 | 
			
		||||
    */
 | 
			
		||||
    template < typename U > T operator () (const U & v) const
 | 
			
		||||
    {
 | 
			
		||||
        return (*mVar.value) + v;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Function call operator for pointers. (required)
 | 
			
		||||
    */
 | 
			
		||||
    template < typename U > T operator () (U * v) const
 | 
			
		||||
    {
 | 
			
		||||
        return (*mVar.value) + v;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Function call operator for const pointers. (required)
 | 
			
		||||
    */
 | 
			
		||||
    template < typename U > T operator () (const U * v) const
 | 
			
		||||
    {
 | 
			
		||||
        return (*mVar.value) + v;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Function call operator for null pointers. (required)
 | 
			
		||||
    */
 | 
			
		||||
    T operator () (std::nullptr_t) const
 | 
			
		||||
    {
 | 
			
		||||
        return (*mVar.value);
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
/* ------------------------------------------------------------------------------------------------
 | 
			
		||||
 * Functor used to perform subtraction.
 | 
			
		||||
*/
 | 
			
		||||
template < typename T > struct SqDynArgSubFn
 | 
			
		||||
{
 | 
			
		||||
    // --------------------------------------------------------------------------------------------
 | 
			
		||||
    HSQUIRRELVM     mVM; // The squirrel virtual machine.
 | 
			
		||||
    Var< T * >      mVar; // The instance of the base type.
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Base constructor. (required)
 | 
			
		||||
    */
 | 
			
		||||
    SqDynArgSubFn(HSQUIRRELVM vm)
 | 
			
		||||
        : mVM(vm), mVar(vm, 1)
 | 
			
		||||
    {
 | 
			
		||||
        /* ... */
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Implicit conversion to boolean. (required)
 | 
			
		||||
    */
 | 
			
		||||
    operator bool () const
 | 
			
		||||
    {
 | 
			
		||||
        return (mVar.value != nullptr);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Function call operator for references. (required)
 | 
			
		||||
    */
 | 
			
		||||
    template < typename U > T operator () (U & v) const
 | 
			
		||||
    {
 | 
			
		||||
        return (*mVar.value) - v;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Function call operator for const references. (required)
 | 
			
		||||
    */
 | 
			
		||||
    template < typename U > T operator () (const U & v) const
 | 
			
		||||
    {
 | 
			
		||||
        return (*mVar.value) - v;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Function call operator for pointers. (required)
 | 
			
		||||
    */
 | 
			
		||||
    template < typename U > T operator () (U * v) const
 | 
			
		||||
    {
 | 
			
		||||
        return (*mVar.value) - v;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Function call operator for const pointers. (required)
 | 
			
		||||
    */
 | 
			
		||||
    template < typename U > T operator () (const U * v) const
 | 
			
		||||
    {
 | 
			
		||||
        return (*mVar.value) - v;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Function call operator for null pointers. (required)
 | 
			
		||||
    */
 | 
			
		||||
    T operator () (std::nullptr_t) const
 | 
			
		||||
    {
 | 
			
		||||
        return (*mVar.value);
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
/* ------------------------------------------------------------------------------------------------
 | 
			
		||||
 * Functor used to perform multiplication.
 | 
			
		||||
*/
 | 
			
		||||
template < typename T > struct SqDynArgMulFn
 | 
			
		||||
{
 | 
			
		||||
    // --------------------------------------------------------------------------------------------
 | 
			
		||||
    HSQUIRRELVM     mVM; // The squirrel virtual machine.
 | 
			
		||||
    Var< T * >      mVar; // The instance of the base type.
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Base constructor. (required)
 | 
			
		||||
    */
 | 
			
		||||
    SqDynArgMulFn(HSQUIRRELVM vm)
 | 
			
		||||
        : mVM(vm), mVar(vm, 1)
 | 
			
		||||
    {
 | 
			
		||||
        /* ... */
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Implicit conversion to boolean. (required)
 | 
			
		||||
    */
 | 
			
		||||
    operator bool () const
 | 
			
		||||
    {
 | 
			
		||||
        return (mVar.value != nullptr);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Function call operator for references. (required)
 | 
			
		||||
    */
 | 
			
		||||
    template < typename U > T operator () (U & v) const
 | 
			
		||||
    {
 | 
			
		||||
        return (*mVar.value) * v;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Function call operator for const references. (required)
 | 
			
		||||
    */
 | 
			
		||||
    template < typename U > T operator () (const U & v) const
 | 
			
		||||
    {
 | 
			
		||||
        return (*mVar.value) * v;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Function call operator for pointers. (required)
 | 
			
		||||
    */
 | 
			
		||||
    template < typename U > T operator () (U * v) const
 | 
			
		||||
    {
 | 
			
		||||
        return (*mVar.value) * v;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Function call operator for const pointers. (required)
 | 
			
		||||
    */
 | 
			
		||||
    template < typename U > T operator () (const U * v) const
 | 
			
		||||
    {
 | 
			
		||||
        return (*mVar.value) * v;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Function call operator for null pointers. (required)
 | 
			
		||||
    */
 | 
			
		||||
    T operator () (std::nullptr_t) const
 | 
			
		||||
    {
 | 
			
		||||
        return (*mVar.value) * static_cast< SQInteger >(0);
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
/* ------------------------------------------------------------------------------------------------
 | 
			
		||||
 * Functor used to perform division.
 | 
			
		||||
*/
 | 
			
		||||
template < typename T > struct SqDynArgDivFn
 | 
			
		||||
{
 | 
			
		||||
    // --------------------------------------------------------------------------------------------
 | 
			
		||||
    HSQUIRRELVM     mVM; // The squirrel virtual machine.
 | 
			
		||||
    Var< T * >      mVar; // The instance of the base type.
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Base constructor. (required)
 | 
			
		||||
    */
 | 
			
		||||
    SqDynArgDivFn(HSQUIRRELVM vm)
 | 
			
		||||
        : mVM(vm), mVar(vm, 1)
 | 
			
		||||
    {
 | 
			
		||||
        /* ... */
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Implicit conversion to boolean. (required)
 | 
			
		||||
    */
 | 
			
		||||
    operator bool () const
 | 
			
		||||
    {
 | 
			
		||||
        return (mVar.value != nullptr);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Function call operator for references. (required)
 | 
			
		||||
    */
 | 
			
		||||
    template < typename U > T operator () (U & v) const
 | 
			
		||||
    {
 | 
			
		||||
        return (*mVar.value) / v;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Function call operator for const references. (required)
 | 
			
		||||
    */
 | 
			
		||||
    template < typename U > T operator () (const U & v) const
 | 
			
		||||
    {
 | 
			
		||||
        return (*mVar.value) / v;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Function call operator for pointers. (required)
 | 
			
		||||
    */
 | 
			
		||||
    template < typename U > T operator () (U * v) const
 | 
			
		||||
    {
 | 
			
		||||
        return (*mVar.value) / v;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Function call operator for const pointers. (required)
 | 
			
		||||
    */
 | 
			
		||||
    template < typename U > T operator () (const U * v) const
 | 
			
		||||
    {
 | 
			
		||||
        return (*mVar.value) / v;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Function call operator for null pointers. (required)
 | 
			
		||||
    */
 | 
			
		||||
    T operator () (std::nullptr_t) const
 | 
			
		||||
    {
 | 
			
		||||
        return (*mVar.value) / static_cast< SQInteger >(0);
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
/* ------------------------------------------------------------------------------------------------
 | 
			
		||||
 * Functor used to perform modulus.
 | 
			
		||||
*/
 | 
			
		||||
template < typename T > struct SqDynArgModFn
 | 
			
		||||
{
 | 
			
		||||
    // --------------------------------------------------------------------------------------------
 | 
			
		||||
    HSQUIRRELVM     mVM; // The squirrel virtual machine.
 | 
			
		||||
    Var< T * >      mVar; // The instance of the base type.
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Base constructor. (required)
 | 
			
		||||
    */
 | 
			
		||||
    SqDynArgModFn(HSQUIRRELVM vm)
 | 
			
		||||
        : mVM(vm), mVar(vm, 1)
 | 
			
		||||
    {
 | 
			
		||||
        /* ... */
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Implicit conversion to boolean. (required)
 | 
			
		||||
    */
 | 
			
		||||
    operator bool () const
 | 
			
		||||
    {
 | 
			
		||||
        return (mVar.value != nullptr);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Function call operator for references. (required)
 | 
			
		||||
    */
 | 
			
		||||
    template < typename U > T operator () (U & v) const
 | 
			
		||||
    {
 | 
			
		||||
        return (*mVar.value) % v;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Function call operator for const references. (required)
 | 
			
		||||
    */
 | 
			
		||||
    template < typename U > T operator () (const U & v) const
 | 
			
		||||
    {
 | 
			
		||||
        return (*mVar.value) % v;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Function call operator for pointers. (required)
 | 
			
		||||
    */
 | 
			
		||||
    template < typename U > T operator () (U * v) const
 | 
			
		||||
    {
 | 
			
		||||
        return (*mVar.value) % v;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Function call operator for const pointers. (required)
 | 
			
		||||
    */
 | 
			
		||||
    template < typename U > T operator () (const U * v) const
 | 
			
		||||
    {
 | 
			
		||||
        return (*mVar.value) % v;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* --------------------------------------------------------------------------------------------
 | 
			
		||||
     * Function call operator for null pointers. (required)
 | 
			
		||||
    */
 | 
			
		||||
    T operator () (std::nullptr_t) const
 | 
			
		||||
    {
 | 
			
		||||
        return (*mVar.value) % static_cast< SQInteger >(0);
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
} // Namespace:: SqMod
 | 
			
		||||
 | 
			
		||||
#endif // _BASE_DYNARG_HPP_
 | 
			
		||||
							
								
								
									
										1087
									
								
								source/Base/Utility.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1087
									
								
								source/Base/Utility.cpp
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										1522
									
								
								source/Base/Utility.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1522
									
								
								source/Base/Utility.hpp
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							@@ -1,14 +1,5 @@
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
#include "Core.hpp"
 | 
			
		||||
#include "Base/Buffer.hpp"
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
#include "Library/Numeric/LongInt.hpp"
 | 
			
		||||
#include "Library/Chrono/Date.hpp"
 | 
			
		||||
#include "Library/Chrono/Time.hpp"
 | 
			
		||||
#include "Library/Chrono/Datetime.hpp"
 | 
			
		||||
#include "Library/Chrono/Timestamp.hpp"
 | 
			
		||||
#include "Library/Utils/Buffer.hpp"
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
#include <cmath>
 | 
			
		||||
@@ -42,597 +33,6 @@ static SQRESULT SqModImpl_LoadScript(const SQChar * filepath, SQBool delay)
 | 
			
		||||
    return SQ_ERROR;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
static SQRESULT SqModImpl_GetSLongValue(HSQUIRRELVM vm, SQInteger idx, Int64 * num)
 | 
			
		||||
{
 | 
			
		||||
    // Validate the specified number pointer and value type
 | 
			
		||||
    if (!num)
 | 
			
		||||
    {
 | 
			
		||||
        return SQ_ERROR; // Nowhere to save!
 | 
			
		||||
    }
 | 
			
		||||
    // Is this an instance that we can treat as a SLongInt type?
 | 
			
		||||
    else if (sq_gettype(vm, idx) == OT_INSTANCE)
 | 
			
		||||
    {
 | 
			
		||||
        // Attempt to obtain the long instance and it's value from the stack
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            *num =  static_cast< Int64 >(Var< const SLongInt & >(vm, idx).value.GetNum());
 | 
			
		||||
        }
 | 
			
		||||
        catch (...)
 | 
			
		||||
        {
 | 
			
		||||
            return SQ_ERROR; // Unable to obtain the value!
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    // Is this a pure integer value?
 | 
			
		||||
    else if(sq_gettype(vm, idx) == OT_INTEGER)
 | 
			
		||||
    {
 | 
			
		||||
        SQInteger val = 0;
 | 
			
		||||
        // Attempt to get the value from the stack
 | 
			
		||||
        sq_getinteger(vm, idx, &val);
 | 
			
		||||
        // Save it into the specified memory location
 | 
			
		||||
        *num = static_cast< Int64 >(val);
 | 
			
		||||
    }
 | 
			
		||||
    // Is this a pure floating point value?
 | 
			
		||||
    else if(sq_gettype(vm, idx) == OT_FLOAT)
 | 
			
		||||
    {
 | 
			
		||||
        SQFloat val = 0.0;
 | 
			
		||||
        // Attempt to get the value from the stack
 | 
			
		||||
        sq_getfloat(vm, idx, &val);
 | 
			
		||||
        // Save it into the specified memory location
 | 
			
		||||
        *num = static_cast< Int64 >(std::llround(val));
 | 
			
		||||
    }
 | 
			
		||||
    // Is this a pure boolean value?
 | 
			
		||||
    else if(sq_gettype(vm, idx) == OT_BOOL)
 | 
			
		||||
    {
 | 
			
		||||
        SQBool val = SQFalse;
 | 
			
		||||
        // Attempt to get the value from the stack
 | 
			
		||||
        sq_getbool(vm, idx, &val);
 | 
			
		||||
        // Save it into the specified memory location
 | 
			
		||||
        *num = static_cast< Int64 >(val);
 | 
			
		||||
    }
 | 
			
		||||
    // Unrecognized value
 | 
			
		||||
    else
 | 
			
		||||
    {
 | 
			
		||||
        return SQ_ERROR;
 | 
			
		||||
    }
 | 
			
		||||
    // Value retrieved
 | 
			
		||||
    return SQ_OK;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
static SQRESULT SqModImpl_PushSLongObject(HSQUIRRELVM vm, Int64 num)
 | 
			
		||||
{
 | 
			
		||||
    // Attempt to push the requested instance
 | 
			
		||||
    try
 | 
			
		||||
    {
 | 
			
		||||
        Var< const SLongInt & >::push(vm, SLongInt(num));
 | 
			
		||||
    }
 | 
			
		||||
    catch (...)
 | 
			
		||||
    {
 | 
			
		||||
        // Specify that we failed
 | 
			
		||||
        return SQ_ERROR;
 | 
			
		||||
    }
 | 
			
		||||
    // Specify that we succeeded
 | 
			
		||||
    return SQ_OK;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
static SQRESULT SqModImpl_GetULongValue(HSQUIRRELVM vm, SQInteger idx, Uint64 * num)
 | 
			
		||||
{
 | 
			
		||||
    // Validate the specified number pointer and value type
 | 
			
		||||
    if (!num)
 | 
			
		||||
    {
 | 
			
		||||
        return SQ_ERROR; // Nowhere to save
 | 
			
		||||
    }
 | 
			
		||||
    // Is this an instance that we can treat as a ULongInt type?
 | 
			
		||||
    else if (sq_gettype(vm, idx) == OT_INSTANCE)
 | 
			
		||||
    {
 | 
			
		||||
        // Attempt to obtain the long instance and it's value from the stack
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            *num =  static_cast< Uint64 >(Var< const ULongInt & >(vm, idx).value.GetNum());
 | 
			
		||||
        }
 | 
			
		||||
        catch (...)
 | 
			
		||||
        {
 | 
			
		||||
            return SQ_ERROR; // Unable to obtain the value!
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    // Is this a pure integer value?
 | 
			
		||||
    else if(sq_gettype(vm, idx) == OT_INTEGER)
 | 
			
		||||
    {
 | 
			
		||||
        SQInteger val = 0;
 | 
			
		||||
        // Attempt to get the value from the stack
 | 
			
		||||
        sq_getinteger(vm, idx, &val);
 | 
			
		||||
        // Save it into the specified memory location
 | 
			
		||||
        *num = val ? static_cast< Uint64 >(val) : 0L;
 | 
			
		||||
    }
 | 
			
		||||
    // Is this a pure floating point value?
 | 
			
		||||
    else if(sq_gettype(vm, idx) == OT_FLOAT)
 | 
			
		||||
    {
 | 
			
		||||
        SQFloat val = 0.0;
 | 
			
		||||
        // Attempt to get the value from the stack
 | 
			
		||||
        sq_getfloat(vm, idx, &val);
 | 
			
		||||
        // Save it into the specified memory location
 | 
			
		||||
        *num = EpsLt(val, SQFloat(0.0)) ? 0L : static_cast< Uint64 >(std::llround(val));
 | 
			
		||||
    }
 | 
			
		||||
    // Is this a pure boolean value?
 | 
			
		||||
    else if(sq_gettype(vm, idx) == OT_BOOL)
 | 
			
		||||
    {
 | 
			
		||||
        SQBool val = SQFalse;
 | 
			
		||||
        // Attempt to get the value from the stack
 | 
			
		||||
        sq_getbool(vm, idx, &val);
 | 
			
		||||
        // Save it into the specified memory location
 | 
			
		||||
        *num = static_cast< Uint64 >(val);
 | 
			
		||||
    }
 | 
			
		||||
    // Unrecognized value
 | 
			
		||||
    else
 | 
			
		||||
    {
 | 
			
		||||
        return SQ_ERROR;
 | 
			
		||||
    }
 | 
			
		||||
    // Value retrieved
 | 
			
		||||
    return SQ_OK;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
static SQRESULT SqModImpl_PushULongObject(HSQUIRRELVM vm, Uint64 num)
 | 
			
		||||
{
 | 
			
		||||
    // Attempt to push the requested instance
 | 
			
		||||
    try
 | 
			
		||||
    {
 | 
			
		||||
        Var< const ULongInt & >::push(vm, ULongInt(num));
 | 
			
		||||
    }
 | 
			
		||||
    catch (...)
 | 
			
		||||
    {
 | 
			
		||||
        // Specify that we failed
 | 
			
		||||
        return SQ_ERROR;
 | 
			
		||||
    }
 | 
			
		||||
    // Specify that we succeeded
 | 
			
		||||
    return SQ_OK;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
SQBool SqModImpl_ValidDate(uint16_t year, uint8_t month, uint8_t day)
 | 
			
		||||
{
 | 
			
		||||
    return Chrono::ValidDate(year, month, day);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
SQBool SqModImpl_IsLeapYear(uint16_t year)
 | 
			
		||||
{
 | 
			
		||||
    return Chrono::IsLeapYear(year);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
static SQRESULT SqModImpl_GetTimestamp(HSQUIRRELVM vm, SQInteger idx, Int64 * num)
 | 
			
		||||
{
 | 
			
		||||
    // Validate the specified number pointer and value type
 | 
			
		||||
    if (!num)
 | 
			
		||||
    {
 | 
			
		||||
        return SQ_ERROR; // Nowhere to save
 | 
			
		||||
    }
 | 
			
		||||
    // Is this an instance that we can treat as a Time-stamp type?
 | 
			
		||||
    else if (sq_gettype(vm, idx) == OT_INSTANCE)
 | 
			
		||||
    {
 | 
			
		||||
        // Attempt to obtain the time-stamp and it's value from the stack
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            *num =  static_cast< Int64 >(Var< const Timestamp & >(vm, idx).value.GetNum());
 | 
			
		||||
        }
 | 
			
		||||
        catch (...)
 | 
			
		||||
        {
 | 
			
		||||
            return SQ_ERROR; // Unable to obtain the value!
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    // Is this a pure integer value?
 | 
			
		||||
    else if(sq_gettype(vm, idx) == OT_INTEGER)
 | 
			
		||||
    {
 | 
			
		||||
        SQInteger val = 0;
 | 
			
		||||
        // Attempt to get the value from the stack
 | 
			
		||||
        sq_getinteger(vm, idx, &val);
 | 
			
		||||
        // Save it into the specified memory location
 | 
			
		||||
        *num = static_cast< Int64 >(val);
 | 
			
		||||
    }
 | 
			
		||||
    // Is this a pure floating point value?
 | 
			
		||||
    else if(sq_gettype(vm, idx) == OT_FLOAT)
 | 
			
		||||
    {
 | 
			
		||||
        SQFloat val = 0.0;
 | 
			
		||||
        // Attempt to get the value from the stack
 | 
			
		||||
        sq_getfloat(vm, idx, &val);
 | 
			
		||||
        // Save it into the specified memory location
 | 
			
		||||
        *num = static_cast< Int64 >(std::llround(val));
 | 
			
		||||
    }
 | 
			
		||||
    // Is this a pure boolean value?
 | 
			
		||||
    else if(sq_gettype(vm, idx) == OT_BOOL)
 | 
			
		||||
    {
 | 
			
		||||
        SQBool val = SQFalse;
 | 
			
		||||
        // Attempt to get the value from the stack
 | 
			
		||||
        sq_getbool(vm, idx, &val);
 | 
			
		||||
        // Save it into the specified memory location
 | 
			
		||||
        *num = static_cast< Int64 >(val);
 | 
			
		||||
    }
 | 
			
		||||
    // Unrecognized value
 | 
			
		||||
    else
 | 
			
		||||
    {
 | 
			
		||||
        return SQ_ERROR;
 | 
			
		||||
    }
 | 
			
		||||
    // Value retrieved
 | 
			
		||||
    return SQ_OK;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
static SQRESULT SqModImpl_PushTimestamp(HSQUIRRELVM vm, Int64 num)
 | 
			
		||||
{
 | 
			
		||||
    // Attempt to push the requested instance
 | 
			
		||||
    try
 | 
			
		||||
    {
 | 
			
		||||
        Var< const Timestamp & >::push(vm, Timestamp(num));
 | 
			
		||||
    }
 | 
			
		||||
    catch (...)
 | 
			
		||||
    {
 | 
			
		||||
        // Specify that we failed
 | 
			
		||||
        return SQ_ERROR;
 | 
			
		||||
    }
 | 
			
		||||
    // Specify that we succeeded
 | 
			
		||||
    return SQ_OK;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
SQRESULT SqModImpl_GetDate(HSQUIRRELVM vm, SQInteger idx, uint16_t * year, uint8_t * month, uint8_t * day)
 | 
			
		||||
{
 | 
			
		||||
    // Is this an instance that we can treat as a Date type?
 | 
			
		||||
    if (sq_gettype(vm, idx) == OT_INSTANCE)
 | 
			
		||||
    {
 | 
			
		||||
        // Attempt to obtain the time-stamp and it's value from the stack
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            // Attempt to retrieve the instance
 | 
			
		||||
            Var< Date * > var(vm, idx);
 | 
			
		||||
            // Assign the year
 | 
			
		||||
            if (year != nullptr)
 | 
			
		||||
            {
 | 
			
		||||
                *year = var.value->GetYear();
 | 
			
		||||
            }
 | 
			
		||||
            // Assign the month
 | 
			
		||||
            if (month != nullptr)
 | 
			
		||||
            {
 | 
			
		||||
                *month = var.value->GetMonth();
 | 
			
		||||
            }
 | 
			
		||||
            // Assign the day
 | 
			
		||||
            if (day != nullptr)
 | 
			
		||||
            {
 | 
			
		||||
                *day = var.value->GetDay();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        catch (...)
 | 
			
		||||
        {
 | 
			
		||||
            return SQ_ERROR; // Unable to obtain the value!
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    // Unrecognized value
 | 
			
		||||
    else
 | 
			
		||||
    {
 | 
			
		||||
        return SQ_ERROR;
 | 
			
		||||
    }
 | 
			
		||||
    // Value retrieved
 | 
			
		||||
    return SQ_OK;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
SQRESULT SqModImpl_PushDate(HSQUIRRELVM vm, uint16_t year, uint8_t month, uint8_t day)
 | 
			
		||||
{
 | 
			
		||||
    // Attempt to push the requested instance
 | 
			
		||||
    try
 | 
			
		||||
    {
 | 
			
		||||
        Var< const Date & >::push(vm, Date(year, month, day));
 | 
			
		||||
    }
 | 
			
		||||
    catch (...)
 | 
			
		||||
    {
 | 
			
		||||
        // Specify that we failed
 | 
			
		||||
        return SQ_ERROR;
 | 
			
		||||
    }
 | 
			
		||||
    // Specify that we succeeded
 | 
			
		||||
    return SQ_OK;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
SQRESULT SqModImpl_GetTime(HSQUIRRELVM vm, SQInteger idx, uint8_t * hour, uint8_t * minute,
 | 
			
		||||
                        uint8_t * second, uint16_t * millisecond)
 | 
			
		||||
{
 | 
			
		||||
    // Is this an instance that we can treat as a Time type?
 | 
			
		||||
    if (sq_gettype(vm, idx) == OT_INSTANCE)
 | 
			
		||||
    {
 | 
			
		||||
        // Attempt to obtain the time-stamp and it's value from the stack
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            // Attempt to retrieve the instance
 | 
			
		||||
            Var< Time * > var(vm, idx);
 | 
			
		||||
            // Assign the hour
 | 
			
		||||
            if (hour != nullptr)
 | 
			
		||||
            {
 | 
			
		||||
                *hour = var.value->GetHour();
 | 
			
		||||
            }
 | 
			
		||||
            // Assign the minute
 | 
			
		||||
            if (minute != nullptr)
 | 
			
		||||
            {
 | 
			
		||||
                *minute = var.value->GetMinute();
 | 
			
		||||
            }
 | 
			
		||||
            // Assign the second
 | 
			
		||||
            if (second != nullptr)
 | 
			
		||||
            {
 | 
			
		||||
                *second = var.value->GetSecond();
 | 
			
		||||
            }
 | 
			
		||||
            // Assign the millisecond
 | 
			
		||||
            if (millisecond != nullptr)
 | 
			
		||||
            {
 | 
			
		||||
                *millisecond = var.value->GetMillisecond();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        catch (...)
 | 
			
		||||
        {
 | 
			
		||||
            return SQ_ERROR; // Unable to obtain the value!
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    // Unrecognized value
 | 
			
		||||
    else
 | 
			
		||||
    {
 | 
			
		||||
        return SQ_ERROR;
 | 
			
		||||
    }
 | 
			
		||||
    // Value retrieved
 | 
			
		||||
    return SQ_OK;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
SQRESULT SqModImpl_PushTime(HSQUIRRELVM vm, uint8_t hour, uint8_t minute, uint8_t second,
 | 
			
		||||
                        uint16_t millisecond)
 | 
			
		||||
{
 | 
			
		||||
    // Attempt to push the requested instance
 | 
			
		||||
    try
 | 
			
		||||
    {
 | 
			
		||||
        Var< const Time & >::push(vm, Time(hour, minute, second, millisecond));
 | 
			
		||||
    }
 | 
			
		||||
    catch (...)
 | 
			
		||||
    {
 | 
			
		||||
        // Specify that we failed
 | 
			
		||||
        return SQ_ERROR;
 | 
			
		||||
    }
 | 
			
		||||
    // Specify that we succeeded
 | 
			
		||||
    return SQ_OK;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
SQRESULT SqModImpl_GetDatetime(HSQUIRRELVM vm, SQInteger idx, uint16_t * year, uint8_t * month, uint8_t * day,
 | 
			
		||||
                            uint8_t * hour, uint8_t * minute, uint8_t * second, uint16_t * millisecond)
 | 
			
		||||
{
 | 
			
		||||
    // Is this an instance that we can treat as a Date-time type?
 | 
			
		||||
    if (sq_gettype(vm, idx) == OT_INSTANCE)
 | 
			
		||||
    {
 | 
			
		||||
        // Attempt to obtain the time-stamp and it's value from the stack
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            // Attempt to retrieve the instance
 | 
			
		||||
            Var< Datetime * > var(vm, idx);
 | 
			
		||||
            // Assign the year
 | 
			
		||||
            if (year != nullptr)
 | 
			
		||||
            {
 | 
			
		||||
                *year = var.value->GetYear();
 | 
			
		||||
            }
 | 
			
		||||
            // Assign the month
 | 
			
		||||
            if (month != nullptr)
 | 
			
		||||
            {
 | 
			
		||||
                *month = var.value->GetMonth();
 | 
			
		||||
            }
 | 
			
		||||
            // Assign the day
 | 
			
		||||
            if (day != nullptr)
 | 
			
		||||
            {
 | 
			
		||||
                *day = var.value->GetDay();
 | 
			
		||||
            }
 | 
			
		||||
            // Assign the hour
 | 
			
		||||
            if (hour != nullptr)
 | 
			
		||||
            {
 | 
			
		||||
                *hour = var.value->GetHour();
 | 
			
		||||
            }
 | 
			
		||||
            // Assign the minute
 | 
			
		||||
            if (minute != nullptr)
 | 
			
		||||
            {
 | 
			
		||||
                *minute = var.value->GetMinute();
 | 
			
		||||
            }
 | 
			
		||||
            // Assign the second
 | 
			
		||||
            if (second != nullptr)
 | 
			
		||||
            {
 | 
			
		||||
                *second = var.value->GetSecond();
 | 
			
		||||
            }
 | 
			
		||||
            // Assign the millisecond
 | 
			
		||||
            if (millisecond != nullptr)
 | 
			
		||||
            {
 | 
			
		||||
                *millisecond = var.value->GetMillisecond();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        catch (...)
 | 
			
		||||
        {
 | 
			
		||||
            return SQ_ERROR; // Unable to obtain the value!
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    // Unrecognized value
 | 
			
		||||
    else
 | 
			
		||||
    {
 | 
			
		||||
        return SQ_ERROR;
 | 
			
		||||
    }
 | 
			
		||||
    // Value retrieved
 | 
			
		||||
    return SQ_OK;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
SQRESULT SqModImpl_PushDatetime(HSQUIRRELVM vm, uint16_t year, uint8_t month, uint8_t day,
 | 
			
		||||
                            uint8_t hour, uint8_t minute, uint8_t second, uint16_t millisecond)
 | 
			
		||||
{
 | 
			
		||||
    // Attempt to push the requested instance
 | 
			
		||||
    try
 | 
			
		||||
    {
 | 
			
		||||
        Var< const Datetime & >::push(vm, Datetime(year, month, day, hour, minute, second, millisecond));
 | 
			
		||||
    }
 | 
			
		||||
    catch (...)
 | 
			
		||||
    {
 | 
			
		||||
        // Specify that we failed
 | 
			
		||||
        return SQ_ERROR;
 | 
			
		||||
    }
 | 
			
		||||
    // Specify that we succeeded
 | 
			
		||||
    return SQ_OK;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
SQRESULT SqModImpl_PushBuffer(HSQUIRRELVM vm, SQInteger size, SQInteger cursor)
 | 
			
		||||
{
 | 
			
		||||
    // Attempt to push the requested instance
 | 
			
		||||
    try
 | 
			
		||||
    {
 | 
			
		||||
        Var< const SqBuffer & >::push(vm, SqBuffer(size, cursor));
 | 
			
		||||
    }
 | 
			
		||||
    catch (...)
 | 
			
		||||
    {
 | 
			
		||||
        // Specify that we failed
 | 
			
		||||
        return SQ_ERROR;
 | 
			
		||||
    }
 | 
			
		||||
    // Specify that we succeeded
 | 
			
		||||
    return SQ_OK;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
SQRESULT SqModImpl_PushBufferData(HSQUIRRELVM vm, const char * data, SQInteger size, SQInteger cursor)
 | 
			
		||||
{
 | 
			
		||||
    // Attempt to push the requested instance
 | 
			
		||||
    try
 | 
			
		||||
    {
 | 
			
		||||
        Var< const SqBuffer & >::push(vm, SqBuffer(data, size, cursor));
 | 
			
		||||
    }
 | 
			
		||||
    catch (...)
 | 
			
		||||
    {
 | 
			
		||||
        // Specify that we failed
 | 
			
		||||
        return SQ_ERROR;
 | 
			
		||||
    }
 | 
			
		||||
    // Specify that we succeeded
 | 
			
		||||
    return SQ_OK;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
SQRESULT SqModImpl_GetBufferInfo(HSQUIRRELVM vm, SQInteger idx, const char ** ptr, SQInteger * size, SQInteger * cursor)
 | 
			
		||||
{
 | 
			
		||||
    // Attempt to obtain the requested information
 | 
			
		||||
    try
 | 
			
		||||
    {
 | 
			
		||||
        // Attempt to retrieve the instance
 | 
			
		||||
        Var< SqBuffer * > var(vm, idx);
 | 
			
		||||
        // Validate the obtained buffer
 | 
			
		||||
        if (!(var.value) || !(var.value->GetRef()) || !(*var.value->GetRef()))
 | 
			
		||||
        {
 | 
			
		||||
            // Should we obtain the buffer contents?
 | 
			
		||||
            if (ptr)
 | 
			
		||||
            {
 | 
			
		||||
                *ptr = nullptr; // Default to null data
 | 
			
		||||
            }
 | 
			
		||||
            // Should we obtain the buffer length?
 | 
			
		||||
            if (size)
 | 
			
		||||
            {
 | 
			
		||||
                *size = 0; // Default to 0 length
 | 
			
		||||
            }
 | 
			
		||||
            // Should we obtain the cursor position?
 | 
			
		||||
            if (cursor)
 | 
			
		||||
            {
 | 
			
		||||
                *cursor = 0; // Default to position 0
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        // Grab the internal buffer
 | 
			
		||||
        const Buffer & b = *var.value->GetRef();
 | 
			
		||||
        // Should we obtain the buffer contents?
 | 
			
		||||
        if (ptr)
 | 
			
		||||
        {
 | 
			
		||||
            *ptr = b.Data();
 | 
			
		||||
        }
 | 
			
		||||
        // Should we obtain the buffer length?
 | 
			
		||||
        if (size)
 | 
			
		||||
        {
 | 
			
		||||
            *size = ConvTo< SQInteger >::From(b.Capacity());
 | 
			
		||||
        }
 | 
			
		||||
        // Should we obtain the cursor position?
 | 
			
		||||
        if (cursor)
 | 
			
		||||
        {
 | 
			
		||||
            *cursor = ConvTo< SQInteger >::From(b.Position());
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    catch (...)
 | 
			
		||||
    {
 | 
			
		||||
        // Specify that we failed
 | 
			
		||||
        return SQ_ERROR;
 | 
			
		||||
    }
 | 
			
		||||
    // Specify that we succeeded
 | 
			
		||||
    return SQ_OK;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
const char * SqModImpl_GetBufferData(HSQUIRRELVM vm, SQInteger idx)
 | 
			
		||||
{
 | 
			
		||||
    // Attempt to obtain the requested information
 | 
			
		||||
    try
 | 
			
		||||
    {
 | 
			
		||||
        // Attempt to retrieve the instance
 | 
			
		||||
        Var< SqBuffer * > var(vm, idx);
 | 
			
		||||
        // Validate the obtained buffer and return the requested information
 | 
			
		||||
        if ((var.value) && (var.value->GetRef()) && (*var.value->GetRef()))
 | 
			
		||||
        {
 | 
			
		||||
            return var.value->GetRef()->Data();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    catch (...)
 | 
			
		||||
    {
 | 
			
		||||
        // Just ignore it...
 | 
			
		||||
    }
 | 
			
		||||
    // Specify that we failed
 | 
			
		||||
    return nullptr;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
SQInteger SqModImpl_GetBufferSize(HSQUIRRELVM vm, SQInteger idx)
 | 
			
		||||
{
 | 
			
		||||
    // Attempt to obtain the requested information
 | 
			
		||||
    try
 | 
			
		||||
    {
 | 
			
		||||
        // Attempt to retrieve the instance
 | 
			
		||||
        Var< SqBuffer * > var(vm, idx);
 | 
			
		||||
        // Validate the obtained buffer and return the requested information
 | 
			
		||||
        if ((var.value) && (var.value->GetRef()) && (*var.value->GetRef()))
 | 
			
		||||
        {
 | 
			
		||||
            return ConvTo< SQInteger >::From(var.value->GetRef()->Capacity());
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    catch (...)
 | 
			
		||||
    {
 | 
			
		||||
        // Just ignore it...
 | 
			
		||||
    }
 | 
			
		||||
    // Specify that we failed
 | 
			
		||||
    return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
SQInteger SqModImpl_GetBufferCursor(HSQUIRRELVM vm, SQInteger idx)
 | 
			
		||||
{
 | 
			
		||||
    // Attempt to obtain the requested information
 | 
			
		||||
    try
 | 
			
		||||
    {
 | 
			
		||||
        // Attempt to retrieve the instance
 | 
			
		||||
        Var< SqBuffer * > var(vm, idx);
 | 
			
		||||
        // Validate the obtained buffer and return the requested information
 | 
			
		||||
        if ((var.value) && (var.value->GetRef()) && (*var.value->GetRef()))
 | 
			
		||||
        {
 | 
			
		||||
            return ConvTo< SQInteger >::From(var.value->GetRef()->Position());
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    catch (...)
 | 
			
		||||
    {
 | 
			
		||||
        // Just ignore it...
 | 
			
		||||
    }
 | 
			
		||||
    // Specify that we failed
 | 
			
		||||
    return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ------------------------------------------------------------------------------------------------
 | 
			
		||||
static int32_t SqExport_PopulateModuleAPI(HSQMODAPI api, size_t size)
 | 
			
		||||
{
 | 
			
		||||
@@ -667,45 +67,6 @@ static int32_t SqExport_PopulateModuleAPI(HSQMODAPI api, size_t size)
 | 
			
		||||
    //script loading
 | 
			
		||||
    api->LoadScript                 = SqModImpl_LoadScript;
 | 
			
		||||
 | 
			
		||||
    //numeric utilities
 | 
			
		||||
    api->GetSLongValue              = SqModImpl_GetSLongValue;
 | 
			
		||||
    api->PushSLongObject            = SqModImpl_PushSLongObject;
 | 
			
		||||
    api->GetULongValue              = SqModImpl_GetULongValue;
 | 
			
		||||
    api->PushULongObject            = SqModImpl_PushULongObject;
 | 
			
		||||
 | 
			
		||||
    //time utilities
 | 
			
		||||
    api->GetCurrentSysTime          = Chrono::GetCurrentSysTime;
 | 
			
		||||
    api->GetEpochTimeMicro          = Chrono::GetEpochTimeMicro;
 | 
			
		||||
    api->GetEpochTimeMilli          = Chrono::GetEpochTimeMilli;
 | 
			
		||||
    api->ValidDate                  = SqModImpl_ValidDate;
 | 
			
		||||
    api->IsLeapYear                 = SqModImpl_IsLeapYear;
 | 
			
		||||
    api->DaysInYear                 = Chrono::DaysInYear;
 | 
			
		||||
    api->DaysInMonth                = Chrono::DaysInMonth;
 | 
			
		||||
    api->DayOfYear                  = Chrono::DayOfYear;
 | 
			
		||||
    api->DateRangeToSeconds         = Chrono::DateRangeToSeconds;
 | 
			
		||||
    api->GetTimestamp               = SqModImpl_GetTimestamp;
 | 
			
		||||
    api->PushTimestamp              = SqModImpl_PushTimestamp;
 | 
			
		||||
    api->GetDate                    = SqModImpl_GetDate;
 | 
			
		||||
    api->PushDate                   = SqModImpl_PushDate;
 | 
			
		||||
    api->GetTime                    = SqModImpl_GetTime;
 | 
			
		||||
    api->PushTime                   = SqModImpl_PushTime;
 | 
			
		||||
    api->GetDatetime                = SqModImpl_GetDatetime;
 | 
			
		||||
    api->PushDatetime               = SqModImpl_PushDatetime;
 | 
			
		||||
 | 
			
		||||
    //stack utilities
 | 
			
		||||
    api->PopStackInteger            = PopStackInteger;
 | 
			
		||||
    api->PopStackFloat              = PopStackFloat;
 | 
			
		||||
    api->PopStackSLong              = PopStackSLong;
 | 
			
		||||
    api->PopStackULong              = PopStackULong;
 | 
			
		||||
 | 
			
		||||
    //buffer utilities
 | 
			
		||||
    api->PushBuffer                 = SqModImpl_PushBuffer;
 | 
			
		||||
    api->PushBufferData             = SqModImpl_PushBufferData;
 | 
			
		||||
    api->GetBufferInfo              = SqModImpl_GetBufferInfo;
 | 
			
		||||
    api->GetBufferData              = SqModImpl_GetBufferData;
 | 
			
		||||
    api->GetBufferSize              = SqModImpl_GetBufferSize;
 | 
			
		||||
    api->GetBufferCursor            = SqModImpl_GetBufferCursor;
 | 
			
		||||
 | 
			
		||||
    return 1; // Successfully populated!
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user