aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/utility
diff options
context:
space:
mode:
author George L. Albany <Megacake1234@gmail.com>2024-01-19 12:08:19 +0100
committer GitHub <noreply@github.com>2024-01-19 12:08:19 +0100
commit04e8d365687e5ce6d803f1b3d4c8fb7f2358079b (patch)
tree4b911cea0cf5e5ac467bbbd2073144866c52b265 /src/openvic-simulation/utility
parentc1bac9acee88a7ce1123aed3a748712fb2441762 (diff)
parent3f9911dd1cbfca513c8615d778a8295906d8b7d5 (diff)
Merge pull request #119 from OpenVicProject/add/country-relations
Diffstat (limited to 'src/openvic-simulation/utility')
-rw-r--r--src/openvic-simulation/utility/ErrorMacros.hpp68
-rw-r--r--src/openvic-simulation/utility/StringUtils.hpp1
-rw-r--r--src/openvic-simulation/utility/Utility.hpp62
3 files changed, 120 insertions, 11 deletions
diff --git a/src/openvic-simulation/utility/ErrorMacros.hpp b/src/openvic-simulation/utility/ErrorMacros.hpp
new file mode 100644
index 0000000..47b73d5
--- /dev/null
+++ b/src/openvic-simulation/utility/ErrorMacros.hpp
@@ -0,0 +1,68 @@
+#pragma once
+
+#include "openvic-simulation/utility/Logger.hpp" // IWYU pragma: keep
+#include "openvic-simulation/utility/Utility.hpp"
+
+/**
+ * Try using `ERR_FAIL_COND_MSG`.
+ * Only use this macro if there is no sensible error message.
+ * If checking for null use ERR_FAIL_NULL_MSG instead.
+ * If checking index bounds use ERR_FAIL_INDEX_MSG instead.
+ *
+ * Ensures `m_cond` is false.
+ * If `m_cond` is true, the current function returns.
+ */
+#define OV_ERR_FAIL_COND(m_cond) \
+ if (OV_unlikely(m_cond)) { \
+ ::OpenVic::Logger::error("Condition \"" _OV_STR(m_cond) "\" is true."); \
+ return; \
+ } else \
+ ((void)0)
+
+/**
+ * Ensures `m_cond` is false.
+ * If `m_cond` is true, prints `m_msg` and the current function returns.
+ *
+ * If checking for null use ERR_FAIL_NULL_MSG instead.
+ * If checking index bounds use ERR_FAIL_INDEX_MSG instead.
+ */
+#define OV_ERR_FAIL_COND_MSG(m_cond, m_msg) \
+ if (OV_unlikely(m_cond)) { \
+ ::OpenVic::Logger::error("Condition \"" _OV_STR(m_cond) "\" is true.", m_msg); \
+ return; \
+ } else \
+ ((void)0)
+
+/**
+ * Try using `ERR_FAIL_COND_V_MSG`.
+ * Only use this macro if there is no sensible error message.
+ * If checking for null use ERR_FAIL_NULL_V_MSG instead.
+ * If checking index bounds use ERR_FAIL_INDEX_V_MSG instead.
+ *
+ * Ensures `m_cond` is false.
+ * If `m_cond` is true, the current function returns `m_retval`.
+ */
+#define OV_ERR_FAIL_COND_V(m_cond, m_retval) \
+ if (OV_unlikely(m_cond)) { \
+ ::OpenVic::Logger::error( \
+ "Condition \"" _OV_STR(m_cond) "\" is true. Returning: " _OV_STR(m_retval) \
+ ); \
+ return m_retval; \
+ } else \
+ ((void)0)
+
+/**
+ * Ensures `m_cond` is false.
+ * If `m_cond` is true, prints `m_msg` and the current function returns `m_retval`.
+ *
+ * If checking for null use ERR_FAIL_NULL_V_MSG instead.
+ * If checking index bounds use ERR_FAIL_INDEX_V_MSG instead.
+ */
+#define OV_ERR_FAIL_COND_V_MSG(m_cond, m_retval, m_msg) \
+ if (OV_unlikely(m_cond)) { \
+ ::OpenVic::Logger::error( \
+ "Condition \"" _OV_STR(m_cond) "\" is true. Returning: " _OV_STR(m_retval), m_msg \
+ ); \
+ return m_retval; \
+ } else \
+ ((void)0)
diff --git a/src/openvic-simulation/utility/StringUtils.hpp b/src/openvic-simulation/utility/StringUtils.hpp
index 570f296..e83fbda 100644
--- a/src/openvic-simulation/utility/StringUtils.hpp
+++ b/src/openvic-simulation/utility/StringUtils.hpp
@@ -4,6 +4,7 @@
#include <cctype>
#include <cstring>
#include <limits>
+#include <string>
#include <string_view>
#include <type_traits>
diff --git a/src/openvic-simulation/utility/Utility.hpp b/src/openvic-simulation/utility/Utility.hpp
index 0387e7f..dbbcf8f 100644
--- a/src/openvic-simulation/utility/Utility.hpp
+++ b/src/openvic-simulation/utility/Utility.hpp
@@ -4,6 +4,21 @@
#include <functional>
#include <type_traits>
+#if defined(__GNUC__)
+#define OV_likely(x) __builtin_expect(!!(x), 1)
+#define OV_unlikely(x) __builtin_expect(!!(x), 0)
+#else
+#define OV_likely(x) x
+#define OV_unlikely(x) x
+#endif
+
+// Turn argument to string constant:
+// https://gcc.gnu.org/onlinedocs/cpp/Stringizing.html#Stringizing
+#ifndef _OV_STR
+#define _OV_STR(m_x) #m_x
+#define _OV_MKSTR(m_x) _OV_STR(m_x)
+#endif
+
namespace OpenVic::utility {
[[noreturn]] inline void unreachable() {
// Uses compiler specific extensions if possible.
@@ -25,29 +40,35 @@ namespace OpenVic::utility {
template<size_t Shift, class T>
constexpr inline void hash_combine_index(std::size_t& s, const T& v) {
std::hash<T> h;
- if constexpr(Shift == 0) {
+ if constexpr (Shift == 0) {
s = h(v);
} else {
s ^= h(v) << Shift;
}
}
- template<class T, typename ...Args>
+ template<class T, typename... Args>
constexpr void perfect_hash(std::size_t& s, T&& v, Args&&... args) {
- static_assert(sizeof(T) + (sizeof(Args) + ...) <= sizeof(std::size_t), "Perfect hashes must be able to fit into size_t");
+ static_assert(
+ sizeof(T) + (sizeof(Args) + ...) <= sizeof(std::size_t), "Perfect hashes must be able to fit into size_t"
+ );
std::hash<T> h;
- if constexpr(sizeof...(args) == 0) {
+ if constexpr (sizeof...(args) == 0) {
s = h(v);
} else {
const std::tuple arg_tuple { args... };
s = h(v) << (sizeof(T) * CHAR_BIT);
- ([&]{
- // If args is not last pointer of args
- if (static_cast<const void*>(&(std::get<sizeof...(args) - 1>(arg_tuple))) != static_cast<const void*>(&args)) {
- s <<= sizeof(Args) * CHAR_BIT;
- }
- s |= std::hash<Args>{}(args);
- }(), ...);
+ (
+ [&] {
+ // If args is not last pointer of args
+ if (static_cast<const void*>(&(std::get<sizeof...(args) - 1>(arg_tuple))) !=
+ static_cast<const void*>(&args)) {
+ s <<= sizeof(Args) * CHAR_BIT;
+ }
+ s |= std::hash<Args> {}(args);
+ }(),
+ ...
+ );
}
}
@@ -59,4 +80,23 @@ namespace OpenVic::utility {
template<typename T, template<typename...> class Z>
inline constexpr bool is_specialization_of_v = is_specialization_of<T, Z>::value;
+
+ inline constexpr auto three_way(auto&& left, auto&& right) {
+ // This is Apple's fault again
+ #if __cpp_lib_three_way_comparison >= 201907L
+ if constexpr (std::three_way_comparable_with<std::decay_t<decltype(left)>, std::decay_t<decltype(right)>>) {
+ return left <=> right;
+ } else
+ #endif
+ {
+ if (left < right) {
+ return std::weak_ordering::less;
+ } else if (left > right) {
+ return std::weak_ordering::greater;
+ } else {
+ return std::weak_ordering::equivalent;
+ }
+ }
+ };
+
}