From bfdbbb0d5f33efd7e074c5523f177e3119ef039a Mon Sep 17 00:00:00 2001 From: CHatingPython Date: Sat, 6 Jun 2026 23:16:59 +0200 Subject: [PATCH] refactor(furvm): better thing implementation --- furvm/include/furvm/context.hpp | 1 + furvm/include/furvm/executor.hpp | 1 + furvm/include/furvm/thing.hpp | 17 +++++++++++++++++ furvm/src/thing.cpp | 29 ++++++++++++++++++++++++++--- 4 files changed, 45 insertions(+), 3 deletions(-) diff --git a/furvm/include/furvm/context.hpp b/furvm/include/furvm/context.hpp index fbeef5c..e475d0e 100644 --- a/furvm/include/furvm/context.hpp +++ b/furvm/include/furvm/context.hpp @@ -12,6 +12,7 @@ namespace furvm { class context { public: friend class executor; + friend class thing; public: context(); ~context() = default; diff --git a/furvm/include/furvm/executor.hpp b/furvm/include/furvm/executor.hpp index 06f957e..b556201 100644 --- a/furvm/include/furvm/executor.hpp +++ b/furvm/include/furvm/executor.hpp @@ -72,6 +72,7 @@ public: * @brief Returns a new executor. * * @param context Furvm context. + * @return Shared pointer to the new executor. */ static executor_p create(const context_p& context); public: diff --git a/furvm/include/furvm/thing.hpp b/furvm/include/furvm/thing.hpp index 497320b..17c96f8 100644 --- a/furvm/include/furvm/thing.hpp +++ b/furvm/include/furvm/thing.hpp @@ -9,6 +9,14 @@ enum class thing_t : std::uint8_t { Int, }; +/** + * @brief Returns data size of a thing. + * + * @param type Type of the thing. + * @return The data size of the thing. + */ +std::size_t thing_type_size(thing_t type); + class thing { friend class executor; private: @@ -44,6 +52,15 @@ public: thing(const thing&) = delete; thing& operator=(const thing&) = delete; +public: + /** + * @brief Returns a new thing. + * + * @param context Furvm context. + * @param type Type of the new thing. + * @return Shared pointer to the new thing. + */ + static thing_p create(const context_p& context, thing_t type); private: thing_handle m_id; thing_t m_type; diff --git a/furvm/src/thing.cpp b/furvm/src/thing.cpp index d59ea59..0676651 100644 --- a/furvm/src/thing.cpp +++ b/furvm/src/thing.cpp @@ -1,10 +1,33 @@ +// NOLINTBEGIN(cppcoreguidelines-no-malloc) + #include "furvm/thing.hpp" +#include "furvm/context.hpp" // IWYU pragma: keep + +#include + namespace furvm { +std::size_t thing_type_size(thing_t type) { + switch (type) { + case thing_t::Int: return 4; + } + return 0; +} + thing::thing(rzecz, thing_handle id, thing_t type, const context_p& context) - : m_id(id), m_type(type), m_context(context) {} + : m_id(id), m_type(type), m_context(context), m_data(std::calloc(1, thing_type_size(type))) {} -thing::~thing() {} +thing::~thing() { + std::free(m_data); +} -} // namespace furvm \ No newline at end of file +thing_p thing::create(const context_p& context, thing_t type) { + auto th = std::make_shared(rzecz{}, context->m_things.size(), type, context); + context->m_things.push_back(th); + return std::move(th); +} + +} // namespace furvm + +// NOLINTEND(cppcoreguidelines-no-malloc) \ No newline at end of file