Commit 230375b3 authored by neverlord's avatar neverlord

get_or_else

parent aba4653d
#ifndef OPTION_HPP #ifndef OPTION_HPP
#define OPTION_HPP #define OPTION_HPP
namespace cppa { namespace util { namespace cppa {
template<typename What> template<typename What>
class option class option
...@@ -36,14 +36,16 @@ class option ...@@ -36,14 +36,16 @@ class option
cr(std::forward<V>(value)); cr(std::forward<V>(value));
} }
option(option const& other) : m_valid(other.m_valid) option(option const& other)
{ {
if (other.m_valid) cr(other.m_value); if (other.m_valid) cr(other.m_value);
else m_valid = false;
} }
option(option&& other) : m_valid(other.m_valid) option(option&& other)
{ {
if (other.m_valid) cr(std::move(other.m_value)); if (other.m_valid) cr(std::move(other.m_value));
else m_valid = false;
} }
~option() ~option()
...@@ -126,8 +128,20 @@ class option ...@@ -126,8 +128,20 @@ class option
inline What const& get() const { return m_value; } inline What const& get() const { return m_value; }
inline What& get_or_else(What const& val)
{
if (!m_valid) cr(val);
return m_value;
}
inline What& get_or_else(What&& val)
{
if (!m_valid) cr(std::move(val));
return m_value;
}
}; };
} } // namespace cppa::util } // namespace cppa::util
#endif // OPTION_HPP #endif // OPTION_HPP
...@@ -43,12 +43,12 @@ namespace cppa { ...@@ -43,12 +43,12 @@ namespace cppa {
// cast using a pattern // cast using a pattern
template<typename... P> template<typename... P>
auto tuple_cast(any_tuple const& tup, pattern<P...> const& p) auto tuple_cast(any_tuple const& tup, pattern<P...> const& p)
-> util::option<typename tuple_from_type_list<typename pattern<P...>::filtered_types>::type> -> option<typename tuple_from_type_list<typename pattern<P...>::filtered_types>::type>
{ {
typedef typename pattern<P...>::mapping_vector mapping_vector; typedef typename pattern<P...>::mapping_vector mapping_vector;
typedef typename pattern<P...>::filtered_types filtered_types; typedef typename pattern<P...>::filtered_types filtered_types;
typedef typename tuple_from_type_list<filtered_types>::type tuple_type; typedef typename tuple_from_type_list<filtered_types>::type tuple_type;
util::option<tuple_type> result; option<tuple_type> result;
mapping_vector mv; mapping_vector mv;
if (detail::matches(detail::pm_decorated(tup.begin(), &mv), p.begin())) if (detail::matches(detail::pm_decorated(tup.begin(), &mv), p.begin()))
{ {
...@@ -66,9 +66,9 @@ auto tuple_cast(any_tuple const& tup, pattern<P...> const& p) ...@@ -66,9 +66,9 @@ auto tuple_cast(any_tuple const& tup, pattern<P...> const& p)
// cast using types // cast using types
template<typename... T> template<typename... T>
util::option< tuple<T...> > tuple_cast(any_tuple const& tup) option< tuple<T...> > tuple_cast(any_tuple const& tup)
{ {
util::option< tuple<T...> > result; option< tuple<T...> > result;
auto& tarr = detail::static_types_array<T...>::arr; auto& tarr = detail::static_types_array<T...>::arr;
if (tup.size() == sizeof...(T)) if (tup.size() == sizeof...(T))
{ {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment