include/wrenbind17/handle.hpp

include/wrenbind17/handle.hpp #

Namespaces #

Name
wrenbind17

Classes #

Name
class wrenbind17::Handle Holds a reference to some Wren type.

Source code #

#pragma once

#include <wren.hpp>

#include <memory>

#include "exception.hpp"

namespace wrenbind17 {
    std::shared_ptr<WrenVM> getSharedVm(WrenVM* vm);

    class Handle {
    public:
        Handle() : handle(nullptr) {
        }
        Handle(const std::shared_ptr<WrenVM> vm, WrenHandle* handle) : vm(vm), handle(handle) {
        }
        ~Handle() {
            reset();
        }
        Handle(const Handle& other) = delete;
        Handle(Handle&& other) noexcept : handle(nullptr) {
            swap(other);
        }
        Handle& operator=(const Handle& other) = delete;
        Handle& operator=(Handle&& other) noexcept {
            if (this != &other) {
                swap(other);
            }
            return *this;
        }
        void swap(Handle& other) noexcept {
            std::swap(vm, other.vm);
            std::swap(handle, other.handle);
        }

        WrenHandle* getHandle() const {
            return handle;
        }

        WrenVM* getVm() const {
            if (const auto ptr = vm.lock()) {
                return ptr.get();
            } else {
                throw RuntimeError("Invalid handle");
            }
        }

        const std::weak_ptr<WrenVM>& getVmWeak() const {
            return vm;
        }

        void reset() {
            if (!vm.expired() && handle) {
                wrenReleaseHandle(vm.lock().get(), handle);
                vm.reset();
                handle = nullptr;
            }
        }

        operator bool() const {
            return !vm.expired() && handle;
        }

    private:
        std::weak_ptr<WrenVM> vm;
        WrenHandle* handle;
    };
} // namespace wrenbind17

Updated on 17 October 2023 at 12:26:25 UTC