Use template specialisation for GUIList parameter reference/init behaviour

This commit is contained in:
Jonathan G Rennison
2024-05-07 17:34:15 +01:00
parent 18a42664fc
commit 5e24882c18

View File

@@ -38,6 +38,19 @@ struct Filtering {
uint8_t criteria; ///< Filtering criteria uint8_t criteria; ///< Filtering criteria
}; };
template <typename T>
struct GUIListParamConfig {
using SortParameterReference = const T&;
static const bool constructor_init = true;
};
template <>
struct GUIListParamConfig<std::nullptr_t>
{
using SortParameterReference = const std::nullptr_t;
static const bool constructor_init = false;
};
/** /**
* List template of 'things' \p T to sort in a GUI. * List template of 'things' \p T to sort in a GUI.
* @tparam T Type of data stored in the list to represent each item. * @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 /* 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. */ * it will not be able to reference anything. */
using SortParameterReference = std::conditional_t<std::is_same_v<P, std::nullptr_t>, P, P&>; using SortParameterReference = typename GUIListParamConfig<P>::SortParameterReference;
const SortParameterReference params; SortParameterReference params;
/** /**
* Check if the list is sortable * Check if the list is sortable
@@ -84,7 +97,7 @@ protected:
public: public:
/* If sort parameters are not used then we don't require a reference to the params. */ /* If sort parameters are not used then we don't require a reference to the params. */
template <typename T_ = T, typename P_ = P, typename _F = F, std::enable_if_t<std::is_same_v<P_, std::nullptr_t>>* = nullptr> template <typename T_ = T, typename P_ = P, typename _F = F, std::enable_if_t<!GUIListParamConfig<P_>::constructor_init>* = nullptr>
GUIList() : GUIList() :
sort_func_list(nullptr), sort_func_list(nullptr),
filter_func_list(nullptr), filter_func_list(nullptr),
@@ -93,12 +106,12 @@ public:
filter_type(0), filter_type(0),
resort_timer(1), resort_timer(1),
resort_interval(DAY_TICKS * 10), /* Resort every 10 days by default */ 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. */ /* If sort parameters are used then we require a reference to the params. */
template <typename T_ = T, typename P_ = P, typename _F = F, std::enable_if_t<!std::is_same_v<P_, std::nullptr_t>>* = nullptr> template <typename T_ = T, typename P_ = P, typename _F = F, std::enable_if_t<GUIListParamConfig<P_>::constructor_init>* = nullptr>
GUIList(const P &params) : GUIList(SortParameterReference params) :
sort_func_list(nullptr), sort_func_list(nullptr),
filter_func_list(nullptr), filter_func_list(nullptr),
flags(VL_NONE), flags(VL_NONE),
@@ -109,6 +122,9 @@ public:
params(params) params(params)
{}; {};
template <typename T_ = T, typename P_ = P, typename _F = F, std::enable_if_t<!std::is_same_v<P_, std::nullptr_t>>* = nullptr>
SortParameterReference &SortParameterData() { return this->params; }
/** /**
* Get the sorttype of the list * Get the sorttype of the list
* *