Skip to content

Commit

Permalink
Add to_u64 as syntactic sugar to switch on atoms
Browse files Browse the repository at this point in the history
  • Loading branch information
Neverlord committed Nov 9, 2016
1 parent 57faf14 commit d729bd2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
15 changes: 11 additions & 4 deletions libcaf_core/caf/atom.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ enum class atom_value : uint64_t {
/// @relates atom_value
std::string to_string(const atom_value& x);

/// @relates atom_value
constexpr uint64_t to_u64(atom_value x) {
return static_cast<uint64_t>(x);
}

atom_value atom_from_string(const std::string& x);

/// Creates an atom from given string literal.
Expand All @@ -58,9 +63,6 @@ struct atom_constant {
constexpr operator atom_value() const {
return V;
}
static constexpr uint64_t uint_value() {
return static_cast<uint64_t>(V);
}
/// Returns an instance *of this constant* (*not* an `atom_value`).
static const atom_constant value;
};
Expand All @@ -76,10 +78,15 @@ struct is_atom_constant<atom_constant<X>> {
};

template <atom_value V>
std::string to_string(const atom_constant<V>&) {
std::string to_string(atom_constant<V>) {
return to_string(V);
}

template <atom_value V>
constexpr uint64_t to_u64(atom_constant<V>) {
return static_cast<uint64_t>(V);
}

template <atom_value V>
const atom_constant<V> atom_constant<V>::value = atom_constant<V>{};

Expand Down
4 changes: 2 additions & 2 deletions libcaf_io/src/basp_broker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ void basp_broker_state::deliver(const node_id& src_nid, actor_id src_aid,
switch (static_cast<uint64_t>(msg.get_as<atom_value>(0))) {
default:
break;
case link_atom::value.uint_value(): {
case to_u64(link_atom::value): {
if (src_nid != this_node()) {
CAF_LOG_WARNING("received link message for an other node");
return;
Expand All @@ -258,7 +258,7 @@ void basp_broker_state::deliver(const node_id& src_nid, actor_id src_aid,
static_cast<actor_proxy*>(ptr->get())->local_link_to(src->get());
return;
}
case unlink_atom::value.uint_value(): {
case to_u64(unlink_atom::value): {
if (src_nid != this_node()) {
CAF_LOG_WARNING("received unlink message for an other node");
return;
Expand Down

0 comments on commit d729bd2

Please sign in to comment.