Commit 3dc9ed81 authored by Dominik Charousset's avatar Dominik Charousset

Port examples to the new error API

parent 59d42ce0
...@@ -27,24 +27,13 @@ std::string to_string(math_error x) { ...@@ -27,24 +27,13 @@ std::string to_string(math_error x) {
} }
} }
namespace caf { CAF_BEGIN_TYPE_ID_BLOCK(divider, first_custom_type_id)
template <> CAF_ADD_TYPE_ID(divider, (math_error))
struct error_category<math_error> {
static constexpr uint8_t value = 101;
};
} // namespace caf CAF_END_TYPE_ID_BLOCK(divider)
class config : public actor_system_config { CAF_ERROR_CODE_ENUM(math_error)
public:
config() {
auto renderer = [](uint8_t x, const message&) {
return to_string(static_cast<math_error>(x));
};
add_error_category(caf::error_category<math_error>::value, renderer);
}
};
using divider = typed_actor<result<double>(div_atom, double, double)>; using divider = typed_actor<result<double>(div_atom, double, double)>;
...@@ -58,7 +47,7 @@ divider::behavior_type divider_impl() { ...@@ -58,7 +47,7 @@ divider::behavior_type divider_impl() {
}; };
} }
void caf_main(actor_system& system, const config&) { void caf_main(actor_system& system) {
double x; double x;
double y; double y;
cout << "x: " << flush; cout << "x: " << flush;
...@@ -76,4 +65,4 @@ void caf_main(actor_system& system, const config&) { ...@@ -76,4 +65,4 @@ void caf_main(actor_system& system, const config&) {
}); });
} }
CAF_MAIN() CAF_MAIN(id_block::divider)
...@@ -3,30 +3,25 @@ ...@@ -3,30 +3,25 @@
#include <cstdint> #include <cstdint>
#include <iostream> #include <iostream>
enum class fixed_stack_errc : uint8_t {
push_to_full = 1,
pop_from_empty,
};
CAF_BEGIN_TYPE_ID_BLOCK(fixed_stack, first_custom_type_id) CAF_BEGIN_TYPE_ID_BLOCK(fixed_stack, first_custom_type_id)
CAF_ADD_TYPE_ID(fixed_stack, (fixed_stack_errc))
CAF_ADD_ATOM(fixed_stack, pop_atom) CAF_ADD_ATOM(fixed_stack, pop_atom)
CAF_ADD_ATOM(fixed_stack, push_atom) CAF_ADD_ATOM(fixed_stack, push_atom)
CAF_END_TYPE_ID_BLOCK(fixed_stack) CAF_END_TYPE_ID_BLOCK(fixed_stack)
CAF_ERROR_CODE_ENUM(fixed_stack_errc)
using std::endl; using std::endl;
using namespace caf; using namespace caf;
enum class fixed_stack_errc : uint8_t {
push_to_full = 1,
pop_from_empty,
};
namespace caf {
template <>
struct error_category<fixed_stack_errc> {
static constexpr uint8_t value = 100;
};
} // namespace caf
class fixed_stack : public event_based_actor { class fixed_stack : public event_based_actor {
public: public:
fixed_stack(actor_config& cfg, size_t stack_size) fixed_stack(actor_config& cfg, size_t stack_size)
......
...@@ -52,12 +52,9 @@ enum class exit_reason : uint8_t { ...@@ -52,12 +52,9 @@ enum class exit_reason : uint8_t {
unreachable unreachable
}; };
/// Returns a string representation of given exit reason. /// @relates exit_reason
CAF_CORE_EXPORT std::string to_string(exit_reason); CAF_CORE_EXPORT std::string to_string(exit_reason);
template <>
struct is_error_code_enum<exit_reason> {
static constexpr bool value = true;
};
} // namespace caf } // namespace caf
CAF_ERROR_CODE_ENUM(exit_reason)
...@@ -32,3 +32,11 @@ template <class T> ...@@ -32,3 +32,11 @@ template <class T>
constexpr bool is_error_code_enum_v = is_error_code_enum<T>::value; constexpr bool is_error_code_enum_v = is_error_code_enum<T>::value;
} // namespace caf } // namespace caf
#define CAF_ERROR_CODE_ENUM(type_name) \
namespace caf { \
template <> \
struct is_error_code_enum<type_name> { \
static constexpr bool value = true; \
}; \
}
...@@ -79,9 +79,6 @@ enum class pec : uint8_t { ...@@ -79,9 +79,6 @@ enum class pec : uint8_t {
CAF_CORE_EXPORT std::string to_string(pec); CAF_CORE_EXPORT std::string to_string(pec);
template <>
struct is_error_code_enum<pec> {
static constexpr bool value = true;
};
} // namespace caf } // namespace caf
CAF_ERROR_CODE_ENUM(pec)
...@@ -150,9 +150,6 @@ enum class sec : uint8_t { ...@@ -150,9 +150,6 @@ enum class sec : uint8_t {
/// @relates sec /// @relates sec
CAF_CORE_EXPORT std::string to_string(sec); CAF_CORE_EXPORT std::string to_string(sec);
template <>
struct is_error_code_enum<sec> {
static constexpr bool value = true;
};
} // namespace caf } // namespace caf
CAF_ERROR_CODE_ENUM(sec)
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