Skip to content

Commit

Permalink
value construction from optional
Browse files Browse the repository at this point in the history
  • Loading branch information
alandefreitas committed Oct 4, 2022
1 parent 102ee20 commit ecdc8f1
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 171 deletions.
99 changes: 99 additions & 0 deletions include/boost/url/detail/optional_string.hpp
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
Loading

0 comments on commit ecdc8f1

Please sign in to comment.