ALib C++ Library
Library Version: 2412 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/// \emoji :copyright: 2013-2024 A-Worx GmbH, Germany.
6/// Published under \ref mainpage_license "Boost Software License".
7//==================================================================================================
8#ifndef HPP_ALIB_EXPRESSIONS_UTIL_EXPRESSION_FORMATTER
9#define HPP_ALIB_EXPRESSIONS_UTIL_EXPRESSION_FORMATTER
10#pragma once
12ALIB_ASSERT_MODULE(EXPRESSIONS)
13
14#include "alib/lang/format/formatterstdimpl.hpp"
16
17
18namespace alib { namespace expressions::util {
19
20//==================================================================================================
21/// This class allows using \alib_expressions_nl within format strings used with
22/// \alib{lang::format;Formatter;ALib Formatters} and thus allows exposing customizable format
23/// strings to end-users, which leverage an application's expression subsystem.
24///
25/// To understand the concept of this class, please read the documentation of class
26/// \alib{lang::format;PropertyFormatter} \b first, which is almost a 1:1 copy of this class but
27/// uses simple "property callback functions" instead of expressions.
28///
29/// \note
30/// This documentation rather documents the differences to \b %PropertyFormatter instead of
31/// repeating what is explained already in the sibling class.
32///
33/// This class can be used in the same way as \alib{lang::format;PropertyFormatter}. However,
34/// instead of the provision of a table of type
35/// \alib{lang::format;PropertyFormatter::TCallbackTable}, this class expects an expression
36/// compiler in the constructor.
37///
38/// With that, simple expressions comprised of just a single identifier term can be used in
39/// exactly the same way as demonstrated with \b PropertyFormatter. For example, format string:
40///
41/// "{name} is aged {age} and his/her hobby is {hobby}"
42///
43/// would compile three expressions (<em>"name"</em>, <em>"age"</em> and <em>"hobby"</em>) and
44/// convert the format string to just
45///
46/// "{} is aged {} and his/her hobby is {}"
47///
48/// Then, with the invocation of method #Format, an expression scope has to be provided, which
49/// is used to evaluate each expression and add the result value to the list of format parameters.
50///
51/// To separate the given expressions strings from other formatting information provided in a
52/// placeholder, a special separator character is used. This character is provided with construction
53/// and defaults to symbol \c '@'. A format string that uses
54/// \alib{lang::format;FormatterPythonStyle;python formatting syntax} might look like this:
55///
56/// "The surface is {width * height@:>5.2} sqm."
57///
58/// <p>
59///\I{##############################################################################################}
60/// # Reference Documentation #
61/// @throws alib::lang::format::FMTExceptions::UnknownPropertyInFormatString
62//==================================================================================================
64{
65 protected:
66 /// The expression compiler (as given in the constructor).
68
69 /// The formatter to use (as given in the constructor).
71
72 /// The original format string. Used only for exception information.
74
75 /// The resulting format string passed to \alib formatters.
77
78 /// The expressions functions to receive the format data.
79 std::vector<Expression> expressions;
80
81 public:
82
83 //==========================================================================================
84 /// Constructor. Processes the given format string and compiles embedded expressions, which
85 /// are then used with invocations of method #Format.
86 ///
87 /// @param formatString The format string as described in the class documentation.
88 /// @param compiler The expression compiler.
89 /// @param formatter The formatter to use. Defaults to \c nullptr which selects
90 /// \alib{lang::format;Formatter::Default;default formatter}.
91 /// @param separatorChar The character that is to be used to separate the expression
92 /// from the placeholder format information in
93 /// \p{customFormatString}.<br>
94 /// Defaults to <c>'@'</c>.
95 ///
96 /// @throws alib::lang::format::FMTExceptions::UnknownPropertyInFormatString.
97 //==========================================================================================
99 ExpressionFormatter( const String formatString,
101 SPFormatter formatter = nullptr,
102 character separatorChar = '@' );
103
104 //==========================================================================================
105 /// Writes the formatted output of the expressions in accordance to the given \p{scope}
106 /// object to given \p{target} string.
107 ///
108 /// @param target The target string to write into.
109 /// @param scope The scope used to evaluate the expressions found in the format string.
110 //==========================================================================================
112 void Format( AString& target, expressions::Scope& scope );
113
114
115}; // class ExpressionFormatter
116
117} // namespace alib[::expressions::util]
118
119/// Type alias in namespace \b alib.
121
122} // namespace [alib]
123
124
125#endif // HPP_ALIB_EXPRESSIONS_UTIL_EXPRESSION_FORMATTER
126
Compiler * compiler
The expression compiler (as given in the constructor).
AString formatStringStripped
The resulting format string passed to ALib formatters.
const AString originalFormatString
The original format string. Used only for exception information.
std::vector< Expression > expressions
The expressions functions to receive the format data.
SPFormatter stdFormatter
The formatter to use (as given in the constructor).
#define ALIB_ASSERT_MODULE(modulename)
Definition alib.hpp:223
#define ALIB_API
Definition alib.hpp:639
Definition alib.cpp:69
expressions::util::ExpressionFormatter ExpressionFormatter
Type alias in namespace alib.
strings::TFormat< character > Format
Type alias in namespace alib.
characters::character character
Type alias in namespace alib.