aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/utility
diff options
context:
space:
mode:
author hop311 <hop3114@gmail.com>2024-02-04 15:08:32 +0100
committer hop311 <hop3114@gmail.com>2024-02-04 15:08:32 +0100
commit3f6d7351816b0e089495b2f15dc1c956f3151f5a (patch)
tree606aacf5861d3897f102917cc3af1d6be3471f9f /src/openvic-simulation/utility
parent068c13ede817d17df599ca3481261bf17ed95604 (diff)
Reworked ReturnByValue, warn_or_error, expect_date_[identifier_or_]string
Diffstat (limited to 'src/openvic-simulation/utility')
-rw-r--r--src/openvic-simulation/utility/Getters.hpp16
-rw-r--r--src/openvic-simulation/utility/Logger.hpp25
2 files changed, 26 insertions, 15 deletions
diff --git a/src/openvic-simulation/utility/Getters.hpp b/src/openvic-simulation/utility/Getters.hpp
index 33aa5a2..fa76e74 100644
--- a/src/openvic-simulation/utility/Getters.hpp
+++ b/src/openvic-simulation/utility/Getters.hpp
@@ -51,13 +51,15 @@ public: \
ACCESS:
namespace OpenVic {
- /* Any struct extending ReturnByValueProperty will be returned by value by PROPERTY-generated getter functions,
+ /* Any struct tagged with ov_return_by_value 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;
- };
+ * dynamically allocated memory, e.g. dates and colours. The tag must be public, as in the example below:
+ *
+ * public:
+ * using ov_return_by_value = void;
+ */
+ template<typename T>
+ concept ReturnByValue = requires { typename T::ov_return_by_value; };
/*
* Template function used to choose the return type and provide the implementation
@@ -72,7 +74,7 @@ namespace OpenVic {
/* Return std::string_view looking at std::string */
return std::string_view { property };
} else if constexpr (
- std::integral<T> || std::floating_point<T> || std::is_enum_v<T> || std::derived_from<T, ReturnByValueProperty>
+ std::integral<T> || std::floating_point<T> || std::is_enum_v<T> || ReturnByValue<T>
) {
/* Return value */
return T { property };
diff --git a/src/openvic-simulation/utility/Logger.hpp b/src/openvic-simulation/utility/Logger.hpp
index 53decb3..7a2c3d0 100644
--- a/src/openvic-simulation/utility/Logger.hpp
+++ b/src/openvic-simulation/utility/Logger.hpp
@@ -75,16 +75,16 @@ namespace OpenVic {
size_t message_count;
};
- template<typename... Ts>
+ template<typename... Args>
struct log {
- log(log_channel_t& log_channel, Ts&&... ts, source_location const& location) {
+ log(log_channel_t& log_channel, Args&&... args, source_location const& location) {
std::stringstream stream;
stream << StringUtils::get_filename(location.file_name()) << "("
/* Function name removed to reduce clutter. It is already included
* in Godot's print functions, so this was repeating it. */
//<< location.line() << ") `" << location.function_name() << "`: ";
<< location.line() << "): ";
- ((stream << std::forward<Ts>(ts)), ...);
+ ((stream << std::forward<Args>(args)), ...);
stream << std::endl;
log_channel.queue.push(stream.str());
if (log_channel.func) {
@@ -109,19 +109,28 @@ public: \
static inline size_t get_##name##_count() { \
return name##_channel.message_count; \
} \
- template<typename... Ts> \
+ template<typename... Args> \
struct name { \
- name(Ts&&... ts, source_location const& location = source_location::current()) { \
- log<Ts...> { name##_channel, std::forward<Ts>(ts)..., location }; \
+ name(Args&&... args, source_location const& location = source_location::current()) { \
+ log<Args...> { name##_channel, std::forward<Args>(args)..., location }; \
} \
}; \
- template<typename... Ts> \
- name(Ts&&...) -> name<Ts...>;
+ template<typename... Args> \
+ name(Args&&...) -> name<Args...>;
LOG_FUNC(info)
LOG_FUNC(warning)
LOG_FUNC(error)
#undef LOG_FUNC
+
+ template<typename... Args>
+ static inline constexpr void warn_or_error(bool warn, Args&&... args) {
+ if (warn) {
+ warning(std::forward<Args>(args)...);
+ } else {
+ error(std::forward<Args>(args)...);
+ }
+ }
};
}