Library of Assembled Shared Sources
thread_fun.h
Go to the documentation of this file.
1/*
2 * *** ATTENTION! DO NOT MODIFY THIS FILE DIRECTLY! ***
3 *
4 * It has automatically been generated from thread_fun.tmpl.h
5 * by param_expander.py on Mon Oct 6 22:51:38 2025.
6 */
7
8/** @file
9 * @author Bram de Greve (bram@cocamware.com)
10 * @author Tom De Muer (tom@cocamware.com)
11 *
12 * *** BEGIN LICENSE INFORMATION ***
13 *
14 * The contents of this file are subject to the Common Public Attribution License
15 * Version 1.0 (the "License"); you may not use this file except in compliance with
16 * the License. You may obtain a copy of the License at
17 * http://lass.sourceforge.net/cpal-license. The License is based on the
18 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
19 * use of software over a computer network and provide for limited attribution for
20 * the Original Developer. In addition, Exhibit A has been modified to be consistent
21 * with Exhibit B.
22 *
23 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
24 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
25 * language governing rights and limitations under the License.
26 *
27 * The Original Code is LASS - Library of Assembled Shared Sources.
28 *
29 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
30 * The Original Developer is the Initial Developer.
31 *
32 * All portions of the code written by the Initial Developer are:
33 * Copyright (C) 2004-2011 the Initial Developer.
34 * All Rights Reserved.
35 *
36 * Contributor(s):
37 *
38 * Alternatively, the contents of this file may be used under the terms of the
39 * GNU General Public License Version 2 or later (the GPL), in which case the
40 * provisions of GPL are applicable instead of those above. If you wish to allow use
41 * of your version of this file only under the terms of the GPL and not to allow
42 * others to use your version of this file under the CPAL, indicate your decision by
43 * deleting the provisions above and replace them with the notice and other
44 * provisions required by the GPL License. If you do not delete the provisions above,
45 * a recipient may use your version of this file under either the CPAL or the GPL.
46 *
47 * *** END LICENSE INFORMATION ***
48 */
49
50/** @class ThreadFun
51 * @brief library to run (member) functions in a thread
52 * @author [Bramz]
53 *
54 * With these objects/functions, you can run (member) functions in threads instead of calling
55 * them directly. you even can specify the arguments to be used. Let's illustrate this with
56 * a simple example:
57 *
58 * @code
59 * void foo(int iNumber, const std::string& iText);
60 *
61 * // direct call
62 * foo(5, "hello");
63 *
64 * // through thread
65 * threadFun(foo, 5, "hello")->run();
66 *
67 * @endcode
68 *
69 * threadFun2 allocates a new thread with the necessary information to call the function,
70 * then creates and runs the thread. It returns a pointer to the allocated thread. If you do
71 * nothing, the thread runs in @e detached mode and will kill itself at completion (so you don't
72 * have to call @c delete on @c thread yourself. So you even don't have to catch the return
73 * value. However, if you want to run it in @e joinable, you'll have delete the thread
74 * yourself. DO NOT FORGET THIS! Maybe we should do something about this situation?
75 *
76 * @code
77 * // run two threads at the same time.
78 * std::unique_ptr<Thread> a(threadFun(foo, 5, "hello", threadJoinable));
79 * std::unique_ptr<Thread> b(threadFun(foo, 6, "world!", threadJoinable));
80 * a->run();
81 * b->run();
82 * a->join();
83 * b->join();
84 * @endcode
85 *
86 * You can also call member functions of specific objects in the thread, both const as non-const
87 * member functions.
88 *
89 * @code
90 * class Bar
91 * {
92 * void fun(float iValue) const;
93 * void beer();
94 * };
95 *
96 * Bar bar;
97 * std::unique_ptr<Thread> a(threadFun(bar, Bar::fun, 3.14, threadJoinable));
98 * std::unique_ptr<Thread> b(threadFun(bar, Bar::beer, threadJoinable));
99 * a->run();
100 * b->run();
101 * a->join();
102 * b->join();
103 * @endcode
104 *
105 * Limitations:
106 * - the functions can't have return values, or they are ignored (who should they pass it
107 * through to, anyway?)
108 * - You can't have more than 15 arguments (unless you tweak the prebuild ;)
109 *
110 * @warning Only ThreadFun0 and ThreadFun1 are partially tested.
111 */
112
113#ifndef LASS_GUARDIAN_OF_INCLUSION_UTIL_THREAD_FUN_H
114#define LASS_GUARDIAN_OF_INCLUSION_UTIL_THREAD_FUN_H
115
116#include "util_common.h"
117#include "callback_0.h"
118#include "thread.h"
119#include "../meta/type_traits.h"
120
121namespace lass
122{
123namespace util
124{
125
126class LASS_DLL ThreadFun: public Thread
127{
128public:
129 ThreadFun(const Callback0& fun, ThreadKind kind = threadDetached);
130private:
131 void doRun() override;
132 Callback0 fun_;
133};
134
135// --- 0 arguments ---------------------------------------------------------------------------------
136
137template <typename Function>
138ThreadFun* threadFun(
139 Function function,
141
142template <typename ObjectPtr, typename Method>
143ThreadFun* threadMemFun(
144 ObjectPtr object, Method method,
146
147
148// --- 1 argument(s) ----------------------------------------------------------------------------
149
150template <typename P1, typename Function>
151ThreadFun* threadFun(
152 Function function,
153 const P1& iP1,
155
156template <typename P1, typename ObjectPtr, typename Method>
157ThreadFun* threadMemFun(
158 ObjectPtr object, Method method,
159 const P1& iP1,
161
162// --- 2 argument(s) ----------------------------------------------------------------------------
163
164template <typename P1, typename P2, typename Function>
165ThreadFun* threadFun(
166 Function function,
167 const P1& iP1, const P2& iP2,
169
170template <typename P1, typename P2, typename ObjectPtr, typename Method>
171ThreadFun* threadMemFun(
172 ObjectPtr object, Method method,
173 const P1& iP1, const P2& iP2,
175
176// --- 3 argument(s) ----------------------------------------------------------------------------
177
178template <typename P1, typename P2, typename P3, typename Function>
179ThreadFun* threadFun(
180 Function function,
181 const P1& iP1, const P2& iP2, const P3& iP3,
183
184template <typename P1, typename P2, typename P3, typename ObjectPtr, typename Method>
185ThreadFun* threadMemFun(
186 ObjectPtr object, Method method,
187 const P1& iP1, const P2& iP2, const P3& iP3,
189
190// --- 4 argument(s) ----------------------------------------------------------------------------
191
192template <typename P1, typename P2, typename P3, typename P4, typename Function>
193ThreadFun* threadFun(
194 Function function,
195 const P1& iP1, const P2& iP2, const P3& iP3, const P4& iP4,
197
198template <typename P1, typename P2, typename P3, typename P4, typename ObjectPtr, typename Method>
199ThreadFun* threadMemFun(
200 ObjectPtr object, Method method,
201 const P1& iP1, const P2& iP2, const P3& iP3, const P4& iP4,
203
204// --- 5 argument(s) ----------------------------------------------------------------------------
205
206template <typename P1, typename P2, typename P3, typename P4, typename P5, typename Function>
207ThreadFun* threadFun(
208 Function function,
209 const P1& iP1, const P2& iP2, const P3& iP3, const P4& iP4, const P5& iP5,
211
212template <typename P1, typename P2, typename P3, typename P4, typename P5, typename ObjectPtr, typename Method>
213ThreadFun* threadMemFun(
214 ObjectPtr object, Method method,
215 const P1& iP1, const P2& iP2, const P3& iP3, const P4& iP4, const P5& iP5,
217
218// --- 6 argument(s) ----------------------------------------------------------------------------
219
220template <typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename Function>
221ThreadFun* threadFun(
222 Function function,
223 const P1& iP1, const P2& iP2, const P3& iP3, const P4& iP4, const P5& iP5, const P6& iP6,
225
226template <typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename ObjectPtr, typename Method>
227ThreadFun* threadMemFun(
228 ObjectPtr object, Method method,
229 const P1& iP1, const P2& iP2, const P3& iP3, const P4& iP4, const P5& iP5, const P6& iP6,
231
232// --- 7 argument(s) ----------------------------------------------------------------------------
233
234template <typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename Function>
235ThreadFun* threadFun(
236 Function function,
237 const P1& iP1, const P2& iP2, const P3& iP3, const P4& iP4, const P5& iP5, const P6& iP6, const P7& iP7,
239
240template <typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename ObjectPtr, typename Method>
241ThreadFun* threadMemFun(
242 ObjectPtr object, Method method,
243 const P1& iP1, const P2& iP2, const P3& iP3, const P4& iP4, const P5& iP5, const P6& iP6, const P7& iP7,
245
246// --- 8 argument(s) ----------------------------------------------------------------------------
247
248template <typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename Function>
249ThreadFun* threadFun(
250 Function function,
251 const P1& iP1, const P2& iP2, const P3& iP3, const P4& iP4, const P5& iP5, const P6& iP6, const P7& iP7, const P8& iP8,
253
254template <typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename ObjectPtr, typename Method>
255ThreadFun* threadMemFun(
256 ObjectPtr object, Method method,
257 const P1& iP1, const P2& iP2, const P3& iP3, const P4& iP4, const P5& iP5, const P6& iP6, const P7& iP7, const P8& iP8,
259
260// --- 9 argument(s) ----------------------------------------------------------------------------
261
262template <typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9, typename Function>
263ThreadFun* threadFun(
264 Function function,
265 const P1& iP1, const P2& iP2, const P3& iP3, const P4& iP4, const P5& iP5, const P6& iP6, const P7& iP7, const P8& iP8, const P9& iP9,
267
268template <typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9, typename ObjectPtr, typename Method>
269ThreadFun* threadMemFun(
270 ObjectPtr object, Method method,
271 const P1& iP1, const P2& iP2, const P3& iP3, const P4& iP4, const P5& iP5, const P6& iP6, const P7& iP7, const P8& iP8, const P9& iP9,
273
274// --- 10 argument(s) ----------------------------------------------------------------------------
275
276template <typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9, typename P10, typename Function>
277ThreadFun* threadFun(
278 Function function,
279 const P1& iP1, const P2& iP2, const P3& iP3, const P4& iP4, const P5& iP5, const P6& iP6, const P7& iP7, const P8& iP8, const P9& iP9, const P10& iP10,
281
282template <typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9, typename P10, typename ObjectPtr, typename Method>
283ThreadFun* threadMemFun(
284 ObjectPtr object, Method method,
285 const P1& iP1, const P2& iP2, const P3& iP3, const P4& iP4, const P5& iP5, const P6& iP6, const P7& iP7, const P8& iP8, const P9& iP9, const P10& iP10,
287
288// --- 11 argument(s) ----------------------------------------------------------------------------
289
290template <typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9, typename P10, typename P11, typename Function>
291ThreadFun* threadFun(
292 Function function,
293 const P1& iP1, const P2& iP2, const P3& iP3, const P4& iP4, const P5& iP5, const P6& iP6, const P7& iP7, const P8& iP8, const P9& iP9, const P10& iP10, const P11& iP11,
295
296template <typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9, typename P10, typename P11, typename ObjectPtr, typename Method>
297ThreadFun* threadMemFun(
298 ObjectPtr object, Method method,
299 const P1& iP1, const P2& iP2, const P3& iP3, const P4& iP4, const P5& iP5, const P6& iP6, const P7& iP7, const P8& iP8, const P9& iP9, const P10& iP10, const P11& iP11,
301
302// --- 12 argument(s) ----------------------------------------------------------------------------
303
304template <typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9, typename P10, typename P11, typename P12, typename Function>
305ThreadFun* threadFun(
306 Function function,
307 const P1& iP1, const P2& iP2, const P3& iP3, const P4& iP4, const P5& iP5, const P6& iP6, const P7& iP7, const P8& iP8, const P9& iP9, const P10& iP10, const P11& iP11, const P12& iP12,
309
310template <typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9, typename P10, typename P11, typename P12, typename ObjectPtr, typename Method>
311ThreadFun* threadMemFun(
312 ObjectPtr object, Method method,
313 const P1& iP1, const P2& iP2, const P3& iP3, const P4& iP4, const P5& iP5, const P6& iP6, const P7& iP7, const P8& iP8, const P9& iP9, const P10& iP10, const P11& iP11, const P12& iP12,
315
316// --- 13 argument(s) ----------------------------------------------------------------------------
317
318template <typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9, typename P10, typename P11, typename P12, typename P13, typename Function>
319ThreadFun* threadFun(
320 Function function,
321 const P1& iP1, const P2& iP2, const P3& iP3, const P4& iP4, const P5& iP5, const P6& iP6, const P7& iP7, const P8& iP8, const P9& iP9, const P10& iP10, const P11& iP11, const P12& iP12, const P13& iP13,
323
324template <typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9, typename P10, typename P11, typename P12, typename P13, typename ObjectPtr, typename Method>
325ThreadFun* threadMemFun(
326 ObjectPtr object, Method method,
327 const P1& iP1, const P2& iP2, const P3& iP3, const P4& iP4, const P5& iP5, const P6& iP6, const P7& iP7, const P8& iP8, const P9& iP9, const P10& iP10, const P11& iP11, const P12& iP12, const P13& iP13,
329
330// --- 14 argument(s) ----------------------------------------------------------------------------
331
332template <typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9, typename P10, typename P11, typename P12, typename P13, typename P14, typename Function>
333ThreadFun* threadFun(
334 Function function,
335 const P1& iP1, const P2& iP2, const P3& iP3, const P4& iP4, const P5& iP5, const P6& iP6, const P7& iP7, const P8& iP8, const P9& iP9, const P10& iP10, const P11& iP11, const P12& iP12, const P13& iP13, const P14& iP14,
337
338template <typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9, typename P10, typename P11, typename P12, typename P13, typename P14, typename ObjectPtr, typename Method>
339ThreadFun* threadMemFun(
340 ObjectPtr object, Method method,
341 const P1& iP1, const P2& iP2, const P3& iP3, const P4& iP4, const P5& iP5, const P6& iP6, const P7& iP7, const P8& iP8, const P9& iP9, const P10& iP10, const P11& iP11, const P12& iP12, const P13& iP13, const P14& iP14,
343
344// --- 15 argument(s) ----------------------------------------------------------------------------
345
346template <typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9, typename P10, typename P11, typename P12, typename P13, typename P14, typename P15, typename Function>
347ThreadFun* threadFun(
348 Function function,
349 const P1& iP1, const P2& iP2, const P3& iP3, const P4& iP4, const P5& iP5, const P6& iP6, const P7& iP7, const P8& iP8, const P9& iP9, const P10& iP10, const P11& iP11, const P12& iP12, const P13& iP13, const P14& iP14, const P15& iP15,
351
352template <typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9, typename P10, typename P11, typename P12, typename P13, typename P14, typename P15, typename ObjectPtr, typename Method>
353ThreadFun* threadMemFun(
354 ObjectPtr object, Method method,
355 const P1& iP1, const P2& iP2, const P3& iP3, const P4& iP4, const P5& iP5, const P6& iP6, const P7& iP7, const P8& iP8, const P9& iP9, const P10& iP10, const P11& iP11, const P12& iP12, const P13& iP13, const P14& iP14, const P15& iP15,
357
358
359}
360
361}
362
363#include "thread_fun.inl"
364
365#endif
366
367// EOF
library to run (member) functions in a thread
ThreadKind
ThreadKind.
Definition thread.h:109
@ threadDetached
detached thread
Definition thread.h:110
#define LASS_DLL
DLL interface: import or export symbols?
Library for Assembled Shared Sources.
Definition config.h:53