diff --git a/src/acs/acsvm.hpp b/src/acs/acsvm.hpp new file mode 100644 index 000000000..b80510ee4 --- /dev/null +++ b/src/acs/acsvm.hpp @@ -0,0 +1,48 @@ +// DR. ROBOTNIK'S RING RACERS +//----------------------------------------------------------------------------- +// Copyright (C) 2016 by James Haley, David Hill, et al. (Team Eternity) +// Copyright (C) 2024 by Sally "TehRealSalt" Cochenour +// Copyright (C) 2024 by Kart Krew +// +// This program is free software distributed under the +// terms of the GNU General Public License, version 2. +// See the 'LICENSE' file for more details. +//----------------------------------------------------------------------------- +/// \file acsvm.hpp +/// \brief ACSVM include file + +#ifndef __SRB2_ACSVM_HPP__ +#define __SRB2_ACSVM_HPP__ + +#include "ACSVM/ACSVM/Action.hpp" +#include "ACSVM/ACSVM/Array.hpp" +#include "ACSVM/ACSVM/BinaryIO.hpp" +#include "ACSVM/ACSVM/CallFunc.hpp" +#include "ACSVM/ACSVM/Code.hpp" +#include "ACSVM/ACSVM/CodeData.hpp" +#include "ACSVM/ACSVM/CodeList.hpp" +#include "ACSVM/ACSVM/Environment.hpp" +#include "ACSVM/ACSVM/Error.hpp" +#include "ACSVM/ACSVM/Function.hpp" +#include "ACSVM/ACSVM/HashMap.hpp" +#include "ACSVM/ACSVM/HashMapFixed.hpp" +#include "ACSVM/ACSVM/ID.hpp" +#include "ACSVM/ACSVM/Init.hpp" +#include "ACSVM/ACSVM/Jump.hpp" +#include "ACSVM/ACSVM/List.hpp" +#include "ACSVM/ACSVM/Module.hpp" +#include "ACSVM/ACSVM/PrintBuf.hpp" +#include "ACSVM/ACSVM/Scope.hpp" +#include "ACSVM/ACSVM/Script.hpp" +#include "ACSVM/ACSVM/Serial.hpp" +#include "ACSVM/ACSVM/Stack.hpp" +#include "ACSVM/ACSVM/Store.hpp" +#include "ACSVM/ACSVM/String.hpp" +#include "ACSVM/ACSVM/Thread.hpp" +#include "ACSVM/ACSVM/Tracer.hpp" +#include "ACSVM/ACSVM/Types.hpp" +#include "ACSVM/ACSVM/Vector.hpp" + +#include "ACSVM/Util/Floats.hpp" + +#endif //__SRB2_ACSVM_HPP__ diff --git a/src/acs/call-funcs.cpp b/src/acs/call-funcs.cpp index 4cce79309..bc8657118 100644 --- a/src/acs/call-funcs.cpp +++ b/src/acs/call-funcs.cpp @@ -11,7 +11,11 @@ /// \file call-funcs.cpp /// \brief Action Code Script: CallFunc instructions -extern "C" { +#include +#include + +#include "acsvm.hpp" + #include "../doomtype.h" #include "../doomdef.h" #include "../doomstat.h" @@ -40,20 +44,10 @@ extern "C" { #include "../k_grandprix.h" #include "../k_bot.h" #include "../z_zone.h" -#include "../m_misc.h" +#include "../r_draw.h" +#include "../k_hud.h" #include "../r_fps.h" -} - -#include "ACSVM/ACSVM/Code.hpp" -#include "ACSVM/ACSVM/CodeData.hpp" -#include "ACSVM/ACSVM/Environment.hpp" -#include "ACSVM/ACSVM/Error.hpp" -#include "ACSVM/ACSVM/Module.hpp" -#include "ACSVM/ACSVM/Scope.hpp" -#include "ACSVM/ACSVM/Script.hpp" -#include "ACSVM/ACSVM/Serial.hpp" -#include "ACSVM/ACSVM/Thread.hpp" -#include "ACSVM/Util/Floats.hpp" +#include "../m_misc.h" #include "call-funcs.hpp" diff --git a/src/acs/call-funcs.hpp b/src/acs/call-funcs.hpp index edb7af8f8..4aa3c1178 100644 --- a/src/acs/call-funcs.hpp +++ b/src/acs/call-funcs.hpp @@ -14,16 +14,7 @@ #ifndef __SRB2_ACS_CALL_FUNCS_HPP__ #define __SRB2_ACS_CALL_FUNCS_HPP__ -#include "ACSVM/ACSVM/Code.hpp" -#include "ACSVM/ACSVM/CodeData.hpp" -#include "ACSVM/ACSVM/Environment.hpp" -#include "ACSVM/ACSVM/Error.hpp" -#include "ACSVM/ACSVM/Module.hpp" -#include "ACSVM/ACSVM/Scope.hpp" -#include "ACSVM/ACSVM/Script.hpp" -#include "ACSVM/ACSVM/Serial.hpp" -#include "ACSVM/ACSVM/Thread.hpp" -#include "ACSVM/Util/Floats.hpp" +#include "acsvm.hpp" /*-------------------------------------------------- bool CallFunc_???(ACSVM::Thread *thread, const ACSVM::Word *argV, ACSVM::Word argC); diff --git a/src/acs/environment.cpp b/src/acs/environment.cpp index af8d2fab0..c015d7ca5 100644 --- a/src/acs/environment.cpp +++ b/src/acs/environment.cpp @@ -11,7 +11,11 @@ /// \file environment.cpp /// \brief Action Code Script: Environment definition -extern "C" { +#include +#include + +#include "acsvm.hpp" + #include "../doomtype.h" #include "../doomdef.h" #include "../doomstat.h" @@ -23,18 +27,6 @@ extern "C" { #include "../w_wad.h" #include "../z_zone.h" #include "../p_local.h" -} - -#include "ACSVM/ACSVM/Code.hpp" -#include "ACSVM/ACSVM/CodeData.hpp" -#include "ACSVM/ACSVM/Environment.hpp" -#include "ACSVM/ACSVM/Error.hpp" -#include "ACSVM/ACSVM/Module.hpp" -#include "ACSVM/ACSVM/Scope.hpp" -#include "ACSVM/ACSVM/Script.hpp" -#include "ACSVM/ACSVM/Serial.hpp" -#include "ACSVM/ACSVM/Thread.hpp" -#include "ACSVM/Util/Floats.hpp" #include "environment.hpp" #include "thread.hpp" diff --git a/src/acs/environment.hpp b/src/acs/environment.hpp index 02385af01..e709162e1 100644 --- a/src/acs/environment.hpp +++ b/src/acs/environment.hpp @@ -14,16 +14,7 @@ #ifndef __SRB2_ACS_ENVIRONMENT_HPP__ #define __SRB2_ACS_ENVIRONMENT_HPP__ -#include "ACSVM/ACSVM/Code.hpp" -#include "ACSVM/ACSVM/CodeData.hpp" -#include "ACSVM/ACSVM/Environment.hpp" -#include "ACSVM/ACSVM/Error.hpp" -#include "ACSVM/ACSVM/Module.hpp" -#include "ACSVM/ACSVM/Scope.hpp" -#include "ACSVM/ACSVM/Script.hpp" -#include "ACSVM/ACSVM/Serial.hpp" -#include "ACSVM/ACSVM/Thread.hpp" -#include "ACSVM/Util/Floats.hpp" +#include "acsvm.hpp" namespace srb2::acs { diff --git a/src/acs/interface.cpp b/src/acs/interface.cpp index 5f08cd173..9aad7ea37 100644 --- a/src/acs/interface.cpp +++ b/src/acs/interface.cpp @@ -11,7 +11,16 @@ /// \file interface.cpp /// \brief Action Code Script: Interface for the rest of SRB2's game logic -extern "C" { +#include +#include +#include +#include +#include + +#include "span.hpp" + +#include "acsvm.hpp" + #include "interface.h" #include "../doomtype.h" @@ -21,24 +30,8 @@ extern "C" { #include "../r_defs.h" #include "../g_game.h" #include "../i_system.h" -#include "../w_wad.h" #include "../p_saveg.h" -} - -#include -#include - -#include "ACSVM/ACSVM/Code.hpp" -#include "ACSVM/ACSVM/CodeData.hpp" -#include "ACSVM/ACSVM/Environment.hpp" -#include "ACSVM/ACSVM/Error.hpp" -#include "ACSVM/ACSVM/Module.hpp" -#include "ACSVM/ACSVM/Scope.hpp" -#include "ACSVM/ACSVM/Script.hpp" -#include "ACSVM/ACSVM/Serial.hpp" -#include "ACSVM/ACSVM/Thread.hpp" -#include "ACSVM/ACSVM/Action.hpp" -#include "ACSVM/Util/Floats.hpp" +#include "../w_wad.h" #include "environment.hpp" #include "thread.hpp" @@ -368,31 +361,63 @@ void ACS_Tick(void) } /*-------------------------------------------------- - boolean ACS_Execute(const char *name, const INT32 *args, size_t numArgs, activator_t *activator) + static std::vector ACS_MixArgs(tcb::span args, tcb::span stringArgs) - See header file for description. + Convert strings to ACS arguments and position them + correctly among integer arguments. + + Input Arguments:- + args: Integer arguments. + stringArgs: C string arguments. + + Return:- + Final argument vector. --------------------------------------------------*/ -boolean ACS_Execute(const char *name, const INT32 *args, size_t numArgs, activator_t *activator) +static std::vector ACS_MixArgs(tcb::span args, tcb::span stringArgs) { - Environment *env = &ACSEnv; + std::vector argV; + size_t first = std::min(args.size(), stringArgs.size()); - ACSVM::GlobalScope *const global = env->getGlobalScope(0); - ACSVM::HubScope *const hub = global->getHubScope(0); - ACSVM::MapScope *const map = hub->getMapScope(0); - ACSVM::ScopeID scope{global->id, hub->id, map->id}; + auto new_string = [env = &ACSEnv](const char* str) -> ACSVM::Word { return ~env->getString(str, strlen(str))->idx; }; - ThreadInfo info{activator}; + for (size_t i = 0; i < first; ++i) + { + // args[i] must be 0. + // + // If ACS_Execute is called from ACS, stringargs[i] + // will always be set, because there is no + // differentiation between integers and strings on + // arguments passed to a function. In this case, + // string arguments already exist in the ACS string + // table beforehand (and set in args[i]), so no + // conversion is required here. + // + // If ACS_Execute is called from a map line special, + // args[i] may be left unset (0), while stringArgs[i] + // is set. In this case, conversion to ACS string + // table is necessary. + argV.push_back(!args[i] && stringArgs[i] ? new_string(stringArgs[i]) : args[i]); + } - ACSVM::String *script = env->getString(name, strlen(name)); - return map->scriptStart(script, scope, {reinterpret_cast(args), numArgs, &info}); + for (size_t i = first; i < args.size(); ++i) + { + argV.push_back(args[i]); + } + + for (size_t i = first; i < stringArgs.size(); ++i) + { + argV.push_back(new_string(stringArgs[i] ? stringArgs[i] : "")); + } + + return argV; } /*-------------------------------------------------- - boolean ACS_ExecuteAlways(const char *name, const INT32 *args, size_t numArgs, activator_t *activator) + boolean ACS_Execute(const char *name, const INT32 *args, size_t numArgs, const char *const *stringArgs, size_t numStringArgs, activator_t *activator) See header file for description. --------------------------------------------------*/ -boolean ACS_ExecuteAlways(const char *name, const INT32 *args, size_t numArgs, activator_t *activator) +boolean ACS_Execute(const char *name, const INT32 *args, size_t numArgs, const char *const *stringArgs, size_t numStringArgs, activator_t *activator) { Environment *env = &ACSEnv; @@ -404,7 +429,29 @@ boolean ACS_ExecuteAlways(const char *name, const INT32 *args, size_t numArgs, a ThreadInfo info{activator}; ACSVM::String *script = env->getString(name, strlen(name)); - return map->scriptStartForced(script, scope, {reinterpret_cast(args), numArgs, &info}); + std::vector argV = ACS_MixArgs(tcb::span {args, numArgs}, tcb::span {stringArgs, numStringArgs}); + return map->scriptStart(script, scope, {argV.data(), argV.size(), &info}); +} + +/*-------------------------------------------------- + boolean ACS_ExecuteAlways(const char *name, const INT32 *args, size_t numArgs, const char *const *stringArgs, size_t numStringArgs, activator_t *activator) + + See header file for description. +--------------------------------------------------*/ +boolean ACS_ExecuteAlways(const char *name, const INT32 *args, size_t numArgs, const char *const *stringArgs, size_t numStringArgs, activator_t *activator) +{ + Environment *env = &ACSEnv; + + ACSVM::GlobalScope *const global = env->getGlobalScope(0); + ACSVM::HubScope *const hub = global->getHubScope(0); + ACSVM::MapScope *const map = hub->getMapScope(0); + ACSVM::ScopeID scope{global->id, hub->id, map->id}; + + ThreadInfo info{activator}; + + ACSVM::String *script = env->getString(name, strlen(name)); + std::vector argV = ACS_MixArgs(tcb::span {args, numArgs}, tcb::span {stringArgs, numStringArgs}); + return map->scriptStartForced(script, scope, {argV.data(), argV.size(), &info}); } /*-------------------------------------------------- diff --git a/src/acs/interface.h b/src/acs/interface.h index beb7e30ae..e4410d29a 100644 --- a/src/acs/interface.h +++ b/src/acs/interface.h @@ -18,6 +18,10 @@ #include "../doomdef.h" #include "../doomstat.h" +#ifdef __cplusplus +extern "C" { +#endif + /*-------------------------------------------------- void ACS_Init(void); @@ -204,7 +208,7 @@ void ACS_Tick(void); /*-------------------------------------------------- - boolean ACS_Execute(const char *name, const INT32 *args, size_t numArgs, activator_t *activator); + boolean ACS_Execute(const INT32 *args, size_t numArgs, const char *const *stringArgs, size_t numStringArgs, activator_t *activator); Runs an ACS script by its string name. Only one instance of the script will run at @@ -216,6 +220,8 @@ void ACS_Tick(void); Strings should be transformed into ACSVM string IDs. numArgs: Number of input arguments. + stringArgs: Array of input string arguments. + numStringArgs: Number of input string arguments. activator: Container for information on what activated this script. @@ -223,11 +229,11 @@ void ACS_Tick(void); true if we were able to run the script, otherwise false. --------------------------------------------------*/ -boolean ACS_Execute(const char *name, const INT32 *args, size_t numArgs, activator_t *activator); +boolean ACS_Execute(const char *name, const INT32 *args, size_t numArgs, const char *const *stringArgs, size_t numStringArgs, activator_t *activator); /*-------------------------------------------------- - boolean ACS_ExecuteAlways(const char *name, const INT32 *args, size_t numArgs, activator_t *activator); + boolean ACS_ExecuteAlways(const INT32 *args, size_t numArgs, const char *const *stringArgs, size_t numStringArgs, activator_t *activator) Runs an ACS script by its string name. If the script is already running, this method @@ -240,6 +246,8 @@ boolean ACS_Execute(const char *name, const INT32 *args, size_t numArgs, activat Strings should be transformed into ACSVM string IDs. numArgs: Number of input arguments. + stringArgs: Array of input string arguments. + numStringArgs: Number of input string arguments. activator: Container for information on what activated this script. @@ -247,11 +255,11 @@ boolean ACS_Execute(const char *name, const INT32 *args, size_t numArgs, activat true if we were able to run the script, otherwise false. --------------------------------------------------*/ -boolean ACS_ExecuteAlways(const char *name, const INT32 *args, size_t numArgs, activator_t *activator); +boolean ACS_ExecuteAlways(const char *name, const INT32 *args, size_t numArgs, const char *const *stringArgs, size_t numStringArgs, activator_t *activator); /*-------------------------------------------------- - INT32 ACS_ExecuteResult(const char *name, const INT32 *args, size_t numArgs, activator_t *activator); + INT32 ACS_ExecuteResult(const char *name, const INT32 *args, size_t numArgs, activator_t *activator) Runs an ACS script by its string name. Will return the scripts special result @@ -332,5 +340,8 @@ void ACS_Archive(savebuffer_t *save); void ACS_UnArchive(savebuffer_t *save); +#ifdef __cplusplus +} +#endif #endif // __SRB2_ACS_INTERFACE_H__ diff --git a/src/acs/span.hpp b/src/acs/span.hpp new file mode 100644 index 000000000..fdc3a988a --- /dev/null +++ b/src/acs/span.hpp @@ -0,0 +1,618 @@ + +/* +This is an implementation of C++20's std::span +http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/n4820.pdf +*/ + +// Copyright Tristan Brindle 2018. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file ../../LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +#ifndef TCB_SPAN_HPP_INCLUDED +#define TCB_SPAN_HPP_INCLUDED + +#include +#include +#include +#include + +#ifndef TCB_SPAN_NO_EXCEPTIONS +// Attempt to discover whether we're being compiled with exception support +#if !(defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) +#define TCB_SPAN_NO_EXCEPTIONS +#endif +#endif + +#ifndef TCB_SPAN_NO_EXCEPTIONS +#include +#include +#endif + +// Various feature test macros + +#ifndef TCB_SPAN_NAMESPACE_NAME +#define TCB_SPAN_NAMESPACE_NAME tcb +#endif + +#if __cplusplus >= 201703L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) +#define TCB_SPAN_HAVE_CPP17 +#endif + +#if __cplusplus >= 201402L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201402L) +#define TCB_SPAN_HAVE_CPP14 +#endif + +namespace TCB_SPAN_NAMESPACE_NAME { + +// Establish default contract checking behavior +#if !defined(TCB_SPAN_THROW_ON_CONTRACT_VIOLATION) && \ + !defined(TCB_SPAN_TERMINATE_ON_CONTRACT_VIOLATION) && \ + !defined(TCB_SPAN_NO_CONTRACT_CHECKING) +#if defined(NDEBUG) || !defined(TCB_SPAN_HAVE_CPP14) +#define TCB_SPAN_NO_CONTRACT_CHECKING +#else +#define TCB_SPAN_TERMINATE_ON_CONTRACT_VIOLATION +#endif +#endif + +#if defined(TCB_SPAN_THROW_ON_CONTRACT_VIOLATION) +struct contract_violation_error : std::logic_error { + explicit contract_violation_error(const char* msg) : std::logic_error(msg) + {} +}; + +inline void contract_violation(const char* msg) +{ + throw contract_violation_error(msg); +} + +#elif defined(TCB_SPAN_TERMINATE_ON_CONTRACT_VIOLATION) +[[noreturn]] inline void contract_violation(const char* /*unused*/) +{ + std::terminate(); +} +#endif + +#if !defined(TCB_SPAN_NO_CONTRACT_CHECKING) +#define TCB_SPAN_STRINGIFY(cond) #cond +#define TCB_SPAN_EXPECT(cond) \ + cond ? (void) 0 : contract_violation("Expected " TCB_SPAN_STRINGIFY(cond)) +#else +#define TCB_SPAN_EXPECT(cond) +#endif + +#if defined(TCB_SPAN_HAVE_CPP17) || defined(__cpp_inline_variables) +#define TCB_SPAN_INLINE_VAR inline +#else +#define TCB_SPAN_INLINE_VAR +#endif + +#if defined(TCB_SPAN_HAVE_CPP14) || \ + (defined(__cpp_constexpr) && __cpp_constexpr >= 201304) +#define TCB_SPAN_HAVE_CPP14_CONSTEXPR +#endif + +#if defined(TCB_SPAN_HAVE_CPP14_CONSTEXPR) +#define TCB_SPAN_CONSTEXPR14 constexpr +#else +#define TCB_SPAN_CONSTEXPR14 +#endif + +#if defined(TCB_SPAN_HAVE_CPP14_CONSTEXPR) && \ + (!defined(_MSC_VER) || _MSC_VER > 1900) +#define TCB_SPAN_CONSTEXPR_ASSIGN constexpr +#else +#define TCB_SPAN_CONSTEXPR_ASSIGN +#endif + +#if defined(TCB_SPAN_NO_CONTRACT_CHECKING) +#define TCB_SPAN_CONSTEXPR11 constexpr +#else +#define TCB_SPAN_CONSTEXPR11 TCB_SPAN_CONSTEXPR14 +#endif + +#if defined(TCB_SPAN_HAVE_CPP17) || defined(__cpp_deduction_guides) +#define TCB_SPAN_HAVE_DEDUCTION_GUIDES +#endif + +#if defined(TCB_SPAN_HAVE_CPP17) || defined(__cpp_lib_byte) +#define TCB_SPAN_HAVE_STD_BYTE +#endif + +#if defined(TCB_SPAN_HAVE_CPP17) || defined(__cpp_lib_array_constexpr) +#define TCB_SPAN_HAVE_CONSTEXPR_STD_ARRAY_ETC +#endif + +#if defined(TCB_SPAN_HAVE_CONSTEXPR_STD_ARRAY_ETC) +#define TCB_SPAN_ARRAY_CONSTEXPR constexpr +#else +#define TCB_SPAN_ARRAY_CONSTEXPR +#endif + +#ifdef TCB_SPAN_HAVE_STD_BYTE +using byte = std::byte; +#else +using byte = unsigned char; +#endif + +#if defined(TCB_SPAN_HAVE_CPP17) +#define TCB_SPAN_NODISCARD [[nodiscard]] +#else +#define TCB_SPAN_NODISCARD +#endif + +TCB_SPAN_INLINE_VAR constexpr std::size_t dynamic_extent = SIZE_MAX; + +template +class span; + +namespace detail { + +template +struct span_storage { + constexpr span_storage() noexcept = default; + + constexpr span_storage(E* p_ptr, std::size_t /*unused*/) noexcept + : ptr(p_ptr) + {} + + E* ptr = nullptr; + static constexpr std::size_t size = S; +}; + +template +struct span_storage { + constexpr span_storage() noexcept = default; + + constexpr span_storage(E* p_ptr, std::size_t p_size) noexcept + : ptr(p_ptr), size(p_size) + {} + + E* ptr = nullptr; + std::size_t size = 0; +}; + +// Reimplementation of C++17 std::size() and std::data() +#if defined(TCB_SPAN_HAVE_CPP17) || \ + defined(__cpp_lib_nonmember_container_access) +using std::data; +using std::size; +#else +template +constexpr auto size(const C& c) -> decltype(c.size()) +{ + return c.size(); +} + +template +constexpr std::size_t size(const T (&)[N]) noexcept +{ + return N; +} + +template +constexpr auto data(C& c) -> decltype(c.data()) +{ + return c.data(); +} + +template +constexpr auto data(const C& c) -> decltype(c.data()) +{ + return c.data(); +} + +template +constexpr T* data(T (&array)[N]) noexcept +{ + return array; +} + +template +constexpr const E* data(std::initializer_list il) noexcept +{ + return il.begin(); +} +#endif // TCB_SPAN_HAVE_CPP17 + +#if defined(TCB_SPAN_HAVE_CPP17) || defined(__cpp_lib_void_t) +using std::void_t; +#else +template +using void_t = void; +#endif + +template +using uncvref_t = + typename std::remove_cv::type>::type; + +template +struct is_span : std::false_type {}; + +template +struct is_span> : std::true_type {}; + +template +struct is_std_array : std::false_type {}; + +template +struct is_std_array> : std::true_type {}; + +template +struct has_size_and_data : std::false_type {}; + +template +struct has_size_and_data())), + decltype(detail::data(std::declval()))>> + : std::true_type {}; + +template > +struct is_container { + static constexpr bool value = + !is_span::value && !is_std_array::value && + !std::is_array::value && has_size_and_data::value; +}; + +template +using remove_pointer_t = typename std::remove_pointer::type; + +template +struct is_container_element_type_compatible : std::false_type {}; + +template +struct is_container_element_type_compatible< + T, E, + typename std::enable_if< + !std::is_same< + typename std::remove_cv()))>::type, + void>::value && + std::is_convertible< + remove_pointer_t()))> (*)[], + E (*)[]>::value + >::type> + : std::true_type {}; + +template +struct is_complete : std::false_type {}; + +template +struct is_complete : std::true_type {}; + +} // namespace detail + +template +class span { + static_assert(std::is_object::value, + "A span's ElementType must be an object type (not a " + "reference type or void)"); + static_assert(detail::is_complete::value, + "A span's ElementType must be a complete type (not a forward " + "declaration)"); + static_assert(!std::is_abstract::value, + "A span's ElementType cannot be an abstract class type"); + + using storage_type = detail::span_storage; + +public: + // constants and types + using element_type = ElementType; + using value_type = typename std::remove_cv::type; + using size_type = std::size_t; + using difference_type = std::ptrdiff_t; + using pointer = element_type*; + using const_pointer = const element_type*; + using reference = element_type&; + using const_reference = const element_type&; + using iterator = pointer; + using reverse_iterator = std::reverse_iterator; + + static constexpr size_type extent = Extent; + + // [span.cons], span constructors, copy, assignment, and destructor + template < + std::size_t E = Extent, + typename std::enable_if<(E == dynamic_extent || E <= 0), int>::type = 0> + constexpr span() noexcept + {} + + TCB_SPAN_CONSTEXPR11 span(pointer ptr, size_type count) + : storage_(ptr, count) + { + TCB_SPAN_EXPECT(extent == dynamic_extent || count == extent); + } + + TCB_SPAN_CONSTEXPR11 span(pointer first_elem, pointer last_elem) + : storage_(first_elem, last_elem - first_elem) + { + TCB_SPAN_EXPECT(extent == dynamic_extent || + last_elem - first_elem == + static_cast(extent)); + } + + template ::value, + int>::type = 0> + constexpr span(element_type (&arr)[N]) noexcept : storage_(arr, N) + {} + + template &, ElementType>::value, + int>::type = 0> + TCB_SPAN_ARRAY_CONSTEXPR span(std::array& arr) noexcept + : storage_(arr.data(), N) + {} + + template &, ElementType>::value, + int>::type = 0> + TCB_SPAN_ARRAY_CONSTEXPR span(const std::array& arr) noexcept + : storage_(arr.data(), N) + {} + + template < + typename Container, std::size_t E = Extent, + typename std::enable_if< + E == dynamic_extent && detail::is_container::value && + detail::is_container_element_type_compatible< + Container&, ElementType>::value, + int>::type = 0> + constexpr span(Container& cont) + : storage_(detail::data(cont), detail::size(cont)) + {} + + template < + typename Container, std::size_t E = Extent, + typename std::enable_if< + E == dynamic_extent && detail::is_container::value && + detail::is_container_element_type_compatible< + const Container&, ElementType>::value, + int>::type = 0> + constexpr span(const Container& cont) + : storage_(detail::data(cont), detail::size(cont)) + {} + + constexpr span(const span& other) noexcept = default; + + template ::value, + int>::type = 0> + constexpr span(const span& other) noexcept + : storage_(other.data(), other.size()) + {} + + ~span() noexcept = default; + + TCB_SPAN_CONSTEXPR_ASSIGN span& + operator=(const span& other) noexcept = default; + + // [span.sub], span subviews + template + TCB_SPAN_CONSTEXPR11 span first() const + { + TCB_SPAN_EXPECT(Count <= size()); + return {data(), Count}; + } + + template + TCB_SPAN_CONSTEXPR11 span last() const + { + TCB_SPAN_EXPECT(Count <= size()); + return {data() + (size() - Count), Count}; + } + + template + using subspan_return_t = + span; + + template + TCB_SPAN_CONSTEXPR11 subspan_return_t subspan() const + { + TCB_SPAN_EXPECT(Offset <= size() && + (Count == dynamic_extent || Offset + Count <= size())); + return {data() + Offset, + Count != dynamic_extent ? Count : size() - Offset}; + } + + TCB_SPAN_CONSTEXPR11 span + first(size_type count) const + { + TCB_SPAN_EXPECT(count <= size()); + return {data(), count}; + } + + TCB_SPAN_CONSTEXPR11 span + last(size_type count) const + { + TCB_SPAN_EXPECT(count <= size()); + return {data() + (size() - count), count}; + } + + TCB_SPAN_CONSTEXPR11 span + subspan(size_type offset, size_type count = dynamic_extent) const + { + TCB_SPAN_EXPECT(offset <= size() && + (count == dynamic_extent || offset + count <= size())); + return {data() + offset, + count == dynamic_extent ? size() - offset : count}; + } + + // [span.obs], span observers + constexpr size_type size() const noexcept { return storage_.size; } + + constexpr size_type size_bytes() const noexcept + { + return size() * sizeof(element_type); + } + + TCB_SPAN_NODISCARD constexpr bool empty() const noexcept + { + return size() == 0; + } + + // [span.elem], span element access + TCB_SPAN_CONSTEXPR11 reference operator[](size_type idx) const + { + TCB_SPAN_EXPECT(idx < size()); + return *(data() + idx); + } + + TCB_SPAN_CONSTEXPR11 reference front() const + { + TCB_SPAN_EXPECT(!empty()); + return *data(); + } + + TCB_SPAN_CONSTEXPR11 reference back() const + { + TCB_SPAN_EXPECT(!empty()); + return *(data() + (size() - 1)); + } + + constexpr pointer data() const noexcept { return storage_.ptr; } + + // [span.iterators], span iterator support + constexpr iterator begin() const noexcept { return data(); } + + constexpr iterator end() const noexcept { return data() + size(); } + + TCB_SPAN_ARRAY_CONSTEXPR reverse_iterator rbegin() const noexcept + { + return reverse_iterator(end()); + } + + TCB_SPAN_ARRAY_CONSTEXPR reverse_iterator rend() const noexcept + { + return reverse_iterator(begin()); + } + +private: + storage_type storage_{}; +}; + +#ifdef TCB_SPAN_HAVE_DEDUCTION_GUIDES + +/* Deduction Guides */ +template +span(T (&)[N])->span; + +template +span(std::array&)->span; + +template +span(const std::array&)->span; + +template +span(Container&)->span()))>::type>; + +template +span(const Container&)->span; + +#endif // TCB_HAVE_DEDUCTION_GUIDES + +template +constexpr span +make_span(span s) noexcept +{ + return s; +} + +template +constexpr span make_span(T (&arr)[N]) noexcept +{ + return {arr}; +} + +template +TCB_SPAN_ARRAY_CONSTEXPR span make_span(std::array& arr) noexcept +{ + return {arr}; +} + +template +TCB_SPAN_ARRAY_CONSTEXPR span +make_span(const std::array& arr) noexcept +{ + return {arr}; +} + +template +constexpr span()))>::type> +make_span(Container& cont) +{ + return {cont}; +} + +template +constexpr span +make_span(const Container& cont) +{ + return {cont}; +} + +template +span +as_bytes(span s) noexcept +{ + return {reinterpret_cast(s.data()), s.size_bytes()}; +} + +template < + class ElementType, size_t Extent, + typename std::enable_if::value, int>::type = 0> +span +as_writable_bytes(span s) noexcept +{ + return {reinterpret_cast(s.data()), s.size_bytes()}; +} + +template +constexpr auto get(span s) -> decltype(s[N]) +{ + return s[N]; +} + +} // namespace TCB_SPAN_NAMESPACE_NAME + +namespace std { + +template +class tuple_size> + : public integral_constant {}; + +template +class tuple_size>; // not defined + +template +class tuple_element> { +public: + static_assert(Extent != TCB_SPAN_NAMESPACE_NAME::dynamic_extent && + I < Extent, + ""); + using type = ElementType; +}; + +} // end namespace std + +#endif // TCB_SPAN_HPP_INCLUDED diff --git a/src/acs/stream.cpp b/src/acs/stream.cpp index ed49b6a62..3d7fb85bf 100644 --- a/src/acs/stream.cpp +++ b/src/acs/stream.cpp @@ -14,21 +14,19 @@ // TODO? Maybe untie this file from ACS? -extern "C" { +#include +#include +#include + #include "../doomtype.h" #include "../doomdef.h" #include "../doomstat.h" #include "../p_saveg.h" -} #include "stream.hpp" #include "../cxxutil.hpp" -#include -#include -#include - using namespace srb2::acs; SaveBuffer::SaveBuffer(savebuffer_t *save_) : diff --git a/src/acs/stream.hpp b/src/acs/stream.hpp index 5b79f9d84..456a72532 100644 --- a/src/acs/stream.hpp +++ b/src/acs/stream.hpp @@ -17,6 +17,10 @@ #ifndef __SRB2_ACS_STREAM_HPP__ #define __SRB2_ACS_STREAM_HPP__ +#include + +#include "acsvm.hpp" + extern "C" { #include "../doomtype.h" #include "../doomdef.h" @@ -24,21 +28,6 @@ extern "C" { #include "../p_saveg.h" } -#include "ACSVM/ACSVM/Code.hpp" -#include "ACSVM/ACSVM/CodeData.hpp" -#include "ACSVM/ACSVM/Environment.hpp" -#include "ACSVM/ACSVM/Error.hpp" -#include "ACSVM/ACSVM/Module.hpp" -#include "ACSVM/ACSVM/Scope.hpp" -#include "ACSVM/ACSVM/Script.hpp" -#include "ACSVM/ACSVM/Serial.hpp" -#include "ACSVM/ACSVM/Thread.hpp" -#include "ACSVM/Util/Floats.hpp" - -#include -#include -#include - namespace srb2::acs { class SaveBuffer : public std::streambuf diff --git a/src/acs/thread.cpp b/src/acs/thread.cpp index 7df65e7b1..35d088e22 100644 --- a/src/acs/thread.cpp +++ b/src/acs/thread.cpp @@ -11,6 +11,8 @@ /// \file thread.cpp /// \brief Action Code Script: Thread definition +#include "acsvm.hpp" + #include "thread.hpp" #include "../doomtype.h" @@ -24,18 +26,6 @@ #include "../r_state.h" #include "../p_polyobj.h" -#include "ACSVM/ACSVM/Code.hpp" -#include "ACSVM/ACSVM/CodeData.hpp" -#include "ACSVM/ACSVM/Environment.hpp" -#include "ACSVM/ACSVM/Error.hpp" -#include "ACSVM/ACSVM/Module.hpp" -#include "ACSVM/ACSVM/Scope.hpp" -#include "ACSVM/ACSVM/Script.hpp" -#include "ACSVM/ACSVM/Serial.hpp" -#include "ACSVM/ACSVM/Thread.hpp" -#include "ACSVM/ACSVM/BinaryIO.hpp" -#include "ACSVM/Util/Floats.hpp" - using namespace srb2::acs; void Thread::start( diff --git a/src/acs/thread.hpp b/src/acs/thread.hpp index b9aead55d..478879d67 100644 --- a/src/acs/thread.hpp +++ b/src/acs/thread.hpp @@ -14,6 +14,8 @@ #ifndef __SRB2_ACS_THREAD_HPP__ #define __SRB2_ACS_THREAD_HPP__ +#include "acsvm.hpp" + extern "C" { #include "../doomtype.h" #include "../doomdef.h" @@ -24,17 +26,6 @@ extern "C" { #include "../p_spec.h" } -#include "ACSVM/ACSVM/Code.hpp" -#include "ACSVM/ACSVM/CodeData.hpp" -#include "ACSVM/ACSVM/Environment.hpp" -#include "ACSVM/ACSVM/Error.hpp" -#include "ACSVM/ACSVM/Module.hpp" -#include "ACSVM/ACSVM/Scope.hpp" -#include "ACSVM/ACSVM/Script.hpp" -#include "ACSVM/ACSVM/Serial.hpp" -#include "ACSVM/ACSVM/Thread.hpp" -#include "ACSVM/Util/Floats.hpp" - namespace srb2::acs { // diff --git a/src/p_spec.c b/src/p_spec.c index 8cd78b24b..bcd5c9ed5 100644 --- a/src/p_spec.c +++ b/src/p_spec.c @@ -4218,40 +4218,24 @@ boolean P_ProcessSpecial(activator_t *activator, INT16 special, INT32 *args, cha case 475: // ACS_Execute { - INT32 newArgs[NUM_SCRIPT_ARGS-1] = {0}; - INT32 i; - if (!stringargs[0]) { CONS_Debug(DBG_GAMELOGIC, "Special type 475: No script name given\n"); return false; } - for (i = 1; i < NUM_SCRIPT_ARGS; i++) - { - newArgs[i - 1] = args[i]; - } - - ACS_Execute(stringargs[0], newArgs, NUM_SCRIPT_ARGS-1, activator); + ACS_Execute(stringargs[0], &args[1], NUM_SCRIPT_ARGS - 1, (const char* const*)&stringargs[1], NUM_SCRIPT_STRINGARGS - 1, activator); } break; case 476: // ACS_ExecuteAlways { - INT32 newArgs[NUM_SCRIPT_ARGS-1] = {0}; - INT32 i; - if (!stringargs[0]) { CONS_Debug(DBG_GAMELOGIC, "Special type 475: No script name given\n"); return false; } - for (i = 1; i < NUM_SCRIPT_ARGS; i++) - { - newArgs[i - 1] = args[i]; - } - - ACS_ExecuteAlways(stringargs[0], newArgs, NUM_SCRIPT_ARGS-1, activator); + ACS_ExecuteAlways(stringargs[0], &args[1], NUM_SCRIPT_ARGS - 1, (const char* const*)&stringargs[1], NUM_SCRIPT_STRINGARGS - 1, activator); } break; case 477: // ACS_Suspend