File object.hpp

File List > include > simplesquirrel > object.hpp

Go to the documentation of this file.

#pragma once
#ifndef SSQ_OBJECT_HEADER_H
#define SSQ_OBJECT_HEADER_H

#if !defined(HAS_NOEXCEPT) && !defined(NOEXCEPT)
    #if defined(__clang__)
        #if __has_feature(cxx_noexcept)
            #define HAS_NOEXCEPT
        #endif
    #else
        #if defined(__GXX_EXPERIMENTAL_CXX0X__) && __GNUC__ * 10 + __GNUC_MINOR__ >= 46 || \
            defined(_MSC_FULL_VER) && _MSC_FULL_VER >= 190023026
            #define HAS_NOEXCEPT
        #endif
    #endif

    #ifdef HAS_NOEXCEPT
        #define NOEXCEPT noexcept
    #else
        #define NOEXCEPT
    #endif
#endif

#include <string>
#include <squirrel.h>
#include <unordered_map>
#include <stdint.h>
#include "exceptions.hpp"
#include "type.hpp"

namespace ssq {
    class Function;
    class Table;
    class Class;
    class Instance;
    class Table;
    class Array;

#ifndef DOXYGEN_SHOULD_SKIP_THIS
    namespace detail {
        template <typename T> inline typename std::enable_if<!std::is_pointer<T>::value, T>::type
            pop(HSQUIRRELVM vm, SQInteger index);
    }
#endif

    class SSQ_API Object {
    public:
        Object();
        Object(HSQUIRRELVM vm);
        virtual ~Object();
        void swap(Object& other) NOEXCEPT;
        Object(const Object& other);
        Object(Object&& other) NOEXCEPT;
        bool isEmpty() const;
        const HSQOBJECT& getRaw() const;
        HSQOBJECT& getRaw();
        Object find(const char* name) const;
        Type getType() const;
        const char* getTypeStr() const;
        const HSQUIRRELVM& getHandle() const;
        size_t getTypeTag() const;
        bool isNull() const;
        void reset();
#ifdef _SQ64
        int64_t toInt() const;
#else
        int32_t toInt() const;
#endif

#ifdef SQUSEDOUBLE
        double toFloat() const;
#else
        float toFloat() const;
#endif

#ifdef SQUNICODE
        std::wstring toString() const;
#else
        std::string toString() const;
#endif

        bool toBool() const;
        Function toFunction() const;
        Instance toInstance() const;
        Class toClass() const;
        Table toTable() const;
        Array toArray() const;
        template<typename T>
        T to() const;
        template<typename T>
        T toPtrUnsafe() const {
            if (getType() != Type::INSTANCE) {
                throw ssq::TypeException("bad cast", "INSTANCE", getTypeStr());
            }
            sq_pushobject(vm, obj);
            SQUserPointer val;
            sq_getinstanceup(vm, -1, &val, nullptr);
            sq_pop(vm, 1);
            return reinterpret_cast<T>(val);
        }
        Object& operator = (const Object& other);
        Object& operator = (Object&& other) NOEXCEPT;

    protected:
        HSQUIRRELVM vm;
        HSQOBJECT obj;
        bool weak;
    };
}

#endif