1 |
/* threads.h |
---|
2 |
* Copyright (C) 2006 by Daniel Wagner |
---|
3 |
* |
---|
4 |
* This file is part of FreeBoB. |
---|
5 |
* |
---|
6 |
* FreeBoB is free software; you can redistribute it and/or modify |
---|
7 |
* it under the terms of the GNU General Public License as published by |
---|
8 |
* the Free Software Foundation; either version 2 of the License, or |
---|
9 |
* (at your option) any later version. |
---|
10 |
* FreeBoB is distributed in the hope that it will be useful, |
---|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 |
* GNU General Public License for more details. |
---|
14 |
* |
---|
15 |
* You should have received a copy of the GNU General Public License |
---|
16 |
* along with FreeBoB; if not, write to the Free Software |
---|
17 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
---|
18 |
* MA 02111-1307 USA. |
---|
19 |
*/ |
---|
20 |
|
---|
21 |
#ifndef THREADS_H |
---|
22 |
#define THREADS_H |
---|
23 |
|
---|
24 |
#include <semaphore.h> |
---|
25 |
|
---|
26 |
class Functor |
---|
27 |
{ |
---|
28 |
public: |
---|
29 |
Functor() {} |
---|
30 |
virtual ~Functor() {} |
---|
31 |
|
---|
32 |
virtual void operator() () = 0; |
---|
33 |
}; |
---|
34 |
|
---|
35 |
//////////////////////////////////////////////////////////////////////// |
---|
36 |
|
---|
37 |
template< typename CalleePtr, typename MemFunPtr > |
---|
38 |
class MemberFunctor0 |
---|
39 |
: public Functor |
---|
40 |
{ |
---|
41 |
public: |
---|
42 |
MemberFunctor0( const CalleePtr& pCallee, |
---|
43 |
MemFunPtr pMemFun, |
---|
44 |
bool bDelete = true ) |
---|
45 |
: m_pCallee( pCallee ) |
---|
46 |
, m_pMemFun( pMemFun ) |
---|
47 |
, m_pSem( 0 ) |
---|
48 |
, m_bDelete( bDelete ) |
---|
49 |
{} |
---|
50 |
|
---|
51 |
MemberFunctor0( const CalleePtr& pCallee, |
---|
52 |
MemFunPtr pMemFun, |
---|
53 |
sem_t* pSem, |
---|
54 |
bool bDelete = true ) |
---|
55 |
: m_pCallee( pCallee ) |
---|
56 |
, m_pMemFun( pMemFun ) |
---|
57 |
, m_pSem( pSem ) |
---|
58 |
, m_bDelete( bDelete ) |
---|
59 |
{} |
---|
60 |
|
---|
61 |
virtual ~MemberFunctor0() |
---|
62 |
{} |
---|
63 |
|
---|
64 |
virtual void operator() () |
---|
65 |
{ |
---|
66 |
( ( *m_pCallee ).*m_pMemFun )(); |
---|
67 |
if ( m_pSem ) { |
---|
68 |
sem_post( m_pSem); |
---|
69 |
} |
---|
70 |
if (m_bDelete) { |
---|
71 |
delete this; |
---|
72 |
} |
---|
73 |
} |
---|
74 |
|
---|
75 |
private: |
---|
76 |
CalleePtr m_pCallee; |
---|
77 |
MemFunPtr m_pMemFun; |
---|
78 |
sem_t* m_pSem; |
---|
79 |
bool m_bDelete; |
---|
80 |
}; |
---|
81 |
|
---|
82 |
template< typename CalleePtr, typename MemFunPtr, typename Parm0 > |
---|
83 |
class MemberFunctor1 |
---|
84 |
: public Functor |
---|
85 |
{ |
---|
86 |
public: |
---|
87 |
MemberFunctor1( const CalleePtr& pCallee, |
---|
88 |
MemFunPtr pMemFun, |
---|
89 |
Parm0 parm0, |
---|
90 |
bool bDelete = true) |
---|
91 |
: m_pCallee( pCallee ) |
---|
92 |
, m_pMemFun( pMemFun ) |
---|
93 |
, m_parm0( parm0 ) |
---|
94 |
, m_pSem( 0 ) |
---|
95 |
, m_bDelete( bDelete ) |
---|
96 |
{} |
---|
97 |
|
---|
98 |
MemberFunctor1( const CalleePtr& pCallee, |
---|
99 |
MemFunPtr pMemFun, |
---|
100 |
Parm0 parm0, |
---|
101 |
sem_t* pSem, |
---|
102 |
bool bDelete = true ) |
---|
103 |
: m_pCallee( pCallee ) |
---|
104 |
, m_pMemFun( pMemFun ) |
---|
105 |
, m_parm0( parm0 ) |
---|
106 |
, m_pSem( 0 ) |
---|
107 |
, m_bDelete( bDelete ) |
---|
108 |
{} |
---|
109 |
virtual ~MemberFunctor1() |
---|
110 |
{} |
---|
111 |
|
---|
112 |
virtual void operator() () |
---|
113 |
{ |
---|
114 |
( ( *m_pCallee ).*m_pMemFun )( m_parm0 ); |
---|
115 |
if ( m_pSem ) { |
---|
116 |
sem_post( m_pSem); |
---|
117 |
} |
---|
118 |
if (m_bDelete) { |
---|
119 |
delete this; |
---|
120 |
} |
---|
121 |
} |
---|
122 |
|
---|
123 |
private: |
---|
124 |
CalleePtr m_pCallee; |
---|
125 |
MemFunPtr m_pMemFun; |
---|
126 |
Parm0 m_parm0; |
---|
127 |
sem_t* m_pSem; |
---|
128 |
bool m_bDelete; |
---|
129 |
}; |
---|
130 |
|
---|
131 |
#endif |
---|
132 |
|
---|
133 |
|
---|