libcaf  0.15.0
Public Types | Public Member Functions | Related Functions | List of all members
caf::local_actor Class Reference

Base class for actors running on this node, either living in an own thread or cooperatively scheduled. More...

#include <local_actor.hpp>

Inheritance diagram for caf::local_actor:
caf::monitorable_actor caf::resumable caf::abstract_actor caf::abstract_channel caf::actor_companion caf::blocking_actor caf::event_based_actor caf::io::abstract_broker caf::typed_event_based_actor< Sigs > caf::io::broker

Public Types

using mailbox_type = detail::single_reader_queue< mailbox_element, detail::disposer >
 
using default_handler = std::function< result< message >(local_actor *self, const type_erased_tuple *)>
 Function object for handling unmatched messages.
 
using error_handler = std::function< void(local_actor *self, error &)>
 Function object for handling error messages.
 
using down_handler = std::function< void(local_actor *self, down_msg &)>
 Function object for handling down messages.
 
using exit_handler = std::function< void(local_actor *self, exit_msg &)>
 Function object for handling exit messages.
 
- Public Types inherited from caf::resumable
enum  resume_result {
  resume_later,
  awaiting_message,
  done,
  shutdown_execution_unit
}
 Denotes the state in which a resumable returned from its last call to resume. More...
 
enum  subtype_t {
  unspecified,
  scheduled_actor,
  io_actor,
  function_object
}
 Denotes common subtypes of resumable. More...
 

Public Member Functions

void on_destroy () override
 Cleans up any remaining state before the destructor is called. More...
 
template<class T , spawn_options Os = no_spawn_options, class... Ts>
infer_handle_from_class< T >::type spawn (Ts &&...xs)
 
template<spawn_options Os = no_spawn_options, class F , class... Ts>
infer_handle_from_fun< F >::type spawn (F fun, Ts &&...xs)
 
template<class T , spawn_options Os = no_spawn_options, class Groups , class... Ts>
actor spawn_in_groups (const Groups &gs, Ts &&...xs)
 
template<class T , spawn_options Os = no_spawn_options, class... Ts>
actor spawn_in_groups (std::initializer_list< group > gs, Ts &&...xs)
 
template<class T , spawn_options Os = no_spawn_options, class... Ts>
actor spawn_in_group (const group &grp, Ts &&...xs)
 
template<spawn_options Os = no_spawn_options, class Groups , class F , class... Ts>
actor spawn_in_groups (const Groups &gs, F fun, Ts &&...xs)
 
template<spawn_options Os = no_spawn_options, class F , class... Ts>
actor spawn_in_groups (std::initializer_list< group > gs, F fun, Ts &&...xs)
 
template<spawn_options Os = no_spawn_options, class F , class... Ts>
actor spawn_in_group (const group &grp, F fun, Ts &&...xs)
 
void send_exit (const actor_addr &dest, error reason)
 Sends an exit message to dest.
 
void send_exit (const strong_actor_ptr &dest, error reason)
 
template<class ActorHandle >
void send_exit (const ActorHandle &dest, error reason)
 Sends an exit message to dest.
 
void set_default_handler (default_handler fun)
 Sets a custom handler for unexpected messages.
 
void set_error_handler (error_handler fun)
 Sets a custom handler for error messages.
 
template<class T >
auto set_error_handler (T fun) -> decltype(fun(std::declval< error & >()))
 Sets a custom handler for error messages.
 
void set_down_handler (down_handler fun)
 Sets a custom handler for down messages.
 
template<class T >
auto set_down_handler (T fun) -> decltype(fun(std::declval< down_msg & >()))
 Sets a custom handler for down messages.
 
void set_exit_handler (exit_handler fun)
 Sets a custom handler for error messages.
 
template<class T >
auto set_exit_handler (T fun) -> decltype(fun(std::declval< exit_msg & >()))
 Sets a custom handler for exit messages.
 
execution_unitcontext () const
 Returns the execution unit currently used by this actor.
 
void context (execution_unit *x)
 Sets the execution unit for this actor.
 
actor_system & system () const
 Returns the hosting actor system.
 
void join (const group &what)
 Causes this actor to subscribe to the group what. More...
 
void leave (const group &what)
 Causes this actor to leave the group what.
 
void quit (error reason=error{})
 Finishes execution of this actor after any currently running message handler is done. More...
 
strong_actor_ptr current_sender ()
 Returns a pointer to the sender of the current message.
 
template<class Handle >
void monitor (const Handle &whom)
 Adds a unidirectional monitor to whom. More...
 
void demonitor (const actor_addr &whom)
 Removes a monitor from whom.
 
void demonitor (const actor &whom)
 Removes a monitor from whom.
 
virtual void on_exit ()
 Can be overridden to perform cleanup code after an actor finished execution. More...
 
std::vector< group > joined_groups () const
 Returns all joined groups.
 
template<class... Ts>
detail::make_response_promise_helper< Ts...>::type make_response_promise ()
 Creates a typed_response_promise to respond to a request later on. More...
 
response_promise make_response_promise ()
 Creates a response_promise to respond to a request later on.
 
