The Battle for Wesnoth  1.17.0-dev
static_registry.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2021
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 using registered_widget_map = std::map<std::string, registered_widget_parser>;
115 
116 /** Returns the list of registered widgets and their parsers. */
118 
119 using widget_builder_map = std::map<std::string, widget_builder_func_t>;
120 
121 /** Returns the list of registered widget builders. */
123 
124 } // namespace gui2
void register_window(const std::string &id)
Registers a window.
std::map< std::string, widget_builder_func_t > widget_builder_map
std::function< styled_widget_definition_ptr(const config &)> widget_parser_t
Function type alias for register_widget.
widget_parser_t parser
The widget definition WML parser function.
std::function< builder_widget_ptr(const config &)> widget_builder_func_t
Function type alias for register_widget_builder.
Generic file dialog.
Definition: field-fwd.hpp:23
void register_widget(const std::string &type, widget_parser_t f, const char *key)
Registers a widget type.
registered_widget_map & registered_widget_types()
Returns the list of registered widgets and their parsers.
widget_builder_map & widget_builder_lookup()
Returns the list of registered widget builders.
#define f
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_map
std::set< std::string > & registered_window_types()
Returns the list of registered windows.
const char * key
The tag containing the definition WML.