summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan.goodliffe@octal.co.uk>2026-04-01 14:21:10 +0100
committerDan Goodliffe <dan.goodliffe@octal.co.uk>2026-04-01 14:21:10 +0100
commit6635cee143eb4cf3882bac519b8c12b83b62de62 (patch)
tree6ec80724a1d622b6933de8afa27cbafba2110471
parentHelper to instantiate transient static values as required (diff)
downloadilt-6635cee143eb4cf3882bac519b8c12b83b62de62.tar.bz2
ilt-6635cee143eb4cf3882bac519b8c12b83b62de62.tar.xz
ilt-6635cee143eb4cf3882bac519b8c12b83b62de62.zip
Add helper to Decompose a member pointer
-rw-r--r--lib/util.cpp1
-rw-r--r--lib/util.h10
-rw-r--r--test/Jamfile.jam1
-rw-r--r--test/test-static-util.cpp29
4 files changed, 40 insertions, 1 deletions
diff --git a/lib/util.cpp b/lib/util.cpp
deleted file mode 100644
index 408a76a..0000000
--- a/lib/util.cpp
+++ /dev/null
@@ -1 +0,0 @@
-#include "util.h"
diff --git a/lib/util.h b/lib/util.h
index b60b093..093462d 100644
--- a/lib/util.h
+++ b/lib/util.h
@@ -35,6 +35,16 @@ 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)
diff --git a/test/Jamfile.jam b/test/Jamfile.jam
index da2a61a..baf0db4 100644
--- a/test/Jamfile.jam
+++ b/test/Jamfile.jam
@@ -79,5 +79,6 @@ run test-environment.cpp : : : <library>test ;
run test-ui.cpp : : : <library>test ;
compile test-static-enumDetails.cpp ;
compile test-static-stream_support.cpp ;
+compile test-static-util.cpp ;
alias perf : perf-assetFactory perf-persistence perf-geoData perf-instancing perf-terrain ;
explicit perf ;
diff --git a/test/test-static-util.cpp b/test/test-static-util.cpp
new file mode 100644
index 0000000..2a8aa81
--- /dev/null
+++ b/test/test-static-util.cpp
@@ -0,0 +1,29 @@
+#include "util.h"
+
+namespace {
+ struct Base1 {
+ int a;
+ float b;
+ };
+
+ struct Base2 {
+ int x;
+ float y;
+ };
+
+ struct Sub : Base1, Base2 {
+ double value;
+ };
+
+ static_assert(std::is_same_v<MemberValueType<&Base1::a>, int>);
+ static_assert(std::is_same_v<MemberValueType<&Base2::y>, float>);
+ static_assert(std::is_same_v<MemberValueType<&Sub::a>, int>);
+ static_assert(std::is_same_v<MemberValueType<&Sub::y>, float>);
+ static_assert(std::is_same_v<MemberValueType<&Sub::value>, double>);
+
+ static_assert(std::is_same_v<ContainerType<&Base1::a>, Base1>);
+ static_assert(std::is_same_v<ContainerType<&Base2::y>, Base2>);
+ static_assert(std::is_same_v<ContainerType<&Sub::a>, Base1>);
+ static_assert(std::is_same_v<ContainerType<&Sub::y>, Base2>);
+ static_assert(std::is_same_v<ContainerType<&Sub::value>, Sub>);
+}