From 5e24882c18b16c2fd516ed348846b9e79df345c2 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Tue, 7 May 2024 17:34:15 +0100 Subject: [PATCH] Use template specialisation for GUIList parameter reference/init behaviour --- src/sortlist_type.h | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/sortlist_type.h b/src/sortlist_type.h index 10de135f6c..1d599e954f 100644 --- a/src/sortlist_type.h +++ b/src/sortlist_type.h @@ -38,6 +38,19 @@ struct Filtering { uint8_t criteria; ///< Filtering criteria }; +template +struct GUIListParamConfig { + using SortParameterReference = const T&; + static const bool constructor_init = true; +}; + +template <> +struct GUIListParamConfig +{ + using SortParameterReference = const std::nullptr_t; + static const bool constructor_init = false; +}; + /** * List template of 'things' \p T to sort in a GUI. * @tparam T Type of data stored in the list to represent each item. @@ -61,8 +74,8 @@ protected: /* If sort parameters are used then params must be a reference, however if not then params cannot be a reference as * it will not be able to reference anything. */ - using SortParameterReference = std::conditional_t, P, P&>; - const SortParameterReference params; + using SortParameterReference = typename GUIListParamConfig

::SortParameterReference; + SortParameterReference params; /** * Check if the list is sortable @@ -84,7 +97,7 @@ protected: public: /* If sort parameters are not used then we don't require a reference to the params. */ - template >* = nullptr> + template ::constructor_init>* = nullptr> GUIList() : sort_func_list(nullptr), filter_func_list(nullptr), @@ -93,12 +106,12 @@ public: filter_type(0), resort_timer(1), resort_interval(DAY_TICKS * 10), /* Resort every 10 days by default */ - params(nullptr) + params(P_()) {}; /* If sort parameters are used then we require a reference to the params. */ - template >* = nullptr> - GUIList(const P ¶ms) : + template ::constructor_init>* = nullptr> + GUIList(SortParameterReference params) : sort_func_list(nullptr), filter_func_list(nullptr), flags(VL_NONE), @@ -109,6 +122,9 @@ public: params(params) {}; + template >* = nullptr> + SortParameterReference &SortParameterData() { return this->params; } + /** * Get the sorttype of the list *