diff --git a/furvm/include/furvm/fwd.hpp b/furvm/include/furvm/fwd.hpp index 8098d78..e5afc2c 100644 --- a/furvm/include/furvm/fwd.hpp +++ b/furvm/include/furvm/fwd.hpp @@ -1,9 +1,12 @@ #ifndef FURVM_FWD_HPP #define FURVM_FWD_HPP +#include "furvm/handle.hpp" + #include // IWYU pragma: export #include // IWYU pragma: export #include +#include /** * @brief Furlang's virtual machine. @@ -83,6 +86,11 @@ using function_p = std::shared_ptr; */ using function_id = std::uint16_t; +/** + * @brief A handle to a furvm function. + */ +using function_h = handle; + // module.hpp /** @@ -98,6 +106,16 @@ class mod; */ using mod_p = std::shared_ptr; +/** + * @brief An alias for a module's identifier. + */ +using mod_id = std::string; + +/** + * @brief A handle to a furvm module. + */ +using mod_h = handle; + // thing.hpp /** @@ -130,6 +148,11 @@ using thing_p = std::shared_ptr; */ using thing_id = std::uint32_t; +/** + * @brief A handle to a furvm's thing. + */ +using thing_h = handle; + // executor.hpp /** @@ -161,6 +184,11 @@ using executor_p = std::shared_ptr; */ using executor_id = std::uint32_t; +/** + * @brief A handle to a furvm's executor. + */ +using executor_h = handle; + // context.hpp /** diff --git a/furvm/include/furvm/handle.hpp b/furvm/include/furvm/handle.hpp new file mode 100644 index 0000000..c1f3503 --- /dev/null +++ b/furvm/include/furvm/handle.hpp @@ -0,0 +1,101 @@ +#ifndef FURVM_HANDLE_HPP +#define FURVM_HANDLE_HPP + +#include + +namespace furvm { + +template +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 + handle(IdF&& id, Value&& value) + : m_id(std::forward(id)), m_value(std::forward(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 diff --git a/furvm/include/furvm/module.hpp b/furvm/include/furvm/module.hpp index 4c394e1..bfa167d 100644 --- a/furvm/include/furvm/module.hpp +++ b/furvm/include/furvm/module.hpp @@ -4,9 +4,7 @@ #include "furvm/function.hpp" #include "furvm/fwd.hpp" -#include #include -#include #include #include #include diff --git a/furvm/src/serializer.cpp b/furvm/src/serializer.cpp index bac4019..58ce2d4 100644 --- a/furvm/src/serializer.cpp +++ b/furvm/src/serializer.cpp @@ -4,10 +4,7 @@ #include "furvm/fwd.hpp" #include "furvm/module.hpp" // IWYU pragma: keep -#include -#include #include -#include #include #include