From e532875c8e6d8fcad81c231a950adf93f8f114b6 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Fri, 5 Apr 2024 23:19:22 +0200 Subject: critter script wip --- src/critter-script-base.cpp | 10 ++++++++++ src/critter-script.cpp | 7 +++++++ src/critter-script.hpp | 40 ++++++++++++++++++++++++++++++++++++++++ src/critter-script.inl | 23 +++++++++++++++++++++++ 4 files changed, 80 insertions(+) create mode 100644 src/critter-script-base.cpp create mode 100644 src/critter-script.cpp create mode 100644 src/critter-script.hpp create mode 100644 src/critter-script.inl diff --git a/src/critter-script-base.cpp b/src/critter-script-base.cpp new file mode 100644 index 00000000..4808e7b5 --- /dev/null +++ b/src/critter-script-base.cpp @@ -0,0 +1,10 @@ +#include "critter-script.inl" + +namespace floormat { + +template class script_wrapper; + +base_script::~base_script() noexcept = default; +base_script::base_script() noexcept = default; + +} // namespace floormat diff --git a/src/critter-script.cpp b/src/critter-script.cpp new file mode 100644 index 00000000..e2355a64 --- /dev/null +++ b/src/critter-script.cpp @@ -0,0 +1,7 @@ +#include "critter-script.hpp" + +namespace floormat { + + + +} // namespace floormat diff --git a/src/critter-script.hpp b/src/critter-script.hpp new file mode 100644 index 00000000..9bd3d977 --- /dev/null +++ b/src/critter-script.hpp @@ -0,0 +1,40 @@ +#pragma once + +namespace floormat { + +struct critter; +struct Ns; + +struct base_script +{ + virtual ~base_script() noexcept; + virtual void delete_self() = 0; + base_script() noexcept; +}; + +template +class script_wrapper final +{ + static_assert(std::is_base_of_v); + T* ptr; + +public: + explicit script_wrapper(T* ptr); + ~script_wrapper() noexcept; + + const T& operator*() const noexcept; + T& operator*() noexcept; + const T* operator->() const noexcept; + T* operator->() noexcept; +}; + +struct critter_script : base_script +{ + critter_script(critter& c); + virtual void update(critter& c, size_t i, const Ns& dt) = 0; + // todo can_activate, activate +}; + +extern template class script_wrapper; + +} // namespace floormat diff --git a/src/critter-script.inl b/src/critter-script.inl new file mode 100644 index 00000000..db6b7000 --- /dev/null +++ b/src/critter-script.inl @@ -0,0 +1,23 @@ +#pragma once +#include "critter-script.hpp" +#include "compat/assert.hpp" + +namespace floormat { + +template script_wrapper::script_wrapper(T* ptr): ptr{ptr} { fm_assert(ptr); } + +template +script_wrapper::~script_wrapper() noexcept +{ + ptr->delete_self(); +#ifndef FM_NO_DEBUG + ptr = nullptr; +#endif +} + +template const T& script_wrapper::operator*() const noexcept { return *ptr; } +template T& script_wrapper::operator*() noexcept { return *ptr; } +template const T* script_wrapper::operator->() const noexcept { return ptr; } +template T* script_wrapper::operator->() noexcept { return ptr; } + +} // namespace floormat -- cgit v1.2.3