scnlib  0.1.2
FormattedinputformodernC++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
options.h
Go to the documentation of this file.
1 // Copyright 2017-2019 Elias Kosunen
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // This file is a part of scnlib:
16 // https://github.com/eliaskosunen/scnlib
17 
18 #ifndef SCN_OPTIONS_H
19 #define SCN_OPTIONS_H
20 
21 #include "config.h"
22 
23 #include <type_traits>
24 
25 #ifndef SCN_DEFAULT_INT_SCANNING_METHOD
26 #define SCN_DEFAULT_INT_SCANNING_METHOD ::scn::method::custom
27 #endif
28 
29 #ifndef SCN_DEFAULT_FLOAT_SCANNING_METHOD
30 #define SCN_DEFAULT_FLOAT_SCANNING_METHOD ::scn::method::strto
31 #endif
32 
33 namespace scn {
35 
36  enum class method {
37  sto, // std::stoi, std::stod, etc.
38  from_chars, // std::from_chars
39  strto, // strtol, strtod, etc.
40  custom // custom hand-written
41  };
42 
43  // std::from_chars availibility
45  {
47  }
49  {
51  }
52 
54  method fallback = SCN_DEFAULT_INT_SCANNING_METHOD) noexcept
55  {
56  return is_int_from_chars_available() ? method::from_chars : fallback;
57  }
59  method fallback = SCN_DEFAULT_FLOAT_SCANNING_METHOD) noexcept
60  {
62  }
63 
64  template <typename T>
66  method fallback = SCN_DEFAULT_INT_SCANNING_METHOD) noexcept ->
67  typename std::enable_if<std::is_integral<T>::value, method>::type
68  {
69  return int_from_chars_if_available(fallback);
70  }
71  template <typename T>
73  method fallback = SCN_DEFAULT_FLOAT_SCANNING_METHOD) noexcept ->
74  typename std::enable_if<std::is_floating_point<T>::value, method>::type
75  {
76  return float_from_chars_if_available(fallback);
77  }
78 
79  template <typename CharT>
80  class basic_locale_ref;
81  template <typename CharT>
82  class basic_default_locale_ref;
83 
85  SCN_CLANG_IGNORE("-Wpadded")
86 
87  struct options {
88  struct builder;
89 
90  SCN_CONSTEXPR options() noexcept = default;
91  SCN_CONSTEXPR options(builder b) noexcept;
92 
93  template <typename CharT>
95  {
97  if (!locale) {
98  return basic_locale_ref<CharT>();
99  }
100  return basic_locale_ref<CharT>(locale);
102  }
103 
104  template <typename T>
105  SCN_CONSTEXPR auto get_method_for() const noexcept ->
106  typename std::enable_if<std::is_integral<T>::value, method>::type
107  {
108  return int_method;
109  }
110  template <typename T>
111  SCN_CONSTEXPR auto get_method_for() const noexcept ->
112  typename std::enable_if<std::is_floating_point<T>::value,
113  method>::type
114  {
115  return float_method;
116  }
117 
118  // Type-erase locale to avoid including <locale>
119  const void* locale{nullptr};
122  };
123 
125 
127  template <typename T>
128  SCN_CONSTEXPR14 builder& locale(const T& l) noexcept
129  {
130  opt.locale = &l;
131  return *this;
132  }
134  {
135  opt.int_method = m;
136  return *this;
137  }
139  {
140  opt.float_method = m;
141  return *this;
142  }
143 
145  {
146  return static_cast<options&&>(opt);
147  }
148 
150  };
151 
153  : locale(b.opt.locale),
154  int_method(b.opt.int_method),
155  float_method(b.opt.float_method)
156  {
157  }
158 
160  template <typename CharT>
162  noexcept
163  {
165  }
166 
167  template <typename T>
168  SCN_CONSTEXPR auto get_method_for() const noexcept ->
169  typename std::enable_if<std::is_integral<T>::value, method>::type
170  {
172  }
173  template <typename T>
174  SCN_CONSTEXPR auto get_method_for() const noexcept ->
175  typename std::enable_if<std::is_floating_point<T>::value,
176  method>::type
177  {
179  }
180  };
181 
183 } // namespace scn
184 
185 #endif // SCN_OPTIONS_H
basic_locale_ref< CharT > get_locale_ref() const noexcept
Definition: options.h:94
SCN_CONSTEXPR auto from_chars_if_available(method fallback=SCN_DEFAULT_INT_SCANNING_METHOD) noexcept-> typename std::enable_if< std::is_integral< T >::value, method >::type
Definition: options.h:65
#define SCN_END_NAMESPACE
Definition: config.h:376
SCN_CONSTEXPR auto get_method_for() const noexcept-> typename std::enable_if< std::is_integral< T >::value, method >::type
Definition: options.h:168
SCN_CONSTEXPR14 builder & int_method(method m) noexcept
Definition: options.h:133
SCN_CONSTEXPR auto get_method_for() const noexcept-> typename std::enable_if< std::is_floating_point< T >::value, method >::type
Definition: options.h:174
SCN_CONSTEXPR basic_default_locale_ref< CharT > get_locale_ref() const noexcept
Definition: options.h:161
SCN_CONSTEXPR bool is_integral(type t) noexcept
Definition: args.h:90
#define SCN_CLANG_IGNORE(x)
Definition: config.h:128
#define SCN_DEFAULT_FLOAT_SCANNING_METHOD
Definition: options.h:30
const void * locale
Definition: options.h:119
#define SCN_DEFAULT_INT_SCANNING_METHOD
Definition: options.h:26
#define SCN_CLANG_POP
Definition: config.h:127
#define SCN_CLANG_POP_IGNORE_UNDEFINED_TEMPLATE
Definition: config.h:130
method float_method
Definition: options.h:121
SCN_CONSTEXPR bool is_float_from_chars_available() noexcept
Definition: options.h:48
SCN_CONSTEXPR method int_from_chars_if_available(method fallback=SCN_DEFAULT_INT_SCANNING_METHOD) noexcept
Definition: options.h:53
#define SCN_BEGIN_NAMESPACE
Definition: config.h:375
SCN_CONSTEXPR method float_from_chars_if_available(method fallback=SCN_DEFAULT_FLOAT_SCANNING_METHOD) noexcept
Definition: options.h:58
method int_method
Definition: options.h:120
SCN_CONSTEXPR bool is_int_from_chars_available() noexcept
Definition: options.h:44
SCN_CONSTEXPR14 builder & locale(const T &l) noexcept
Definition: options.h:128
SCN_CONSTEXPR14 builder & float_method(method m) noexcept
Definition: options.h:138
SCN_CONSTEXPR14 options make() noexcept
Definition: options.h:144
#define SCN_CLANG_PUSH_IGNORE_UNDEFINED_TEMPLATE
Definition: config.h:129
SCN_CONSTEXPR auto get_method_for() const noexcept-> typename std::enable_if< std::is_integral< T >::value, method >::type
Definition: options.h:105
#define SCN_CONSTEXPR
Definition: config.h:226
SCN_CONSTEXPR options() noexcept=default
method
Definition: options.h:36
SCN_CONSTEXPR auto get_method_for() const noexcept-> typename std::enable_if< std::is_floating_point< T >::value, method >::type
Definition: options.h:111
#define SCN_CLANG_PUSH
Definition: config.h:126
#define SCN_HAS_FLOAT_CHARCONV
Definition: config.h:271
#define SCN_HAS_INTEGER_CHARCONV
Definition: config.h:270
#define SCN_CONSTEXPR14
Definition: config.h:227