ALib C++ Library
Library Version: 2412 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
config/plugins.hpp
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header file is part of module \alib_config of the \aliblong.
4///
5/// \emoji :copyright: 2013-2024 A-Worx GmbH, Germany.
6/// Published under \ref mainpage_license "Boost Software License".
7//==================================================================================================
8#ifndef HPP_ALIB_CONFIG_PLUGINS
9#define HPP_ALIB_CONFIG_PLUGINS 1
10
11#include "configuration.hpp"
14
15namespace alib::config {
16
17// forwards
18class Configuration;
19class Variable;
20
21//==================================================================================================
22/// Abstract class that defines the plug-in interface for class \alib{config;Configuration}.
23/// Plug-ins provide configuration data (variables) from external configuration data sources
24/// at the moment those are requested.
25///
26/// The rationale for this concept is that some external configuration sources may provide a lot of
27/// data that is not related to an application. This is for example true for for environment
28/// variables, the windows registry or the Gnome variable systems <em>gconf/dconf</em>.
29/// In these cases, variables have to be read into the \alib configuration system only at the moment
30/// they are declared.
31///
32/// With other sources, like dedicated INI-files, a different technique is usually preferred:
33/// Here, all data is read and imported into the configuration system with an application's
34/// bootstrap and variables (respectively whole subtrees of variables) may be exported when an
35/// application exits, just to populate empty INI-files with default values or to add new variables
36/// which occur with a new software version.
37///
38/// Category and Variable names are character case insensitive for the plug-ins predefined
39/// with \alib. It is up to a custom implementation to decide to ignore character case in custom
40/// specializations of this class as well.
41//==================================================================================================
42class ConfigurationPlugin : public lang::Plugin<Configuration, Priority>
43{
44 protected:
45 /// A default string escaper instance. This is used with all plugins provided with \alib.
46 /// (Namely \alib{config;CLIVariablesPlugin} and \alib{config;EnvironmentVariablesPlugin}.)
48
49 /// Constructor which is protected, as this is an abstract class.
50 /// @param pPriority The priority that this plug-in uses.
52 : Plugin(pPriority) {}
53
54 public:
55 /// Virtual Destructor.
57
58 /// Derived types may return a different, customized implementation specific to their needs.
59 /// This default implementation returns field #stringEscaper.
60 /// @return An escaper used to convert string values from and to escaped sequences as provided
61 /// by a configuration source that is based on serialization of variable values to and
62 /// from ASCII/unicode strings which are human-readable and placeable in text files,
63 /// command line parameters, etc.
64 virtual const StringEscaper& GetEscaper() const { return stringEscaper; }
65
66 /// Abstract method. Descendents need to return a plug-in name. The name may be used in human
67 /// readable output, e.g., log-files or exception messages to tell a user for example, which
68 /// plug-in loaded a variable containing a syntax error.
69 /// @return The name of the plug-in.
70 virtual String Name() const =0;
71
72 //==============================================================================================
73 /// Abstract method that has to be overwritten by descendants.
74 /// Searches and retrieves the value of a configuration variable.
75 /// @param name The name of the variable to retrieve.
76 /// @param[out] target A reference to the buffer to write the variable's exported value to.
77 /// @return \c true if variable was found within this configuration source, \c false if not.
78 //==============================================================================================
80 virtual bool Get( const String& name, AString& target ) =0;
81};
82
83
84//==================================================================================================
85/// Specialization of abstract interface class #ConfigurationPlugin, which reads command line
86/// parameters from namespace globals #alib::ARG_C and #alib::ARG_VN / \alib{ARG_VW} on request.
87/// Its priority value usually is \alib{config,Priority,Priority::CLI}, which is higher
88/// than all other default plug-ins provided.
89///
90/// To recognize variables, the separation character <c>'/'</c> of the configuration tree is
91/// converted to underscore character <c>'_'</c>. For example, the \alib locale variable with path
92/// ALIB/LOCALE
93/// is recognized as
94/// ALIB_LOCALE
95///
96/// Variable names are insensitive in respect to character case.
97///
98/// Command line variables may be passed with either one hyphen ('-') or two ('--').
99/// Both are accepted.
100///
101/// An application can specify one or more "default categories" by adding their string names to
102/// public field #DefaultCategories. Variables of these categories are recognized by the plug-in
103/// also when given without the name prefix of category name and underscore \c '_'.
104///
105/// ## Friends ##
106/// class \alib{config,detail::nextCLIArg} (Used for implementation of iterator.)
107//==================================================================================================
109{
110 public:
111 /// If any value is added to this vector, its values are used as the source of command line
112 /// arguments instead of using \alib namespace variables \alib{ARG_C} and
113 /// \alib{ARG_VN}/\alib{ARG_VW}.<br>
114 /// This mechanic provides an alternative method to set the command line argument list.
115 ///
116 /// Applications that have a dedicated (more sophisticated) CLI interface which performs
117 /// more complex processing of CLI arguments, may collect any unrecognized
118 /// CLI argument here to be duly recognized as a configuration variable instead
120
121 /// An application can specify one or more "default categories" by adding a prefix of the
122 /// variable path here. Variables of these "categories" are recognized by the plug-in also
123 /// when given without this prefix.<br>
124 /// Please note, that if \alib and \alox variables should be abbreviatable, for example that
125 /// the locale can be given with
126 /// --locale=de_DE.UTF-8
127 /// instead of
128 /// --alib_locale=de_DE.UTF-8
129 /// this vector has to be populated during bootstrap phase \alib{BootstrapPhases;PrepareConfig}.
130 /// See chapter \ref alib_manual_bootstrapping_customize for more information on how to
131 /// customize \alib bootstrapping.
133
134 /// Determines whether zero, one or two introducing hyphen characters <c>'-'</c> are mandatory.
135 /// An command line argument is ignored if the number of leading hyphens is smaller than
136 /// the value set here.<br>
137 /// Defaults to \c 0.
138 /// @see Sibling option #QtyOptionalHyphens.
140
141 /// Determines whether zero, one or two optional hyphen characters <c>'-'</c> might be given.
142 /// An command line argument is ignored if the number of leading hyphens is greater than
143 /// the value set here.<br>
144 /// Defaults to \c 2.
145 /// @see Sibling option #QtyMandatoryHyphens.
147
148 /// Constructor.
149 /// @param ma The monotonic allocator to use. This usually is the one of the
150 /// configuration instance.
151 /// @param pPriority The priority that this plug-in uses. Defaults to
152 /// \alib{config;Priority;CLI}.
154 CLIVariablesPlugin( MonoAllocator& ma, Priority pPriority= Priority::CLI );
155
156 /// Virtual Destructor.
157 virtual ~CLIVariablesPlugin() override {}
158
159 /// @return The plug-in name, in this case, we read resource variable "CfgPlgCLI".
160 ALIB_API virtual String Name() const override;
161
162 /// Searches the variable in the command line parameters.
163 /// @param name The name of the variable to retrieve.
164 /// @param[out] target A reference to the buffer to write the variable's exported value to.
165 /// @return \c true if variable was found, \c false if not.
166 ALIB_API virtual bool Get( const String& name, AString& target ) override;
167};
168
169//==================================================================================================
170/// Specialization of abstract interface class #ConfigurationPlugin, retrieves configuration
171/// data from the system environment.
172///
173/// The priority value of this plug-in usually is \alib{config;Priority;Priority::Environment},
174/// which is higher than \alib{config;Priority;Standard} but lower than \alib{config;Priority;CLI}.
175///
176/// To recognize variables, the separation character <c>'/'</c> of the configuration tree is
177/// converted to underscore character <c>'_'</c>. For example, the \alib locale variable with path
178/// ALIB/LOCALE
179/// is recognized as
180/// ALIB_LOCALE
181///
182/// Category and Variable names are insensitive in respect to character case.
183//==================================================================================================
185{
186 public:
187 /// An application can specify one or more "default categories" by adding a prefix of the
188 /// variable path here. Variables of these "categories" are recognized by the plug-in also
189 /// when given without this prefix.<br>
190 /// Please note, that if \alib and \alox variables should be abbreviatable, for example that
191 /// the locale can be given with
192 /// --locale=de_DE.UTF-8
193 /// instead of
194 /// --alib_locale=de_DE.UTF-8
195 /// this vector has to be populated during bootstrap phase \alib{BootstrapPhases;PrepareConfig}.
196 /// See chapter \ref alib_manual_bootstrapping_customize for more information on how to
197 /// customize \alib bootstrapping.
199
200 /// Constructor.
201 /// @param ma The monotonic allocator to use. This usually is the one of the
202 /// configuration instance.
203 /// @param pPriority The priority that this plug-in uses. Defaults to
204 /// \alib{config;Priority;Environment}.
206 Priority pPriority = Priority::Environment );
207
208 /// Virtual Destructor.
209 virtual ~EnvironmentVariablesPlugin() override {}
210
211 /// @return The name of the plug-in, in this case, the value of resource variable "CfgPlgEnv".
212 ALIB_API virtual String Name() const override;
213
214 /// Searches the variable in the environment.
215 /// @param name The name of the variable to retrieve.
216 /// @param[out] target A reference to the buffer to write the variable's exported value to.
217 /// @return \c true if variable was found, \c false if not.
218 ALIB_API virtual bool Get( const String& name, AString& target ) override;
219};
220
221} // namespace [alib::config]
222
223#endif // HPP_ALIB_CONFIG_PLUGINS
224
virtual ALIB_API bool Get(const String &name, AString &target) override
Definition plugins.cpp:35
virtual ALIB_API String Name() const override
Definition plugins.cpp:30
virtual ~CLIVariablesPlugin() override
Virtual Destructor.
ALIB_API CLIVariablesPlugin(MonoAllocator &ma, Priority pPriority=Priority::CLI)
Definition plugins.cpp:24
virtual ~ConfigurationPlugin()
Virtual Destructor.
StringEscaperStandard stringEscaper
virtual ALIB_API bool Get(const String &name, AString &target)=0
ConfigurationPlugin(Priority pPriority)
virtual String Name() const =0
virtual const StringEscaper & GetEscaper() const
virtual ALIB_API bool Get(const String &name, AString &target) override
Definition plugins.cpp:141
virtual ALIB_API String Name() const override
Definition plugins.cpp:136
virtual ~EnvironmentVariablesPlugin() override
Virtual Destructor.
ALIB_API EnvironmentVariablesPlugin(MonoAllocator &ma, Priority pPriority=Priority::Environment)
Definition plugins.cpp:130
#define ALIB_API
Definition alib.hpp:639
config::Configuration Configuration
Type alias in namespace alib.
config::Variable Variable
Type alias in namespace alib.