comparison anagram/vaclgui/dispatch.hpp @ 0:13d2b8934445

Import AnaGram (near-)release tree into Mercurial.
author David A. Holland
date Sat, 22 Dec 2007 17:52:45 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:13d2b8934445
1 /*
2 * AnaGram, A System for Syntax Directed Programming
3 * Copyright 1997-2002 Parsifal Software. All Rights Reserved.
4 * See the file COPYING for license and usage terms.
5 *
6 * dispatch.hpp
7 */
8
9 #ifndef DISPATCH_HPP
10 #define DISPATCH_HPP
11
12 #include <ievent.hpp>
13
14
15 class AgDispatcher {
16 public:
17 class Kernel {
18 private:
19 int useCount;
20 public:
21 virtual ~Kernel() {}
22 virtual void dispatch(IEvent &) const = 0;
23
24 Kernel() : useCount(0) {}
25 void lock() { useCount++; }
26 int unlock() { return --useCount <= 0; }
27 };
28
29 protected:
30 Kernel *kernel;
31
32 AgDispatcher(AgDispatcher::Kernel *kernel_) :
33 kernel(kernel_)
34 {
35 if (kernel) {
36 kernel->lock();
37 }
38 }
39
40 public:
41 AgDispatcher() :
42 kernel(0)
43 {}
44 AgDispatcher(const AgDispatcher &a) :
45 kernel(a.kernel)
46 {
47 if (kernel) {
48 kernel->lock();
49 }
50 }
51 AgDispatcher &operator = (const AgDispatcher &a) {
52 if (kernel && kernel->unlock()) {
53 delete kernel;
54 }
55 kernel = a.kernel;
56 if (kernel) {
57 kernel->lock();
58 }
59 return *this;
60 }
61 ~AgDispatcher() {
62 if (kernel && kernel->unlock()) {
63 delete kernel;
64 }
65 }
66 void dispatch(IEvent &e) const {
67 if (kernel) {
68 kernel->dispatch(e);
69 }
70 }
71 };
72
73
74 template<class T>
75 class AgObjectDispatcher : public AgDispatcher {
76 class Kernel : public AgDispatcher::Kernel {
77 T &object;
78 void (T:: * memberFunction)(IEvent &);
79 public:
80 Kernel(T &object_, void (T:: * memberFunction_)(IEvent &)) :
81 object(object_) ,
82 memberFunction(memberFunction_)
83 {}
84 void dispatch(IEvent &e) const { (object.*memberFunction)(e); }
85 };
86
87 public:
88 AgObjectDispatcher(T &object_, void (T:: * memberFunction_)(IEvent &)) :
89 AgDispatcher(new AgObjectDispatcher<T>::Kernel(object_,memberFunction_))
90 {}
91 AgObjectDispatcher(const AgObjectDispatcher<T> &a) :
92 AgDispatcher(a)
93 {}
94 };
95
96
97 #endif /* DISPATCH_HPP */