File table.hpp

File List > include > simplesquirrel > table.hpp

Go to the documentation of this file.

#pragma once
#ifndef SSQ_TABLE_HEADER_H
#define SSQ_TABLE_HEADER_H

#include "class.hpp"

namespace ssq {
    class Enum;
    class SSQ_API Table: public Object {
    public:
        Table();
        virtual ~Table() = default;
        explicit Table(const Object& other);
        explicit Table(HSQUIRRELVM vm);
        Table(const Table& other);
        Table(Table&& other) NOEXCEPT;
        Function findFunc(const char* name) const;
        Class findClass(const char* name) const;
        template<typename T, typename... Args>
        Class addClass(const char* name, const std::function<T*(Args...)>& allocator = std::bind(&detail::defaultClassAllocator<T>), bool release = true){
            sq_pushobject(vm, obj);
            Class cls(detail::addClass(vm, name, allocator, release));
            sq_pop(vm, 1);
            return cls;
        }
        template<typename T, typename... Args>
        Class addClass(const char* name, const Class::Ctor<T(Args...)>& constructor, bool release = true){
            const std::function<T*(Args...)> func = &constructor.allocate;
            return addClass<T>(name, func, release);
        }
        template<typename F>
        Class addClass(const char* name, const F& lambda, bool release = true) {
            return addClass(name, detail::make_function(lambda), release);
        }
        template<typename T>
        Class addAbstractClass(const char* name) {
            sq_pushobject(vm, obj);
            Class cls(detail::addAbstractClass<T>(vm, name));
            sq_pop(vm, 1);
            return cls;
        }
        template<typename R, typename... Args>
        Function addFunc(const char* name, const std::function<R(Args...)>& func){
            Function ret(vm);
            sq_pushobject(vm, obj);
            detail::addFunc(vm, name, func);
            sq_pop(vm, 1);
            return ret;
        }
        template<typename F>
        Function addFunc(const char* name, const F& lambda) {
            return addFunc(name, detail::make_function(lambda));
        }
        template<typename T>
        inline void set(const char* name, const T& value) {
            sq_pushobject(vm, obj);
            sq_pushstring(vm, name, strlen(name));
            detail::push<T>(vm, value);
            sq_newslot(vm, -3, false);
            sq_pop(vm,1); // pop table
        }
        template<typename T>
        inline T get(const char* name) {
            return find(name).to<T>();
        }
        size_t size();
        Table addTable(const char* name);
        Table& operator = (const Table& other);
        Table& operator = (Table&& other) NOEXCEPT;
    };
#ifndef DOXYGEN_SHOULD_SKIP_THIS
    namespace detail {
        template<>
        inline Table popValue(HSQUIRRELVM vm, SQInteger index){
            checkType(vm, index, OT_TABLE);
            Table val(vm);
            if (SQ_FAILED(sq_getstackobj(vm, index, &val.getRaw()))) throw TypeException("Could not get Table from squirrel stack");
            sq_addref(vm, &val.getRaw());
            return val;
        }
    }
#endif
}

#endif