The Battle for Wesnoth  1.19.7+dev
static_registry.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2024
3  by Mark de Wever <koraq@xs4all.nl>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program 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  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #pragma once
17 
20 
21 namespace gui2
22 {
23 /***** ***** ***** ***** Registrars ***** ***** ***** *****/
24 
25 /**
26  * Registers a window.
27  *
28  * This function is utilized by the @ref REGISTER_WINDOW macro and notes a
29  * window to look for when a GUI definition is initialized.
30  *
31  * All windows need to register themselves before @ref gui2::init is called.
32  *
33  * @warning This function runs before @ref main() so needs to be careful
34  * regarding the static initialization problem.
35  *
36  * @note A window can't be registered twice. Any subsequently added windows
37  * with duplicate IDs will be ignored. Might be worth looking into adding an
38  * unregister function in the future if this becomes an issue.
39  *
40  * @param id The id of the window to register.
41  */
42 void register_window(const std::string& id);
43 
44 /** Function type alias for @ref register_widget. */
45 using widget_parser_t = std::function<styled_widget_definition_ptr(const config&)>;
46 
47 /**
48  * Registers a widget type.
49  *
50  * This function is utilized by the @ref REGISTER_WIDGET macro and sets the
51  * the parser function used to process the widget type's WML when a GUI
52  * definition is initialized.
53  *
54  * All widgets need to register themselves before @ref gui2::init is called.
55  *
56  * @warning This function runs before @ref main() so needs to be careful
57  * regarding the static initialization problem.
58  *
59  * @param type The type of the widget to register.
60  * @param f The function to parse the definition config.
61  * @param key The tagname from which to read the widget's
62  * definition in the game config. If nullptr the
63  * default [<id>_definition] is used.
64  */
65 void register_widget(const std::string& type, widget_parser_t f, const char* key = nullptr);
66 
67 /** Function type alias for @ref register_widget_builder. */
68 using widget_builder_func_t = std::function<builder_widget_ptr(const config&)>;
69 
70 /**
71  * Registers a widget builder.
72  *
73  * A widget builder simply creates and returns a pointer to a widget type's
74  * builder struct. This is part of the static registry since widget builders
75  * are simply used to instantiate a widget object when a dialog is being
76  * assembled.
77  *
78  * If the widget inherits from @ref styled_widget, any theme-dependent info
79  * will be fetched from the current GUI theme upon construction.
80  *
81  * @warning This function runs before @ref main() so needs to be careful
82  * regarding the static initialization problem.
83  *
84  * @param type The type of the widget as used in WML.
85  * @param functor The functor to create the widget.
86  */
87 void register_widget_builder(const std::string& type, widget_builder_func_t functor);
88 
89 
90 /***** ***** ***** ***** Accessors ***** ***** ***** *****/
91 
92 /*
93  * Notes on the registered widget and window lists.
94  *
95  * These lists are independent of the active GUI definition and are filled
96  * during static initialization via @ref register_widget and @register_window.
97 
98  * Also note these cannot be free-standing static data members within this file
99  * since that can cause a crash.
100  */
101 
102 /** Returns the list of registered windows. */
103 std::set<std::string>& registered_window_types();
104 
106 {
107  /** The widget definition WML parser function. */
109 
110  /** The tag containing the definition WML. */
111  const char* key;
112 };
113 
114 /** Returns the list of registered widgets and their parsers. */
115 std::map<std::string, registered_widget_parser>& registered_widget_types();
116 
117 /** Returns the list of registered widget builders. */
118 std::map<std::string, widget_builder_func_t>& widget_builder_lookup();
119 
120 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
Generic file dialog.
std::shared_ptr< builder_widget > builder_widget_ptr
std::shared_ptr< styled_widget_definition > styled_widget_definition_ptr
std::set< std::string > & registered_window_types()
Returns the list of registered windows.
void register_window(const std::string &id)
Registers a window.
std::function< styled_widget_definition_ptr(const config &)> widget_parser_t
Function type alias for register_widget.
void register_widget_builder(const std::string &type, widget_builder_func_t functor)
Registers a widget builder.
std::map< std::string, registered_widget_parser > & registered_widget_types()
Returns the list of registered widgets and their parsers.
void register_widget(const std::string &type, widget_parser_t f, const char *key)
Registers a widget type.
std::function< builder_widget_ptr(const config &)> widget_builder_func_t
Function type alias for register_widget_builder.
std::map< std::string, widget_builder_func_t > & widget_builder_lookup()
Returns the list of registered widget builders.
const char * key
The tag containing the definition WML.
widget_parser_t parser
The widget definition WML parser function.
#define f