feat(furvm): implement a handle

Refs: #12
This commit is contained in:
2026-06-16 16:47:00 +02:00
parent c2b32c9604
commit e8ccaa6390
4 changed files with 129 additions and 5 deletions
+28
View File
@@ -1,9 +1,12 @@
#ifndef FURVM_FWD_HPP
#define FURVM_FWD_HPP
#include "furvm/handle.hpp"
#include <cstddef> // IWYU pragma: export
#include <cstdint> // IWYU pragma: export
#include <memory>
#include <string>
/**
* @brief Furlang's virtual machine.
@@ -83,6 +86,11 @@ using function_p = std::shared_ptr<function>;
*/
using function_id = std::uint16_t;
/**
* @brief A handle to a furvm function.
*/
using function_h = handle<function_id, function>;
// module.hpp
/**
@@ -98,6 +106,16 @@ class mod;
*/
using mod_p = std::shared_ptr<mod>;
/**
* @brief An alias for a module's identifier.
*/
using mod_id = std::string;
/**
* @brief A handle to a furvm module.
*/
using mod_h = handle<mod_id, mod_p>;
// thing.hpp
/**
@@ -130,6 +148,11 @@ using thing_p = std::shared_ptr<thing>;
*/
using thing_id = std::uint32_t;
/**
* @brief A handle to a furvm's thing.
*/
using thing_h = handle<thing_id, thing>;
// executor.hpp
/**
@@ -161,6 +184,11 @@ using executor_p = std::shared_ptr<executor>;
*/
using executor_id = std::uint32_t;
/**
* @brief A handle to a furvm's executor.
*/
using executor_h = handle<executor_id, executor>;
// context.hpp
/**
+101
View File
@@ -0,0 +1,101 @@
#ifndef FURVM_HANDLE_HPP
#define FURVM_HANDLE_HPP
#include <utility>
namespace furvm {
template <typename Id, typename T>
class handle {
public:
using id_type = Id;
using value_type = T;
using reference = T&;
using const_reference = const T&;
using pointer = T*;
using const_pointer = const T*;
public:
/**
* @brief Returns a new handle.
*
* @param id Id of the handle.
* @param value Value of the handle.
*/
template <typename IdF, typename Value>
handle(IdF&& id, Value&& value)
: m_id(std::forward<IdF>(id)), m_value(std::forward<Value>(value)) {}
public:
handle& operator=(value_type&& value) noexcept {
m_value = std::move(value);
return *this;
}
handle& operator=(const value_type& value) noexcept {
m_value = value;
return *this;
}
public:
/**
* @brief Returns the handle's id.
*
* @return The id.
*/
constexpr Id id() const { return m_id; }
/**
* @brief Returns the handle's pointer.
*
* @return The pointer.
*/
reference value() { return m_value; }
/**
* @brief Returns the handle's pointer.
*
* @return The pointer.
*/
const_reference value() const { return m_value; }
public:
/**
* @brief Returns the handle's id.
*
* @return The id.
*/
explicit operator Id() const { return m_id; }
public:
/**
* @brief Returns a reference to the handle's value.
*
* @return The value.
*/
reference operator*() { return m_value; }
/**
* @brief Returns a reference to the handle's value.
*
* @return The value.
*/
const_reference operator*() const { return m_value; }
/**
* @brief Returns a pointer to the handle's value.
*
* @return The value pointer.
*/
pointer operator->() { return &m_value; }
/**
* @brief Returns a pointer to the handle's value.
*
* @return The value pointer.
*/
const_pointer operator->() const { return &m_value; }
private:
id_type m_id;
value_type m_value;
};
} // namespace furvm
#endif // FURVM_HANDLE_HPP
-2
View File
@@ -4,9 +4,7 @@
#include "furvm/function.hpp"
#include "furvm/fwd.hpp"
#include <algorithm>
#include <memory>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <utility>
-3
View File
@@ -4,10 +4,7 @@
#include "furvm/fwd.hpp"
#include "furvm/module.hpp" // IWYU pragma: keep
#include <array>
#include <cstring>
#include <istream>
#include <optional>
#include <ostream>
#include <stdexcept>