actor-framework
attachable.hpp
1 // This file is part of CAF, the C++ Actor Framework. See the file LICENSE in
2 // the main distribution directory for license terms and copyright or visit
3 // https://github.com/actor-framework/actor-framework/blob/master/LICENSE.
4 
5 #pragma once
6 
7 #include "caf/detail/core_export.hpp"
8 #include "caf/error.hpp"
9 #include "caf/exit_reason.hpp"
10 #include "caf/fwd.hpp"
11 #include "caf/message_priority.hpp"
12 
13 #include <cstdint>
14 #include <memory>
15 #include <typeinfo>
16 
17 namespace caf {
18 
20 using attachable_ptr = std::unique_ptr<attachable>;
21 
23 class CAF_CORE_EXPORT attachable {
24 public:
25  // -- member types -----------------------------------------------------------
26 
28  struct token {
30  static constexpr size_t anonymous = 0;
31 
33  static constexpr size_t subscription = 1;
34 
36  static constexpr size_t observer = 2;
37 
38  template <class T>
39  token(const T& tk) : subtype(T::token_type), ptr(&tk) {
40  // nop
41  }
42 
44  size_t subtype;
45 
47  const void* ptr;
48 
49  token(size_t typenr, const void* vptr);
50  };
51 
52  // -- constructors and destructors -------------------------------------------
53 
54  attachable() = default;
55  attachable(const attachable&) = delete;
56  attachable& operator=(const attachable&) = delete;
57 
58  virtual ~attachable();
59 
60  // -- interface for the actor ------------------------------------------------
61 
65  virtual void actor_exited(const error& fail_state, scheduler* sched);
66 
68  virtual bool matches(const token& what);
69 
71  template <class T>
72  bool matches(const T& what) {
73  return matches(token{T::token_type, &what});
74  }
75 
76  // -- member variables -------------------------------------------------------
77 
78  attachable_ptr next;
79 };
80 
81 } // namespace caf
std::unique_ptr< attachable > attachable_ptr
Definition: attachable.hpp:20
A serializable type for storing error codes with category and optional, human-readable context inform...
Definition: error.hpp:50
A scheduler is responsible for managing the execution of resumables.
Definition: scheduler.hpp:18
Represents a pointer to a value with its subtype as type ID number.
Definition: attachable.hpp:28
bool matches(const T &what)
Returns true if what selects this instance, otherwise false.
Definition: attachable.hpp:72
Callback utility class.
Definition: attachable.hpp:23
size_t subtype
Denotes the type of ptr.
Definition: attachable.hpp:44
Root namespace of libcaf.
Definition: custom_types_4.cpp:139
const void * ptr
Any value, used to identify attachable instances.
Definition: attachable.hpp:47