ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/ermyth/include/common/gencallbacks.pl
Revision: 1.2
Committed: Wed Jul 25 01:05:17 2007 UTC (19 years, 2 months ago) by pippijn
Content type: text/plain
Branch: MAIN
Changes since 1.1: +9 -1 lines
Log Message:
- moved to GPL
- added license information to files

File Contents

# User Rev Content
1 pippijn 1.1 #!/usr/bin/perl
2 pippijn 1.2 #
3     # Copyright © 2007 Pippijn van Steenhoven / The Ermyth Team
4     # Rights to this code are as documented in doc/pod/gplicense.pod.
5     #
6     # Generate callbacks.
7     #
8     # $Id: account.h,v 1.2 2007-07-21 01:29:07 pippijn Exp $
9 pippijn 1.1
10     use strict;
11     use warnings;
12     use utf8;
13    
14 pippijn 1.2 my $rcsid = '$Id$';
15    
16 pippijn 1.1 my $namespace = "callback";
17     my $events = $ARGV[0] || 0;
18    
19     $events > 1
20     or die "Usage: $0 NUMBER\nwhere NUMBER > 1.";
21    
22     my $gen = "";
23    
24     for my $num (2 .. $events) {
25     my @argt;
26     my @argn;
27     my $arg_type_list_named;
28    
29     for (1 .. $num) {
30     my $a = "arg${_}_type";
31     my $aname = "a$_";
32     push @argt, $a;
33     push @argn, $aname;
34     $arg_type_list_named .= "$a $aname, ";
35     }
36    
37     $arg_type_list_named =~ s/, *$//;
38    
39     my $arg_params_names = join ", ", @argn;
40     my $arg_tmpl_decl = "class " . join ", class ", @argt;
41     my $arg_type_list = join ", ", @argt;
42    
43     $gen .= <<EOF;
44     ////////////////////// events for $num arguments //////////////////////
45     // auto-generated by $0
46     //
47     // tmpl decl = $arg_tmpl_decl
48     // args = $arg_type_list
49     // named args = $arg_type_list_named
50    
51     EOF
52    
53     ########################################################################
54     # connection_base
55     $gen .= <<EOF;
56     template <${arg_tmpl_decl}>
57     class connection_base${num}
58     {
59     public:
60     typedef connection_base${num}<${arg_type_list}> this_type;
61    
62     virtual ~connection_base${num} (){}
63    
64     virtual listening_base* getdest () const = 0;
65     virtual void invoke (${arg_type_list}) const = 0;
66     virtual this_type *clone () const { return 0; }
67     virtual this_type *duplicate (listening_base* pnewdest) const { return 0; }
68     #if 0
69     virtual this_type *clone () const = 0; // { return 0; }
70     virtual this_type *duplicate (listening_base* pnewdest) const = 0; // { return 0; }
71     #endif
72     };
73    
74     EOF
75    
76    
77     ########################################################################
78     # connection
79     $gen .= <<EOF;
80     template<class dest_type, ${arg_tmpl_decl}>
81     class connection${num} : public connection_base${num}<${arg_type_list}>
82     {
83     public:
84     typedef connection_base${num}<${arg_type_list} > base_type;
85     typedef connection${num}< dest_type, ${arg_type_list} > this_type;
86     typedef void (dest_type::*listener_func) (${arg_type_list});
87    
88     connection${num} () : m_pobject (0), m_pmemfun (0) { }
89    
90     virtual ~connection${num} () { }
91    
92     connection${num} (dest_type *pobject, listener_func pfunc) : m_pobject (pobject), m_pmemfun (pfunc) { }
93    
94     virtual base_type *clone () const
95     {
96     return new this_type (*this);
97     }
98    
99     virtual base_type *duplicate (listening_base *pnewdest) const
100     {
101     return new this_type (dynamic_cast<dest_type *> (pnewdest), m_pmemfun);
102     }
103    
104     virtual listening_base* getdest () const
105     {
106     return m_pobject;
107     }
108    
109     virtual void invoke (${arg_type_list_named}) const
110     {
111     (m_pobject->*m_pmemfun) (${arg_params_names});
112     }
113    
114     private:
115     dest_type* m_pobject;
116     listener_func m_pmemfun;
117     };
118    
119     EOF
120    
121    
122     ########################################################################
123     # event
124     $gen .= <<EOF;
125     template<${arg_tmpl_decl} >
126     class event${num} : public event_base
127     {
128     public:
129     typedef has_listeners listening_base;
130     typedef std::list<connection_base${num}<${arg_type_list}> *>
131     connection_list;
132    
133     event${num} () { }
134    
135     event${num} (const event${num}<${arg_type_list}> &s) : event_base (s)
136     {
137     if (&s == this)
138     return;
139    
140     this->copy_connections (s.connections (), this->connections ());
141     }
142    
143    
144     template<class desttype>
145     void attach (desttype* plistener, void (desttype::*pmemfun) (${arg_type_list}))
146     {
147     connection_base${num}<${arg_type_list}> *conn = new connection${num}<desttype, ${arg_type_list}> (plistener, pmemfun);
148     this->connections ().push_back (conn);
149     plistener->add_invoker (this);
150     }
151    
152     void invoke (${arg_type_list_named}) const
153     {
154     typename connection_list::const_iterator it;
155     typename connection_list::const_iterator it_end = this->connections ().end ();
156    
157     for (it = this->connections ().begin (); it != it_end; ++it)
158     (*it)->invoke (${arg_params_names});
159     }
160    
161     void operator () (${arg_type_list_named}) const
162     {
163     this->invoke (${arg_params_names});
164     }
165    
166     event${num} &operator= (const event${num} &s)
167     {
168     this->copy_connections (s.connections (), this->connections ());
169     return *this;
170     }
171    
172     ~event${num} ()
173     {
174     disconnect_all ();
175     }
176    
177     void disconnect_all ()
178     {
179     this->free_connections (connections ());
180     }
181    
182     bool listener_duplicate (const listening_base *oldtarget, listening_base *newtarget)
183     {
184     return this->duplicate_connection (oldtarget, newtarget, connections ());
185     }
186    
187     protected:
188     bool listener_detach (listening_base *plistener)
189     {
190     return this->detach (plistener, this->connections ());
191     }
192    
193     connection_list & connections ()
194     {
195     return this->m_connected_listeners;
196     }
197    
198     const connection_list &connections () const
199     {
200     return this->m_connected_listeners;
201     }
202    
203     private:
204     connection_list m_connected_listeners;
205     };
206     EOF
207    
208     }
209    
210     print $gen;