Commit 84c56c73 authored by Dominik Charousset's avatar Dominik Charousset

Switch back from maybe to optional

The class `maybe` is obsoleted by `result` in all cases that could return an
error. In all other cases, `optional` is equally well suited to do the job.
Since C++17 will contain an `optional` class, it makes more sense to use this
abstraction and switch to the standard library version once available.
parent eaf9cdc2
......@@ -177,7 +177,7 @@ behavior server(broker* self, const actor& buddy) {
};
}
maybe<uint16_t> as_u16(const std::string& str) {
optional<uint16_t> as_u16(const std::string& str) {
return static_cast<uint16_t>(stoul(str));
}
......@@ -209,19 +209,15 @@ int main(int argc, char** argv) {
cout << "run in server mode" << endl;
auto pong_actor = system.spawn(pong);
auto server_actor = system.middleman().spawn_server(server, port, pong_actor);
if (server_actor) {
print_on_exit(*server_actor, "server");
print_on_exit(pong_actor, "pong");
}
print_on_exit(server_actor, "server");
print_on_exit(pong_actor, "pong");
} else if (res.opts.count("client") > 0) {
auto ping_actor = system.spawn(ping, size_t{20});
auto io_actor = system.middleman().spawn_client(broker_impl, host,
port, ping_actor);
if (io_actor) {
print_on_exit(ping_actor, "ping");
print_on_exit(*io_actor, "protobuf_io");
send_as(*io_actor, ping_actor, kickoff_atom::value, *io_actor);
}
print_on_exit(ping_actor, "ping");
print_on_exit(io_actor, "protobuf_io");
send_as(io_actor, ping_actor, kickoff_atom::value, io_actor);
} else {
cerr << "*** neither client nor server mode set" << endl
<< res.helptext << endl;
......
......@@ -67,7 +67,7 @@ behavior server(broker* self) {
};
}
maybe<uint16_t> as_u16(const std::string& str) {
optional<uint16_t> as_u16(const std::string& str) {
return static_cast<uint16_t>(stoul(str));
}
......@@ -97,12 +97,9 @@ int main(int argc, const char** argv) {
cout << "*** to quit the program, simply press <enter>" << endl;
actor_system system;
auto server_actor = system.middleman().spawn_server(server, port);
if (! server_actor)
return cerr << "*** spawn_server failed: "
<< system.render(server_actor.error()) << endl, 1;
// wait for any input
std::string dummy;
std::getline(std::cin, dummy);
// kill server
anon_send_exit(*server_actor, exit_reason::user_shutdown);
anon_send_exit(server_actor, exit_reason::user_shutdown);
}
......@@ -171,7 +171,7 @@ string trim(std::string s) {
}
// tries to convert `str` to an int
maybe<int> toint(const string& str) {
optional<int> toint(const string& str) {
char* end;
auto result = static_cast<int>(strtol(str.c_str(), &end, 10));
if (end == str.c_str() + str.size()) {
......@@ -181,7 +181,7 @@ maybe<int> toint(const string& str) {
}
// converts "+" to the atom '+' and "-" to the atom '-'
maybe<atom_value> plus_or_minus(const string& str) {
optional<atom_value> plus_or_minus(const string& str) {
if (str == "+")
return plus_atom::value;
if (str == "-")
......
......@@ -25,8 +25,9 @@
#include <typeinfo>
#include <exception>
#include "caf/maybe.hpp"
#include "caf/optional.hpp"
#include "caf/exit_reason.hpp"
#include "caf/execution_unit.hpp"
namespace caf {
......@@ -71,7 +72,7 @@ public:
/// Note that the first handler to handle `eptr` "wins" and no other
/// handler will be invoked.
/// @returns The exit reason the actor should use.
virtual maybe<exit_reason> handle_exception(const std::exception_ptr& eptr);
virtual optional<exit_reason> handle_exception(const std::exception_ptr& eptr);
/// Executed if the actor finished execution with given `reason`.
/// The default implementation does nothing.
......
......@@ -97,7 +97,7 @@ public:
}
/// Runs this handler and returns its (optional) result.
inline maybe<message> operator()(message& x) {
inline optional<message> operator()(message& x) {
return impl_ ? impl_->invoke(x) : none;
}
......
......@@ -25,7 +25,7 @@
#include "caf/none.hpp"
#include "caf/variant.hpp"
#include "caf/maybe.hpp"
#include "caf/optional.hpp"
#include "caf/match_case.hpp"
#include "caf/make_counted.hpp"
#include "caf/intrusive_ptr.hpp"
......@@ -75,7 +75,7 @@ public:
return invoke(f, tmp);
}
maybe<message> invoke(message&);
optional<message> invoke(message&);
virtual void handle_timeout();
......
......@@ -25,7 +25,7 @@
#include <utility>
#include <algorithm>
#include "caf/maybe.hpp"
#include "caf/optional.hpp"
#include "caf/config.hpp"
#include "caf/behavior.hpp"
......
......@@ -22,7 +22,7 @@
#include <tuple>
#include "caf/maybe.hpp"
#include "caf/optional.hpp"
#include "caf/delegated.hpp"
#include "caf/replies_to.hpp"
#include "caf/typed_response_promise.hpp"
......@@ -52,7 +52,7 @@ struct ctm_cmp<typed_mpi<In, OutList>,
template <class In, class Out>
struct ctm_cmp<typed_mpi<In, type_list<Out>>,
typed_mpi<In, type_list<maybe<Out>>>>
typed_mpi<In, type_list<optional<Out>>>>
: std::true_type { };
template <class In, class... Ts>
......@@ -62,7 +62,12 @@ struct ctm_cmp<typed_mpi<In, type_list<Ts...>>,
template <class In, class... Ts>
struct ctm_cmp<typed_mpi<In, type_list<Ts...>>,
typed_mpi<In, type_list<maybe<std::tuple<Ts...>>>>>
typed_mpi<In, type_list<optional<std::tuple<Ts...>>>>>
: std::true_type { };
template <class In, class... Ts>
struct ctm_cmp<typed_mpi<In, type_list<Ts...>>,
typed_mpi<In, type_list<result<Ts...>>>>
: std::true_type { };
template <class In, class Out>
......
......@@ -25,7 +25,7 @@
#include "caf/fwd.hpp"
#include "caf/none.hpp"
#include "caf/unit.hpp"
#include "caf/maybe.hpp"
#include "caf/optional.hpp"
#include "caf/result.hpp"
#include "caf/message.hpp"
#include "caf/skip_message.hpp"
......@@ -66,15 +66,11 @@ public:
// unwrap maybes
template <class T>
void operator()(maybe<T>& x) {
void operator()(optional<T>& x) {
if (x)
(*this)(*x);
else if (x.empty())
else
(*this)(none);
else {
auto tmp = x.error();
(*this)(tmp);
}
}
// convert values to messages
......@@ -132,8 +128,8 @@ public:
return false;
}
inline bool visit(maybe<skip_message_t>& x) {
if (x.valid())
inline bool visit(optional<skip_message_t>& x) {
if (x)
return false;
(*this)(x);
return true;
......
......@@ -22,7 +22,7 @@
#include "caf/none.hpp"
#include "caf/unit.hpp"
#include "caf/maybe.hpp"
#include "caf/optional.hpp"
#include "caf/delegated.hpp"
#include "caf/skip_message.hpp"
#include "caf/static_visitor.hpp"
......@@ -64,17 +64,17 @@ struct optional_message_visitor_enable_tpl {
none_t,
unit_t,
skip_message_t,
maybe<skip_message_t>
optional<skip_message_t>
>::value
&& ! is_message_id_wrapper<T>::value
&& ! is_response_promise<T>::value;
};
class optional_message_visitor : public static_visitor<maybe<message>> {
class optional_message_visitor : public static_visitor<optional<message>> {
public:
optional_message_visitor() = default;
using opt_msg = maybe<message>;
using opt_msg = optional<message>;
inline opt_msg operator()(const none_t&) const {
return none;
......@@ -88,7 +88,7 @@ public:
return message{};
}
inline opt_msg operator()(maybe<skip_message_t>& val) const {
inline opt_msg operator()(optional<skip_message_t>& val) const {
if (val)
return none;
return message{};
......@@ -126,7 +126,7 @@ public:
}
template <class T>
opt_msg operator()(maybe<T>& value) const {
opt_msg operator()(optional<T>& value) const {
if (value)
return (*this)(*value);
if (value.empty())
......
......@@ -25,7 +25,7 @@
#include <functional>
#include "caf/atom.hpp"
#include "caf/maybe.hpp"
#include "caf/optional.hpp"
#include "caf/variant.hpp"
namespace caf {
......@@ -45,7 +45,7 @@ struct parse_ini_t {
/// @param consumer Callback consuming generated key-value pairs.
void operator()(std::istream& raw_data,
config_consumer consumer,
maybe<std::ostream&> errors = none) const;
optional<std::ostream&> errors = none) const;
};
......
......@@ -27,6 +27,7 @@ namespace caf {
// templates
template <class> class maybe;
template <class> class optional;
template <class> class intrusive_ptr;
template <class> struct actor_cast_access;
template <class> class typed_continue_helper;
......
......@@ -404,7 +404,7 @@ public:
functor_attachable(F arg) : functor_(std::move(arg)) {
// nop
}
maybe<exit_reason> handle_exception(const std::exception_ptr& eptr) {
optional<exit_reason> handle_exception(const std::exception_ptr& eptr) {
return functor_(eptr);
}
};
......
......@@ -21,7 +21,7 @@
#define CAF_MATCH_CASE_HPP
#include "caf/none.hpp"
#include "caf/maybe.hpp"
#include "caf/optional.hpp"
#include "caf/skip_message.hpp"
#include "caf/match_case.hpp"
......@@ -86,7 +86,7 @@ T& unopt(T& v) {
}
template <class T>
T& unopt(maybe<T>& v) {
T& unopt(optional<T>& v) {
return *v;
}
......@@ -101,7 +101,7 @@ struct has_none {
}
template <class T, class... Ts>
bool operator()(const maybe<T>& x, const Ts&... xs) const {
bool operator()(const optional<T>& x, const Ts&... xs) const {
return ! x || (*this)(xs...);
}
};
......@@ -350,13 +350,13 @@ public:
static constexpr uint32_t static_type_token =
detail::make_type_token_from_list<Pattern>();
// Let F be "R (Ts...)" then match_case<F...> returns maybe<R>
// Let F be "R (Ts...)" then match_case<F...> returns optional<R>
// unless R is void in which case bool is returned
using optional_result_type =
typename std::conditional<
std::is_same<result_type, void>::value,
maybe<unit_t>,
maybe<result_type>
optional<unit_t>,
optional<result_type>
>::type;
// Needed for static type checking when assigning to a typed behavior.
......
......@@ -407,7 +407,7 @@ private:
// Represents a computation performing side effects only and
// optionally return a `std::error_condition`.
// maybely return a `std::error_condition`.
template <>
class maybe<void> {
public:
......
......@@ -27,6 +27,7 @@
#include "caf/fwd.hpp"
#include "caf/atom.hpp"
#include "caf/config.hpp"
#include "caf/optional.hpp"
#include "caf/make_counted.hpp"
#include "caf/skip_message.hpp"
#include "caf/index_mapping.hpp"
......@@ -121,7 +122,7 @@ public:
}
/// Returns `handler(*this)`.
maybe<message> apply(message_handler handler);
optional<message> apply(message_handler handler);
/// Filters this message by applying slices of it to `handler` and returns
/// the remaining elements of this operation. Slices are generated in the
......
......@@ -88,7 +88,7 @@ public:
}
/// @copydoc message::apply
maybe<message> apply(message_handler handler);
optional<message> apply(message_handler handler);
/// Removes all elements from the buffer.
void clear();
......
......@@ -90,7 +90,7 @@ public:
void assign(message_handler other);
/// Runs this handler and returns its (optional) result.
inline maybe<message> operator()(message& arg) {
inline optional<message> operator()(message& arg) {
return (impl_) ? impl_->invoke(arg) : none;
}
......
......@@ -99,7 +99,7 @@ protected:
bool remove_backlink_impl(const actor_addr& other);
// tries to run a custom exception handler for `eptr`
maybe<exit_reason> handle(const std::exception_ptr& eptr);
optional<exit_reason> handle(const std::exception_ptr& eptr);
// precondition: `mtx_` is acquired
inline void attach_impl(attachable_ptr& ptr) {
......
......@@ -90,8 +90,8 @@ constexpr auto arg_match = detail::boxed<detail::arg_match_t>::type();
/// Generates function objects from a binary predicate and a value.
template <class T, typename BinaryPredicate>
std::function<maybe<T>(const T&)> guarded(BinaryPredicate p, T value) {
return [=](const T& other) -> maybe<T> {
std::function<optional<T>(const T&)> guarded(BinaryPredicate p, T value) {
return [=](const T& other) -> optional<T> {
if (p(other, value)) {
return value;
}
......@@ -120,7 +120,7 @@ unit_t to_guard(const detail::wrapped<T>&) {
}
template <class T>
std::function<maybe<typename detail::strip_and_convert<T>::type>(
std::function<optional<typename detail::strip_and_convert<T>::type>(
const typename detail::strip_and_convert<T>::type&)>
to_guard(const T& value,
typename std::enable_if<! detail::is_callable<T>::value>::type* = 0) {
......
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright (C) 2011 - 2014 *
* Dominik Charousset <dominik.charousset (at) haw-hamburg.de> *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#ifndef CAF_OPTIONAL_HPP
#define CAF_OPTIONAL_HPP
#include <new>
#include <utility>
#include "caf/none.hpp"
#include "caf/unit.hpp"
#include "caf/config.hpp"
#include "caf/detail/safe_equal.hpp"
namespace caf {
/// A since C++17 compatible `optional` implementation.
template <class T>
class optional {
public:
/// Typdef for `T`.
using type = T;
/// Creates an instance without value.
optional(const none_t& = none) : m_valid(false) {
// nop
}
/// Creates an valid instance from `value`.
template <class U,
class E = typename std::enable_if<
std::is_convertible<U, T>::value
>::type>
optional(U value) : m_valid(false) {
cr(std::move(value));
}
optional(const optional& other) : m_valid(false) {
if (other.m_valid) {
cr(other.m_value);
}
}
optional(optional&& other) : m_valid(false) {
if (other.m_valid) {
cr(std::move(other.m_value));
}
}
~optional() {
destroy();
}
optional& operator=(const optional& other) {
if (m_valid) {
if (other.m_valid) m_value = other.m_value;
else destroy();
}
else if (other.m_valid) {
cr(other.m_value);
}
return *this;
}
optional& operator=(optional&& other) {
if (m_valid) {
if (other.m_valid) m_value = std::move(other.m_value);
else destroy();
}
else if (other.m_valid) {
cr(std::move(other.m_value));
}
return *this;
}
/// Checks whether this object contains a value.
explicit operator bool() const {
return m_valid;
}
/// Checks whether this object does not contain a value.
bool operator!() const {
return ! m_valid;
}
/// Returns the value.
T& operator*() {
CAF_ASSERT(m_valid);
return m_value;
}
/// Returns the value.
const T& operator*() const {
CAF_ASSERT(m_valid);
return m_value;
}
/// Returns the value.
const T* operator->() const {
CAF_ASSERT(m_valid);
return &m_value;
}
/// Returns the value.
T* operator->() {
CAF_ASSERT(m_valid);
return &m_value;
}
/// Returns the value.
T& value() {
CAF_ASSERT(m_valid);
return m_value;
}
/// Returns the value.
const T& value() const {
CAF_ASSERT(m_valid);
return m_value;
}
/// Returns the value if `m_valid`, otherwise returns `default_value`.
const T& value_or(const T& default_value) const {
return m_valid ? value() : default_value;
}
private:
void destroy() {
if (m_valid) {
m_value.~T();
m_valid = false;
}
}
template <class V>
void cr(V&& value) {
CAF_ASSERT(! m_valid);
m_valid = true;
new (&m_value) T(std::forward<V>(value));
}
bool m_valid;
union { T m_value; };
};
/// Template specialization to allow `optional`
/// to hold a reference rather than an actual value.
template <class T>
class optional<T&> {
public:
using type = T;
optional(const none_t& = none) : m_value(nullptr) {
// nop
}
optional(T& value) : m_value(&value) {
// nop
}
optional(const optional& other) = default;
optional& operator=(const optional& other) = default;
explicit operator bool() const {
return m_value != nullptr;
}
bool operator!() const {
return m_value != nullptr;
}
T& operator*() {
CAF_ASSERT(m_value);
return *m_value;
}
const T& operator*() const {
CAF_ASSERT(m_value);
return *m_value;
}
T* operator->() {
CAF_ASSERT(m_value);
return m_value;
}
const T* operator->() const {
CAF_ASSERT(m_value);
return m_value;
}
T& value() {
CAF_ASSERT(m_value);
return *m_value;
}
const T& value() const {
CAF_ASSERT(m_value);
return *m_value;
}
const T& value_or(const T& default_value) const {
if (m_value)
return value();
return default_value;
}
private:
T* m_value;
};
/// @relates optional
template <class T, class U>
bool operator==(const optional<T>& lhs, const optional<U>& rhs) {
if (lhs)
return rhs ? detail::safe_equal(*lhs, *rhs) : false;
return ! rhs;
}
/// @relates optional
template <class T, class U>
bool operator==(const optional<T>& lhs, const U& rhs) {
return (lhs) ? *lhs == rhs : false;
}
/// @relates optional
template <class T, class U>
bool operator==(const T& lhs, const optional<U>& rhs) {
return rhs == lhs;
}
/// @relates optional
template <class T, class U>
bool operator!=(const optional<T>& lhs, const optional<U>& rhs) {
return ! (lhs == rhs);
}
/// @relates optional
template <class T, class U>
bool operator!=(const optional<T>& lhs, const U& rhs) {
return ! (lhs == rhs);
}
/// @relates optional
template <class T, class U>
bool operator!=(const T& lhs, const optional<U>& rhs) {
return ! (lhs == rhs);
}
/// @relates optional
template <class T, class U>
bool operator<(const optional<T>& lhs, const optional<U>& rhs) {
// none is considered smaller than any actual value
if (lhs)
return rhs ? *lhs < *rhs : false;
return static_cast<bool>(rhs);
}
/// @relates optional
template <class T, class U>
bool operator<(const optional<T>& lhs, const U& rhs) {
if (lhs)
return *lhs < rhs;
return true;
}
/// @relates optional
template <class T, class U>
bool operator<(T& lhs, const optional<U>& rhs) {
if (rhs)
return lhs < *rhs;
return false;
}
} // namespace caf
#endif // CAF_OPTIONAL_HPP
......@@ -137,7 +137,8 @@ public:
}
}
}
mfun(msg_ptr->msg);
msg_ptr->msg.apply(mfun);
//mfun(msg_ptr->msg);
msg_ptr.reset();
}
}
......@@ -249,7 +250,7 @@ void printer_loop(blocking_actor* self) {
sink_handle global_redirect;
data_map data;
auto get_data = [&](const actor_addr& addr, bool insert_missing)
-> maybe<actor_data&> {
-> optional<actor_data&> {
if (addr == invalid_actor_addr)
return none;
auto i = data.find(addr);
......@@ -261,7 +262,7 @@ void printer_loop(blocking_actor* self) {
return i->second;
return none;
};
auto flush = [&](maybe<actor_data&> what, bool forced) {
auto flush = [&](optional<actor_data&> what, bool forced) {
if (! what)
return;
auto& line = what->current_line;
......
......@@ -257,7 +257,7 @@ void actor_registry::start() {
CAF_LOG_TRACE("");
return {
[=](get_atom, const std::string& name, message& args)
-> maybe<std::tuple<ok_atom, actor_addr, std::set<std::string>>> {
-> result<ok_atom, actor_addr, std::set<std::string>> {
CAF_LOG_TRACE(CAF_ARG(name) << CAF_ARG(args));
actor_config cfg{self->context()};
auto res = self->system().types().make_actor(name, cfg, args);
......
......@@ -30,7 +30,7 @@ attachable::token::token(size_t typenr, const void* vptr)
// nop
}
maybe<exit_reason> attachable::handle_exception(const std::exception_ptr&) {
optional<exit_reason> attachable::handle_exception(const std::exception_ptr&) {
return none;
}
......
......@@ -56,14 +56,14 @@ private:
class maybe_message_visitor : public detail::invoke_result_visitor {
public:
maybe<message> value;
optional<message> value;
void operator()() override {
value = message{};
}
void operator()(error& x) override {
value = std::move(x);
value = make_message(std::move(x));
}
void operator()(message& x) override {
......@@ -103,7 +103,7 @@ bool behavior_impl::invoke(detail::invoke_result_visitor& f, message& msg) {
return false;
}
maybe<message> behavior_impl::invoke(message& x) {
optional<message> behavior_impl::invoke(message& x) {
maybe_message_visitor f;
invoke(f, x);
return std::move(f.value);
......
......@@ -118,7 +118,7 @@ message message::slice(size_t pos, size_t n) const {
return message{detail::decorated_tuple::make(vals_, std::move(mapping))};
}
maybe<message> message::apply(message_handler handler) {
optional<message> message::apply(message_handler handler) {
return handler(*this);
}
......@@ -237,7 +237,7 @@ message::cli_res message::extract_opts(std::vector<cli_arg> xs,
// store any occurred error in a temporary variable returned at the end
std::string error;
auto res = extract({
[&](const std::string& arg) -> maybe<skip_message_t> {
[&](const std::string& arg) -> optional<skip_message_t> {
if (arg.empty() || arg.front() != '-') {
return skip_message();
}
......@@ -282,7 +282,7 @@ message::cli_res message::extract_opts(std::vector<cli_arg> xs,
return skip_message();
},
[&](const std::string& arg1,
const std::string& arg2) -> maybe<skip_message_t> {
const std::string& arg2) -> optional<skip_message_t> {
if (arg1.size() < 2 || arg1[0] != '-' || arg1[1] == '-') {
return skip_message();
}
......
......@@ -72,7 +72,7 @@ message message_builder::move_to_message() {
return result;
}
maybe<message> message_builder::apply(message_handler handler) {
optional<message> message_builder::apply(message_handler handler) {
// avoid detaching of data_ by moving the data to a message object,
// calling message::apply and moving the data back
message::data_ptr ptr;
......
......@@ -97,7 +97,7 @@ monitorable_actor::monitorable_actor(actor_system* sys, actor_id aid,
// nop
}
maybe<exit_reason> monitorable_actor::handle(const std::exception_ptr& eptr) {
optional<exit_reason> monitorable_actor::handle(const std::exception_ptr& eptr) {
std::unique_lock<std::mutex> guard{mtx_};
for (auto i = attachables_head_.get(); i != nullptr; i = i->next.get()) {
try {
......
......@@ -28,7 +28,7 @@ namespace caf {
namespace detail {
void parse_ini_t::operator()(std::istream& input, config_consumer consumer_fun,
maybe<std::ostream&> errors) const {
optional<std::ostream&> errors) const {
std::string group;
std::string line;
size_t ln = 0; // line number
......
......@@ -30,7 +30,7 @@ class exception_testee : public event_based_actor {
public:
~exception_testee();
exception_testee(actor_config& cfg) : event_based_actor(cfg) {
set_exception_handler([](const std::exception_ptr&) -> maybe<exit_reason> {
set_exception_handler([](const std::exception_ptr&) -> optional<exit_reason> {
return exit_reason::remote_link_unreachable;
});
}
......@@ -49,7 +49,7 @@ exception_testee::~exception_testee() {
CAF_TEST(test_custom_exception_handler) {
actor_system system;
auto handler = [](const std::exception_ptr& eptr) -> maybe<exit_reason> {
auto handler = [](const std::exception_ptr& eptr) -> optional<exit_reason> {
try {
std::rethrow_exception(eptr);
}
......
......@@ -683,7 +683,7 @@ namespace {
class exception_testee : public event_based_actor {
public:
exception_testee(actor_config& cfg) : event_based_actor(cfg) {
set_exception_handler([](const std::exception_ptr&) -> maybe<exit_reason> {
set_exception_handler([](const std::exception_ptr&) -> optional<exit_reason> {
return exit_reason::unhandled_exception;
});
}
......@@ -699,7 +699,7 @@ public:
} // namespace <anonymous>
CAF_TEST(custom_exception_handler) {
auto handler = [](const std::exception_ptr& eptr) -> maybe<exit_reason> {
auto handler = [](const std::exception_ptr& eptr) -> optional<exit_reason> {
try {
std::rethrow_exception(eptr);
}
......
......@@ -38,7 +38,7 @@ CAF_TEST(simple_ints) {
auto one = on(1) >> [] { };
auto two = on(2) >> [] { };
auto three = on(3) >> [] { };
auto skip_two = [](int i) -> maybe<skip_message_t> {
auto skip_two = [](int i) -> optional<skip_message_t> {
if (i == 2)
return skip_message();
return none;
......
......@@ -51,7 +51,7 @@ calculator::behavior_type multiplier() {
calculator::behavior_type divider() {
return {
[](int x, int y) -> maybe<int> {
[](int x, int y) -> optional<int> {
if (y == 0)
return none;
return x / y;
......
......@@ -37,15 +37,15 @@ using namespace std;
using hi_atom = atom_constant<atom("hi")>;
using ho_atom = atom_constant<atom("ho")>;
function<maybe<string>(const string&)> starts_with(const string& s) {
return [=](const string& str) -> maybe<string> {
function<optional<string>(const string&)> starts_with(const string& s) {
return [=](const string& str) -> optional<string> {
if (str.size() > s.size() && str.compare(0, s.size(), s) == 0)
return str.substr(s.size());
return none;
};
}
maybe<int> toint(const string& str) {
optional<int> toint(const string& str) {
char* endptr = nullptr;
int result = static_cast<int>(strtol(str.c_str(), &endptr, 10));
if (endptr != nullptr && *endptr == '\0') {
......@@ -124,7 +124,7 @@ CAF_TEST(atom_constants) {
CAF_TEST(guards_called) {
bool guard_called = false;
auto guard = [&](int arg) -> maybe<int> {
auto guard = [&](int arg) -> optional<int> {
guard_called = true;
return arg;
};
......
......@@ -376,7 +376,7 @@ printf("******* %s %d\n", __FILE__, __LINE__);
[](error_atom) {
CAF_ERROR("A didn't receive sync response");
},
[&](const down_msg& dm) -> maybe<skip_message_t> {
[&](const down_msg& dm) -> optional<skip_message_t> {
if (dm.reason == exit_reason::normal)
return skip_message();
CAF_ERROR("A exited for reason " << to_string(dm.reason));
......
......@@ -74,7 +74,7 @@ struct fixture {
void await_down(const actor& x) {
self->monitor(x);
self->receive(
[&](const down_msg& dm) -> maybe<skip_message_t> {
[&](const down_msg& dm) -> optional<skip_message_t> {
if (dm.source != x)
return skip_message();
return none;
......
......@@ -249,11 +249,9 @@ using maybe_string_actor = typed_actor<replies_to<string>
maybe_string_actor::behavior_type maybe_string_reverter() {
return {
[](string& str) -> maybe<std::tuple<ok_atom, string>> {
[](string& str) -> result<ok_atom, string> {
if (str.empty())
return mock_errc::cannot_revert_empty;
if (str.empty())
return none;
std::reverse(str.begin(), str.end());
return {ok_atom::value, std::move(str)};
}
......@@ -461,7 +459,7 @@ CAF_TEST(sending_typed_actors_and_down_msg) {
CAF_TEST(check_signature) {
using foo_type = typed_actor<replies_to<put_atom>::with<ok_atom>>;
using foo_result_type = maybe<ok_atom>;
using foo_result_type = optional<ok_atom>;
using bar_type = typed_actor<reacts_to<ok_atom>>;
auto foo_action = [](foo_type::pointer self) -> foo_type::behavior_type {
return {
......
......@@ -121,7 +121,7 @@ public:
void handle_node_shutdown(const node_id& affected_node);
/// Returns a route to `target` or `none` on error.
maybe<routing_table::route> lookup(const node_id& target);
optional<routing_table::route> lookup(const node_id& target);
/// Flushes the underlying buffer of `path`.
void flush(const routing_table::route& path);
......@@ -195,7 +195,7 @@ public:
/// if no actor is published at this port then a standard handshake is
/// written (e.g. used when establishing direct connections on-the-fly).
void write_server_handshake(execution_unit* ctx,
buffer_type& buf, maybe<uint16_t> port);
buffer_type& buf, optional<uint16_t> port);
/// Writes the client handshake to `buf`.
void write_client_handshake(execution_unit* ctx,
......
......@@ -56,7 +56,7 @@ public:
using erase_callback = callback<const node_id&>;
/// Returns a route to `target` or `none` on error.
maybe<route> lookup(const node_id& target);
optional<route> lookup(const node_id& target);
/// Returns the ID of the peer connected via `hdl` or
/// `invalid_node_id` if `hdl` is unknown.
......
......@@ -98,7 +98,7 @@ struct basp_broker_state : proxy_registry::backend, basp::instance::callee {
// connected port
uint16_t remote_port;
// pending operations to be performed after handhsake completed
maybe<response_promise> callback;
optional<response_promise> callback;
};
void set_context(connection_handle hdl);
......
......@@ -26,7 +26,6 @@
#include <thread>
#include "caf/fwd.hpp"
#include "caf/maybe.hpp"
#include "caf/node_id.hpp"
#include "caf/actor_system.hpp"
#include "caf/proxy_registry.hpp"
......@@ -219,7 +218,7 @@ public:
/// @warning Blocks the caller for the duration of the connection process.
template <spawn_options Os = no_spawn_options,
class F = std::function<void(broker*)>, class... Ts>
maybe<typename infer_handle_from_fun<F>::type>
typename infer_handle_from_fun<F>::type
spawn_client(F fun, const std::string& host, uint16_t port, Ts&&... xs) {
using impl = typename infer_handle_from_fun<F>::impl;
return spawn_client_impl<Os, impl>(std::move(fun), host, port,
......@@ -230,7 +229,7 @@ public:
/// @warning Blocks the caller until the server socket is initialized.
template <spawn_options Os = no_spawn_options,
class F = std::function<void(broker*)>, class... Ts>
maybe<typename infer_handle_from_fun<F>::type>
typename infer_handle_from_fun<F>::type
spawn_server(F fun, uint16_t port, Ts&&... xs) {
using impl = typename infer_handle_from_fun<F>::impl;
return spawn_server_impl<Os, impl>(std::move(fun), port,
......
......@@ -481,7 +481,7 @@ private:
};
native_socket new_tcp_connection(const std::string& host, uint16_t port,
maybe<protocol> preferred = none);
optional<protocol> preferred = none);
std::pair<native_socket, uint16_t>
new_tcp_acceptor_impl(uint16_t port, const char* addr, bool reuse_addr);
......
......@@ -27,7 +27,7 @@
#include <functional>
#include <initializer_list>
#include "caf/maybe.hpp"
#include "caf/optional.hpp"
#include "caf/io/network/protocol.hpp"
......@@ -70,8 +70,8 @@ public:
bool include_localhost = true);
/// Returns a native IPv4 or IPv6 translation of `host`.
static maybe<std::pair<std::string, protocol>>
native_address(const std::string& host, maybe<protocol> preferred = none);
static optional<std::pair<std::string, protocol>>
native_address(const std::string& host, optional<protocol> preferred = none);
};
} // namespace network
......
......@@ -511,7 +511,7 @@ printf("enable automatic connections\n");
// received from some system calls like whereis
[=](forward_atom, const actor_addr& sender,
const node_id& receiving_node, atom_value receiver_name,
const message& msg) -> maybe<message> {
const message& msg) -> result<void> {
CAF_LOG_TRACE(CAF_ARG(sender)
<< ", " << CAF_ARG(receiving_node)
<< ", " << CAF_ARG(receiver_name)
......@@ -540,7 +540,7 @@ printf("enable automatic connections\n");
std::numeric_limits<actor_id>::max(),
&writer);
state.instance.flush(*path);
return none;
return unit;
},
// received from underlying broker implementation
[=](const new_connection_msg& msg) {
......@@ -614,7 +614,7 @@ printf("enable automatic connections\n");
CAF_LOG_TRACE(CAF_ARG(nid) << ", " << CAF_ARG(aid));
state.proxies().erase(nid, aid);
},
[=](unpublish_atom, const actor_addr& whom, uint16_t port) -> maybe<void> {
[=](unpublish_atom, const actor_addr& whom, uint16_t port) -> result<void> {
CAF_LOG_TRACE(CAF_ARG(whom) << CAF_ARG(port));
if (whom == invalid_actor_addr)
return sec::no_actor_to_unpublish;
......@@ -624,9 +624,9 @@ printf("enable automatic connections\n");
});
if (state.instance.remove_published_actor(whom, port, &cb) == 0)
return sec::no_actor_published_at_port;
return {};
return unit;
},
[=](close_atom, uint16_t port) -> maybe<void> {
[=](close_atom, uint16_t port) -> result<void> {
if (port == 0)
return sec::cannot_close_invalid_port;
// it is well-defined behavior to not have an actor published here,
......@@ -638,7 +638,7 @@ printf("enable automatic connections\n");
catch (std::exception&) {
return sec::cannot_close_invalid_port;
}
return {};
return unit;
},
[=](spawn_atom, const node_id& nid, std::string& type, message& xs)
-> delegated<ok_atom, actor_addr, std::set<std::string>> {
......
......@@ -20,7 +20,7 @@
#include "caf/io/network/default_multiplexer.hpp"
#include "caf/config.hpp"
#include "caf/maybe.hpp"
#include "caf/optional.hpp"
#include "caf/exception.hpp"
#include "caf/make_counted.hpp"
#include "caf/scheduler/abstract_coordinator.hpp"
......@@ -1278,7 +1278,7 @@ bool ip_connect(native_socket fd, const std::string& host, uint16_t port) {
}
native_socket new_tcp_connection(const std::string& host, uint16_t port,
maybe<protocol> preferred) {
optional<protocol> preferred) {
CAF_LOG_TRACE(CAF_ARG(host) << CAF_ARG(port) << CAF_ARG(preferred));
CAF_LOG_INFO("try to connect to:" << CAF_ARG(host) << CAF_ARG(port));
auto res = interfaces::native_address(host, preferred);
......
......@@ -224,7 +224,7 @@ void instance::handle_node_shutdown(const node_id& affected_node) {
tbl_.erase(affected_node, cb);
}
maybe<routing_table::route> instance::lookup(const node_id& target) {
optional<routing_table::route> instance::lookup(const node_id& target) {
return tbl_.lookup(target);
}
......@@ -363,7 +363,7 @@ void instance::write(execution_unit* ctx, buffer_type& buf,
void instance::write_server_handshake(execution_unit* ctx,
buffer_type& out_buf,
maybe<uint16_t> port) {
optional<uint16_t> port) {
using namespace detail;
published_actor* pa = nullptr;
if (port) {
......
......@@ -216,9 +216,9 @@ std::vector<std::string> interfaces::list_addresses(protocol proc,
return list_addresses({proc}, include_localhost);
}
maybe<std::pair<std::string, protocol>>
optional<std::pair<std::string, protocol>>
interfaces::native_address(const std::string& host,
maybe<protocol> preferred) {
optional<protocol> preferred) {
addrinfo hint;
memset(&hint, 0, sizeof(hint));
hint.ai_socktype = SOCK_STREAM;
......
......@@ -58,7 +58,7 @@ public:
return "middleman_actor";
}
using put_res = maybe<std::tuple<ok_atom, uint16_t>>;
using put_res = result<ok_atom, uint16_t>;
using mpi_set = std::set<std::string>;
......@@ -74,7 +74,7 @@ public:
CAF_LOG_TRACE("");
return {
[=](publish_atom, uint16_t port, actor_addr& whom,
mpi_set& sigs, std::string& addr, bool reuse) {
mpi_set& sigs, std::string& addr, bool reuse) -> put_res {
CAF_LOG_TRACE("");
return put(port, whom, sigs, addr.c_str(), reuse);
},
......@@ -197,14 +197,14 @@ private:
}
}
maybe<endpoint_data&> cached(const endpoint& ep) {
optional<endpoint_data&> cached(const endpoint& ep) {
auto i = cached_.find(ep);
if (i != cached_.end())
return i->second;
return none;
}
maybe<std::vector<response_promise>&> pending(const endpoint& ep) {
optional<std::vector<response_promise>&> pending(const endpoint& ep) {
auto i = pending_.find(ep);
if (i != pending_.end())
return i->second;
......
......@@ -33,7 +33,7 @@ routing_table::~routing_table() {
// nop
}
maybe<routing_table::route> routing_table::lookup(const node_id& target) {
optional<routing_table::route> routing_table::lookup(const node_id& target) {
auto hdl = lookup_direct(target);
if (hdl != invalid_connection_handle)
return route{parent_->wr_buf(hdl), target, hdl};
......
......@@ -257,7 +257,7 @@ public:
}
void connect_node(size_t i,
maybe<accept_handle> ax = none,
optional<accept_handle> ax = none,
actor_id published_actor_id = invalid_actor_id,
set<string> published_actor_ifs = std::set<std::string>{}) {
auto src = ax ? *ax : ahdl_;
......@@ -297,7 +297,7 @@ public:
// test whether basp instance correctly updates the
// routing table upon receiving client handshakes
auto path = tbl().lookup(remote_node(i));
CAF_REQUIRE(path != none);
CAF_REQUIRE(path);
CAF_CHECK(path->hdl == remote_hdl(i));
CAF_CHECK(path->next_hop == remote_node(i));
}
......@@ -475,12 +475,15 @@ CAF_TEST(non_empty_server_handshake) {
}
CAF_TEST(remote_address_and_port) {
CAF_MESSAGE("connect node 1");
connect_node(1);
auto mm = system.middleman().actor_handle();
CAF_MESSAGE("ask MM about node 1");
self()->send(mm, get_atom::value, remote_node(1));
do {
mpx()->exec_runnable();
} while (! self()->has_next_message());
CAF_MESSAGE("receive result of MM");
self()->receive(
[&](const node_id& nid, const std::string& addr, uint16_t port) {
CAF_CHECK(nid == remote_node(1));
......
......@@ -167,9 +167,8 @@ void run_client(int argc, char** argv, uint16_t port) {
auto p = system.spawn(ping, size_t{10});
CAF_MESSAGE("spawn_client...");
auto cl = system.middleman().spawn_client(peer_fun, "127.0.0.1", port, p);
CAF_REQUIRE(cl);
CAF_MESSAGE("spawn_client finished");
anon_send(p, kickoff_atom::value, *cl);
anon_send(p, kickoff_atom::value, cl);
CAF_MESSAGE("`kickoff_atom` has been send");
}
......
......@@ -165,7 +165,7 @@ acceptor::behavior_type acceptor_fun(acceptor::broker_pointer self,
[](const acceptor_closed_msg&) {
// nop
},
[=](publish_atom) -> maybe<uint16_t> {
[=](publish_atom) -> optional<uint16_t> {
return get<1>(self->add_tcp_doorman(0, "127.0.0.1"));
}
};
......@@ -178,7 +178,7 @@ void run_client(int argc, char** argv, uint16_t port) {
auto cl = system.middleman().spawn_client(peer_fun, "localhost", port, p);
CAF_REQUIRE(cl);
CAF_MESSAGE("spawn_client_typed finished");
anon_send(p, kickoff_atom::value, *cl);
anon_send(p, kickoff_atom::value, cl);
CAF_MESSAGE("`kickoff_atom` has been send");
}
......
......@@ -69,8 +69,9 @@ struct fixture {
CAF_CHECK_EQUAL(s_dtor_called.load(), 2);
}
maybe<actor> remote_actor(const char* hostname, uint16_t port) {
maybe<actor> result;
actor remote_actor(const char* hostname, uint16_t port,
bool expect_fail = false) {
actor result;
scoped_actor self{system, true};
self->request(system.middleman().actor_handle(), infinite,
connect_atom::value, hostname, port).receive(
......@@ -78,10 +79,14 @@ struct fixture {
CAF_REQUIRE(xs.empty());
result = actor_cast<actor>(std::move(res));
},
[&](error& err) {
result = std::move(err);
[&](error&) {
// nop
}
);
if (! expect_fail)
CAF_REQUIRE(result != invalid_actor);
else
CAF_REQUIRE(result == invalid_actor);
return result;
}
......@@ -108,12 +113,12 @@ CAF_TEST(unpublishing) {
system.middleman().unpublish(testee, port);
CAF_MESSAGE("check whether testee is still available via cache");
auto x1 = remote_actor("127.0.0.1", port);
CAF_CHECK(x1 && *x1 == testee);
CAF_CHECK(x1 == testee);
CAF_MESSAGE("fake dead of testee and check if testee becomes unavailable");
anon_send(system.middleman().actor_handle(), down_msg{testee.address(),
exit_reason::normal});
// must fail now
auto x2 = remote_actor("127.0.0.1", port);
auto x2 = remote_actor("127.0.0.1", port, true);
CAF_CHECK(! x2);
}
......
......@@ -32,7 +32,7 @@
#include <iostream>
#include "caf/fwd.hpp"
#include "caf/maybe.hpp"
#include "caf/optional.hpp"
#include "caf/deep_to_string.hpp"
......@@ -165,7 +165,7 @@ public:
}
template <class T>
stream& operator<<(const maybe<T>& x) {
stream& operator<<(const optional<T>& x) {
if (! x)
return *this << "-none-";
return *this << *x;
......
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