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 VariableDecl . 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 namespace documentation of module ALib Boxing , such interface definition should be done only once and be placed in the bootstrap section of a software.
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. |
TVariable | The enumeration of variables which defines the format strings. |
Definition at line 142 of file propertyformatters.hpp.
#include <propertyformatters.hpp>
Public Field Index: | |
SPFormatter | Formatter |
Public Method Index: | |
PropertyFormatters (typename PropertyFormatter::TCallbackTable &callbackTable, config::Configuration &configuration, SPFormatter formatter=nullptr) | |
~PropertyFormatters () | |
void | Format (AString &target, TVariable option, TFormattable &formattable) |
|
protected |
The callback table for the property formatters (as given in the constructor).
Definition at line 146 of file propertyformatters.hpp.
|
protected |
The configuration used to load (and store the default) format strings from.
Definition at line 149 of file propertyformatters.hpp.
SPFormatter Formatter |
The formatter (as given in the constructor).
Definition at line 158 of file propertyformatters.hpp.
|
protected |
The map of formatters
Definition at line 152 of file propertyformatters.hpp.
|
protected |
A temporary variable to be reused (allocate once pattern).
Definition at line 155 of file propertyformatters.hpp.
|
inline |
Constructor storing arguments in fields. All default values of the variables are stored in the configuration and thus - if the configuration is write enabled - 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 formatter returned by Formatter::GetDefault . 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 TVariable ). |
formatter | The underlying formatter. Used for construction of PropertyFormatter s. Defaults to nullptr which denotes to create a copy of the formatter returned by Formatter::GetDefault . |
Definition at line 188 of file propertyformatters.hpp.
|
inline |
Destructor. Deletes the PropertyFormatter s which got created.
Definition at line 206 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 221 of file propertyformatters.hpp.