ALib C++ Framework
by
Library Version: 2605 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
expressionformatter.hpp
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header-file is part of module \alib_expressions of the \aliblong.
4///
5/// Copyright 2013-2026 A-Worx GmbH, Germany.
6/// Published under #"mainpage_license".
7//==================================================================================================
8ALIB_EXPORT namespace alib { namespace expressions::util {
9
10//==================================================================================================
11/// This class allows using \alib_expressions_nl within format strings used with
12/// #"format::Formatter;ALib Formatters" and thus allows exposing customizable format
13/// strings to end-users, which leverage an application's expression subsystem.
14///
15/// To understand the concept of this class, please read the documentation of class
16/// #"PropertyFormatter" \b first, which is almost a 1:1 copy of this class but
17/// uses simple "property callback functions" instead of expressions.
18///
19/// \note
20/// This documentation rather documents the differences to #"%PropertyFormatter" instead of
21/// repeating what is explained already in the sibling class.
22///
23/// This class can be used in the same way as #"PropertyFormatter". However,
24/// instead of the provision of a table of type
25/// #"PropertyFormatter::TCallbackTable;*", this class expects an expression
26/// compiler in the constructor.
27///
28/// With that, simple expressions comprising just a single identifier term can be used in
29/// exactly the same way as demonstrated with #"%PropertyFormatter". For example, format string:
30///
31/// "{name} is aged {age} and his/her hobby is {hobby}"
32///
33/// would compile three expressions (<em>"name"</em>, <em>"age"</em> and <em>"hobby"</em>) and
34/// convert the format string to just
35///
36/// "{} is aged {} and his/her hobby is {}"
37///
38/// Then, with the invocation of method #".Format", an expression scope has to be provided, which
39/// is used to evaluate each expression and add the result value to the list of format parameters.
40///
41/// To separate the given expressions strings from other formatting information provided in a
42/// placeholder, a special separator character is used. This character is provided with construction
43/// and defaults to the symbol \c '@'. A format string that uses
44/// #"FormatterPythonStyle;python formatting syntax" might look like this:
45///
46/// "The surface is {width * height@:>5.2} sqm."
47///
48/// <p>
49///\I{#############################################################################################}
50/// # Reference Documentation #
51/// @throws alib::format::FMTExceptions::UnknownPropertyInFormatString \I{CLANGDUMMY}
52//==================================================================================================
54 protected:
55 /// The expression compiler (as given in the constructor).
57
58 /// The formatter to use (as given in the constructor).
60
61 /// The original format string. Used only for exception information.
63
64 /// The resulting format string passed to \alib formatters.
66
67 /// The expression functions to receive the format data.
68 std::vector<Expression> expressions;
69
70 /// The end-positions of the substrings in #"formatStringStripped". Starting with the second,
71 /// each contains one placeholder.
72 std::vector<integer> formatSubstrings;
73 public:
74
75 /// Constructor. Processes the given format string and compiles embedded expressions, which
76 /// are then used with invocations of method #".Format".
77 ///
78 /// @param formatString The format string as described in the class documentation.
79 /// @param compiler The expression compiler.
80 /// @param formatter The formatter to use. Defaults to \c nullptr which selects
81 /// #"Formatter::DEFAULT;default formatter".
82 /// @param separatorChar The character that is to be used to separate the expression
83 /// from the placeholder format information in
84 /// \p{customFormatString}.<br>
85 /// Defaults to <c>'@'</c>.
86 ///
87 /// @throws alib::format::FMTExceptions::UnknownPropertyInFormatString. \I{CLANGDUMMY}
89 ExpressionFormatter( const String& formatString,
91 SPFormatter formatter = nullptr,
92 character separatorChar = '@' );
93
94 /// Writes the formatted output of the expressions in accordance to the given \p{scope}
95 /// object to given \p{target} string.
96 ///
97 /// @param target The target string to write into.
98 /// @param scope The scope used to evaluate the expressions found in the format string.
100 void Format( AString& target, expressions::Scope& scope );
101
102}; // class ExpressionFormatter
103
104} // namespace alib[::expressions::util]
105
106/// Type alias in namespace #"%alib".
108
109} // namespace [alib]
#define ALIB_DLL
#define ALIB_EXPORT
Compiler * compiler
The expression compiler (as given in the constructor).
AString formatStringStripped
The resulting format string passed to ALib formatters.
ExpressionFormatter(const String &formatString, Compiler *compiler, SPFormatter formatter=nullptr, character separatorChar='@')
SPFormatter stdFormatter
The formatter to use (as given in the constructor).
void Format(AString &target, expressions::Scope &scope)
const AString originalFormatString
The original format string. Used only for exception information.
std::vector< Expression > expressions
The expression functions to receive the format data.
Utility types of camp ALib Expressions.
Definition alox.cpp:14
containers::SharedPtr< format::Formatter > SPFormatter
Definition formatter.hpp:41
strings::TString< character > String
Type alias in namespace #"%alib".
Definition string.hpp:2165
expressions::util::ExpressionFormatter ExpressionFormatter
Type alias in namespace #"%alib".
strings::TAString< character, lang::HeapAllocator > AString
Type alias in namespace #"%alib".
characters::character character
Type alias in namespace #"%alib".