template<class... Ts, class R = typename detail::make_response_promise_helper< typename std::decay<Ts>::type... >::type>
response (Ts &&...xs)
 Creates a typed_response_promise and responds immediately. More...
 
template<class F >
void set_exception_handler (F f)
 Sets a custom exception handler for this actor. More...
 
const char * name () const override
 Returns an implementation-dependent name for logging purposes, which is only valid as long as the actor is running. More...
 
virtual void save_state (serializer &sink, const unsigned int version)
 Serializes the state of this actor to sink. More...
 
virtual void load_state (deserializer &source, const unsigned int version)
 Deserializes the state of this actor from source. More...
 
subtype_t subtype () const override
 Returns a subtype hint for this object. More...
 
resume_result resume (execution_unit *, size_t) override
 Resume any pending computation until it is either finished or needs to be re-scheduled later. More...
 
- Public Member Functions inherited from caf::monitorable_actor
void attach (attachable_ptr ptr) override
 Attaches ptr to this actor. More...
 
size_t detach (const attachable::token &what) override
 Detaches the first attached object that matches what.
 
void link_to (const actor_addr &x)
 Links this actor to x.
 
template<class ActorHandle >
void link_to (const ActorHandle &x)
 Links this actor to x.
 
void unlink_from (const actor_addr &x)
 Unlinks this actor from x.
 
template<class ActorHandle >
void unlink_from (const ActorHandle &x)
 Links this actor to x.
 
- Public Member Functions inherited from caf::abstract_actor
void * operator new (std::size_t, void *ptr)
 
actor_control_blockctrl () const
 
void enqueue (strong_actor_ptr sender, message_id mid, message content, execution_unit *host) override
 Enqueues a new message without forwarding stack to the channel.
 
virtual void enqueue (mailbox_element_ptr what, execution_unit *host)=0
 Enqueues a new message wrapped in a mailbox_element to the actor. More...
 
template<class F >
void attach_functor (F f)
 Convenience function that attaches the functor f to this actor. More...
 
actor_addr address () const
 Returns the logical actor address.
 
bool establish_backlink (abstract_actor *x)
 Establishes a link relation between this actor and x and returns whether the operation succeeded. More...
 
bool remove_backlink (abstract_actor *x)
 Removes the link relation between this actor and x and returns whether the operation succeeded. More...
 
virtual std::set< std::string > message_types () const
 Returns the set of accepted messages types as strings or an empty set if this actor is untyped. More...
 
actor_id id () const noexcept
 Returns the ID of this actor.
 
node_id node () const noexcept
 Returns the node this actor is living on.
 
actor_system & home_system () const noexcept
 Returns the system that created this actor (or proxy).
 
- Public Member Functions inherited from caf::abstract_channel
bool is_abstract_actor () const
 
bool is_abstract_group () const
 
bool is_actor_decorator () const
 
- Public Member Functions inherited from caf::resumable
virtual void intrusive_ptr_add_ref_impl ()=0
 Add a strong reference count to this object.
 
virtual void intrusive_ptr_release_impl ()=0
 Remove a strong reference count from this object.
 

Related Functions

(Note that these are not member functions.)

using local_actor_ptr = intrusive_ptr< local_actor >
 A smart pointer to a local_actor instance.
 
constexpr keep_behavior_t keep_behavior = keep_behavior_t{}
 Policy tag that causes event_based_actor::become to keep the current behavior available.
 
result< messagereflect (local_actor *, const type_erased_tuple *)
 
result< messagereflect_and_quit (local_actor *, const type_erased_tuple *)
 
result< messageprint_and_drop (local_actor *, const type_erased_tuple *)
 
result< messagedrop (local_actor *, const type_erased_tuple *)
 

Additional Inherited Members

- Static Public Attributes inherited from caf::abstract_channel
static constexpr int is_abstract_actor_flag = 0x01000000
 
static constexpr int is_abstract_group_flag = 0x02000000
 
static constexpr int is_actor_bind_decorator_flag = 0x04000000
 
static constexpr int is_actor_dot_decorator_flag = 0x08000000
 
static constexpr int is_actor_decorator_mask = 0x0C000000
 
- Protected Member Functions inherited from caf::monitorable_actor
virtual void on_cleanup ()
 Allows subclasses to add additional cleanup code to the critical secion in cleanup. More...
 
void bounce (mailbox_element_ptr &what)
 Sends a response message if what is a request.
 
void bounce (mailbox_element_ptr &what, const error &err)
 Sends a response message if what is a request.
 
 monitorable_actor (actor_config &cfg)
 Creates a new actor instance.
 
bool link_impl (linking_operation op, abstract_actor *x) override
 
bool establish_link_impl (abstract_actor *other)
 
bool remove_link_impl (abstract_actor *other)
 
bool establish_backlink_impl (abstract_actor *other)
 
bool remove_backlink_impl (abstract_actor *other)
 
optional< exit_reasonhandle (const std::exception_ptr &eptr)
 
void attach_impl (attachable_ptr &ptr)
 
bool handle_system_message (mailbox_element &node, execution_unit *context, bool trap_exit)
 
