From 4895489511a1e49038699e336ce6a14629178388 Mon Sep 17 00:00:00 2001 From: CHatingPython Date: Sat, 6 Jun 2026 12:53:31 +0200 Subject: [PATCH] refactor(furvm): add id to module --- furvm/include/furvm/context.hpp | 19 +++++++++---------- furvm/include/furvm/fwd.hpp | 2 +- furvm/include/furvm/module.hpp | 15 ++++++++++++--- furvm/src/main.cpp | 2 +- 4 files changed, 23 insertions(+), 15 deletions(-) diff --git a/furvm/include/furvm/context.hpp b/furvm/include/furvm/context.hpp index 4919bea..6bed0a4 100644 --- a/furvm/include/furvm/context.hpp +++ b/furvm/include/furvm/context.hpp @@ -36,11 +36,10 @@ public: * @param args Arguments to forward to module's constructor. * @return An index to the emplaced module. */ - template >> - constexpr module_index emplace(Args&&... args) { - module_index index = m_modules.size(); - m_modules.emplace_back(std::make_unique(std::forward(args)...)); - return index; + template >> + constexpr const auto& emplace(Args&&... args) { + module_handle id = static_cast(m_modules.size()); + return m_modules.emplace_back(std::make_unique(id, std::forward(args)...)); } /** @@ -49,7 +48,7 @@ public: * @param index Index to the module. * @return Old value. */ - value_type erase(module_index index) { + value_type erase(module_handle index) { if (index >= m_modules.size()) return nullptr; return std::move(m_modules[index]); } @@ -60,7 +59,7 @@ public: * @param index Position of the module. * @return The module. */ - constexpr value_type& operator[](module_index index) { return m_modules[index]; } + constexpr value_type& operator[](module_handle index) { return m_modules[index]; } /** * @brief Returns a module of this context. @@ -68,7 +67,7 @@ public: * @param index Position of the module. * @return The module. */ - constexpr const value_type& operator[](module_index index) const { return m_modules[index]; } + constexpr const value_type& operator[](module_handle index) const { return m_modules[index]; } /** * @brief Returns a module of this context. @@ -76,7 +75,7 @@ public: * @param index Position of the module. * @return The module. */ - constexpr value_type& at(module_index index) { return m_modules.at(index); } + constexpr value_type& at(module_handle index) { return m_modules.at(index); } /** * @brief Returns a module of this context. @@ -84,7 +83,7 @@ public: * @param index Position of the module. * @return The module. */ - constexpr const value_type& at(module_index index) const { return m_modules.at(index); } + constexpr const value_type& at(module_handle index) const { return m_modules.at(index); } /** * @brief Returns how many does this context have modules. diff --git a/furvm/include/furvm/fwd.hpp b/furvm/include/furvm/fwd.hpp index 3deb2f6..545eeef 100644 --- a/furvm/include/furvm/fwd.hpp +++ b/furvm/include/furvm/fwd.hpp @@ -59,7 +59,7 @@ class mod; /** * @brief Furvm module's index. */ -using module_index = std::size_t; +using module_handle = std::uint32_t; // context.hpp diff --git a/furvm/include/furvm/module.hpp b/furvm/include/furvm/module.hpp index 4a0666a..c8497d1 100644 --- a/furvm/include/furvm/module.hpp +++ b/furvm/include/furvm/module.hpp @@ -14,11 +14,12 @@ public: /** * @brief Construct a new module. * + * @param id Id of this module. * @param args Arguments to forward to bytecode's constructor. */ template >> - mod(Args&&... args) - : m_bytecode(std::forward(args)...) {} + mod(module_handle id, Args&&... args) + : m_id(id), m_bytecode(std::forward(args)...) {} ~mod() = default; @@ -34,8 +35,16 @@ public: mod(const mod&) = delete; mod& operator=(const mod&) = delete; +public: + /** + * @brief Returns an id of this module. + * + * @return The id. + */ + constexpr module_handle id() const { return m_id; } private: - bytecode_t m_bytecode; + module_handle m_id; + bytecode_t m_bytecode; }; } // namespace furvm diff --git a/furvm/src/main.cpp b/furvm/src/main.cpp index 58201af..b35caaa 100644 --- a/furvm/src/main.cpp +++ b/furvm/src/main.cpp @@ -13,7 +13,7 @@ static constexpr std::array s_bytecode = { int main(void) { furvm::context context; - auto mainModule = context.emplace(s_bytecode.begin(), s_bytecode.end()); + const auto& mainModule = context.emplace(s_bytecode.begin(), s_bytecode.end()); return 0; }