From c56131a7d615e20e117114e005d0a3e4c9fae2ca Mon Sep 17 00:00:00 2001 From: hop311 Date: Tue, 23 Jan 2024 23:12:36 +0000 Subject: Made Date constexpr so that PROPERTY getters can be constexpr --- src/openvic-simulation/utility/Getters.hpp | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'src/openvic-simulation/utility') diff --git a/src/openvic-simulation/utility/Getters.hpp b/src/openvic-simulation/utility/Getters.hpp index 1fb82b1..33aa5a2 100644 --- a/src/openvic-simulation/utility/Getters.hpp +++ b/src/openvic-simulation/utility/Getters.hpp @@ -36,6 +36,8 @@ constexpr std::string_view get_base_type() const override { \ return ::ovdl::detail::type_name>(); } +/* Create const and non-const reference getters for a variable, applied to its name in its declaration, e + * for example: GameManager PROPERTY_REF(game_manager); */ #define PROPERTY_REF(NAME) PROPERTY_REF_FULL(NAME, private) #define PROPERTY_REF_FULL(NAME, ACCESS) \ NAME; \ @@ -49,6 +51,9 @@ public: \ ACCESS: namespace OpenVic { + /* Any struct extending ReturnByValueProperty will be returned by value by PROPERTY-generated getter functions, + * instead of by const reference as structs are by default. Use this for small structs which don't contain any + * dynamically allocated memory, e.g. Date and fixed_point_t. */ struct ReturnByValueProperty { constexpr bool operator==(ReturnByValueProperty const&) const = default; constexpr std::strong_ordering operator<=>(ReturnByValueProperty const&) const = default; @@ -60,18 +65,18 @@ namespace OpenVic { */ template inline constexpr decltype(auto) _get_property(const T& property) { - if constexpr(std::is_reference_v) { + if constexpr (std::is_reference_v) { /* Return const reference */ return property; } else if constexpr (std::same_as) { /* Return std::string_view looking at std::string */ return std::string_view { property }; } else if constexpr ( - std::integral || std::floating_point || std::is_enum::value || std::derived_from + std::integral || std::floating_point || std::is_enum_v || std::derived_from ) { /* Return value */ return T { property }; - } else if constexpr(std::is_pointer::value) { + } else if constexpr (std::is_pointer_v) { /* Return const pointer */ return static_cast>>>(property); } else { @@ -79,6 +84,7 @@ namespace OpenVic { return property; } } +} /* * Use this on a variable declaration to generate a getter function. PROPERTY assumes the variable is private and so @@ -101,7 +107,7 @@ namespace OpenVic { #define PROPERTY_FULL(NAME, GETTER_NAME, ACCESS) \ NAME; \ public: \ - auto GETTER_NAME() const -> decltype(OpenVic::_get_property(NAME)) { \ + constexpr auto GETTER_NAME() const -> decltype(OpenVic::_get_property(NAME)) { \ return OpenVic::_get_property(NAME); \ } \ ACCESS: @@ -117,5 +123,3 @@ public: \ NAME = new_##NAME; \ } \ ACCESS: - -} -- cgit v1.2.3-56-ga3b1