This template class provides a map of PropertyFormatter objects that use format strings which are defined by variables of a Configuration.
The use case is about having different versions of how an object is formatted and to have these versions configurable to end users through the ALib configuration system.
The concept is best explained by a code sample. From the documentation of class PropertyFormatter, we are using the sample setup:
Three different output formats, Short, Default and All should be supported We define an enumeration type for this purpose and use macro ALIB_ENUMS_ASSIGN_RECORD to equip the enumeration with ALib Enum Records of type Declaration. In addition, macro ALIB_RESOURCED is used to announce that the record data is defined using resourced string data.
The following piece of code is to be performed during bootstrapping. We take the freedom to use the resource pool of module BaseCamp:
In the next phase of bootstrapping, our enumeration records have to be initialized from the resource pool:
With this in place, the PropertyFormatters can be created and method Format can be used:
But we're still not fully done yet: The next goal is to have a lightweight object that is "appendable" to objects of class AString. Such object is given with template type PropertyFormatterMapAppendable. It is a simple helper-struct that holds a reference to the formatter map, the format enumeration value and the object to format.
We define a shortcut to the type with the right template arguments in place:
Now, we are set and can start using the formatter:
Running the code above produces the following output:
Sue John aged 35 loves hacking
To make the code even shorter, macros may be used:
The output is:
Sue Sue Sue (28) Sue aged 28 loves hacking
The sample above showcased how to append a lightweight object of helper-class PropertyFormatterMapAppendable to objects of class AString. A final step is to define the boxing interface FAppend to make boxed objects of our helper-type applicable. As the type itself is already appendable, we can use a built-in simple macro for that:
As described in the Programmer's Manual of module ALib Boxing, such interface definition should be done only once and be placed in the bootstrap section of a process.
With this, using formatters in combination with our helper-struct works fine:
This code produces the following output.
The person is: John (35)
This class is only available if module ALib Configuration is included in the ALib Distribution.
TFormattable | The type that is formatted. |
TVariables | The enumeration of variables which defines the format strings. |
Definition at line 130 of file propertyformatters.hpp.
#include <propertyformatters.hpp>
Public Field Index: | |
SPFormatter | Formatter |
The formatter (as given in the constructor). | |
Public Method Index: | |
PropertyFormatters (typename PropertyFormatter::TCallbackTable &callbackTable, config::Configuration &configuration, SPFormatter formatter=nullptr) | |
~PropertyFormatters () | |
Destructor. Deletes the PropertyFormatters which got created. | |
void | Format (AString &target, TVariables option, TFormattable &formattable) |
Protected Field Index: | |
PropertyFormatter::TCallbackTable & | callbacks |
The callback table for the property formatters (as given in the constructor). | |
config::Configuration & | config |
The configuration used to load (and store the default) format strings from. | |
std::unordered_map< TVariables, PropertyFormatter * > | formatters |
The map of formatters. | |
|
protected |
The callback table for the property formatters (as given in the constructor).
Definition at line 134 of file propertyformatters.hpp.
|
protected |
The configuration used to load (and store the default) format strings from.
Definition at line 137 of file propertyformatters.hpp.
SPFormatter Formatter |
The formatter (as given in the constructor).
Definition at line 144 of file propertyformatters.hpp.
|
protected |
The map of formatters.
Definition at line 140 of file propertyformatters.hpp.
|
inline |
Constructor storing arguments in fields. All variables are declared within the given configuration and defined with their default values given with their declaration records (associated with TVariables) and thus - if exported to a write-enabled external configuration source like for example IniFileFeeder - can be noted by the end-user to be configurable.
Parameter formatter defaults to nullptr
, which causes this constructor to create a clone of the static formatter given with Formatter::Default. Note, that this is in contrast to the similar parameter of class PropertyFormatter, which uses the default formatter itself in the case that nullptr
is provided. The rationale behind creating a copy is that it is quite likely that the PropertyFormatter objects created here are used nested within other format operations. This is especially true with the use of helper struct PropertyFormatterMapAppendable. Now, nested formatting must not be done with the same formatter object.
callbackTable | The callback table as specified with PropertyFormatter::TCallbackTable. |
configuration | The configuration to load the format strings from (and to store the default values defined by the enum record of TVariables). |
formatter | The underlying formatter. Used for construction of PropertyFormatters. Defaults to nullptr which denotes to create a copy of the static formatter given with Formatter::Default. |
Definition at line 173 of file propertyformatters.hpp.
|
inline |
Destructor. Deletes the PropertyFormatters which got created.
Definition at line 191 of file propertyformatters.hpp.
|
inline |
Chooses or - if not yet available - creates the right PropertyFormatter and invokes its method Format.
target | The target string to write into. |
option | The user-defined formatting option. |
formattable | The custom object which is passed to the callback methods to collect the formatter arguments. |
Definition at line 204 of file propertyformatters.hpp.