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 |
|
---|
29 |
namespace Util { |
---|
30 |
|
---|
31 |
class Functor |
---|
32 |
{ |
---|
33 |
public: |
---|
34 |
Functor() {} |
---|
35 |
virtual ~Functor() {} |
---|
36 |
|
---|
37 |
virtual void operator() () = 0; |
---|
38 |
}; |
---|
39 |
|
---|
40 |
//////////////////////////////////////////////////////////////////////// |
---|
41 |
|
---|
42 |
template< typename CalleePtr, typename MemFunPtr > |
---|
43 |
class MemberFunctor0 |
---|
44 |
: public Functor |
---|
45 |
{ |
---|
46 |
public: |
---|
47 |
MemberFunctor0( const CalleePtr& pCallee, |
---|
48 |
MemFunPtr pMemFun, |
---|
49 |
bool bDelete = true ) |
---|
50 |
: m_pCallee( pCallee ) |
---|
51 |
, m_pMemFun( pMemFun ) |
---|
52 |
, m_pSem( 0 ) |
---|
53 |
, m_bDelete( bDelete ) |
---|
54 |
{} |
---|
55 |
|
---|
56 |
MemberFunctor0( const CalleePtr& pCallee, |
---|
57 |
MemFunPtr pMemFun, |
---|
58 |
sem_t* pSem, |
---|
59 |
bool bDelete = true ) |
---|
60 |
: m_pCallee( pCallee ) |
---|
61 |
, m_pMemFun( pMemFun ) |
---|
62 |
, m_pSem( pSem ) |
---|
63 |
, m_bDelete( bDelete ) |
---|
64 |
{} |
---|
65 |
|
---|
66 |
virtual ~MemberFunctor0() |
---|
67 |
{} |
---|
68 |
|
---|
69 |
virtual void operator() () |
---|
70 |
{ |
---|
71 |
( ( *m_pCallee ).*m_pMemFun )(); |
---|
72 |
if ( m_pSem ) { |
---|
73 |
sem_post( m_pSem); |
---|
74 |
} |
---|
75 |
if (m_bDelete) { |
---|
76 |
delete this; |
---|
77 |
} |
---|
78 |
} |
---|
79 |
|
---|
80 |
private: |
---|
81 |
CalleePtr m_pCallee; |
---|
82 |
MemFunPtr m_pMemFun; |
---|
83 |
sem_t* m_pSem; |
---|
84 |
bool m_bDelete; |
---|
85 |
}; |
---|
86 |
|
---|
87 |
template< typename CalleePtr, typename MemFunPtr, typename Parm0 > |
---|
88 |
class MemberFunctor1 |
---|
89 |
: public Functor |
---|
90 |
{ |
---|
91 |
public: |
---|
92 |
MemberFunctor1( const CalleePtr& pCallee, |
---|
93 |
MemFunPtr pMemFun, |
---|
94 |
Parm0 parm0, |
---|
95 |
bool bDelete = true) |
---|
96 |
: m_pCallee( pCallee ) |
---|
97 |
, m_pMemFun( pMemFun ) |
---|
98 |
, m_parm0( parm0 ) |
---|
99 |
, m_pSem( 0 ) |
---|
100 |
, m_bDelete( bDelete ) |
---|
101 |
{} |
---|
102 |
|
---|
103 |
MemberFunctor1( const CalleePtr& pCallee, |
---|
104 |
MemFunPtr pMemFun, |
---|
105 |
Parm0 parm0, |
---|
106 |
sem_t* pSem, |
---|
107 |
bool bDelete = true ) |
---|
108 |
: m_pCallee( pCallee ) |
---|
109 |
, m_pMemFun( pMemFun ) |
---|
110 |
, m_parm0( parm0 ) |
---|
111 |
, m_pSem( 0 ) |
---|
112 |
, m_bDelete( bDelete ) |
---|
113 |
{} |
---|
114 |
virtual ~MemberFunctor1() |
---|
115 |
{} |
---|
116 |
|
---|
117 |
virtual void operator() () |
---|
118 |
{ |
---|
119 |
( ( *m_pCallee ).*m_pMemFun )( m_parm0 ); |
---|
120 |
if ( m_pSem ) { |
---|
121 |
sem_post( m_pSem); |
---|
122 |
} |
---|
123 |
if (m_bDelete) { |
---|
124 |
delete this; |
---|
125 |
} |
---|
126 |
} |
---|
127 |
|
---|
128 |
private: |
---|
129 |
CalleePtr m_pCallee; |
---|
130 |
MemFunPtr m_pMemFun; |
---|
131 |
Parm0 m_parm0; |
---|
132 |
sem_t* m_pSem; |
---|
133 |
bool m_bDelete; |
---|
134 |
}; |
---|
135 |
|
---|
136 |
template< typename FunPtr > |
---|
137 |
class CallbackFunctor0 |
---|
138 |
: public Functor |
---|
139 |
{ |
---|
140 |
public: |
---|
141 |
CallbackFunctor0( FunPtr pMemFun, |
---|
142 |
bool bDelete = true ) |
---|
143 |
: m_pMemFun( pMemFun ) |
---|
144 |
, m_pSem( 0 ) |
---|
145 |
, m_bDelete( bDelete ) |
---|
146 |
{} |
---|
147 |
|
---|
148 |
CallbackFunctor0( FunPtr pMemFun, |
---|
149 |
sem_t* pSem, |
---|
150 |
bool bDelete = true ) |
---|
151 |
: m_pMemFun( pMemFun ) |
---|
152 |
, m_pSem( pSem ) |
---|
153 |
, m_bDelete( bDelete ) |
---|
154 |
{} |
---|
155 |
|
---|
156 |
virtual ~CallbackFunctor0() |
---|
157 |
{} |
---|
158 |
|
---|
159 |
virtual void operator() () |
---|
160 |
{ |
---|
161 |
( *m_pMemFun )(); |
---|
162 |
if ( m_pSem ) { |
---|
163 |
sem_post( m_pSem); |
---|
164 |
} |
---|
165 |
if (m_bDelete) { |
---|
166 |
delete this; |
---|
167 |
} |
---|
168 |
} |
---|
169 |
|
---|
170 |
private: |
---|
171 |
FunPtr m_pMemFun; |
---|
172 |
sem_t* m_pSem; |
---|
173 |
bool m_bDelete; |
---|
174 |
}; |
---|
175 |
}; // end of namespace Util |
---|
176 |
|
---|
177 |
#endif |
---|
178 |
|
---|
179 |
|
---|