@@ -519,6 +519,7 @@ class BasicFormatString
519
519
template <typename StringType, typename ... ParameterTypes>
520
520
class BasicFormatParameters
521
521
{
522
+ using FormatParameters = std::tuple<std::type_identity_t <ParameterTypes>...>;
522
523
using FormatSpecifier = BasicFormatSpecifier<typename StringType::value_type>;
523
524
524
525
public:
@@ -534,17 +535,38 @@ class BasicFormatParameters
534
535
* position. If the provided position is found, invokes the provided callback with the
535
536
* replacement field and a reference to the found format parameter.
536
537
*
538
+ * @tparam Callback Type of the callback to invoke.
539
+ *
537
540
* @param specifier The replacement field corresponding to the parameter to search for.
538
541
* @param callback The callback to invoke if the parameter is found.
539
542
*/
540
543
template <typename Callback, size_t N = 0 >
541
544
void visit (FormatSpecifier &&specifier, Callback callback) const ;
542
545
546
+ /* *
547
+ * Visitor to provide runtime access to the stored parameter at the provided index. If the index
548
+ * is found, and is convertible to the desired type, returns a copy of the found format
549
+ * parameter.
550
+ *
551
+ * This is only allowed for integral format parameters. Attempting to copy other format types is
552
+ * forbidden.
553
+ *
554
+ * @tparam T Desired type of the format parameter.
555
+ *
556
+ * @param index The index of the parameter to search for.
557
+ *
558
+ * @return If successful, a copy of the format parameter. Otherwise, an uninitialized value.
559
+ */
560
+ template <typename T, size_t N = 0 >
561
+ std::optional<T> get (std::size_t index) const ;
562
+
543
563
private:
544
564
BasicFormatParameters (const BasicFormatParameters &) = delete ;
545
565
BasicFormatParameters &operator =(const BasicFormatParameters &) = delete ;
546
566
547
- const std::tuple<ParameterTypes...> m_parameters;
567
+ const FormatParameters m_parameters;
568
+
569
+ static constexpr const std::size_t s_parameter_count = sizeof ...(ParameterTypes);
548
570
};
549
571
550
572
// ==================================================================================================
@@ -1167,9 +1189,7 @@ void BasicFormatParameters<StringType, ParameterTypes...>::visit(
1167
1189
FormatSpecifier &&specifier,
1168
1190
Callback callback) const
1169
1191
{
1170
- static constexpr const std::size_t s_tuple_size = std::tuple_size_v<decltype (m_parameters)>;
1171
-
1172
- if constexpr (N < s_tuple_size)
1192
+ if constexpr (N < s_parameter_count)
1173
1193
{
1174
1194
if (N == specifier.m_position )
1175
1195
{
@@ -1181,4 +1201,29 @@ void BasicFormatParameters<StringType, ParameterTypes...>::visit(
1181
1201
}
1182
1202
}
1183
1203
1204
+ // ==================================================================================================
1205
+ template <typename StringType, typename ... ParameterTypes>
1206
+ template <typename T, size_t N>
1207
+ std::optional<T> BasicFormatParameters<StringType, ParameterTypes...>::get(std::size_t index) const
1208
+ {
1209
+ if constexpr (N < s_parameter_count)
1210
+ {
1211
+ if (N == index )
1212
+ {
1213
+ using P = std::remove_cvref_t <std::tuple_element_t <N, FormatParameters>>;
1214
+
1215
+ if constexpr (std::is_integral_v<P> && std::is_convertible_v<P, T>)
1216
+ {
1217
+ return static_cast <T>(std::get<N>(m_parameters));
1218
+ }
1219
+ }
1220
+
1221
+ return get<T, N + 1 >(index );
1222
+ }
1223
+ else
1224
+ {
1225
+ return std::nullopt;
1226
+ }
1227
+ }
1228
+
1184
1229
} // namespace fly::detail
0 commit comments