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