refactor(furvm): better thing implementation

This commit is contained in:
2026-06-06 23:16:59 +02:00
parent daf16c18e1
commit bfdbbb0d5f
4 changed files with 45 additions and 3 deletions
+1
View File
@@ -12,6 +12,7 @@ namespace furvm {
class context { class context {
public: public:
friend class executor; friend class executor;
friend class thing;
public: public:
context(); context();
~context() = default; ~context() = default;
+1
View File
@@ -72,6 +72,7 @@ public:
* @brief Returns a new executor. * @brief Returns a new executor.
* *
* @param context Furvm context. * @param context Furvm context.
* @return Shared pointer to the new executor.
*/ */
static executor_p create(const context_p& context); static executor_p create(const context_p& context);
public: public:
+17
View File
@@ -9,6 +9,14 @@ enum class thing_t : std::uint8_t {
Int, 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 { class thing {
friend class executor; friend class executor;
private: private:
@@ -44,6 +52,15 @@ public:
thing(const thing&) = delete; thing(const thing&) = delete;
thing& operator=(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: private:
thing_handle m_id; thing_handle m_id;
thing_t m_type; thing_t m_type;
+26 -3
View File
@@ -1,10 +1,33 @@
// NOLINTBEGIN(cppcoreguidelines-no-malloc)
#include "furvm/thing.hpp" #include "furvm/thing.hpp"
#include "furvm/context.hpp" // IWYU pragma: keep
#include <cstdlib>
namespace furvm { namespace furvm {
thing::thing(rzecz, thing_handle id, thing_t type, const context_p& context) std::size_t thing_type_size(thing_t type) {
: m_id(id), m_type(type), m_context(context) {} switch (type) {
case thing_t::Int: return 4;
}
return 0;
}
thing::~thing() {} thing::thing(rzecz, thing_handle id, thing_t type, const context_p& context)
: m_id(id), m_type(type), m_context(context), m_data(std::calloc(1, thing_type_size(type))) {}
thing::~thing() {
std::free(m_data);
}
thing_p thing::create(const context_p& context, thing_t type) {
auto th = std::make_shared<thing>(rzecz{}, context->m_things.size(), type, context);
context->m_things.push_back(th);
return std::move(th);
}
} // namespace furvm } // namespace furvm
// NOLINTEND(cppcoreguidelines-no-malloc)