summaryrefslogtreecommitdiff
path: root/lib/util.h
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2026-04-03 12:04:14 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2026-04-03 12:04:14 +0100
commit4d04572e48950493ce85255d181be43967f362fd (patch)
treef74d055f151472cda3fa3fc0f760eb6219dd9fad /lib/util.h
parentUse std::chrono for worldTime (diff)
parentFix direction of difference vector calculating railVehicle new position (diff)
downloadilt-4d04572e48950493ce85255d181be43967f362fd.tar.bz2
ilt-4d04572e48950493ce85255d181be43967f362fd.tar.xz
ilt-4d04572e48950493ce85255d181be43967f362fd.zip
Merge branch 'shared-locations'
Diffstat (limited to 'lib/util.h')
-rw-r--r--lib/util.h22
1 files changed, 22 insertions, 0 deletions
diff --git a/lib/util.h b/lib/util.h
index cd7971b..093462d 100644
--- a/lib/util.h
+++ b/lib/util.h
@@ -3,6 +3,7 @@
#include <algorithm> // IWYU pragma: keep
#include <array>
#include <cstddef>
+#include <memory>
#include <tuple>
template<typename T, std::size_t N>
@@ -33,3 +34,24 @@ template<size_t... N> inline constexpr auto Nth = GetNth<N...> {};
inline constexpr auto GetFirst = Nth<0>;
inline constexpr auto GetSecond = Nth<1>;
inline constexpr auto GetSwapped = Nth<0, 1>;
+
+template<typename T, typename M> struct Decompose {
+ consteval Decompose(M T::*) { }
+
+ using ValueType = M;
+ using ContainerType = T;
+};
+
+template<auto MbrPtr> using MemberValueType = typename decltype(Decompose {MbrPtr})::ValueType;
+template<auto MbrPtr> using ContainerType = typename decltype(Decompose {MbrPtr})::ContainerType;
+
+template<typename T>
+bool
+createIfRequired(std::shared_ptr<T> & instance, std::weak_ptr<T> & common)
+{
+ if (!instance && !(instance = common.lock())) {
+ common = instance = std::make_shared<T>();
+ return true;
+ }
+ return false;
+}