|
| 1 | +#ifndef JSS_OPTIONAL_H_INCLUDED |
| 2 | +#define JSS_OPTIONAL_H_INCLUDED |
| 3 | + |
| 4 | +#include "tmp_util/fundamental/eval_if.hpp" |
| 5 | +#include "tmp_util/fundamental/is_same.hpp" |
| 6 | +#include "tmp_util/fundamental/null_type.hpp" |
| 7 | + |
| 8 | +// really want to swap with std::optional |
| 9 | +#include <boost/optional.hpp> |
| 10 | + |
| 11 | +namespace JSON |
| 12 | +{ |
| 13 | + namespace Internal |
| 14 | + { |
| 15 | + template <typename T> |
| 16 | + struct has_value_type { |
| 17 | + typedef char yes[1]; |
| 18 | + typedef char no[2]; |
| 19 | + |
| 20 | + template <typename C> |
| 21 | + static yes& test(typename C::value_type*); |
| 22 | + |
| 23 | + template <typename> |
| 24 | + static no& test(...); |
| 25 | + |
| 26 | + constexpr static const bool value = sizeof(test<T>(nullptr)) == sizeof(yes); |
| 27 | + using type = mplex::bool_ <value>; |
| 28 | + }; |
| 29 | + |
| 30 | + template <typename T> |
| 31 | + using has_value_type_t = typename has_value_type <T>::type; |
| 32 | + |
| 33 | + struct get_value_type |
| 34 | + { |
| 35 | + template <typename T> |
| 36 | + struct apply |
| 37 | + { |
| 38 | + using type = typename T::value_type; |
| 39 | + }; |
| 40 | + }; |
| 41 | + |
| 42 | + template <typename T> |
| 43 | + struct is_optional |
| 44 | + { |
| 45 | + using __value_type = mplex::eval_if_default_t <has_value_type_t <T>, mplex::null_t, get_value_type, T>; |
| 46 | + using type = mplex::eval_if_default_t < |
| 47 | + mplex::bool_ <!std::is_same <__value_type, mplex::null_t>::value>, |
| 48 | + mplex::false_, |
| 49 | + mplex::is_same, |
| 50 | + T, |
| 51 | + boost::optional <__value_type> |
| 52 | + >; |
| 53 | + constexpr static const bool value = type::value; |
| 54 | + }; |
| 55 | + |
| 56 | + template <typename T, bool _is_optional> |
| 57 | + struct is_optional_set_impl |
| 58 | + { |
| 59 | + static inline bool isSet(T&&) noexcept { return true; } |
| 60 | + }; |
| 61 | + |
| 62 | + template <typename T> |
| 63 | + struct is_optional_set_impl <T, true> |
| 64 | + { |
| 65 | + static inline bool isSet(T&& t) noexcept { return t.operator bool(); } |
| 66 | + }; |
| 67 | + |
| 68 | + template <typename T> |
| 69 | + inline bool is_optional_set(T&& t) noexcept { |
| 70 | + using decayed = typename std::decay <T>::type; |
| 71 | + return is_optional_set_impl <T, is_optional <decayed>::value>::isSet(std::forward <T> (t)); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + template <typename T> |
| 76 | + std::ostream& stringify (std::ostream& stream, std::string const& name, boost::optional <T> const& value, StringificationOptions const& options = DEFAULT_OPTIONS) |
| 77 | + { |
| 78 | + // only write if actually existing |
| 79 | + if (value) |
| 80 | + return stringify (stream, name, value.value(), options); |
| 81 | + else |
| 82 | + return stream; // do nothing |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +#endif // JSS_OPTIONAL_H_INCLUDED |
0 commit comments