ALib C++ Library
Library Version: 2402 R1
Documentation generated by doxygen
No Matches
ExpressionFormatter Class Reference


This class allows to use ALib Expressions within format strings used with ALib Formatters and thus allows to expose 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 documentation rather documents the differences to PropertyFormatter instead of repeating what is explained already in the sibling class.

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."

Reference Documentation


Definition at line 72 of file expressionformatter.hpp.

#include <expressionformatter.hpp>

Collaboration diagram for ExpressionFormatter:

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)

Field Details:

◆ compiler

Compiler* compiler

The expression compiler (as given in the constructor).

Definition at line 76 of file expressionformatter.hpp.

◆ expressions

std::vector<SPExpression> expressions

The expressions functions to receive the format data.

Definition at line 88 of file expressionformatter.hpp.

◆ formatStringStripped

AString formatStringStripped

The resulting format string passed to ALib formatters.

Definition at line 85 of file expressionformatter.hpp.

◆ originalFormatString

const AString originalFormatString

The original format string. Used only for exception information.

Definition at line 82 of file expressionformatter.hpp.

◆ stdFormatter

SPFormatter stdFormatter

The formatter to use (as given in the constructor).

Definition at line 79 of file expressionformatter.hpp.

Constructor(s) / Destructor Details::

◆ ExpressionFormatter()

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.

formatStringThe format string as described in the class documentation.
compilerThe expression compiler.
formatterThe formatter to use. Defaults to nullptr which selects default formatter .
separatorCharThe character that is to be used to separate the expression from the placeholder format information in customFormatString .
Defaults to '@'.

Definition at line 24 of file expressionformatter.cpp.

Here is the call graph for this function:

Method Details:

◆ Format()

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.

targetThe target string to write into.
scopeThe scope used to evaluate the expressions found in the format string.

Definition at line 90 of file expressionformatter.cpp.

Here is the call graph for this function:

The documentation for this class was generated from the following files: