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