forked from KPGPMC/furlang
refactor(furvm): better thing implementation
This commit is contained in:
@@ -12,6 +12,7 @@ namespace furvm {
|
||||
class context {
|
||||
public:
|
||||
friend class executor;
|
||||
friend class thing;
|
||||
public:
|
||||
context();
|
||||
~context() = default;
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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;
|
||||
|
||||
+26
-3
@@ -1,10 +1,33 @@
|
||||
// NOLINTBEGIN(cppcoreguidelines-no-malloc)
|
||||
|
||||
#include "furvm/thing.hpp"
|
||||
|
||||
#include "furvm/context.hpp" // IWYU pragma: keep
|
||||
|
||||
#include <cstdlib>
|
||||
|
||||
namespace furvm {
|
||||
|
||||
thing::thing(rzecz, thing_handle id, thing_t type, const context_p& context)
|
||||
: m_id(id), m_type(type), m_context(context) {}
|
||||
std::size_t thing_type_size(thing_t type) {
|
||||
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
|
||||
|
||||
// NOLINTEND(cppcoreguidelines-no-malloc)
|
||||
Reference in New Issue
Block a user