OSSIA
Open Scenario System for Interactive Application
parse_strict.hpp
1 #pragma once
2 #include <ossia/detail/config.hpp>
3 
4 #include <optional>
5 #include <string_view>
6 #include <version>
7 
8 #if defined(__cpp_lib_to_chars)
9 #include <charconv>
10 #else
11 #include <boost/lexical_cast.hpp>
12 #endif
13 
14 namespace ossia
15 {
16 // parse_strict:
17 // "123" => 123
18 // "123x" => nullopt
19 // " 123" => nullopt
20 // "123 " => nullopt
21 
22 template <typename T>
23 static std::optional<T> parse_strict(std::string_view instance) noexcept
24 {
25  T n{};
26 #if defined(__cpp_lib_to_chars)
27  const auto begin = instance.data();
28  const auto end = instance.data() + instance.size();
29  const auto [ptr, ec] = std::from_chars(begin, end, n);
30  return (ec == std::errc{} && ptr == end) ? std::optional<T>{n} : std::nullopt;
31 #else
32  if(boost::conversion::detail::try_lexical_convert(instance, n))
33  return n;
34  return std::nullopt;
35 #endif
36 }
37 }
Definition: git_info.h:7