refactor(furvm): add shared pointer aliases

This commit is contained in:
2026-06-06 23:08:20 +02:00
parent 6824015728
commit daf16c18e1
6 changed files with 56 additions and 40 deletions
+8 -9
View File
@@ -4,7 +4,6 @@
#include "furvm/fwd.hpp" #include "furvm/fwd.hpp"
#include "furvm/module.hpp" #include "furvm/module.hpp"
#include <memory>
#include <type_traits> #include <type_traits>
#include <vector> #include <vector>
@@ -48,7 +47,7 @@ public:
* @param index Index to the module. * @param index Index to the module.
* @return Old value. * @return Old value.
*/ */
std::shared_ptr<mod> erase(module_handle index) { mod_p 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]);
} }
@@ -59,7 +58,7 @@ public:
* @param index Position of the module. * @param index Position of the module.
* @return The module. * @return The module.
*/ */
constexpr std::shared_ptr<mod>& operator[](module_handle index) { return m_modules[index]; } constexpr mod_p& operator[](module_handle index) { return m_modules[index]; }
/** /**
* @brief Returns a module of this context. * @brief Returns a module of this context.
@@ -67,7 +66,7 @@ public:
* @param index Position of the module. * @param index Position of the module.
* @return The module. * @return The module.
*/ */
constexpr const std::shared_ptr<mod>& operator[](module_handle index) const { return m_modules[index]; } constexpr const mod_p& operator[](module_handle index) const { return m_modules[index]; }
/** /**
* @brief Returns a module of this context. * @brief Returns a module of this context.
@@ -75,7 +74,7 @@ public:
* @param index Position of the module. * @param index Position of the module.
* @return The module. * @return The module.
*/ */
constexpr std::shared_ptr<mod>& at(module_handle index) { return m_modules.at(index); } constexpr mod_p& at(module_handle index) { return m_modules.at(index); }
/** /**
* @brief Returns a module of this context. * @brief Returns a module of this context.
@@ -83,7 +82,7 @@ public:
* @param index Position of the module. * @param index Position of the module.
* @return The module. * @return The module.
*/ */
constexpr const std::shared_ptr<mod>& at(module_handle index) const { return m_modules.at(index); } constexpr const mod_p& 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.
@@ -92,9 +91,9 @@ public:
*/ */
constexpr size_t size() const { return m_modules.size(); } constexpr size_t size() const { return m_modules.size(); }
private: private:
std::vector<std::shared_ptr<mod>> m_modules; std::vector<mod_p> m_modules;
std::vector<std::shared_ptr<thing>> m_things; std::vector<thing_p> m_things;
std::vector<std::shared_ptr<executor>> m_executors; std::vector<executor_p> m_executors;
}; };
} // namespace furvm } // namespace furvm
+9 -11
View File
@@ -2,9 +2,7 @@
#define FURVM_EXECUTOR_HPP #define FURVM_EXECUTOR_HPP
#include "furvm/fwd.hpp" #include "furvm/fwd.hpp"
#include "furvm/thing.hpp"
#include <memory>
#include <stack> #include <stack>
namespace furvm { namespace furvm {
@@ -42,7 +40,7 @@ public:
* Call frame. * Call frame.
*/ */
struct frame { struct frame {
std::shared_ptr<mod> mod; /**< Shared pointer to a module with the bytecode. */ mod_p mod; /**< Shared pointer to a module with the bytecode. */
std::size_t position; /**< Cursor to a current instruction in the bytecode. */ std::size_t position; /**< Cursor to a current instruction in the bytecode. */
std::size_t stackBase; /**< Snapshot of the stack size before this frame. */ std::size_t stackBase; /**< Snapshot of the stack size before this frame. */
}; };
@@ -53,7 +51,7 @@ public:
* @param id * @param id
* @param context * @param context
*/ */
executor(egzekutor, executor_handle id, const std::shared_ptr<context>& context); executor(egzekutor, executor_handle id, const context_p& context);
~executor(); ~executor();
@@ -75,7 +73,7 @@ public:
* *
* @param context Furvm context. * @param context Furvm context.
*/ */
static std::shared_ptr<executor> create(const std::shared_ptr<context>& context); static executor_p create(const context_p& context);
public: public:
/** /**
* @brief Returns an id of this executor. * @brief Returns an id of this executor.
@@ -97,7 +95,7 @@ public:
* @param mod A shared pointer to a furvm module. * @param mod A shared pointer to a furvm module.
* @param position Position in the module's bytecode. * @param position Position in the module's bytecode.
*/ */
void push_frame(const std::shared_ptr<mod>& mod, std::size_t position); void push_frame(const mod_p& mod, std::size_t position);
/** /**
* @brief Pops the top frame. * @brief Pops the top frame.
@@ -118,28 +116,28 @@ public:
* *
* @param thing The thing to push. * @param thing The thing to push.
*/ */
void push_thing(const std::shared_ptr<class thing>& thing); void push_thing(const thing_p& thing);
/** /**
* @brief Pops a thing from the stack. * @brief Pops a thing from the stack.
* *
* @return The popped thing. * @return The popped thing.
*/ */
std::shared_ptr<class thing> pop_thing(); thing_p pop_thing();
/** /**
* @brief Returns the top thing on the stack. * @brief Returns the top thing on the stack.
* *
* @return The thing. * @return The thing.
*/ */
std::shared_ptr<class thing> thing() const; thing_p thing() const;
private: private:
executor_handle m_id; executor_handle m_id;
executor_flags m_flags{}; // NOLINT(bugprone-invalid-enum-default-initialization) executor_flags m_flags{}; // NOLINT(bugprone-invalid-enum-default-initialization)
std::shared_ptr<context> m_context; context_p m_context;
std::stack<struct frame> m_frames; std::stack<struct frame> m_frames;
std::stack<std::shared_ptr<class thing>> m_stack; std::stack<thing_p> m_stack;
}; };
} // namespace furvm } // namespace furvm
+21
View File
@@ -3,6 +3,7 @@
#include <cstddef> // IWYU pragma: export #include <cstddef> // IWYU pragma: export
#include <cstdint> // IWYU pragma: export #include <cstdint> // IWYU pragma: export
#include <memory>
/** /**
* @brief Furlang's virtual machine. * @brief Furlang's virtual machine.
@@ -56,6 +57,11 @@ struct instruction;
*/ */
class mod; class mod;
/**
* @brief An alias to a module shared pointer.
*/
using mod_p = std::shared_ptr<mod>;
/** /**
* @brief Furvm module's index. * @brief Furvm module's index.
*/ */
@@ -77,6 +83,11 @@ enum class thing_t : std::uint8_t;
*/ */
class thing; class thing;
/**
* @brief An alias to a thing shared pointer.
*/
using thing_p = std::shared_ptr<thing>;
/** /**
* @brief Furvm thing's index. * @brief Furvm thing's index.
*/ */
@@ -98,6 +109,11 @@ enum class executor_flags : std::uint32_t;
*/ */
class executor; class executor;
/**
* @brief An alias to a executor shared pointer.
*/
using executor_p = std::shared_ptr<executor>;
/** /**
* @brief Furvm executor's index. * @brief Furvm executor's index.
*/ */
@@ -113,6 +129,11 @@ using executor_handle = std::uint32_t;
*/ */
class context; class context;
/**
* @brief An alias to a context shared pointer.
*/
using context_p = std::shared_ptr<context>;
} // namespace furvm } // namespace furvm
#endif // FURVM_FWD_HPP #endif // FURVM_FWD_HPP
+2 -4
View File
@@ -3,8 +3,6 @@
#include "furvm/fwd.hpp" #include "furvm/fwd.hpp"
#include <memory>
namespace furvm { namespace furvm {
enum class thing_t : std::uint8_t { enum class thing_t : std::uint8_t {
@@ -30,7 +28,7 @@ public:
* @param type * @param type
* @param context * @param context
*/ */
thing(rzecz, thing_handle id, thing_t type, const std::shared_ptr<context>& context); thing(rzecz, thing_handle id, thing_t type, const context_p& context);
~thing(); ~thing();
@@ -49,7 +47,7 @@ public:
private: private:
thing_handle m_id; thing_handle m_id;
thing_t m_type; thing_t m_type;
std::shared_ptr<context> m_context; context_p m_context;
void* m_data = nullptr; void* m_data = nullptr;
}; };
+8 -8
View File
@@ -1,23 +1,23 @@
#include "furvm/executor.hpp" #include "furvm/executor.hpp"
#include "furvm/context.hpp" #include "furvm/context.hpp" // IWYU pragma: keep
namespace furvm { namespace furvm {
executor::executor(egzekutor, executor_handle id, const std::shared_ptr<context>& context) executor::executor(egzekutor, executor_handle id, const context_p& context)
: m_id(id), m_context(context) {} : m_id(id), m_context(context) {}
executor::~executor() { executor::~executor() {
m_context->m_executors[m_id] = nullptr; m_context->m_executors[m_id] = nullptr;
} }
std::shared_ptr<executor> executor::create(const std::shared_ptr<context>& context) { executor_p executor::create(const context_p& context) {
auto ex = std::make_shared<executor>(egzekutor{}, context->m_executors.size(), context); auto ex = std::make_shared<executor>(egzekutor{}, context->m_executors.size(), context);
context->m_executors.push_back(ex); context->m_executors.push_back(ex);
return std::move(ex); return std::move(ex);
} }
void executor::push_frame(const std::shared_ptr<mod>& mod, std::size_t position) { void executor::push_frame(const mod_p& mod, std::size_t position) {
m_frames.emplace((struct executor::frame){ mod, position, m_stack.size() }); m_frames.emplace((struct executor::frame){ mod, position, m_stack.size() });
} }
@@ -31,18 +31,18 @@ struct executor::frame executor::frame() const {
return m_frames.top(); return m_frames.top();
} }
void executor::push_thing(const std::shared_ptr<class thing>& thing) { void executor::push_thing(const thing_p& thing) {
m_stack.push(thing); m_stack.push(thing);
} }
std::shared_ptr<class thing> executor::pop_thing() { thing_p executor::pop_thing() {
if (m_frames.top().stackBase >= m_stack.size()) return {}; if (m_frames.top().stackBase >= m_stack.size()) return {};
std::shared_ptr<class thing> top = std::move(m_stack.top()); thing_p top = std::move(m_stack.top());
m_stack.pop(); m_stack.pop();
return top; return top;
} }
std::shared_ptr<class thing> executor::thing() const { thing_p executor::thing() const {
if (m_frames.top().stackBase >= m_stack.size()) return {}; if (m_frames.top().stackBase >= m_stack.size()) return {};
return m_stack.top(); return m_stack.top();
} }
+1 -1
View File
@@ -2,7 +2,7 @@
namespace furvm { namespace furvm {
thing::thing(rzecz, thing_handle id, thing_t type, const std::shared_ptr<context>& context) 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) {}
thing::~thing() {} thing::~thing() {}