Pyrogenesis  trunk
app_hooks.h
Go to the documentation of this file.
1 /* Copyright (C) 2022 Wildfire Games.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining
4  * a copy of this software and associated documentation files (the
5  * "Software"), to deal in the Software without restriction, including
6  * without limitation the rights to use, copy, modify, merge, publish,
7  * distribute, sublicense, and/or sell copies of the Software, and to
8  * permit persons to whom the Software is furnished to do so, subject to
9  * the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 /*
24  * hooks to allow customization / app-specific behavior.
25  */
26 
27 /*
28 
29 Background
30 ----------
31 
32 This codebase is shared between several projects, each with differing needs.
33 Some of them have e.g. complicated i18n/translation facilities and require
34 all text output to go through it; others strive to minimize size and
35 therefore do not want to include that.
36 Since commenting things out isn't an option with shared source and
37 conditional compilation is ugly, we bridge the differences via "hooks".
38 These are functions whose behavior is expected to differ between projects;
39 they are defined by the app, registered here and called from lib code via
40 our trampolines.
41 
42 
43 Introduction
44 ------------
45 
46 This module provides a clean interface for other code to call hooks
47 and allows the app to register them. It also defines default stub
48 implementations.
49 
50 
51 Usage
52 -----
53 
54 In the simplest case, the stubs are already acceptable. Otherwise,
55 you need to implement a new version of some hooks, fill an
56 AppHooks struct with pointers to those functions (zero the rest),
57 and call app_hooks_update.
58 
59 
60 Adding New Functions
61 --------------------
62 
63 Several steps are needed (see below for rationale):
64 0) HOOKNAME is the name of the desired procedure (e.g. "bundle_logs")
65 1) add a 'trampoline' (user visible function) declaration to this header
66  (typically named ah_HOOKNAME)
67 2) add the corresponding implementation, i.e. call to ah.HOOKNAME
68 3) add a default implementation of the new functionality
69  (typically named def_HOOKNAME)
70 4) add HOOKNAME member to struct AppHooks declaration
71 5) set HOOKNAME member to def_HOOKNAME in initialization of
72  'struct AppHooks ah'
73 6) add HOOKNAME to list in app_hooks_update code
74 
75 
76 Rationale
77 ---------
78 
79 While X-Macros would reduce the amount of work needed when adding new
80 functions, they confuse static code analysis and VisualAssist X
81 (the function definitions are not visible to them).
82 We prefer convenience during usage instead of in the rare cases
83 where new app hook functions are defined.
84 
85 note: an X-Macro would define the app hook as such:
86 extern const wchar_t*, translate, (const wchar_t* text), (text), return)
87 .. and in its various invocations perform the above steps automatically.
88 
89 */
90 
91 #ifndef INCLUDED_APP_HOOKS
92 #define INCLUDED_APP_HOOKS
93 
94 #include "lib/os_path.h"
95 
96 // trampolines for user code to call the hooks. they encapsulate
97 // the details of how exactly to do this.
98 
99 /**
100  * return path to directory into which crash dumps should be written.
101  *
102  * must be callable at any time - in particular, before VFS init.
103  * paths are typically relative to sys_ExecutablePathname.
104  *
105  * @return path ending with directory separator (e.g. '/').
106  **/
107 extern const OsPath& ah_get_log_dir();
108 
109 /**
110  * gather all app-related logs/information and write it to file.
111  *
112  * used when writing a crash log so that all relevant info is in one file.
113  *
114  * the default implementation attempts to gather 0ad data, but is
115  * fail-safe (doesn't complain if file not found).
116  *
117  * @param f file into which to write.
118  **/
119 extern void ah_bundle_logs(FILE* f);
120 
121 /**
122  * display an error dialog, thus overriding sys_display_error.
123  *
124  * @param text error message.
125  * @param flags see DebugDisplayErrorFlags.
126  * @return ErrorReactionInternal.
127  *
128  * the default implementation just returns ERI_NOT_IMPLEMENTED, which
129  * causes the normal sys_display_error to be used.
130  **/
131 extern ErrorReactionInternal ah_display_error(const wchar_t* text, size_t flags);
132 
133 
134 /**
135  * holds a function pointer (allowed to be NULL) for each hook.
136  * passed to app_hooks_update.
137  **/
138 struct AppHooks
139 {
140  const OsPath& (*get_log_dir)();
141  void (*bundle_logs)(FILE* f);
142  ErrorReactionInternal (*display_error)(const wchar_t* text, size_t flags);
143 };
144 
145 /**
146  * update the app hook function pointers.
147  *
148  * @param ah AppHooks struct. any of its function pointers that are non-zero
149  * override the previous function pointer value
150  * (these default to the stub hooks which are functional but basic).
151  **/
153 
154 /**
155  * was the app hook changed via app_hooks_update from its default value?
156  *
157  * @param offset_in_struct byte offset within AppHooks (determined via
158  * offsetof) of the app hook function pointer.
159  **/
160 extern bool app_hook_was_redefined(size_t offset_in_struct);
161 // name is identifier of the function pointer within AppHooks to test.
162 #define AH_IS_DEFINED(name) app_hook_was_redefined(offsetof(AppHooks, name))
163 
164 #endif // INCLUDED_APP_HOOKS
void app_hooks_update(AppHooks *ah)
update the app hook function pointers.
Definition: app_hooks.cpp:78
const OsPath & ah_get_log_dir()
return path to directory into which crash dumps should be written.
Definition: app_hooks.cpp:105
void ah_bundle_logs(FILE *f)
gather all app-related logs/information and write it to file.
Definition: app_hooks.cpp:110
Definition: path.h:79
void(* bundle_logs)(FILE *f)
Definition: app_hooks.h:141
ErrorReactionInternal(* display_error)(const wchar_t *text, size_t flags)
Definition: app_hooks.h:142
bool app_hook_was_redefined(size_t offset_in_struct)
was the app hook changed via app_hooks_update from its default value?
Definition: app_hooks.cpp:89
ErrorReactionInternal ah_display_error(const wchar_t *text, size_t flags)
display an error dialog, thus overriding sys_display_error.
Definition: app_hooks.cpp:115
holds a function pointer (allowed to be NULL) for each hook.
Definition: app_hooks.h:138
Definition: mongoose.cpp:2106
ErrorReactionInternal
all choices offered by the error dialog.
Definition: debug.h:153