CLI11 2.4.1
Loading...
Searching...
No Matches
TypeTools.hpp
Go to the documentation of this file.
1// Copyright (c) 2017-2024, University of Cincinnati, developed by Henry Schreiner
2// under NSF AWARD 1414736 and by the respective contributors.
3// All rights reserved.
4//
5// SPDX-License-Identifier: BSD-3-Clause
6
7#pragma once
8
9// [CLI11:public_includes:set]
10#include <algorithm>
11#include <cmath>
12#include <cstdint>
13#include <exception>
14#include <limits>
15#include <memory>
16#include <string>
17#include <type_traits>
18#include <utility>
19#include <vector>
20// [CLI11:public_includes:end]
21
22#include "Encoding.hpp"
23#include "StringTools.hpp"
24
25namespace CLI {
26// [CLI11:type_tools_hpp:verbatim]
27
28// Type tools
29
30// Utilities for type enabling
31namespace detail {
32// Based generally on https://rmf.io/cxx11/almost-static-if
34enum class enabler {};
35
37constexpr enabler dummy = {};
38} // namespace detail
39
45template <bool B, class T = void> using enable_if_t = typename std::enable_if<B, T>::type;
46
48template <typename... Ts> struct make_void {
49 using type = void;
50};
51
53template <typename... Ts> using void_t = typename make_void<Ts...>::type;
54
56template <bool B, class T, class F> using conditional_t = typename std::conditional<B, T, F>::type;
57
59template <typename T> struct is_bool : std::false_type {};
60
62template <> struct is_bool<bool> : std::true_type {};
63
65template <typename T> struct is_shared_ptr : std::false_type {};
66
68template <typename T> struct is_shared_ptr<std::shared_ptr<T>> : std::true_type {};
69
71template <typename T> struct is_shared_ptr<const std::shared_ptr<T>> : std::true_type {};
72
74template <typename T> struct is_copyable_ptr {
75 static bool const value = is_shared_ptr<T>::value || std::is_pointer<T>::value;
76};
77
79template <typename T> struct IsMemberType {
80 using type = T;
81};
82
84template <> struct IsMemberType<const char *> {
85 using type = std::string;
86};
87
88namespace detail {
89
90// These are utilities for IsMember and other transforming objects
91
94
96template <typename T, typename Enable = void> struct element_type {
97 using type = T;
98};
99
100template <typename T> struct element_type<T, typename std::enable_if<is_copyable_ptr<T>::value>::type> {
101 using type = typename std::pointer_traits<T>::element_type;
102};
103
106template <typename T> struct element_value_type {
108};
109
111template <typename T, typename _ = void> struct pair_adaptor : std::false_type {
112 using value_type = typename T::value_type;
113 using first_type = typename std::remove_const<value_type>::type;
114 using second_type = typename std::remove_const<value_type>::type;
115
117 template <typename Q> static auto first(Q &&pair_value) -> decltype(std::forward<Q>(pair_value)) {
118 return std::forward<Q>(pair_value);
119 }
121 template <typename Q> static auto second(Q &&pair_value) -> decltype(std::forward<Q>(pair_value)) {
122 return std::forward<Q>(pair_value);
123 }
124};
125
128template <typename T>
130 T,
131 conditional_t<false, void_t<typename T::value_type::first_type, typename T::value_type::second_type>, void>>
132 : std::true_type {
133 using value_type = typename T::value_type;
134 using first_type = typename std::remove_const<typename value_type::first_type>::type;
135 using second_type = typename std::remove_const<typename value_type::second_type>::type;
136
138 template <typename Q> static auto first(Q &&pair_value) -> decltype(std::get<0>(std::forward<Q>(pair_value))) {
139 return std::get<0>(std::forward<Q>(pair_value));
140 }
142 template <typename Q> static auto second(Q &&pair_value) -> decltype(std::get<1>(std::forward<Q>(pair_value))) {
143 return std::get<1>(std::forward<Q>(pair_value));
144 }
145};
146
147// Warning is suppressed due to "bug" in gcc<5.0 and gcc 7.0 with c++17 enabled that generates a Wnarrowing warning
148// in the unevaluated context even if the function that was using this wasn't used. The standard says narrowing in
149// brace initialization shouldn't be allowed but for backwards compatibility gcc allows it in some contexts. It is a
150// little fuzzy what happens in template constructs and I think that was something GCC took a little while to work out.
151// But regardless some versions of gcc generate a warning when they shouldn't from the following code so that should be
152// suppressed
153#ifdef __GNUC__
154#pragma GCC diagnostic push
155#pragma GCC diagnostic ignored "-Wnarrowing"
156#endif
157// check for constructibility from a specific type and copy assignable used in the parse detection
158template <typename T, typename C> class is_direct_constructible {
159 template <typename TT, typename CC>
160 static auto test(int, std::true_type) -> decltype(
161// NVCC warns about narrowing conversions here
162#ifdef __CUDACC__
163#ifdef __NVCC_DIAG_PRAGMA_SUPPORT__
164#pragma nv_diag_suppress 2361
165#else
166#pragma diag_suppress 2361
167#endif
168#endif
169 TT{std::declval<CC>()}
170#ifdef __CUDACC__
171#ifdef __NVCC_DIAG_PRAGMA_SUPPORT__
172#pragma nv_diag_default 2361
173#else
174#pragma diag_default 2361
175#endif
176#endif
177 ,
178 std::is_move_assignable<TT>());
179
180 template <typename TT, typename CC> static auto test(int, std::false_type) -> std::false_type;
181
182 template <typename, typename> static auto test(...) -> std::false_type;
183
184 public:
185 static constexpr bool value = decltype(test<T, C>(0, typename std::is_constructible<T, C>::type()))::value;
186};
187#ifdef __GNUC__
188#pragma GCC diagnostic pop
189#endif
190
191// Check for output streamability
192// Based on https://stackoverflow.com/questions/22758291/how-can-i-detect-if-a-type-can-be-streamed-to-an-stdostream
193
194template <typename T, typename S = std::ostringstream> class is_ostreamable {
195 template <typename TT, typename SS>
196 static auto test(int) -> decltype(std::declval<SS &>() << std::declval<TT>(), std::true_type());
197
198 template <typename, typename> static auto test(...) -> std::false_type;
199
200 public:
201 static constexpr bool value = decltype(test<T, S>(0))::value;
202};
203
205template <typename T, typename S = std::istringstream> class is_istreamable {
206 template <typename TT, typename SS>
207 static auto test(int) -> decltype(std::declval<SS &>() >> std::declval<TT &>(), std::true_type());
208
209 template <typename, typename> static auto test(...) -> std::false_type;
210
211 public:
212 static constexpr bool value = decltype(test<T, S>(0))::value;
213};
214
216template <typename T> class is_complex {
217 template <typename TT>
218 static auto test(int) -> decltype(std::declval<TT>().real(), std::declval<TT>().imag(), std::true_type());
219
220 template <typename> static auto test(...) -> std::false_type;
221
222 public:
223 static constexpr bool value = decltype(test<T>(0))::value;
224};
225
227template <typename T, enable_if_t<is_istreamable<T>::value, detail::enabler> = detail::dummy>
228bool from_stream(const std::string &istring, T &obj) {
229 std::istringstream is;
230 is.str(istring);
231 is >> obj;
232 return !is.fail() && !is.rdbuf()->in_avail();
233}
234
235template <typename T, enable_if_t<!is_istreamable<T>::value, detail::enabler> = detail::dummy>
236bool from_stream(const std::string & /*istring*/, T & /*obj*/) {
237 return false;
238}
239
240// check to see if an object is a mutable container (fail by default)
241template <typename T, typename _ = void> struct is_mutable_container : std::false_type {};
242
246template <typename T>
248 T,
249 conditional_t<false,
250 void_t<typename T::value_type,
251 decltype(std::declval<T>().end()),
252 decltype(std::declval<T>().clear()),
253 decltype(std::declval<T>().insert(std::declval<decltype(std::declval<T>().end())>(),
254 std::declval<const typename T::value_type &>()))>,
255 void>> : public conditional_t<std::is_constructible<T, std::string>::value ||
256 std::is_constructible<T, std::wstring>::value,
257 std::false_type,
258 std::true_type> {};
259
260// check to see if an object is a mutable container (fail by default)
261template <typename T, typename _ = void> struct is_readable_container : std::false_type {};
262
266template <typename T>
268 T,
269 conditional_t<false, void_t<decltype(std::declval<T>().end()), decltype(std::declval<T>().begin())>, void>>
270 : public std::true_type {};
271
272// check to see if an object is a wrapper (fail by default)
273template <typename T, typename _ = void> struct is_wrapper : std::false_type {};
274
275// check if an object is a wrapper (it has a value_type defined)
276template <typename T>
277struct is_wrapper<T, conditional_t<false, void_t<typename T::value_type>, void>> : public std::true_type {};
278
279// Check for tuple like types, as in classes with a tuple_size type trait
280template <typename S> class is_tuple_like {
281 template <typename SS>
282 // static auto test(int)
283 // -> decltype(std::conditional<(std::tuple_size<SS>::value > 0), std::true_type, std::false_type>::type());
284 static auto test(int) -> decltype(std::tuple_size<typename std::decay<SS>::type>::value, std::true_type{});
285 template <typename> static auto test(...) -> std::false_type;
286
287 public:
288 static constexpr bool value = decltype(test<S>(0))::value;
289};
290
292template <typename T, enable_if_t<std::is_convertible<T, std::string>::value, detail::enabler> = detail::dummy>
293auto to_string(T &&value) -> decltype(std::forward<T>(value)) {
294 return std::forward<T>(value);
295}
296
298template <typename T,
299 enable_if_t<std::is_constructible<std::string, T>::value && !std::is_convertible<T, std::string>::value,
301std::string to_string(const T &value) {
302 return std::string(value); // NOLINT(google-readability-casting)
303}
304
306template <typename T,
307 enable_if_t<!std::is_convertible<std::string, T>::value && !std::is_constructible<std::string, T>::value &&
310std::string to_string(T &&value) {
311 std::stringstream stream;
312 stream << value;
313 return stream.str();
314}
315
317template <typename T,
319 !is_readable_container<typename std::remove_const<T>::type>::value,
321std::string to_string(T &&) {
322 return {};
323}
324
326template <typename T,
327 enable_if_t<!std::is_constructible<std::string, T>::value && !is_ostreamable<T>::value &&
328 is_readable_container<T>::value,
330std::string to_string(T &&variable) {
331 auto cval = variable.begin();
332 auto end = variable.end();
333 if(cval == end) {
334 return {"{}"};
335 }
336 std::vector<std::string> defaults;
337 while(cval != end) {
338 defaults.emplace_back(CLI::detail::to_string(*cval));
339 ++cval;
340 }
341 return {"[" + detail::join(defaults) + "]"};
342}
343
345template <typename T1,
346 typename T2,
347 typename T,
348 enable_if_t<std::is_same<T1, T2>::value, detail::enabler> = detail::dummy>
349auto checked_to_string(T &&value) -> decltype(to_string(std::forward<T>(value))) {
350 return to_string(std::forward<T>(value));
351}
352
354template <typename T1,
355 typename T2,
356 typename T,
358std::string checked_to_string(T &&) {
359 return std::string{};
360}
362template <typename T, enable_if_t<std::is_arithmetic<T>::value, detail::enabler> = detail::dummy>
363std::string value_string(const T &value) {
364 return std::to_string(value);
365}
367template <typename T, enable_if_t<std::is_enum<T>::value, detail::enabler> = detail::dummy>
368std::string value_string(const T &value) {
369 return std::to_string(static_cast<typename std::underlying_type<T>::type>(value));
370}
372template <typename T,
373 enable_if_t<!std::is_enum<T>::value && !std::is_arithmetic<T>::value, detail::enabler> = detail::dummy>
374auto value_string(const T &value) -> decltype(to_string(value)) {
375 return to_string(value);
376}
377
379template <typename T, typename def, typename Enable = void> struct wrapped_type {
380 using type = def;
381};
382
384template <typename T, typename def> struct wrapped_type<T, def, typename std::enable_if<is_wrapper<T>::value>::type> {
385 using type = typename T::value_type;
386};
387
389template <typename T, typename Enable = void> struct type_count_base {
390 static const int value{0};
391};
392
394template <typename T>
396 typename std::enable_if<!is_tuple_like<T>::value && !is_mutable_container<T>::value &&
397 !std::is_void<T>::value>::type> {
398 static constexpr int value{1};
399};
400
402template <typename T>
403struct type_count_base<T, typename std::enable_if<is_tuple_like<T>::value && !is_mutable_container<T>::value>::type> {
404 static constexpr int value{std::tuple_size<T>::value};
405};
406
408template <typename T> struct type_count_base<T, typename std::enable_if<is_mutable_container<T>::value>::type> {
410};
411
413
415template <typename T> struct subtype_count;
416
418template <typename T> struct subtype_count_min;
419
421template <typename T, typename Enable = void> struct type_count {
422 static const int value{0};
423};
424
426template <typename T>
427struct type_count<T,
428 typename std::enable_if<!is_wrapper<T>::value && !is_tuple_like<T>::value && !is_complex<T>::value &&
429 !std::is_void<T>::value>::type> {
430 static constexpr int value{1};
431};
432
434template <typename T> struct type_count<T, typename std::enable_if<is_complex<T>::value>::type> {
435 static constexpr int value{2};
436};
437
439template <typename T> struct type_count<T, typename std::enable_if<is_mutable_container<T>::value>::type> {
441};
442
444template <typename T>
445struct type_count<T,
446 typename std::enable_if<is_wrapper<T>::value && !is_complex<T>::value && !is_tuple_like<T>::value &&
447 !is_mutable_container<T>::value>::type> {
449};
450
452template <typename T, std::size_t I>
453constexpr typename std::enable_if<I == type_count_base<T>::value, int>::type tuple_type_size() {
454 return 0;
455}
456
458template <typename T, std::size_t I>
459 constexpr typename std::enable_if < I<type_count_base<T>::value, int>::type tuple_type_size() {
460 return subtype_count<typename std::tuple_element<I, T>::type>::value + tuple_type_size<T, I + 1>();
461}
462
464template <typename T> struct type_count<T, typename std::enable_if<is_tuple_like<T>::value>::type> {
465 static constexpr int value{tuple_type_size<T, 0>()};
466};
467
469template <typename T> struct subtype_count {
470 static constexpr int value{is_mutable_container<T>::value ? expected_max_vector_size : type_count<T>::value};
471};
472
474template <typename T, typename Enable = void> struct type_count_min {
475 static const int value{0};
476};
477
479template <typename T>
480struct type_count_min<
481 T,
482 typename std::enable_if<!is_mutable_container<T>::value && !is_tuple_like<T>::value && !is_wrapper<T>::value &&
483 !is_complex<T>::value && !std::is_void<T>::value>::type> {
484 static constexpr int value{type_count<T>::value};
485};
486
488template <typename T> struct type_count_min<T, typename std::enable_if<is_complex<T>::value>::type> {
489 static constexpr int value{1};
490};
491
493template <typename T>
494struct type_count_min<
495 T,
496 typename std::enable_if<is_wrapper<T>::value && !is_complex<T>::value && !is_tuple_like<T>::value>::type> {
497 static constexpr int value{subtype_count_min<typename T::value_type>::value};
498};
499
501template <typename T, std::size_t I>
502constexpr typename std::enable_if<I == type_count_base<T>::value, int>::type tuple_type_size_min() {
503 return 0;
504}
505
507template <typename T, std::size_t I>
508 constexpr typename std::enable_if < I<type_count_base<T>::value, int>::type tuple_type_size_min() {
509 return subtype_count_min<typename std::tuple_element<I, T>::type>::value + tuple_type_size_min<T, I + 1>();
510}
511
513template <typename T> struct type_count_min<T, typename std::enable_if<is_tuple_like<T>::value>::type> {
514 static constexpr int value{tuple_type_size_min<T, 0>()};
515};
516
518template <typename T> struct subtype_count_min {
519 static constexpr int value{is_mutable_container<T>::value
521 : type_count_min<T>::value};
522};
523
525template <typename T, typename Enable = void> struct expected_count {
526 static const int value{0};
527};
528
530template <typename T>
531struct expected_count<T,
532 typename std::enable_if<!is_mutable_container<T>::value && !is_wrapper<T>::value &&
533 !std::is_void<T>::value>::type> {
534 static constexpr int value{1};
535};
537template <typename T> struct expected_count<T, typename std::enable_if<is_mutable_container<T>::value>::type> {
538 static constexpr int value{expected_max_vector_size};
539};
540
542template <typename T>
543struct expected_count<T, typename std::enable_if<!is_mutable_container<T>::value && is_wrapper<T>::value>::type> {
544 static constexpr int value{expected_count<typename T::value_type>::value};
545};
546
547// Enumeration of the different supported categorizations of objects
548enum class object_category : int {
549 char_value = 1,
550 integral_value = 2,
551 unsigned_integral = 4,
552 enumeration = 6,
553 boolean_value = 8,
554 floating_point = 10,
555 number_constructible = 12,
556 double_constructible = 14,
557 integer_constructible = 16,
558 // string like types
559 string_assignable = 23,
560 string_constructible = 24,
561 wstring_assignable = 25,
562 wstring_constructible = 26,
563 other = 45,
564 // special wrapper or container types
565 wrapper_value = 50,
566 complex_number = 60,
567 tuple_value = 70,
568 container_value = 80,
569
570};
571
573
575template <typename T, typename Enable = void> struct classify_object {
576 static constexpr object_category value{object_category::other};
577};
578
580template <typename T>
581struct classify_object<
582 T,
583 typename std::enable_if<std::is_integral<T>::value && !std::is_same<T, char>::value && std::is_signed<T>::value &&
584 !is_bool<T>::value && !std::is_enum<T>::value>::type> {
585 static constexpr object_category value{object_category::integral_value};
586};
587
589template <typename T>
590struct classify_object<T,
591 typename std::enable_if<std::is_integral<T>::value && std::is_unsigned<T>::value &&
592 !std::is_same<T, char>::value && !is_bool<T>::value>::type> {
593 static constexpr object_category value{object_category::unsigned_integral};
594};
595
597template <typename T>
598struct classify_object<T, typename std::enable_if<std::is_same<T, char>::value && !std::is_enum<T>::value>::type> {
599 static constexpr object_category value{object_category::char_value};
600};
601
603template <typename T> struct classify_object<T, typename std::enable_if<is_bool<T>::value>::type> {
604 static constexpr object_category value{object_category::boolean_value};
605};
606
608template <typename T> struct classify_object<T, typename std::enable_if<std::is_floating_point<T>::value>::type> {
609 static constexpr object_category value{object_category::floating_point};
610};
611#if defined _MSC_VER
612// in MSVC wstring should take precedence if available this isn't as useful on other compilers due to the broader use of
613// utf-8 encoding
614#define WIDE_STRING_CHECK \
615 !std::is_assignable<T &, std::wstring>::value && !std::is_constructible<T, std::wstring>::value
616#define STRING_CHECK true
617#else
618#define WIDE_STRING_CHECK true
619#define STRING_CHECK !std::is_assignable<T &, std::string>::value && !std::is_constructible<T, std::string>::value
620#endif
621
623template <typename T>
624struct classify_object<
625 T,
626 typename std::enable_if<!std::is_floating_point<T>::value && !std::is_integral<T>::value && WIDE_STRING_CHECK &&
627 std::is_assignable<T &, std::string>::value>::type> {
628 static constexpr object_category value{object_category::string_assignable};
629};
630
632template <typename T>
633struct classify_object<
634 T,
635 typename std::enable_if<!std::is_floating_point<T>::value && !std::is_integral<T>::value &&
636 !std::is_assignable<T &, std::string>::value && (type_count<T>::value == 1) &&
637 WIDE_STRING_CHECK && std::is_constructible<T, std::string>::value>::type> {
638 static constexpr object_category value{object_category::string_constructible};
639};
640
642template <typename T>
643struct classify_object<T,
644 typename std::enable_if<!std::is_floating_point<T>::value && !std::is_integral<T>::value &&
645 STRING_CHECK && std::is_assignable<T &, std::wstring>::value>::type> {
646 static constexpr object_category value{object_category::wstring_assignable};
647};
648
649template <typename T>
650struct classify_object<
651 T,
652 typename std::enable_if<!std::is_floating_point<T>::value && !std::is_integral<T>::value &&
653 !std::is_assignable<T &, std::wstring>::value && (type_count<T>::value == 1) &&
654 STRING_CHECK && std::is_constructible<T, std::wstring>::value>::type> {
655 static constexpr object_category value{object_category::wstring_constructible};
656};
657
659template <typename T> struct classify_object<T, typename std::enable_if<std::is_enum<T>::value>::type> {
660 static constexpr object_category value{object_category::enumeration};
661};
662
663template <typename T> struct classify_object<T, typename std::enable_if<is_complex<T>::value>::type> {
664 static constexpr object_category value{object_category::complex_number};
665};
666
669template <typename T> struct uncommon_type {
670 using type = typename std::conditional<
671 !std::is_floating_point<T>::value && !std::is_integral<T>::value &&
672 !std::is_assignable<T &, std::string>::value && !std::is_constructible<T, std::string>::value &&
673 !std::is_assignable<T &, std::wstring>::value && !std::is_constructible<T, std::wstring>::value &&
674 !is_complex<T>::value && !is_mutable_container<T>::value && !std::is_enum<T>::value,
675 std::true_type,
676 std::false_type>::type;
677 static constexpr bool value = type::value;
678};
679
681template <typename T>
682struct classify_object<T,
683 typename std::enable_if<(!is_mutable_container<T>::value && is_wrapper<T>::value &&
684 !is_tuple_like<T>::value && uncommon_type<T>::value)>::type> {
685 static constexpr object_category value{object_category::wrapper_value};
686};
687
689template <typename T>
690struct classify_object<T,
691 typename std::enable_if<uncommon_type<T>::value && type_count<T>::value == 1 &&
692 !is_wrapper<T>::value && is_direct_constructible<T, double>::value &&
693 is_direct_constructible<T, int>::value>::type> {
694 static constexpr object_category value{object_category::number_constructible};
695};
696
698template <typename T>
699struct classify_object<T,
700 typename std::enable_if<uncommon_type<T>::value && type_count<T>::value == 1 &&
701 !is_wrapper<T>::value && !is_direct_constructible<T, double>::value &&
702 is_direct_constructible<T, int>::value>::type> {
703 static constexpr object_category value{object_category::integer_constructible};
704};
705
707template <typename T>
708struct classify_object<T,
709 typename std::enable_if<uncommon_type<T>::value && type_count<T>::value == 1 &&
710 !is_wrapper<T>::value && is_direct_constructible<T, double>::value &&
711 !is_direct_constructible<T, int>::value>::type> {
712 static constexpr object_category value{object_category::double_constructible};
713};
714
716template <typename T>
717struct classify_object<
718 T,
719 typename std::enable_if<is_tuple_like<T>::value &&
720 ((type_count<T>::value >= 2 && !is_wrapper<T>::value) ||
721 (uncommon_type<T>::value && !is_direct_constructible<T, double>::value &&
722 !is_direct_constructible<T, int>::value) ||
723 (uncommon_type<T>::value && type_count<T>::value >= 2))>::type> {
724 static constexpr object_category value{object_category::tuple_value};
725 // the condition on this class requires it be like a tuple, but on some compilers (like Xcode) tuples can be
726 // constructed from just the first element so tuples of <string, int,int> can be constructed from a string, which
727 // could lead to issues so there are two variants of the condition, the first isolates things with a type size >=2
728 // mainly to get tuples on Xcode with the exception of wrappers, the second is the main one and just separating out
729 // those cases that are caught by other object classifications
730};
731
733template <typename T> struct classify_object<T, typename std::enable_if<is_mutable_container<T>::value>::type> {
734 static constexpr object_category value{object_category::container_value};
735};
736
737// Type name print
738
742
743template <typename T,
744 enable_if_t<classify_object<T>::value == object_category::char_value, detail::enabler> = detail::dummy>
745constexpr const char *type_name() {
746 return "CHAR";
747}
748
749template <typename T,
750 enable_if_t<classify_object<T>::value == object_category::integral_value ||
751 classify_object<T>::value == object_category::integer_constructible,
753constexpr const char *type_name() {
754 return "INT";
755}
756
757template <typename T,
758 enable_if_t<classify_object<T>::value == object_category::unsigned_integral, detail::enabler> = detail::dummy>
759constexpr const char *type_name() {
760 return "UINT";
761}
762
763template <typename T,
764 enable_if_t<classify_object<T>::value == object_category::floating_point ||
765 classify_object<T>::value == object_category::number_constructible ||
766 classify_object<T>::value == object_category::double_constructible,
768constexpr const char *type_name() {
769 return "FLOAT";
770}
771
773template <typename T,
774 enable_if_t<classify_object<T>::value == object_category::enumeration, detail::enabler> = detail::dummy>
775constexpr const char *type_name() {
776 return "ENUM";
777}
778
780template <typename T,
781 enable_if_t<classify_object<T>::value == object_category::boolean_value, detail::enabler> = detail::dummy>
782constexpr const char *type_name() {
783 return "BOOLEAN";
784}
785
787template <typename T,
788 enable_if_t<classify_object<T>::value == object_category::complex_number, detail::enabler> = detail::dummy>
789constexpr const char *type_name() {
790 return "COMPLEX";
791}
792
794template <typename T,
795 enable_if_t<classify_object<T>::value >= object_category::string_assignable &&
796 classify_object<T>::value <= object_category::other,
798constexpr const char *type_name() {
799 return "TEXT";
800}
802template <typename T,
803 enable_if_t<classify_object<T>::value == object_category::tuple_value && type_count_base<T>::value >= 2,
805std::string type_name(); // forward declaration
806
808template <typename T,
809 enable_if_t<classify_object<T>::value == object_category::container_value ||
810 classify_object<T>::value == object_category::wrapper_value,
812std::string type_name(); // forward declaration
813
815template <typename T,
816 enable_if_t<classify_object<T>::value == object_category::tuple_value && type_count_base<T>::value == 1,
818inline std::string type_name() {
819 return type_name<typename std::decay<typename std::tuple_element<0, T>::type>::type>();
820}
821
823template <typename T, std::size_t I>
824inline typename std::enable_if<I == type_count_base<T>::value, std::string>::type tuple_name() {
825 return std::string{};
826}
827
829template <typename T, std::size_t I>
830inline typename std::enable_if<(I < type_count_base<T>::value), std::string>::type tuple_name() {
831 auto str = std::string{type_name<typename std::decay<typename std::tuple_element<I, T>::type>::type>()} + ',' +
832 tuple_name<T, I + 1>();
833 if(str.back() == ',')
834 str.pop_back();
835 return str;
836}
837
839template <typename T,
840 enable_if_t<classify_object<T>::value == object_category::tuple_value && type_count_base<T>::value >= 2,
842inline std::string type_name() {
843 auto tname = std::string(1, '[') + tuple_name<T, 0>();
844 tname.push_back(']');
845 return tname;
846}
847
849template <typename T,
850 enable_if_t<classify_object<T>::value == object_category::container_value ||
851 classify_object<T>::value == object_category::wrapper_value,
853inline std::string type_name() {
854 return type_name<typename T::value_type>();
855}
856
857// Lexical cast
858
860template <typename T, enable_if_t<std::is_unsigned<T>::value, detail::enabler> = detail::dummy>
861bool integral_conversion(const std::string &input, T &output) noexcept {
862 if(input.empty() || input.front() == '-') {
863 return false;
864 }
865 char *val{nullptr};
866 errno = 0;
867 std::uint64_t output_ll = std::strtoull(input.c_str(), &val, 0);
868 if(errno == ERANGE) {
869 return false;
870 }
871 output = static_cast<T>(output_ll);
872 if(val == (input.c_str() + input.size()) && static_cast<std::uint64_t>(output) == output_ll) {
873 return true;
874 }
875 val = nullptr;
876 std::int64_t output_sll = std::strtoll(input.c_str(), &val, 0);
877 if(val == (input.c_str() + input.size())) {
878 output = (output_sll < 0) ? static_cast<T>(0) : static_cast<T>(output_sll);
879 return (static_cast<std::int64_t>(output) == output_sll);
880 }
881 // remove separators
882 if(input.find_first_of("_'") != std::string::npos) {
883 std::string nstring = input;
884 nstring.erase(std::remove(nstring.begin(), nstring.end(), '_'), nstring.end());
885 nstring.erase(std::remove(nstring.begin(), nstring.end(), '\''), nstring.end());
886 return integral_conversion(nstring, output);
887 }
888 if(input.compare(0, 2, "0o") == 0) {
889 val = nullptr;
890 errno = 0;
891 output_ll = std::strtoull(input.c_str() + 2, &val, 8);
892 if(errno == ERANGE) {
893 return false;
894 }
895 output = static_cast<T>(output_ll);
896 return (val == (input.c_str() + input.size()) && static_cast<std::uint64_t>(output) == output_ll);
897 }
898 if(input.compare(0, 2, "0b") == 0) {
899 val = nullptr;
900 errno = 0;
901 output_ll = std::strtoull(input.c_str() + 2, &val, 2);
902 if(errno == ERANGE) {
903 return false;
904 }
905 output = static_cast<T>(output_ll);
906 return (val == (input.c_str() + input.size()) && static_cast<std::uint64_t>(output) == output_ll);
907 }
908 return false;
909}
910
912template <typename T, enable_if_t<std::is_signed<T>::value, detail::enabler> = detail::dummy>
913bool integral_conversion(const std::string &input, T &output) noexcept {
914 if(input.empty()) {
915 return false;
916 }
917 char *val = nullptr;
918 errno = 0;
919 std::int64_t output_ll = std::strtoll(input.c_str(), &val, 0);
920 if(errno == ERANGE) {
921 return false;
922 }
923 output = static_cast<T>(output_ll);
924 if(val == (input.c_str() + input.size()) && static_cast<std::int64_t>(output) == output_ll) {
925 return true;
926 }
927 if(input == "true") {
928 // this is to deal with a few oddities with flags and wrapper int types
929 output = static_cast<T>(1);
930 return true;
931 }
932 // remove separators
933 if(input.find_first_of("_'") != std::string::npos) {
934 std::string nstring = input;
935 nstring.erase(std::remove(nstring.begin(), nstring.end(), '_'), nstring.end());
936 nstring.erase(std::remove(nstring.begin(), nstring.end(), '\''), nstring.end());
937 return integral_conversion(nstring, output);
938 }
939 if(input.compare(0, 2, "0o") == 0) {
940 val = nullptr;
941 errno = 0;
942 output_ll = std::strtoll(input.c_str() + 2, &val, 8);
943 if(errno == ERANGE) {
944 return false;
945 }
946 output = static_cast<T>(output_ll);
947 return (val == (input.c_str() + input.size()) && static_cast<std::int64_t>(output) == output_ll);
948 }
949 if(input.compare(0, 2, "0b") == 0) {
950 val = nullptr;
951 errno = 0;
952 output_ll = std::strtoll(input.c_str() + 2, &val, 2);
953 if(errno == ERANGE) {
954 return false;
955 }
956 output = static_cast<T>(output_ll);
957 return (val == (input.c_str() + input.size()) && static_cast<std::int64_t>(output) == output_ll);
958 }
959 return false;
960}
961
963inline std::int64_t to_flag_value(std::string val) noexcept {
964 static const std::string trueString("true");
965 static const std::string falseString("false");
966 if(val == trueString) {
967 return 1;
968 }
969 if(val == falseString) {
970 return -1;
971 }
972 val = detail::to_lower(val);
973 std::int64_t ret = 0;
974 if(val.size() == 1) {
975 if(val[0] >= '1' && val[0] <= '9') {
976 return (static_cast<std::int64_t>(val[0]) - '0');
977 }
978 switch(val[0]) {
979 case '0':
980 case 'f':
981 case 'n':
982 case '-':
983 ret = -1;
984 break;
985 case 't':
986 case 'y':
987 case '+':
988 ret = 1;
989 break;
990 default:
991 errno = EINVAL;
992 return -1;
993 }
994 return ret;
995 }
996 if(val == trueString || val == "on" || val == "yes" || val == "enable") {
997 ret = 1;
998 } else if(val == falseString || val == "off" || val == "no" || val == "disable") {
999 ret = -1;
1000 } else {
1001 char *loc_ptr{nullptr};
1002 ret = std::strtoll(val.c_str(), &loc_ptr, 0);
1003 if(loc_ptr != (val.c_str() + val.size()) && errno == 0) {
1004 errno = EINVAL;
1005 }
1006 }
1007 return ret;
1008}
1009
1011template <typename T,
1012 enable_if_t<classify_object<T>::value == object_category::integral_value ||
1013 classify_object<T>::value == object_category::unsigned_integral,
1015bool lexical_cast(const std::string &input, T &output) {
1016 return integral_conversion(input, output);
1017}
1018
1020template <typename T,
1021 enable_if_t<classify_object<T>::value == object_category::char_value, detail::enabler> = detail::dummy>
1022bool lexical_cast(const std::string &input, T &output) {
1023 if(input.size() == 1) {
1024 output = static_cast<T>(input[0]);
1025 return true;
1026 }
1027 return integral_conversion(input, output);
1028}
1029
1031template <typename T,
1032 enable_if_t<classify_object<T>::value == object_category::boolean_value, detail::enabler> = detail::dummy>
1033bool lexical_cast(const std::string &input, T &output) {
1034 errno = 0;
1035 auto out = to_flag_value(input);
1036 if(errno == 0) {
1037 output = (out > 0);
1038 } else if(errno == ERANGE) {
1039 output = (input[0] != '-');
1040 } else {
1041 return false;
1042 }
1043 return true;
1044}
1045
1047template <typename T,
1048 enable_if_t<classify_object<T>::value == object_category::floating_point, detail::enabler> = detail::dummy>
1049bool lexical_cast(const std::string &input, T &output) {
1050 if(input.empty()) {
1051 return false;
1052 }
1053 char *val = nullptr;
1054 auto output_ld = std::strtold(input.c_str(), &val);
1055 output = static_cast<T>(output_ld);
1056 if(val == (input.c_str() + input.size())) {
1057 return true;
1058 }
1059 // remove separators
1060 if(input.find_first_of("_'") != std::string::npos) {
1061 std::string nstring = input;
1062 nstring.erase(std::remove(nstring.begin(), nstring.end(), '_'), nstring.end());
1063 nstring.erase(std::remove(nstring.begin(), nstring.end(), '\''), nstring.end());
1064 return lexical_cast(nstring, output);
1065 }
1066 return false;
1067}
1068
1070template <typename T,
1071 enable_if_t<classify_object<T>::value == object_category::complex_number, detail::enabler> = detail::dummy>
1072bool lexical_cast(const std::string &input, T &output) {
1073 using XC = typename wrapped_type<T, double>::type;
1074 XC x{0.0}, y{0.0};
1075 auto str1 = input;
1076 bool worked = false;
1077 auto nloc = str1.find_last_of("+-");
1078 if(nloc != std::string::npos && nloc > 0) {
1079 worked = lexical_cast(str1.substr(0, nloc), x);
1080 str1 = str1.substr(nloc);
1081 if(str1.back() == 'i' || str1.back() == 'j')
1082 str1.pop_back();
1083 worked = worked && lexical_cast(str1, y);
1084 } else {
1085 if(str1.back() == 'i' || str1.back() == 'j') {
1086 str1.pop_back();
1087 worked = lexical_cast(str1, y);
1088 x = XC{0};
1089 } else {
1090 worked = lexical_cast(str1, x);
1091 y = XC{0};
1092 }
1093 }
1094 if(worked) {
1095 output = T{x, y};
1096 return worked;
1097 }
1098 return from_stream(input, output);
1099}
1100
1102template <typename T,
1103 enable_if_t<classify_object<T>::value == object_category::string_assignable, detail::enabler> = detail::dummy>
1104bool lexical_cast(const std::string &input, T &output) {
1105 output = input;
1106 return true;
1107}
1108
1110template <
1111 typename T,
1112 enable_if_t<classify_object<T>::value == object_category::string_constructible, detail::enabler> = detail::dummy>
1113bool lexical_cast(const std::string &input, T &output) {
1114 output = T(input);
1115 return true;
1116}
1117
1119template <
1120 typename T,
1121 enable_if_t<classify_object<T>::value == object_category::wstring_assignable, detail::enabler> = detail::dummy>
1122bool lexical_cast(const std::string &input, T &output) {
1123 output = widen(input);
1124 return true;
1125}
1126
1127template <
1128 typename T,
1129 enable_if_t<classify_object<T>::value == object_category::wstring_constructible, detail::enabler> = detail::dummy>
1130bool lexical_cast(const std::string &input, T &output) {
1131 output = T{widen(input)};
1132 return true;
1133}
1134
1136template <typename T,
1137 enable_if_t<classify_object<T>::value == object_category::enumeration, detail::enabler> = detail::dummy>
1138bool lexical_cast(const std::string &input, T &output) {
1139 typename std::underlying_type<T>::type val;
1140 if(!integral_conversion(input, val)) {
1141 return false;
1142 }
1143 output = static_cast<T>(val);
1144 return true;
1145}
1146
1148template <typename T,
1149 enable_if_t<classify_object<T>::value == object_category::wrapper_value &&
1150 std::is_assignable<T &, typename T::value_type>::value,
1152bool lexical_cast(const std::string &input, T &output) {
1153 typename T::value_type val;
1154 if(lexical_cast(input, val)) {
1155 output = val;
1156 return true;
1157 }
1158 return from_stream(input, output);
1159}
1160
1161template <typename T,
1162 enable_if_t<classify_object<T>::value == object_category::wrapper_value &&
1163 !std::is_assignable<T &, typename T::value_type>::value && std::is_assignable<T &, T>::value,
1165bool lexical_cast(const std::string &input, T &output) {
1166 typename T::value_type val;
1167 if(lexical_cast(input, val)) {
1168 output = T{val};
1169 return true;
1170 }
1171 return from_stream(input, output);
1172}
1173
1175template <
1176 typename T,
1177 enable_if_t<classify_object<T>::value == object_category::number_constructible, detail::enabler> = detail::dummy>
1178bool lexical_cast(const std::string &input, T &output) {
1179 int val = 0;
1180 if(integral_conversion(input, val)) {
1181 output = T(val);
1182 return true;
1183 }
1184
1185 double dval = 0.0;
1186 if(lexical_cast(input, dval)) {
1187 output = T{dval};
1188 return true;
1189 }
1190
1191 return from_stream(input, output);
1192}
1193
1195template <
1196 typename T,
1197 enable_if_t<classify_object<T>::value == object_category::integer_constructible, detail::enabler> = detail::dummy>
1198bool lexical_cast(const std::string &input, T &output) {
1199 int val = 0;
1200 if(integral_conversion(input, val)) {
1201 output = T(val);
1202 return true;
1203 }
1204 return from_stream(input, output);
1205}
1206
1208template <
1209 typename T,
1210 enable_if_t<classify_object<T>::value == object_category::double_constructible, detail::enabler> = detail::dummy>
1211bool lexical_cast(const std::string &input, T &output) {
1212 double val = 0.0;
1213 if(lexical_cast(input, val)) {
1214 output = T{val};
1215 return true;
1216 }
1217 return from_stream(input, output);
1218}
1219
1221template <typename T,
1222 enable_if_t<classify_object<T>::value == object_category::other && std::is_assignable<T &, int>::value,
1224bool lexical_cast(const std::string &input, T &output) {
1225 int val = 0;
1226 if(integral_conversion(input, val)) {
1227#ifdef _MSC_VER
1228#pragma warning(push)
1229#pragma warning(disable : 4800)
1230#endif
1231 // with Atomic<XX> this could produce a warning due to the conversion but if atomic gets here it is an old style
1232 // so will most likely still work
1233 output = val;
1234#ifdef _MSC_VER
1235#pragma warning(pop)
1236#endif
1237 return true;
1238 }
1239 // LCOV_EXCL_START
1240 // This version of cast is only used for odd cases in an older compilers the fail over
1241 // from_stream is tested elsewhere an not relevant for coverage here
1242 return from_stream(input, output);
1243 // LCOV_EXCL_STOP
1244}
1245
1247template <typename T,
1248 enable_if_t<classify_object<T>::value == object_category::other && !std::is_assignable<T &, int>::value,
1250bool lexical_cast(const std::string &input, T &output) {
1251 static_assert(is_istreamable<T>::value,
1252 "option object type must have a lexical cast overload or streaming input operator(>>) defined, if it "
1253 "is convertible from another type use the add_option<T, XC>(...) with XC being the known type");
1254 return from_stream(input, output);
1255}
1256
1259template <typename AssignTo,
1260 typename ConvertTo,
1261 enable_if_t<std::is_same<AssignTo, ConvertTo>::value &&
1262 (classify_object<AssignTo>::value == object_category::string_assignable ||
1263 classify_object<AssignTo>::value == object_category::string_constructible ||
1264 classify_object<AssignTo>::value == object_category::wstring_assignable ||
1265 classify_object<AssignTo>::value == object_category::wstring_constructible),
1267bool lexical_assign(const std::string &input, AssignTo &output) {
1268 return lexical_cast(input, output);
1269}
1270
1272template <typename AssignTo,
1273 typename ConvertTo,
1274 enable_if_t<std::is_same<AssignTo, ConvertTo>::value && std::is_assignable<AssignTo &, AssignTo>::value &&
1275 classify_object<AssignTo>::value != object_category::string_assignable &&
1276 classify_object<AssignTo>::value != object_category::string_constructible &&
1277 classify_object<AssignTo>::value != object_category::wstring_assignable &&
1278 classify_object<AssignTo>::value != object_category::wstring_constructible,
1280bool lexical_assign(const std::string &input, AssignTo &output) {
1281 if(input.empty()) {
1282 output = AssignTo{};
1283 return true;
1284 }
1285
1286 return lexical_cast(input, output);
1287}
1288
1290template <typename AssignTo,
1291 typename ConvertTo,
1292 enable_if_t<std::is_same<AssignTo, ConvertTo>::value && !std::is_assignable<AssignTo &, AssignTo>::value &&
1293 classify_object<AssignTo>::value == object_category::wrapper_value,
1295bool lexical_assign(const std::string &input, AssignTo &output) {
1296 if(input.empty()) {
1297 typename AssignTo::value_type emptyVal{};
1298 output = emptyVal;
1299 return true;
1300 }
1301 return lexical_cast(input, output);
1302}
1303
1306template <typename AssignTo,
1307 typename ConvertTo,
1308 enable_if_t<std::is_same<AssignTo, ConvertTo>::value && !std::is_assignable<AssignTo &, AssignTo>::value &&
1309 classify_object<AssignTo>::value != object_category::wrapper_value &&
1310 std::is_assignable<AssignTo &, int>::value,
1312bool lexical_assign(const std::string &input, AssignTo &output) {
1313 if(input.empty()) {
1314 output = 0;
1315 return true;
1316 }
1317 int val{0};
1318 if(lexical_cast(input, val)) {
1319#if defined(__clang__)
1320/* on some older clang compilers */
1321#pragma clang diagnostic push
1322#pragma clang diagnostic ignored "-Wsign-conversion"
1323#endif
1324 output = val;
1325#if defined(__clang__)
1326#pragma clang diagnostic pop
1327#endif
1328 return true;
1329 }
1330 return false;
1331}
1332
1334template <typename AssignTo,
1335 typename ConvertTo,
1336 enable_if_t<!std::is_same<AssignTo, ConvertTo>::value && std::is_assignable<AssignTo &, ConvertTo &>::value,
1338bool lexical_assign(const std::string &input, AssignTo &output) {
1339 ConvertTo val{};
1340 bool parse_result = (!input.empty()) ? lexical_cast(input, val) : true;
1341 if(parse_result) {
1342 output = val;
1343 }
1344 return parse_result;
1345}
1346
1348template <
1349 typename AssignTo,
1350 typename ConvertTo,
1351 enable_if_t<!std::is_same<AssignTo, ConvertTo>::value && !std::is_assignable<AssignTo &, ConvertTo &>::value &&
1352 std::is_move_assignable<AssignTo>::value,
1354bool lexical_assign(const std::string &input, AssignTo &output) {
1355 ConvertTo val{};
1356 bool parse_result = input.empty() ? true : lexical_cast(input, val);
1357 if(parse_result) {
1358 output = AssignTo(val); // use () form of constructor to allow some implicit conversions
1359 }
1360 return parse_result;
1361}
1362
1364template <typename AssignTo,
1365 typename ConvertTo,
1366 enable_if_t<classify_object<ConvertTo>::value <= object_category::other &&
1367 classify_object<AssignTo>::value <= object_category::wrapper_value,
1369bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1370 return lexical_assign<AssignTo, ConvertTo>(strings[0], output);
1371}
1372
1375template <typename AssignTo,
1376 typename ConvertTo,
1377 enable_if_t<(type_count<AssignTo>::value <= 2) && expected_count<AssignTo>::value == 1 &&
1380bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1381 // the remove const is to handle pair types coming from a container
1382 using FirstType = typename std::remove_const<typename std::tuple_element<0, ConvertTo>::type>::type;
1383 using SecondType = typename std::tuple_element<1, ConvertTo>::type;
1384 FirstType v1;
1385 SecondType v2;
1386 bool retval = lexical_assign<FirstType, FirstType>(strings[0], v1);
1387 retval = retval && lexical_assign<SecondType, SecondType>((strings.size() > 1) ? strings[1] : std::string{}, v2);
1388 if(retval) {
1389 output = AssignTo{v1, v2};
1390 }
1391 return retval;
1392}
1393
1395template <class AssignTo,
1396 class ConvertTo,
1397 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1400bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1401 output.erase(output.begin(), output.end());
1402 if(strings.empty()) {
1403 return true;
1404 }
1405 if(strings.size() == 1 && strings[0] == "{}") {
1406 return true;
1407 }
1408 bool skip_remaining = false;
1409 if(strings.size() == 2 && strings[0] == "{}" && is_separator(strings[1])) {
1410 skip_remaining = true;
1411 }
1412 for(const auto &elem : strings) {
1413 typename AssignTo::value_type out;
1414 bool retval = lexical_assign<typename AssignTo::value_type, typename ConvertTo::value_type>(elem, out);
1415 if(!retval) {
1416 return false;
1417 }
1418 output.insert(output.end(), std::move(out));
1419 if(skip_remaining) {
1420 break;
1421 }
1422 }
1423 return (!output.empty());
1424}
1425
1427template <class AssignTo, class ConvertTo, enable_if_t<is_complex<ConvertTo>::value, detail::enabler> = detail::dummy>
1428bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output) {
1429
1430 if(strings.size() >= 2 && !strings[1].empty()) {
1431 using XC2 = typename wrapped_type<ConvertTo, double>::type;
1432 XC2 x{0.0}, y{0.0};
1433 auto str1 = strings[1];
1434 if(str1.back() == 'i' || str1.back() == 'j') {
1435 str1.pop_back();
1436 }
1437 auto worked = lexical_cast(strings[0], x) && lexical_cast(str1, y);
1438 if(worked) {
1439 output = ConvertTo{x, y};
1440 }
1441 return worked;
1442 }
1443 return lexical_assign<AssignTo, ConvertTo>(strings[0], output);
1444}
1445
1447template <class AssignTo,
1448 class ConvertTo,
1449 enable_if_t<is_mutable_container<AssignTo>::value && (expected_count<ConvertTo>::value == 1) &&
1452bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1453 bool retval = true;
1454 output.clear();
1455 output.reserve(strings.size());
1456 for(const auto &elem : strings) {
1457
1458 output.emplace_back();
1459 retval = retval && lexical_assign<typename AssignTo::value_type, ConvertTo>(elem, output.back());
1460 }
1461 return (!output.empty()) && retval;
1462}
1463
1464// forward declaration
1465
1467template <class AssignTo,
1468 class ConvertTo,
1469 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1472bool lexical_conversion(std::vector<std::string> strings, AssignTo &output);
1473
1475template <class AssignTo,
1476 class ConvertTo,
1477 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1482bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output);
1483
1485template <class AssignTo,
1486 class ConvertTo,
1487 enable_if_t<is_tuple_like<AssignTo>::value && is_tuple_like<ConvertTo>::value &&
1491bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output); // forward declaration
1492
1495template <typename AssignTo,
1496 typename ConvertTo,
1497 enable_if_t<!is_tuple_like<AssignTo>::value && !is_mutable_container<AssignTo>::value &&
1498 classify_object<ConvertTo>::value != object_category::wrapper_value &&
1499 (is_mutable_container<ConvertTo>::value || type_count<ConvertTo>::value > 2),
1501bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1502
1503 if(strings.size() > 1 || (!strings.empty() && !(strings.front().empty()))) {
1504 ConvertTo val;
1505 auto retval = lexical_conversion<ConvertTo, ConvertTo>(strings, val);
1506 output = AssignTo{val};
1507 return retval;
1508 }
1509 output = AssignTo{};
1510 return true;
1511}
1512
1514template <class AssignTo, class ConvertTo, std::size_t I>
1515inline typename std::enable_if<(I >= type_count_base<AssignTo>::value), bool>::type
1516tuple_conversion(const std::vector<std::string> &, AssignTo &) {
1517 return true;
1518}
1519
1521template <class AssignTo, class ConvertTo>
1522inline typename std::enable_if<!is_mutable_container<ConvertTo>::value && type_count<ConvertTo>::value == 1, bool>::type
1523tuple_type_conversion(std::vector<std::string> &strings, AssignTo &output) {
1524 auto retval = lexical_assign<AssignTo, ConvertTo>(strings[0], output);
1525 strings.erase(strings.begin());
1526 return retval;
1527}
1528
1530template <class AssignTo, class ConvertTo>
1531inline typename std::enable_if<!is_mutable_container<ConvertTo>::value && (type_count<ConvertTo>::value > 1) &&
1532 type_count<ConvertTo>::value == type_count_min<ConvertTo>::value,
1533 bool>::type
1534tuple_type_conversion(std::vector<std::string> &strings, AssignTo &output) {
1535 auto retval = lexical_conversion<AssignTo, ConvertTo>(strings, output);
1536 strings.erase(strings.begin(), strings.begin() + type_count<ConvertTo>::value);
1537 return retval;
1538}
1539
1541template <class AssignTo, class ConvertTo>
1542inline typename std::enable_if<is_mutable_container<ConvertTo>::value ||
1543 type_count<ConvertTo>::value != type_count_min<ConvertTo>::value,
1544 bool>::type
1545tuple_type_conversion(std::vector<std::string> &strings, AssignTo &output) {
1546
1547 std::size_t index{subtype_count_min<ConvertTo>::value};
1548 const std::size_t mx_count{subtype_count<ConvertTo>::value};
1549 const std::size_t mx{(std::min)(mx_count, strings.size() - 1)};
1550
1551 while(index < mx) {
1552 if(is_separator(strings[index])) {
1553 break;
1554 }
1555 ++index;
1556 }
1557 bool retval = lexical_conversion<AssignTo, ConvertTo>(
1558 std::vector<std::string>(strings.begin(), strings.begin() + static_cast<std::ptrdiff_t>(index)), output);
1559 if(strings.size() > index) {
1560 strings.erase(strings.begin(), strings.begin() + static_cast<std::ptrdiff_t>(index) + 1);
1561 } else {
1562 strings.clear();
1563 }
1564 return retval;
1565}
1566
1568template <class AssignTo, class ConvertTo, std::size_t I>
1569inline typename std::enable_if<(I < type_count_base<AssignTo>::value), bool>::type
1570tuple_conversion(std::vector<std::string> strings, AssignTo &output) {
1571 bool retval = true;
1572 using ConvertToElement = typename std::
1573 conditional<is_tuple_like<ConvertTo>::value, typename std::tuple_element<I, ConvertTo>::type, ConvertTo>::type;
1574 if(!strings.empty()) {
1575 retval = retval && tuple_type_conversion<typename std::tuple_element<I, AssignTo>::type, ConvertToElement>(
1576 strings, std::get<I>(output));
1577 }
1578 retval = retval && tuple_conversion<AssignTo, ConvertTo, I + 1>(std::move(strings), output);
1579 return retval;
1580}
1581
1583template <class AssignTo,
1584 class ConvertTo,
1585 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1588bool lexical_conversion(std::vector<std::string> strings, AssignTo &output) {
1589 output.clear();
1590 while(!strings.empty()) {
1591
1592 typename std::remove_const<typename std::tuple_element<0, typename ConvertTo::value_type>::type>::type v1;
1593 typename std::tuple_element<1, typename ConvertTo::value_type>::type v2;
1594 bool retval = tuple_type_conversion<decltype(v1), decltype(v1)>(strings, v1);
1595 if(!strings.empty()) {
1596 retval = retval && tuple_type_conversion<decltype(v2), decltype(v2)>(strings, v2);
1597 }
1598 if(retval) {
1599 output.insert(output.end(), typename AssignTo::value_type{v1, v2});
1600 } else {
1601 return false;
1602 }
1603 }
1604 return (!output.empty());
1605}
1606
1608template <class AssignTo,
1609 class ConvertTo,
1610 enable_if_t<is_tuple_like<AssignTo>::value && is_tuple_like<ConvertTo>::value &&
1614bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1615 static_assert(
1617 "if the conversion type is defined as a tuple it must be the same size as the type you are converting to");
1618 return tuple_conversion<AssignTo, ConvertTo, 0>(strings, output);
1619}
1620
1622template <class AssignTo,
1623 class ConvertTo,
1624 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1629bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1630 bool retval = true;
1631 output.clear();
1632 std::vector<std::string> temp;
1633 std::size_t ii{0};
1634 std::size_t icount{0};
1635 std::size_t xcm{type_count<ConvertTo>::value};
1636 auto ii_max = strings.size();
1637 while(ii < ii_max) {
1638 temp.push_back(strings[ii]);
1639 ++ii;
1640 ++icount;
1641 if(icount == xcm || is_separator(temp.back()) || ii == ii_max) {
1642 if(static_cast<int>(xcm) > type_count_min<ConvertTo>::value && is_separator(temp.back())) {
1643 temp.pop_back();
1644 }
1645 typename AssignTo::value_type temp_out;
1646 retval = retval &&
1647 lexical_conversion<typename AssignTo::value_type, typename ConvertTo::value_type>(temp, temp_out);
1648 temp.clear();
1649 if(!retval) {
1650 return false;
1651 }
1652 output.insert(output.end(), std::move(temp_out));
1653 icount = 0;
1654 }
1655 }
1656 return retval;
1657}
1658
1660template <typename AssignTo,
1661 class ConvertTo,
1662 enable_if_t<classify_object<ConvertTo>::value == object_category::wrapper_value &&
1663 std::is_assignable<ConvertTo &, ConvertTo>::value,
1665bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output) {
1666 if(strings.empty() || strings.front().empty()) {
1667 output = ConvertTo{};
1668 return true;
1669 }
1670 typename ConvertTo::value_type val;
1671 if(lexical_conversion<typename ConvertTo::value_type, typename ConvertTo::value_type>(strings, val)) {
1672 output = ConvertTo{val};
1673 return true;
1674 }
1675 return false;
1676}
1677
1679template <typename AssignTo,
1680 class ConvertTo,
1681 enable_if_t<classify_object<ConvertTo>::value == object_category::wrapper_value &&
1682 !std::is_assignable<AssignTo &, ConvertTo>::value,
1684bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output) {
1685 using ConvertType = typename ConvertTo::value_type;
1686 if(strings.empty() || strings.front().empty()) {
1687 output = ConvertType{};
1688 return true;
1689 }
1690 ConvertType val;
1691 if(lexical_conversion<typename ConvertTo::value_type, typename ConvertTo::value_type>(strings, val)) {
1692 output = val;
1693 return true;
1694 }
1695 return false;
1696}
1697
1699inline std::string sum_string_vector(const std::vector<std::string> &values) {
1700 double val{0.0};
1701 bool fail{false};
1702 std::string output;
1703 for(const auto &arg : values) {
1704 double tv{0.0};
1705 auto comp = lexical_cast(arg, tv);
1706 if(!comp) {
1707 errno = 0;
1708 auto fv = detail::to_flag_value(arg);
1709 fail = (errno != 0);
1710 if(fail) {
1711 break;
1712 }
1713 tv = static_cast<double>(fv);
1714 }
1715 val += tv;
1716 }
1717 if(fail) {
1718 for(const auto &arg : values) {
1719 output.append(arg);
1720 }
1721 } else {
1722 std::ostringstream out;
1723 out.precision(16);
1724 out << val;
1725 output = out.str();
1726 }
1727 return output;
1728}
1729
1730} // namespace detail
1731// [CLI11:type_tools_hpp:end]
1732} // namespace CLI
Check for complex.
Definition: TypeTools.hpp:216
static constexpr bool value
Definition: TypeTools.hpp:223
Definition: TypeTools.hpp:158
static constexpr bool value
Definition: TypeTools.hpp:185
Check for input streamability.
Definition: TypeTools.hpp:205
static constexpr bool value
Definition: TypeTools.hpp:212
Definition: TypeTools.hpp:194
static constexpr bool value
Definition: TypeTools.hpp:201
Definition: TypeTools.hpp:280
static constexpr bool value
Definition: TypeTools.hpp:288
constexpr enabler dummy
An instance to use in EnableIf.
Definition: TypeTools.hpp:37
auto to_string(T &&value) -> decltype(std::forward< T >(value))
Convert an object to a string (directly forward if this can become a string)
Definition: TypeTools.hpp:293
auto checked_to_string(T &&value) -> decltype(to_string(std::forward< T >(value)))
special template overload
Definition: TypeTools.hpp:349
bool is_separator(const std::string &str)
check if a string is a container segment separator (empty or "%%")
Definition: StringTools.hpp:168
bool from_stream(const std::string &istring, T &obj)
Templated operation to get a value from a stream.
Definition: TypeTools.hpp:228
constexpr int expected_max_vector_size
Definition: StringTools.hpp:45
std::string value_string(const T &value)
get a string as a convertible value for arithmetic types
Definition: TypeTools.hpp:363
std::string join(const T &v, std::string delim=",")
Simple function to join a string.
Definition: StringTools.hpp:51
bool lexical_assign(const std::string &input, AssignTo &output)
Assign a value through lexical cast operations.
Definition: TypeTools.hpp:1267
std::int64_t to_flag_value(std::string val) noexcept
Convert a flag into an integer value typically binary flags sets errno to nonzero if conversion faile...
Definition: TypeTools.hpp:963
constexpr std::enable_if< I< type_count_base< T >::value, int >::type tuple_type_size() { return subtype_count< typename std::tuple_element< I, T >::type >::value+tuple_type_size< T, I+1 >();}template< typename T > struct type_count< T, typename std::enable_if< is_tuple_like< T >::value >::type > { static constexpr int value{tuple_type_size< T, 0 >()};};template< typename T > struct subtype_count { static constexpr int value{is_mutable_container< T >::value ? expected_max_vector_size :type_count< T >::value};};template< typename T, typename Enable=void > struct type_count_min { static const int value{0};};template< typename T >struct type_count_min< T, typename std::enable_if<!is_mutable_container< T >::value &&!is_tuple_like< T >::value &&!is_wrapper< T >::value &&!is_complex< T >::value &&!std::is_void< T >::value >::type > { static constexpr int value{type_count< T >::value};};template< typename T > struct type_count_min< T, typename std::enable_if< is_complex< T >::value >::type > { static constexpr int value{1};};template< typename T >struct type_count_min< T, typename std::enable_if< is_wrapper< T >::value &&!is_complex< T >::value &&!is_tuple_like< T >::value >::type > { static constexpr int value{subtype_count_min< typename T::value_type >::value};};template< typename T, std::size_t I >constexpr typename std::enable_if< I==type_count_base< T >::value, int >::type tuple_type_size_min() { return 0;}template< typename T, std::size_t I > constexpr typename std::enable_if< I< type_count_base< T >::value, int >::type tuple_type_size_min() { return subtype_count_min< typename std::tuple_element< I, T >::type >::value+tuple_type_size_min< T, I+1 >();}template< typename T > struct type_count_min< T, typename std::enable_if< is_tuple_like< T >::value >::type > { static constexpr int value{tuple_type_size_min< T, 0 >()};};template< typename T > struct subtype_count_min { static constexpr int value{is_mutable_container< T >::value ?((type_count< T >::value< expected_max_vector_size) ? type_count< T >::value :0) :type_count_min< T >::value};};template< typename T, typename Enable=void > struct expected_count { static const int value{0};};template< typename T >struct expected_count< T, typename std::enable_if<!is_mutable_container< T >::value &&!is_wrapper< T >::value &&!std::is_void< T >::value >::type > { static constexpr int value{1};};template< typename T > struct expected_count< T, typename std::enable_if< is_mutable_container< T >::value >::type > { static constexpr int value{expected_max_vector_size};};template< typename T >struct expected_count< T, typename std::enable_if<!is_mutable_container< T >::value &&is_wrapper< T >::value >::type > { static constexpr int value{expected_count< typename T::value_type >::value};};enum class object_category :int { char_value=1, integral_value=2, unsigned_integral=4, enumeration=6, boolean_value=8, floating_point=10, number_constructible=12, double_constructible=14, integer_constructible=16, string_assignable=23, string_constructible=24, wstring_assignable=25, wstring_constructible=26, other=45, wrapper_value=50, complex_number=60, tuple_value=70, container_value=80,};template< typename T, typename Enable=void > struct classify_object { static constexpr object_category value{object_category::other};};template< typename T >struct classify_object< T, typename std::enable_if< std::is_integral< T >::value &&!std::is_same< T, char >::value &&std::is_signed< T >::value &&!is_bool< T >::value &&!std::is_enum< T >::value >::type > { static constexpr object_category value{object_category::integral_value};};template< typename T >struct classify_object< T, typename std::enable_if< std::is_integral< T >::value &&std::is_unsigned< T >::value &&!std::is_same< T, char >::value &&!is_bool< T >::value >::type > { static constexpr object_category value{object_category::unsigned_integral};};template< typename T >struct classify_object< T, typename std::enable_if< std::is_same< T, char >::value &&!std::is_enum< T >::value >::type > { static constexpr object_category value{object_category::char_value};};template< typename T > struct classify_object< T, typename std::enable_if< is_bool< T >::value >::type > { static constexpr object_category value{object_category::boolean_value};};template< typename T > struct classify_object< T, typename std::enable_if< std::is_floating_point< T >::value >::type > { static constexpr object_category value{object_category::floating_point};};#define WIDE_STRING_CHECK #define STRING_CHECK template< typename T >struct classify_object< T, typename std::enable_if<!std::is_floating_point< T >::value &&!std::is_integral< T >::value &&WIDE_STRING_CHECK &&std::is_assignable< T &, std::string >::value >::type > { static constexpr object_category value{object_category::string_assignable};};template< typename T >struct classify_object< T, typename std::enable_if<!std::is_floating_point< T >::value &&!std::is_integral< T >::value &&!std::is_assignable< T &, std::string >::value &&(type_count< T >::value==1) &&WIDE_STRING_CHECK &&std::is_constructible< T, std::string >::value >::type > { static constexpr object_category value{object_category::string_constructible};};template< typename T >struct classify_object< T, typename std::enable_if<!std::is_floating_point< T >::value &&!std::is_integral< T >::value &&STRING_CHECK &&std::is_assignable< T &, std::wstring >::value >::type > { static constexpr object_category value{object_category::wstring_assignable};};template< typename T >struct classify_object< T, typename std::enable_if<!std::is_floating_point< T >::value &&!std::is_integral< T >::value &&!std::is_assignable< T &, std::wstring >::value &&(type_count< T >::value==1) &&STRING_CHECK &&std::is_constructible< T, std::wstring >::value >::type > { static constexpr object_category value{object_category::wstring_constructible};};template< typename T > struct classify_object< T, typename std::enable_if< std::is_enum< T >::value >::type > { static constexpr object_category value{object_category::enumeration};};template< typename T > struct classify_object< T, typename std::enable_if< is_complex< T >::value >::type > { static constexpr object_category value{object_category::complex_number};};template< typename T > struct uncommon_type { using type=typename std::conditional< !std::is_floating_point< T >::value &&!std::is_integral< T >::value &&!std::is_assignable< T &, std::string >::value &&!std::is_constructible< T, std::string >::value &&!std::is_assignable< T &, std::wstring >::value &&!std::is_constructible< T, std::wstring >::value &&!is_complex< T >::value &&!is_mutable_container< T >::value &&!std::is_enum< T >::value, std::true_type, std::false_type >::type;static constexpr bool value=type::value;};template< typename T >struct classify_object< T, typename std::enable_if<(!is_mutable_container< T >::value &&is_wrapper< T >::value &&!is_tuple_like< T >::value &&uncommon_type< T >::value)>::type > { static constexpr object_category value{object_category::wrapper_value};};template< typename T >struct classify_object< T, typename std::enable_if< uncommon_type< T >::value &&type_count< T >::value==1 &&!is_wrapper< T >::value &&is_direct_constructible< T, double >::value &&is_direct_constructible< T, int >::value >::type > { static constexpr object_category value{object_category::number_constructible};};template< typename T >struct classify_object< T, typename std::enable_if< uncommon_type< T >::value &&type_count< T >::value==1 &&!is_wrapper< T >::value &&!is_direct_constructible< T, double >::value &&is_direct_constructible< T, int >::value >::type > { static constexpr object_category value{object_category::integer_constructible};};template< typename T >struct classify_object< T, typename std::enable_if< uncommon_type< T >::value &&type_count< T >::value==1 &&!is_wrapper< T >::value &&is_direct_constructible< T, double >::value &&!is_direct_constructible< T, int >::value >::type > { static constexpr object_category value{object_category::double_constructible};};template< typename T >struct classify_object< T, typename std::enable_if< is_tuple_like< T >::value &&((type_count< T >::value >=2 &&!is_wrapper< T >::value)||(uncommon_type< T >::value &&!is_direct_constructible< T, double >::value &&!is_direct_constructible< T, int >::value)||(uncommon_type< T >::value &&type_count< T >::value >=2))>::type > { static constexpr object_category value{object_category::tuple_value};};template< typename T > struct classify_object< T, typename std::enable_if< is_mutable_container< T >::value >::type > { static constexpr object_category value{object_category::container_value};};template< typename T, enable_if_t< classify_object< T >::value==object_category::char_value, detail::enabler >=detail::dummy >constexpr const char *type_name() { return "CHAR";}template< typename T, enable_if_t< classify_object< T >::value==object_category::integral_value||classify_object< T >::value==object_category::integer_constructible, detail::enabler >=detail::dummy >constexpr const char *type_name() { return "INT";}template< typename T, enable_if_t< classify_object< T >::value==object_category::unsigned_integral, detail::enabler >=detail::dummy >constexpr const char *type_name() { return "UINT";}template< typename T, enable_if_t< classify_object< T >::value==object_category::floating_point||classify_object< T >::value==object_category::number_constructible||classify_object< T >::value==object_category::double_constructible, detail::enabler >=detail::dummy >constexpr const char *type_name() { return "FLOAT";}template< typename T, enable_if_t< classify_object< T >::value==object_category::enumeration, detail::enabler >=detail::dummy >constexpr const char *type_name() { return "ENUM";}template< typename T, enable_if_t< classify_object< T >::value==object_category::boolean_value, detail::enabler >=detail::dummy >constexpr const char *type_name() { return "BOOLEAN";}template< typename T, enable_if_t< classify_object< T >::value==object_category::complex_number, detail::enabler >=detail::dummy >constexpr const char *type_name() { return "COMPLEX";}template< typename T, enable_if_t< classify_object< T >::value >=object_category::string_assignable &&classify_object< T >::value<=object_category::other, detail::enabler >=detail::dummy >constexpr const char *type_name() { return "TEXT";}template< typename T, enable_if_t< classify_object< T >::value==object_category::tuple_value &&type_count_base< T >::value >=2, detail::enabler >=detail::dummy >std::string type_name();template< typename T, enable_if_t< classify_object< T >::value==object_category::container_value||classify_object< T >::value==object_category::wrapper_value, detail::enabler >=detail::dummy >std::string type_name();template< typename T, enable_if_t< classify_object< T >::value==object_category::tuple_value &&type_count_base< T >::value==1, detail::enabler >=detail::dummy >inline std::string type_name() { return type_name< typename std::decay< typename std::tuple_element< 0, T >::type >::type >();}template< typename T, std::size_t I >inline typename std::enable_if< I==type_count_base< T >::value, std::string >::type tuple_name() { return std::string{};}template< typename T, std::size_t I >inline typename std::enable_if<(I< type_count_base< T >::value), std::string >::type tuple_name() { auto str=std::string{type_name< typename std::decay< typename std::tuple_element< I, T >::type >::type >()}+','+tuple_name< T, I+1 >();if(str.back()==',') str.pop_back();return str;}template< typename T, enable_if_t< classify_object< T >::value==object_category::tuple_value &&type_count_base< T >::value >=2, detail::enabler > > std::string type_name()
Recursively generate the tuple type name.
Definition: TypeTools.hpp:842
std::string to_lower(std::string str)
Return a lower case version of a string.
Definition: StringTools.hpp:179
enabler
Simple empty scoped class.
Definition: TypeTools.hpp:34
bool lexical_cast(const std::string &input, T &output)
Integer conversion.
Definition: TypeTools.hpp:1015
bool integral_conversion(const std::string &input, T &output) noexcept
Convert to an unsigned integral.
Definition: TypeTools.hpp:861
constexpr std::enable_if< I==type_count_base< T >::value, int >::type tuple_type_size()
0 if the index > tuple size
Definition: TypeTools.hpp:453
Definition: App.hpp:34
typename std::enable_if< B, T >::type enable_if_t
Definition: TypeTools.hpp:45
typename std::conditional< B, T, F >::type conditional_t
A copy of std::conditional_t from C++14 - same reasoning as enable_if_t, it does not hurt to redefine...
Definition: TypeTools.hpp:56
typename make_void< Ts... >::type void_t
A copy of std::void_t from C++17 - same reasoning as enable_if_t, it does not hurt to redefine.
Definition: TypeTools.hpp:53
CLI11_INLINE std::wstring widen(const std::string &str)
Convert a narrow string to a wide string.
std::string type
Definition: TypeTools.hpp:85
This can be specialized to override the type deduction for IsMember.
Definition: TypeTools.hpp:79
T type
Definition: TypeTools.hpp:80
typename std::pointer_traits< T >::element_type type
Definition: TypeTools.hpp:101
not a pointer
Definition: TypeTools.hpp:96
T type
Definition: TypeTools.hpp:97
Definition: TypeTools.hpp:106
typename element_type< T >::type::value_type type
Definition: TypeTools.hpp:107
Definition: TypeTools.hpp:241
Definition: TypeTools.hpp:261
Definition: TypeTools.hpp:273
typename std::remove_const< typename value_type::first_type >::type first_type
Definition: TypeTools.hpp:134
static auto first(Q &&pair_value) -> decltype(std::get< 0 >(std::forward< Q >(pair_value)))
Get the first value (really just the underlying value)
Definition: TypeTools.hpp:138
static auto second(Q &&pair_value) -> decltype(std::get< 1 >(std::forward< Q >(pair_value)))
Get the second value (really just the underlying value)
Definition: TypeTools.hpp:142
typename std::remove_const< typename value_type::second_type >::type second_type
Definition: TypeTools.hpp:135
Adaptor for set-like structure: This just wraps a normal container in a few utilities that do almost ...
Definition: TypeTools.hpp:111
typename T::value_type value_type
Definition: TypeTools.hpp:112
typename std::remove_const< value_type >::type second_type
Definition: TypeTools.hpp:114
static auto second(Q &&pair_value) -> decltype(std::forward< Q >(pair_value))
Get the second value (really just the underlying value)
Definition: TypeTools.hpp:121
typename std::remove_const< value_type >::type first_type
Definition: TypeTools.hpp:113
static auto first(Q &&pair_value) -> decltype(std::forward< Q >(pair_value))
Get the first value (really just the underlying value)
Definition: TypeTools.hpp:117
forward declare the subtype_count_min structure
Definition: TypeTools.hpp:418
Set of overloads to get the type size of an object.
Definition: TypeTools.hpp:415
This will only trigger for actual void type.
Definition: TypeTools.hpp:389
static const int value
Definition: TypeTools.hpp:390
This will only trigger for actual void type.
Definition: TypeTools.hpp:421
static const int value
Definition: TypeTools.hpp:422
template to get the underlying value type if it exists or use a default
Definition: TypeTools.hpp:379
def type
Definition: TypeTools.hpp:380
Check to see if something is bool (fail check by default)
Definition: TypeTools.hpp:59
Check to see if something is copyable pointer.
Definition: TypeTools.hpp:74
static bool const value
Definition: TypeTools.hpp:75
Check to see if something is a shared pointer.
Definition: TypeTools.hpp:65
A copy of std::void_t from C++17 (helper for C++11 and C++14)
Definition: TypeTools.hpp:48
void type
Definition: TypeTools.hpp:49