template<class F >
bool handle_system_message (mailbox_element &x, execution_unit *context, bool trap_exit, F &down_msg_handler)
 
template<class F >
auto exclusive_critical_section (F fun) -> decltype(fun())
 
template<class F >
auto shared_critical_section (F fun) -> decltype(fun())
 
- Protected Member Functions inherited from caf::abstract_actor
 abstract_actor (actor_config &cfg)
 Creates a new actor instance.
 
- Protected Member Functions inherited from caf::abstract_channel
int flags () const
 
void flags (int new_value)
 
- Static Protected Member Functions inherited from caf::monitorable_actor
static size_t detach_impl (const attachable::token &what, attachable_ptr &ptr, bool stop_on_first_hit=false, bool dry_run=false)
 
- Protected Attributes inherited from caf::monitorable_actor
error fail_state_
 
std::mutex mtx_
 
std::condition_variable cv_
 
attachable_ptr attachables_head_
 

Detailed Description

Base class for actors running on this node, either living in an own thread or cooperatively scheduled.

Member Function Documentation

void caf::local_actor::join ( const group &  what)

Causes this actor to subscribe to the group what.

The group will be unsubscribed if the actor finishes execution.

virtual void caf::local_actor::load_state ( deserializer source,
const unsigned int  version 
)
virtual

Deserializes the state of this actor from source.

This function is only called if this actor has set the is_serializable flag. The default implementation throws a std::logic_error.

template<class... Ts>
detail::make_response_promise_helper<Ts...>::type caf::local_actor::make_response_promise ( )

Creates a typed_response_promise to respond to a request later on.

make_response_promise<typed_response_promise<int, int>>() is equivalent to make_response_promise<int, int>().

template<class Handle >
void caf::local_actor::monitor ( const Handle &  whom)

Adds a unidirectional monitor to whom.

Note
Each call to monitor creates a new, independent monitor.
const char* caf::local_actor::name ( ) const
overridevirtual

Returns an implementation-dependent name for logging purposes, which is only valid as long as the actor is running.

The default implementation simply returns "actor".

Reimplemented from caf::monitorable_actor.

Reimplemented in caf::io::abstract_broker.

void caf::local_actor::on_destroy ( )
overridevirtual

Cleans up any remaining state before the destructor is called.

This function makes sure it is safe to call virtual functions in sub classes before destroying the object, because calling virtual function in the destructor itself is not safe. Any override implementation is required to call super::destroy() at the end.

Reimplemented from caf::abstract_actor.

virtual void caf::local_actor::on_exit ( )
virtual

Can be overridden to perform cleanup code after an actor finished execution.

void caf::local_actor::quit ( error  reason = error{})

Finishes execution of this actor after any currently running message handler is done.

This member function clears the behavior stack of the running actor and invokes on_exit(). The actors does not finish execution if the implementation of on_exit() sets a new behavior. When setting a new behavior in on_exit(), one has to make sure to not produce an infinite recursion.

If on_exit() did not set a new behavior, the actor sends an exit message to all of its linked actors, sets its state to exited and finishes execution.

In case this actor uses the blocking API, this member function unwinds the stack by throwing an actor_exited exception.

Warning
This member function throws immediately in thread-based actors that do not use the behavior stack, i.e., actors that use blocking API calls such as receive().
template<class... Ts, class R = typename detail::make_response_promise_helper< typename std::decay<Ts>::type... >::type>
R caf::local_actor::response ( Ts &&...  xs)

Creates a typed_response_promise and responds immediately.

Return type is deduced from arguments. Return value is implicitly convertible to untyped response promise.

resume_result caf::local_actor::resume ( execution_unit ,
size_t  max_throughput 
)
overridevirtual

Resume any pending computation until it is either finished or needs to be re-scheduled later.

Implements caf::resumable.

Reimplemented in caf::io::abstract_broker.

virtual void caf::local_actor::save_state ( serializer sink,
const unsigned int  version 
)
virtual

Serializes the state of this actor to sink.

This function is only called if this actor has set the is_serializable flag. The default implementation throws a std::logic_error.

template<class F >
void caf::local_actor::set_exception_handler ( f)

Sets a custom exception handler for this actor.

If multiple handlers are defined, only the functor that was added last is being executed.

subtype_t caf::local_actor::subtype ( ) const
overridevirtual

Returns a subtype hint for this object.

This allows an execution unit to limit processing to a specific set of resumables and delegate other subtypes to dedicated workers.

Reimplemented from caf::resumable.

Reimplemented in caf::io::abstract_broker.

Friends And Related Function Documentation

result< message > drop ( local_actor ,
const type_erased_tuple  
)
related

Default handler function that simply drops messages.

result< message > print_and_drop ( local_actor ,
const type_erased_tuple  
)
related

Default handler function that prints messages message via aout and drops them afterwards.

result< message > reflect ( local_actor ,
const type_erased_tuple  
)
related

Default handler function that sends the message back to the sender.

result< message > reflect_and_quit ( local_actor ,
const type_erased_tuple  
)
related

Default handler function that sends the message back to the sender and then quits.


The documentation for this class was generated from the following files: