root/trunk/libffado/src/libutil/Functors.h

Revision 1498, 4.5 kB (checked in by ppalmers, 15 years ago)

Merge all changes from 2.0 branch into trunk (since r1361). This _should_ contain all forward merges done in the mean time. At this moment in time both branches should be in sync.

Line 
1 /*
2  * Copyright (C) 2005-2008 by Daniel Wagner
3  *
4  * This file is part of FFADO
5  * FFADO = Free Firewire (pro-)audio drivers for linux
6  *
7  * FFADO is based upon FreeBoB
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 2 of the License, or
12  * (at your option) version 3 of the License.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  */
23
24 #ifndef __FFADO_FUNCTORS__
25 #define __FFADO_FUNCTORS__
26
27 #include <semaphore.h>
28 #include <vector>
29
30 namespace Util {
31
32 class Functor
33 {
34 public:
35     Functor() {}
36     virtual ~Functor() {}
37
38     virtual void operator() () = 0;
39     virtual bool matchCallee(void *) = 0;
40 };
41
42 typedef std::vector<Functor *> FunctorVector;
43 typedef std::vector<Functor *>::iterator FunctorVectorIterator;
44
45 ////////////////////////////////////////////////////////////////////////
46
47 template< typename CalleePtr, typename MemFunPtr >
48 class MemberFunctor0
49     : public Functor
50 {
51 public:
52     MemberFunctor0( const CalleePtr& pCallee,
53             MemFunPtr pMemFun,
54             bool bDelete = true )
55         : m_pCallee( pCallee )
56         , m_pMemFun( pMemFun )
57         , m_pSem( 0 )
58         , m_bDelete( bDelete )
59         {}
60
61     MemberFunctor0( const CalleePtr& pCallee,
62             MemFunPtr pMemFun,
63             sem_t* pSem,
64             bool bDelete = true )
65         : m_pCallee( pCallee )
66         , m_pMemFun( pMemFun )
67         , m_pSem( pSem )
68         , m_bDelete( bDelete )
69         {}
70
71     virtual ~MemberFunctor0()
72         {}
73
74     virtual void operator() ()
75         {
76             ( ( *m_pCallee ).*m_pMemFun )();
77             if ( m_pSem ) {
78                 sem_post( m_pSem);
79             }
80             if (m_bDelete) {
81                 delete this;
82             }
83         }
84     virtual bool matchCallee(void *p)
85         {
86             return p == (void *)m_pCallee;
87         }
88
89 private:
90     CalleePtr  m_pCallee;
91     MemFunPtr  m_pMemFun;
92     sem_t* m_pSem;
93     bool       m_bDelete;
94 };
95
96 template< typename CalleePtr, typename MemFunPtr, typename Parm0 >
97 class MemberFunctor1
98     : public Functor
99 {
100 public:
101     MemberFunctor1( const CalleePtr& pCallee,
102             MemFunPtr pMemFun,
103             Parm0 parm0,
104             bool bDelete = true)
105         : m_pCallee( pCallee )
106         , m_pMemFun( pMemFun )
107         , m_parm0( parm0 )
108         , m_pSem( 0 )
109         , m_bDelete( bDelete )
110         {}
111
112     MemberFunctor1( const CalleePtr& pCallee,
113             MemFunPtr pMemFun,
114             Parm0 parm0,
115             sem_t* pSem,
116             bool bDelete = true )
117         : m_pCallee( pCallee )
118         , m_pMemFun( pMemFun )
119         , m_parm0( parm0 )
120         , m_pSem( 0 )
121         , m_bDelete( bDelete )
122         {}
123     virtual ~MemberFunctor1()
124     {}
125
126     virtual void operator() ()
127     {
128             ( ( *m_pCallee ).*m_pMemFun )( m_parm0 );
129             if ( m_pSem ) {
130                 sem_post( m_pSem);
131             }
132             if (m_bDelete) {
133                 delete this;
134             }
135     }
136
137     virtual bool matchCallee(void *p)
138         {
139             return p == (void *)m_pCallee;
140         }
141
142 private:
143     CalleePtr  m_pCallee;
144     MemFunPtr  m_pMemFun;
145     Parm0      m_parm0;
146     sem_t* m_pSem;
147     bool       m_bDelete;
148 };
149
150 template< typename FunPtr >
151 class CallbackFunctor0
152     : public Functor
153 {
154 public:
155     CallbackFunctor0( FunPtr pMemFun,
156             bool bDelete = true )
157         : m_pMemFun( pMemFun )
158         , m_pSem( 0 )
159         , m_bDelete( bDelete )
160         {}
161
162     CallbackFunctor0( FunPtr pMemFun,
163             sem_t* pSem,
164             bool bDelete = true )
165         : m_pMemFun( pMemFun )
166         , m_pSem( pSem )
167         , m_bDelete( bDelete )
168         {}
169
170     virtual ~CallbackFunctor0()
171         {}
172
173     virtual void operator() ()
174         {
175             ( *m_pMemFun )();
176             if ( m_pSem ) {
177                 sem_post( m_pSem);
178             }
179             if (m_bDelete) {
180                 delete this;
181             }
182         }
183     virtual bool matchCallee(void *p)
184         {
185             return false;
186         }
187
188 private:
189     FunPtr      m_pMemFun;
190     sem_t*      m_pSem;
191     bool        m_bDelete;
192 };
193 }; // end of namespace Util
194
195 #endif
196
197
Note: See TracBrowser for help on using the browser.