diff options
Diffstat (limited to 'src/3rdParty/efsw/Thread.hpp')
-rwxr-xr-x | src/3rdParty/efsw/Thread.hpp | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/src/3rdParty/efsw/Thread.hpp b/src/3rdParty/efsw/Thread.hpp new file mode 100755 index 0000000..b60373c --- /dev/null +++ b/src/3rdParty/efsw/Thread.hpp | |||
@@ -0,0 +1,100 @@ | |||
1 | #ifndef EFSW_THREAD_HPP | ||
2 | #define EFSW_THREAD_HPP | ||
3 | |||
4 | #include <efsw/base.hpp> | ||
5 | |||
6 | namespace efsw { | ||
7 | |||
8 | namespace Platform { | ||
9 | class ThreadImpl; | ||
10 | } | ||
11 | namespace Private { | ||
12 | struct ThreadFunc; | ||
13 | } | ||
14 | |||
15 | /** @brief Thread manager class */ | ||
16 | class Thread { | ||
17 | public: | ||
18 | typedef void ( *FuncType )( void* ); | ||
19 | |||
20 | template <typename F> Thread( F function ); | ||
21 | |||
22 | template <typename F, typename A> Thread( F function, A argument ); | ||
23 | |||
24 | template <typename C> Thread( void ( C::*function )(), C* object ); | ||
25 | |||
26 | virtual ~Thread(); | ||
27 | |||
28 | /** Launch the thread */ | ||
29 | virtual void launch(); | ||
30 | |||
31 | /** Wait the thread until end */ | ||
32 | void wait(); | ||
33 | |||
34 | /** Terminate the thread */ | ||
35 | void terminate(); | ||
36 | |||
37 | protected: | ||
38 | Thread(); | ||
39 | |||
40 | private: | ||
41 | friend class Platform::ThreadImpl; | ||
42 | |||
43 | /** The virtual function to run in the thread */ | ||
44 | virtual void run(); | ||
45 | |||
46 | Platform::ThreadImpl* mThreadImpl; ///< OS-specific implementation of the thread | ||
47 | Private::ThreadFunc* mEntryPoint; ///< Abstraction of the function to run | ||
48 | }; | ||
49 | |||
50 | //! NOTE: Taken from SFML2 threads | ||
51 | namespace Private { | ||
52 | |||
53 | // Base class for abstract thread functions | ||
54 | struct ThreadFunc { | ||
55 | virtual ~ThreadFunc() {} | ||
56 | virtual void run() = 0; | ||
57 | }; | ||
58 | |||
59 | // Specialization using a functor (including free functions) with no argument | ||
60 | template <typename T> struct ThreadFunctor : ThreadFunc { | ||
61 | ThreadFunctor( T functor ) : m_functor( functor ) {} | ||
62 | virtual void run() { m_functor(); } | ||
63 | T m_functor; | ||
64 | }; | ||
65 | |||
66 | // Specialization using a functor (including free functions) with one argument | ||
67 | template <typename F, typename A> struct ThreadFunctorWithArg : ThreadFunc { | ||
68 | ThreadFunctorWithArg( F function, A arg ) : m_function( function ), m_arg( arg ) {} | ||
69 | virtual void run() { m_function( m_arg ); } | ||
70 | F m_function; | ||
71 | A m_arg; | ||
72 | }; | ||
73 | |||
74 | // Specialization using a member function | ||
75 | template <typename C> struct ThreadMemberFunc : ThreadFunc { | ||
76 | ThreadMemberFunc( void ( C::*function )(), C* object ) : | ||
77 | m_function( function ), m_object( object ) {} | ||
78 | virtual void run() { ( m_object->*m_function )(); } | ||
79 | void ( C::*m_function )(); | ||
80 | C* m_object; | ||
81 | }; | ||
82 | |||
83 | } // namespace Private | ||
84 | |||
85 | template <typename F> | ||
86 | Thread::Thread( F functor ) : | ||
87 | mThreadImpl( NULL ), mEntryPoint( new Private::ThreadFunctor<F>( functor ) ) {} | ||
88 | |||
89 | template <typename F, typename A> | ||
90 | Thread::Thread( F function, A argument ) : | ||
91 | mThreadImpl( NULL ), | ||
92 | mEntryPoint( new Private::ThreadFunctorWithArg<F efCOMMA A>( function, argument ) ) {} | ||
93 | |||
94 | template <typename C> | ||
95 | Thread::Thread( void ( C::*function )(), C* object ) : | ||
96 | mThreadImpl( NULL ), mEntryPoint( new Private::ThreadMemberFunc<C>( function, object ) ) {} | ||
97 | |||
98 | } // namespace efsw | ||
99 | |||
100 | #endif | ||