refactor(furvm): add id to module

This commit is contained in:
2026-06-06 12:53:31 +02:00
parent 5407f8cda7
commit 4895489511
4 changed files with 23 additions and 15 deletions
+9 -10
View File
@@ -36,11 +36,10 @@ public:
* @param args Arguments to forward to module's constructor. * @param args Arguments to forward to module's constructor.
* @return An index to the emplaced module. * @return An index to the emplaced module.
*/ */
template <typename... Args, typename = std::enable_if_t<std::is_constructible_v<mod, Args...>>> template <typename... Args, typename = std::enable_if_t<std::is_constructible_v<mod, module_handle, Args...>>>
constexpr module_index emplace(Args&&... args) { constexpr const auto& emplace(Args&&... args) {
module_index index = m_modules.size(); module_handle id = static_cast<module_handle>(m_modules.size());
m_modules.emplace_back(std::make_unique<mod>(std::forward<Args>(args)...)); return m_modules.emplace_back(std::make_unique<mod>(id, std::forward<Args>(args)...));
return index;
} }
/** /**
@@ -49,7 +48,7 @@ public:
* @param index Index to the module. * @param index Index to the module.
* @return Old value. * @return Old value.
*/ */
value_type erase(module_index index) { value_type erase(module_handle index) {
if (index >= m_modules.size()) return nullptr; if (index >= m_modules.size()) return nullptr;
return std::move(m_modules[index]); return std::move(m_modules[index]);
} }
@@ -60,7 +59,7 @@ public:
* @param index Position of the module. * @param index Position of the module.
* @return 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. * @brief Returns a module of this context.
@@ -68,7 +67,7 @@ public:
* @param index Position of the module. * @param index Position of the module.
* @return 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. * @brief Returns a module of this context.
@@ -76,7 +75,7 @@ public:
* @param index Position of the module. * @param index Position of the module.
* @return 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. * @brief Returns a module of this context.
@@ -84,7 +83,7 @@ public:
* @param index Position of the module. * @param index Position of the module.
* @return 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. * @brief Returns how many does this context have modules.
+1 -1
View File
@@ -59,7 +59,7 @@ class mod;
/** /**
* @brief Furvm module's index. * @brief Furvm module's index.
*/ */
using module_index = std::size_t; using module_handle = std::uint32_t;
// context.hpp // context.hpp
+11 -2
View File
@@ -14,11 +14,12 @@ public:
/** /**
* @brief Construct a new module. * @brief Construct a new module.
* *
* @param id Id of this module.
* @param args Arguments to forward to bytecode's constructor. * @param args Arguments to forward to bytecode's constructor.
*/ */
template <typename... Args, typename = std::enable_if_t<std::is_constructible_v<bytecode_t, Args...>>> template <typename... Args, typename = std::enable_if_t<std::is_constructible_v<bytecode_t, Args...>>>
mod(Args&&... args) mod(module_handle id, Args&&... args)
: m_bytecode(std::forward<Args>(args)...) {} : m_id(id), m_bytecode(std::forward<Args>(args)...) {}
~mod() = default; ~mod() = default;
@@ -34,7 +35,15 @@ public:
mod(const mod&) = delete; mod(const mod&) = delete;
mod& operator=(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: private:
module_handle m_id;
bytecode_t m_bytecode; bytecode_t m_bytecode;
}; };
+1 -1
View File
@@ -13,7 +13,7 @@ static constexpr std::array<furvm::byte, 1> s_bytecode = {
int main(void) { int main(void) {
furvm::context context; 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; return 0;
} }