bumped version to 0.1.0
[beacon-ss.git] / src / beacon / invokable.hpp
blobf8c79df3e9a4f019b80f2c50c987eec90c7d5dec
1 /**
2 * beacon
3 * Author: Lukas Krejci <krejci.l@centrum.cz>, (C) 2008
4 * Copyright: See COPYING file that comes with this distribution
5 */
7 #ifndef BEACON_INVOKABLE_H
8 #define BEACON_INVOKABLE_H
10 #include <beacon/reference_countable.hpp>
11 #include <beacon/detail/quick_wait.hpp>
12 #include <beacon/intrusive_ptr.hpp>
14 #include <boost/thread/recursive_mutex.hpp>
16 extern "C" {
17 #include <atomic_ops.h>
20 namespace beacon {
22 /**
23 * This class represents the token that identifies an invokable in
24 * \a event_queue::dequeue.
26 class invocation_token : public reference_countable {
28 public:
30 /** The type of the lock for the \a guard. */
31 typedef boost::recursive_mutex::scoped_lock lock_t;
33 invocation_token(bool valid = true) : _valid(valid) {
36 ~invocation_token() {
39 /**
40 * A true return value indicates that the invokable accompanied
41 * by this token is valid.
43 bool valid() const {
44 return _valid;
47 /**
48 * This causes the \a valid method to return false, indicating that
49 * the invokable accompanied by this token is no longer valid.
50 * The guard is locked during the method invocation.
52 void invalidate() {
53 lock_t l(_guard);
54 _valid = false;
57 /**
58 * The guard can be used to lock access to the token.
60 boost::recursive_mutex const & guard() const {
61 return _guard;
64 /**
65 * The guard can be used to lock access to the token.
67 boost::recursive_mutex & guard() {
68 return _guard;
70 private:
71 bool _valid;
72 boost::recursive_mutex _guard;
75 /**
76 * An abstract class that anything invokable from within an
77 * {@link event_queue} needs to implement.
79 class invokable {
80 public:
82 typedef intrusive_ptr<invocation_token> token_ptr;
83 typedef invocation_token token_type;
85 protected:
87 /**
88 * Creates new instance of an invokable object.
89 * @param token a token used to match different invokables
91 invokable(token_ptr token) : _token(token) {
94 public:
96 virtual ~invokable() {
99 virtual void invoke() = 0;
101 token_ptr token() const {
102 return _token;
105 private:
106 token_ptr _token;
111 #endif