Interactive CLI 2.3
A cross-platform C++14 library for interactive command-line interfaces
Loading...
Searching...
No Matches
select.h
Go to the documentation of this file.
1/*******************************************************************************
2 * CLI - A simple command line interface.
3 * Copyright (C) 2016-2026 Daniele Pallastrelli
4 *
5 * Boost Software License - Version 1.0 - August 17th, 2003
6 *
7 * Permission is hereby granted, free of charge, to any person or organization
8 * obtaining a copy of the software and accompanying documentation covered by
9 * this license (the "Software") to use, reproduce, display, distribute,
10 * execute, and transmit the Software, and to prepare derivative works of the
11 * Software, and to permit third-parties to whom the Software is furnished to
12 * do so, all subject to the following:
13 *
14 * The copyright notices in the Software and this entire statement, including
15 * the above license grant, this restriction and the following disclaimer,
16 * must be included in all copies of the Software, in whole or in part, and
17 * all derivative works of the Software, unless such copies or derivative
18 * works are solely in the form of machine-executable object code generated by
19 * a source language processor.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 * DEALINGS IN THE SOFTWARE.
28 ******************************************************************************/
29
30#ifndef CLI_SELECT_H_
31#define CLI_SELECT_H_
32
33#include <cassert>
34#include <iterator>
35#include <type_traits>
36#include <vector>
37
38#include "detail/fromstring.h"
39
40namespace cli
41{
42 template <typename T> struct TypeDesc { static const char* Name() { return ""; } };
43 template <> struct TypeDesc<char> { static const char* Name() { return "char"; } };
44 template <> struct TypeDesc<unsigned char> { static const char* Name() { return "unsigned char"; } };
45 template <> struct TypeDesc<signed char> { static const char* Name() { return "signed char"; } };
46 template <> struct TypeDesc<short> { static const char* Name() { return "short"; } };
47 template <> struct TypeDesc<unsigned short> { static const char* Name() { return "unsigned short"; } };
48 template <> struct TypeDesc<int> { static const char* Name() { return "int"; } };
49 template <> struct TypeDesc<unsigned int> { static const char* Name() { return "unsigned int"; } };
50 template <> struct TypeDesc<long> { static const char* Name() { return "long"; } };
51 template <> struct TypeDesc<unsigned long> { static const char* Name() { return "unsigned long"; } };
52 template <> struct TypeDesc<long long> { static const char* Name() { return "long long"; } };
53 template <> struct TypeDesc<unsigned long long> { static const char* Name() { return "unsigned long long"; } };
54 template <> struct TypeDesc<float> { static const char* Name() { return "float"; } };
55 template <> struct TypeDesc<double> { static const char* Name() { return "double"; } };
56 template <> struct TypeDesc<long double> { static const char* Name() { return "long double"; } };
57 template <> struct TypeDesc<bool> { static const char* Name() { return "bool"; } };
58 template <> struct TypeDesc<std::string> { static const char* Name() { return "string"; } };
59 template <> struct TypeDesc<std::vector<std::string>> { static const char* Name() { return "words"; } };
60
61 template <typename ... Args>
62 struct Select;
63
64 template <typename P, typename ... Args>
65 struct Select<P, Args...>
66 {
67 template <typename F, typename InputIt>
68 static void Exec(const F& f, InputIt first, InputIt last)
69 {
70 assert(first != last);
71 assert(std::distance(first, last) == 1 + sizeof...(Args));
72 const P p = detail::from_string<typename std::decay<P>::type>(*first);
73 auto g = [&](auto ... pars) { f(p, pars...); };
74 Select<Args...>::Exec(g, std::next(first), last);
75 }
76 };
77
78 template <>
79 struct Select<>
80 {
81 template <typename F, typename InputIt>
82 static void Exec(const F& f, InputIt first, InputIt last)
83 {
84 static_cast<void>(first);
85 static_cast<void>(last);
86 assert(first == last);
87 f();
88 }
89 };
90
91 template <typename ... Args>
92 struct ParamNames;
93
94 template <typename P, typename ... Args>
95 struct ParamNames<P, Args...>
96 {
97 static void Collect(std::vector<std::string>& params)
98 {
99 params.push_back(TypeDesc<typename std::decay<P>::type>::Name());
101 }
102 };
103
104 template <>
105 struct ParamNames<>
106 {
107 static void Collect(std::vector<std::string>& /*params*/) {}
108 };
109}
110
111#endif // CLI_SELECT_H_
static void Collect(std::vector< std::string > &params)
Definition select.h:97
static void Collect(std::vector< std::string > &)
Definition select.h:107
static void Exec(const F &f, InputIt first, InputIt last)
Definition select.h:68
static void Exec(const F &f, InputIt first, InputIt last)
Definition select.h:82
static const char * Name()
Definition select.h:57
static const char * Name()
Definition select.h:43
static const char * Name()
Definition select.h:55
static const char * Name()
Definition select.h:54
static const char * Name()
Definition select.h:48
static const char * Name()
Definition select.h:50
static const char * Name()
Definition select.h:56
static const char * Name()
Definition select.h:52
static const char * Name()
Definition select.h:46
static const char * Name()
Definition select.h:45
static const char * Name()
Definition select.h:58
static const char * Name()
Definition select.h:44
static const char * Name()
Definition select.h:49
static const char * Name()
Definition select.h:51
static const char * Name()
Definition select.h:53
static const char * Name()
Definition select.h:47
static const char * Name()
Definition select.h:42