Pyrogenesis  trunk
LoaderThunks.h
Go to the documentation of this file.
1 /* Copyright (C) 2021 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef INCLUDED_LOADERTHUNKS
19 #define INCLUDED_LOADERTHUNKS
20 
21 // does this return code indicate the coroutine yielded and
22 // wasn't yet finished?
23 static inline bool ldr_was_interrupted(int ret)
24 {
25  return (0 < ret && ret <= 100);
26 }
27 
28 template<class T> struct MemFun_t
29 {
31 public:
32  T* const this_;
33  int (T::*func)(void);
34  MemFun_t(T* this__, int(T::*func_)(void))
35  : this_(this__), func(func_) {}
36 };
37 
38 template<class T> static int MemFunThunk(std::shared_ptr<void> param, double UNUSED(time_left))
39 {
40  MemFun_t<T>* const mf = static_cast<MemFun_t<T>*>(param.get());
41  return (mf->this_->*mf->func)();
42 }
43 
44 template<class T> void RegMemFun(T* this_, int(T::*func)(void),
45  const wchar_t* description, int estimated_duration_ms)
46 {
47  LDR_Register(MemFunThunk<T>, std::make_shared<MemFun_t<T>>(this_, func), description, estimated_duration_ms);
48 }
49 
50 
51 ////////////////////////////////////////////////////////
52 
53 
54 template<class T, class Arg> struct MemFun1_t
55 {
57 public:
58  T* const this_;
59  Arg arg;
60  int (T::*func)(Arg);
61  MemFun1_t(T* this__, int(T::*func_)(Arg), Arg arg_)
62  : this_(this__), func(func_), arg(arg_) {}
63 };
64 
65 template<class T, class Arg> static int MemFun1Thunk(std::shared_ptr<void> param, double UNUSED(time_left))
66 {
67  MemFun1_t<T, Arg>* const mf = static_cast<MemFun1_t<T, Arg>*>(param.get());
68  return (mf->this_->*mf->func)(mf->arg);
69 }
70 
71 template<class T, class Arg> void RegMemFun1(T* this_, int(T::*func)(Arg), Arg arg,
72  const wchar_t* description, int estimated_duration_ms)
73 {
74  LDR_Register(MemFun1Thunk<T, Arg>, std::make_shared<MemFun1_t<T, Arg> >(this_, func, arg), description, estimated_duration_ms);
75 }
76 
77 #endif // INCLUDED_LOADERTHUNKS
Definition: LoaderThunks.h:54
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning. ...
Definition: code_annotation.h:38
T *const this_
Definition: LoaderThunks.h:58
Definition: LoaderThunks.h:28
int(T::* func)(void)
Definition: LoaderThunks.h:33
Arg arg
Definition: LoaderThunks.h:59
int(T::* func)(Arg)
Definition: LoaderThunks.h:60
static int MemFun1Thunk(std::shared_ptr< void > param, double time_left)
Definition: LoaderThunks.h:65
void LDR_Register(LoadFunc func, std::shared_ptr< void > param, const wchar_t *description, int estimated_duration_ms)
Definition: Loader.cpp:106
MemFun1_t(T *this__, int(T::*func_)(Arg), Arg arg_)
Definition: LoaderThunks.h:61
static int MemFunThunk(std::shared_ptr< void > param, double time_left)
Definition: LoaderThunks.h:38
#define T(string_literal)
Definition: secure_crt.cpp:77
T *const this_
Definition: LoaderThunks.h:32
static bool ldr_was_interrupted(int ret)
Definition: LoaderThunks.h:23
NONCOPYABLE(MemFun_t)
MemFun_t(T *this__, int(T::*func_)(void))
Definition: LoaderThunks.h:34
void RegMemFun1(T *this_, int(T::*func)(Arg), Arg arg, const wchar_t *description, int estimated_duration_ms)
Definition: LoaderThunks.h:71
void RegMemFun(T *this_, int(T::*func)(void), const wchar_t *description, int estimated_duration_ms)
Definition: LoaderThunks.h:44