forked from boostorg/url
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
219 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// | ||
// Copyright (c) 2022 Alan de Freitas ([email protected]) | ||
// | ||
// Distributed under the Boost Software License, Version 1.0. (See accompanying | ||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
// | ||
// Official repository: https://github.com/boostorg/url | ||
// | ||
|
||
#ifndef BOOST_URL_DETAIL_OPTIONAL_STRING_HPP | ||
#define BOOST_URL_DETAIL_OPTIONAL_STRING_HPP | ||
|
||
#include <boost/url/string_view.hpp> | ||
#include <boost/type_traits/make_void.hpp> | ||
|
||
namespace boost { | ||
namespace urls { | ||
|
||
#ifndef BOOST_URL_DOCS | ||
struct no_value_t; | ||
#endif | ||
|
||
namespace detail { | ||
struct optional_string | ||
{ | ||
string_view s; | ||
bool b = false; | ||
}; | ||
|
||
template <class String> | ||
typename std::enable_if< | ||
std::is_convertible<String, string_view>::value, | ||
optional_string>::type | ||
get_optional_string( | ||
String const& s) | ||
{ | ||
optional_string r; | ||
r.s = s; | ||
r.b = true; | ||
return r; | ||
} | ||
|
||
template <class T, class = void> | ||
struct is_dereferenceable : std::false_type | ||
{}; | ||
|
||
template <class T> | ||
struct is_dereferenceable< | ||
T, | ||
boost::void_t< | ||
decltype(*std::declval<T>()) | ||
>> : std::true_type | ||
{}; | ||
|
||
template <class OptionalString> | ||
typename std::enable_if< | ||
!std::is_convertible<OptionalString, string_view>::value, | ||
optional_string>::type | ||
get_optional_string( | ||
OptionalString const& opt) | ||
{ | ||
// If this goes off, it means the rule | ||
// passed in did not meet the requirements. | ||
// Please check the documentation of functions | ||
// that call get_optional_string. | ||
static_assert( | ||
is_dereferenceable<OptionalString>::value && | ||
std::is_constructible<bool, OptionalString>::value && | ||
!std::is_convertible<OptionalString, string_view>::value && | ||
std::is_convertible<typename std::decay<decltype(*std::declval<OptionalString>())>::type, string_view>::value, | ||
"OptionalString requirements not met"); | ||
optional_string r; | ||
r.s = opt ? string_view(*opt) : string_view{}; | ||
r.b = static_cast<bool>(opt); | ||
return r; | ||
} | ||
|
||
inline | ||
optional_string | ||
get_optional_string( | ||
std::nullptr_t) | ||
{ | ||
return {}; | ||
} | ||
|
||
inline | ||
optional_string | ||
get_optional_string( | ||
no_value_t const&) | ||
{ | ||
return {}; | ||
} | ||
|
||
|
||
} // detail | ||
} // urls | ||
} // boost | ||
|
||
#endif |
Oops, something went wrong.