This class allows using ALib Expressions within format strings used with ALib Formatters and thus allows exposing customizable format strings to end-users, which leverage an application's expression subsystem.
To understand the concept of this class, please read the documentation of class PropertyFormatter first, which is almost a 1:1 copy of this class but uses simple "property callback functions" instead of expressions.
This class can be used in the same way as PropertyFormatter. However, instead of the provision of a table of type PropertyFormatter::TCallbackTable, this class expects an expression compiler in the constructor.
With that, simple expressions comprised of just a single identifier term can be used in exactly the same way as demonstrated with PropertyFormatter. For example, format string:
"{name} is aged {age} and his/her hobby is {hobby}"
would compile three expressions ("name", "age" and "hobby") and convert the format string to just
"{} is aged {} and his/her hobby is {}"
Then, with the invocation of method Format, an expression scope has to be provided, which is used to evaluate each expression and add the result value to the list of format parameters.
To separate the given expressions strings from other formatting information provided in a placeholder, a special separator character is used. This character is provided with construction and defaults to symbol '@'
. A format string that uses python formatting syntax might look like this:
"The surface is {width * height@:>5.2} sqm."
Definition at line 63 of file expressionformatter.hpp.
#include <expressionformatter.hpp>
Public Method Index: | |
ALIB_API | ExpressionFormatter (const String formatString, Compiler *compiler, SPFormatter formatter=nullptr, character separatorChar='@') |
ALIB_API void | Format (AString &target, expressions::Scope &scope) |
Protected Field Index: | |
Compiler * | compiler |
The expression compiler (as given in the constructor). | |
std::vector< Expression > | expressions |
The expressions functions to receive the format data. | |
AString | formatStringStripped |
The resulting format string passed to ALib formatters. | |
const AString | originalFormatString |
The original format string. Used only for exception information. | |
SPFormatter | stdFormatter |
The formatter to use (as given in the constructor). | |
|
protected |
The expression compiler (as given in the constructor).
Definition at line 67 of file expressionformatter.hpp.
|
protected |
The expressions functions to receive the format data.
Definition at line 79 of file expressionformatter.hpp.
|
protected |
The resulting format string passed to ALib formatters.
Definition at line 76 of file expressionformatter.hpp.
|
protected |
The original format string. Used only for exception information.
Definition at line 73 of file expressionformatter.hpp.
|
protected |
The formatter to use (as given in the constructor).
Definition at line 70 of file expressionformatter.hpp.
ExpressionFormatter | ( | const String | formatString, |
Compiler * | compiler, | ||
SPFormatter | formatter = nullptr, | ||
character | separatorChar = '@' ) |
Constructor. Processes the given format string and compiles embedded expressions, which are then used with invocations of method Format.
formatString | The format string as described in the class documentation. |
compiler | The expression compiler. |
formatter | The formatter to use. Defaults to nullptr which selects default formatter. |
separatorChar | The character that is to be used to separate the expression from the placeholder format information in customFormatString. Defaults to '@' . |
alib::lang::format::FMTExceptions::UnknownPropertyInFormatString. |
Definition at line 18 of file expressionformatter.cpp.
void Format | ( | AString & | target, |
expressions::Scope & | scope ) |
Writes the formatted output of the expressions in accordance to the given scope object to given target string.
target | The target string to write into. |
scope | The scope used to evaluate the expressions found in the format string. |
Definition at line 84 of file expressionformatter.cpp.