diff --git a/furvm/include/furvm/context.hpp b/furvm/include/furvm/context.hpp index 6e05f87..fbeef5c 100644 --- a/furvm/include/furvm/context.hpp +++ b/furvm/include/furvm/context.hpp @@ -4,7 +4,6 @@ #include "furvm/fwd.hpp" #include "furvm/module.hpp" -#include #include #include @@ -48,7 +47,7 @@ public: * @param index Index to the module. * @return Old value. */ - std::shared_ptr erase(module_handle index) { + mod_p erase(module_handle index) { if (index >= m_modules.size()) return nullptr; return std::move(m_modules[index]); } @@ -59,7 +58,7 @@ public: * @param index Position of the module. * @return The module. */ - constexpr std::shared_ptr& 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. @@ -67,7 +66,7 @@ public: * @param index Position of the module. * @return The module. */ - constexpr const std::shared_ptr& 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. @@ -75,7 +74,7 @@ public: * @param index Position of the module. * @return The module. */ - constexpr std::shared_ptr& 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. @@ -83,7 +82,7 @@ public: * @param index Position of the module. * @return The module. */ - constexpr const std::shared_ptr& 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. @@ -92,9 +91,9 @@ public: */ constexpr size_t size() const { return m_modules.size(); } private: - std::vector> m_modules; - std::vector> m_things; - std::vector> m_executors; + std::vector m_modules; + std::vector m_things; + std::vector m_executors; }; } // namespace furvm diff --git a/furvm/include/furvm/executor.hpp b/furvm/include/furvm/executor.hpp index 88d9e3a..06f957e 100644 --- a/furvm/include/furvm/executor.hpp +++ b/furvm/include/furvm/executor.hpp @@ -2,9 +2,7 @@ #define FURVM_EXECUTOR_HPP #include "furvm/fwd.hpp" -#include "furvm/thing.hpp" -#include #include namespace furvm { @@ -42,9 +40,9 @@ public: * Call frame. */ struct frame { - std::shared_ptr mod; /**< Shared pointer to a module with 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. */ + 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 stackBase; /**< Snapshot of the stack size before this frame. */ }; public: /** @@ -53,7 +51,7 @@ public: * @param id * @param context */ - executor(egzekutor, executor_handle id, const std::shared_ptr& context); + executor(egzekutor, executor_handle id, const context_p& context); ~executor(); @@ -75,7 +73,7 @@ public: * * @param context Furvm context. */ - static std::shared_ptr create(const std::shared_ptr& context); + static executor_p create(const context_p& context); public: /** * @brief Returns an id of this executor. @@ -97,7 +95,7 @@ public: * @param mod A shared pointer to a furvm module. * @param position Position in the module's bytecode. */ - void push_frame(const std::shared_ptr& mod, std::size_t position); + void push_frame(const mod_p& mod, std::size_t position); /** * @brief Pops the top frame. @@ -118,28 +116,28 @@ public: * * @param thing The thing to push. */ - void push_thing(const std::shared_ptr& thing); + void push_thing(const thing_p& thing); /** * @brief Pops a thing from the stack. * * @return The popped thing. */ - std::shared_ptr pop_thing(); + thing_p pop_thing(); /** * @brief Returns the top thing on the stack. * * @return The thing. */ - std::shared_ptr thing() const; + thing_p thing() const; private: - executor_handle m_id; - executor_flags m_flags{}; // NOLINT(bugprone-invalid-enum-default-initialization) - std::shared_ptr m_context; + executor_handle m_id; + executor_flags m_flags{}; // NOLINT(bugprone-invalid-enum-default-initialization) + context_p m_context; - std::stack m_frames; - std::stack> m_stack; + std::stack m_frames; + std::stack m_stack; }; } // namespace furvm diff --git a/furvm/include/furvm/fwd.hpp b/furvm/include/furvm/fwd.hpp index a049982..dac2a9f 100644 --- a/furvm/include/furvm/fwd.hpp +++ b/furvm/include/furvm/fwd.hpp @@ -3,6 +3,7 @@ #include // IWYU pragma: export #include // IWYU pragma: export +#include /** * @brief Furlang's virtual machine. @@ -56,6 +57,11 @@ struct instruction; */ class mod; +/** + * @brief An alias to a module shared pointer. + */ +using mod_p = std::shared_ptr; + /** * @brief Furvm module's index. */ @@ -77,6 +83,11 @@ enum class thing_t : std::uint8_t; */ class thing; +/** + * @brief An alias to a thing shared pointer. + */ +using thing_p = std::shared_ptr; + /** * @brief Furvm thing's index. */ @@ -98,6 +109,11 @@ enum class executor_flags : std::uint32_t; */ class executor; +/** + * @brief An alias to a executor shared pointer. + */ +using executor_p = std::shared_ptr; + /** * @brief Furvm executor's index. */ @@ -113,6 +129,11 @@ using executor_handle = std::uint32_t; */ class context; +/** + * @brief An alias to a context shared pointer. + */ +using context_p = std::shared_ptr; + } // namespace furvm #endif // FURVM_FWD_HPP \ No newline at end of file diff --git a/furvm/include/furvm/thing.hpp b/furvm/include/furvm/thing.hpp index 6e76bf8..497320b 100644 --- a/furvm/include/furvm/thing.hpp +++ b/furvm/include/furvm/thing.hpp @@ -3,8 +3,6 @@ #include "furvm/fwd.hpp" -#include - namespace furvm { enum class thing_t : std::uint8_t { @@ -30,7 +28,7 @@ public: * @param type * @param context */ - thing(rzecz, thing_handle id, thing_t type, const std::shared_ptr& context); + thing(rzecz, thing_handle id, thing_t type, const context_p& context); ~thing(); @@ -47,9 +45,9 @@ public: thing(const thing&) = delete; thing& operator=(const thing&) = delete; private: - thing_handle m_id; - thing_t m_type; - std::shared_ptr m_context; + thing_handle m_id; + thing_t m_type; + context_p m_context; void* m_data = nullptr; }; diff --git a/furvm/src/executor.cpp b/furvm/src/executor.cpp index bb26a61..110a3a2 100644 --- a/furvm/src/executor.cpp +++ b/furvm/src/executor.cpp @@ -1,23 +1,23 @@ #include "furvm/executor.hpp" -#include "furvm/context.hpp" +#include "furvm/context.hpp" // IWYU pragma: keep namespace furvm { -executor::executor(egzekutor, executor_handle id, const std::shared_ptr& context) +executor::executor(egzekutor, executor_handle id, const context_p& context) : m_id(id), m_context(context) {} executor::~executor() { m_context->m_executors[m_id] = nullptr; } -std::shared_ptr executor::create(const std::shared_ptr& context) { +executor_p executor::create(const context_p& context) { auto ex = std::make_shared(egzekutor{}, context->m_executors.size(), context); context->m_executors.push_back(ex); return std::move(ex); } -void executor::push_frame(const std::shared_ptr& 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() }); } @@ -31,18 +31,18 @@ struct executor::frame executor::frame() const { return m_frames.top(); } -void executor::push_thing(const std::shared_ptr& thing) { +void executor::push_thing(const thing_p& thing) { m_stack.push(thing); } -std::shared_ptr executor::pop_thing() { +thing_p executor::pop_thing() { if (m_frames.top().stackBase >= m_stack.size()) return {}; - std::shared_ptr top = std::move(m_stack.top()); + thing_p top = std::move(m_stack.top()); m_stack.pop(); return top; } -std::shared_ptr executor::thing() const { +thing_p executor::thing() const { if (m_frames.top().stackBase >= m_stack.size()) return {}; return m_stack.top(); } diff --git a/furvm/src/thing.cpp b/furvm/src/thing.cpp index ca59f61..d59ea59 100644 --- a/furvm/src/thing.cpp +++ b/furvm/src/thing.cpp @@ -2,7 +2,7 @@ namespace furvm { -thing::thing(rzecz, thing_handle id, thing_t type, const std::shared_ptr& context) +thing::thing(rzecz, thing_handle id, thing_t type, const context_p& context) : m_id(id), m_type(type), m_context(context) {} thing::~thing() {}