CLI11
C++11 Command Line Interface Parser
Optional.hpp
1 #pragma once
2 
3 // Distributed under the 3-Clause BSD License. See accompanying
4 // file LICENSE or https://github.com/CLIUtils/CLI11 for details.
5 
6 #include <istream>
7 
8 #include "CLI/Macros.hpp"
9 
10 // [CLI11:verbatim]
11 
12 // You can explicitly enable or disable support
13 // by defining to 1 or 0. Extra check here to ensure it's in the stdlib too.
14 // We nest the check for __has_include and it's usage
15 #ifndef CLI11_STD_OPTIONAL
16 #ifdef __has_include
17 #if defined(CLI11_CPP17) && __has_include(<optional>)
18 #define CLI11_STD_OPTIONAL 1
19 #else
20 #define CLI11_STD_OPTIONAL 0
21 #endif
22 #else
23 #define CLI11_STD_OPTIONAL 0
24 #endif
25 #endif
26 
27 #ifndef CLI11_EXPERIMENTAL_OPTIONAL
28 #define CLI11_EXPERIMENTAL_OPTIONAL 0
29 #endif
30 
31 #ifndef CLI11_BOOST_OPTIONAL
32 #define CLI11_BOOST_OPTIONAL 0
33 #endif
34 
35 #if CLI11_BOOST_OPTIONAL
36 #include <boost/version.hpp>
37 #if BOOST_VERSION < 106100
38 #error "This boost::optional version is not supported, use 1.61 or better"
39 #endif
40 #endif
41 
42 #if CLI11_STD_OPTIONAL
43 #include <optional>
44 #endif
45 #if CLI11_EXPERIMENTAL_OPTIONAL
46 #include <experimental/optional>
47 #endif
48 #if CLI11_BOOST_OPTIONAL
49 #include <boost/optional.hpp>
50 #include <boost/optional/optional_io.hpp>
51 #endif
52 // [CLI11:verbatim]
53 
54 namespace CLI {
55 
56 #if CLI11_STD_OPTIONAL
57 template <typename T> std::istream &operator>>(std::istream &in, std::optional<T> &val) {
58  T v;
59  in >> v;
60  val = v;
61  return in;
62 }
63 #endif
64 
65 #if CLI11_EXPERIMENTAL_OPTIONAL
66 template <typename T> std::istream &operator>>(std::istream &in, std::experimental::optional<T> &val) {
67  T v;
68  in >> v;
69  val = v;
70  return in;
71 }
72 #endif
73 
74 #if CLI11_BOOST_OPTIONAL
75 template <typename T> std::istream &operator>>(std::istream &in, boost::optional<T> &val) {
76  T v;
77  in >> v;
78  val = v;
79  return in;
80 }
81 #endif
82 
83 // Export the best optional to the CLI namespace
84 #if CLI11_STD_OPTIONAL
85 using std::optional;
86 #elif CLI11_EXPERIMENTAL_OPTIONAL
87 using std::experimental::optional;
88 #elif CLI11_BOOST_OPTIONAL
89 using boost::optional;
90 #endif
91 
92 // This is true if any optional is found
93 #if CLI11_STD_OPTIONAL || CLI11_EXPERIMENTAL_OPTIONAL || CLI11_BOOST_OPTIONAL
94 #define CLI11_OPTIONAL 1
95 #endif
96 
97 } // namespace CLI
Definition: App.hpp:29
std::istream & operator>>(std::istream &in, T &item)
input streaming for enumerations
Definition: StringTools.hpp:30