ALib C++ Library
Library Version: 2510 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
arithmetics.inl
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-2025 A-Worx GmbH, Germany.
6/// Published under \ref mainpage_license "Boost Software License".
7//==================================================================================================
8ALIB_EXPORT namespace alib { namespace expressions { namespace plugins {
9
10/** ************************************************************************************************
11 * This built-in \alib{expressions;CompilerPlugin} of \alib_expressions_nl
12 * primarily compiles unary and binary operators for permutations of types
13 * \alib{expressions::Types;Boolean}, \alib{expressions::Types;Integer} and
14 * \alib{expressions::Types;Float}.
15 *
16 * By default, this plug-in is \alib{expressions;Compiler::CfgBuiltInPlugins;automatically created}
17 * and inserted into each instance of class \alib{expressions;Compiler} with the invocation of
18 * \alib{expressions::Compiler;SetupDefaults}.
19 *
20 * <b>General Notes:</b><br>
21 * - All identifier and function names are defined case-insensitive.
22 *
23 * - All callback functions are defined compile-time invokable.
24 * This means that redundancies in expressions emerging from operations on constant numbers are
25 * optimized (pruned) by the compiler. For example, the two expressions:
26 \verbatim
27 size > 81920
28 size > 8 * 1024
29 \endverbatim
30 * are resulting in an identical expression program and thus the latter has no evaluation penalty.
31 *
32 * - The following additional binary operator optimization are defined:
33 * - Addition and subtraction of \c 0.
34 * - Multiplication by \c 0 and \c 1.
35 * - Division by \c 1.
36 * - Modulo by \c 1.
37 * - Boolean or with \c true and \c false.
38 * - Boolean and with \c true and \c false.
39 *
40 * - Dependent on configuration flags of the given \b %Compiler, the following alias operators
41 * are defined:
42 * - With flag \alib{expressions::Compilation;AliasEqualsOperatorWithAssignOperator}:<br>
43 * Operator <c>'=='</c> is aliased with <c>'='</c>.
44 * - With flag \alib{expressions::Compilation;AllowBitwiseBooleanOperators}:<br>
45 * Operators <c>'&&'</c>) and <c>'||'</c> are aliased with operators <c>'&'</c> and <c>'|'</c>.
46 *
47 *
48 * <b>Constant Identifiers:</b><br>
49 * The following constant identifiers provide variants for boolean values. No abbreviation
50 * is allowed.
51 *
52 * <br>
53 * Return Type | Name | Description
54 * ------------|-----------|-------------
55 * Boolean |\b True | Returns constant \c true.
56 * Boolean |\b False | Returns constant \c false.
57 * Boolean |\b Yes | Returns constant \c true.
58 * Boolean |\b No | Returns constant \c false.
59 * Boolean |\b On | Returns constant \c true.
60 * Boolean |\b Off | Returns constant \c false.
61 *
62 * <br>
63 * <b>Type Conversion Functions:</b><br>
64 *
65 * | Return Type | Name |Min. Abbreviation| Signature| Description
66 * |-------------|-----------|-----------------|--------- |------------
67 * | Boolean |\b Boolean |bool | <any> | Converts any type of boxed value by invoking box-function \alib{boxing;FIsTrue}.
68 * | Integer |\b Integer |int | Boolean | Converts \c true to \c 1, \c false to \c 0.
69 * | Integer |\b Integer |int | Integer | Does nothing (identity function).
70 * | Integer |\b Integer |int | Float | Returns the integral part of a floating point number.
71 * | Float |\b Float |float | Boolean | Converts \c true to \c 1.0, \c false to \c 0.0.
72 * | Float |\b Float |float | Integer | Converts an integral value to floating point.
73 * | Float |\b Float |float | Float | Does nothing (identity function).
74 *
75 * <br>
76 * <b>Functions:</b><br>
77 *
78 * | Return Type | Name |Min. Abbreviation| Signature| Description
79 * |-------------|-----------|-----------------|--------- |------------
80 * | Integer |\b Length |len | <any array> | Returns the length of an array. \note Because built-in type \b %String is a boxed character array, this function can be used to determine the length of strings.
81 *
82 * <br>
83 * <b>Unary Operators:</b><br>
84 *
85 * | Return Type | Operator | Argument Type | Description|
86 * |--------------|----------|----------|---------------------
87 * | Integer |<b>+</b> |Boolean | Converts the boolean value to integer.
88 * | Integer |<b>+</b> |Integer | Identity operator (has no effect).
89 * | Float |<b>+</b> |Float | Identity operator (has no effect).
90 * | Integer |<b>-</b> |Boolean | Converts the boolean value to integer and negates it.
91 * | Integer |<b>-</b> |Integer | Negates an integral value.
92 * | Float |<b>-</b> |Float | Negates a floating point value.
93 * | Boolean |<b>!</b> |Boolean | Boolean not operator.
94 * | Boolean |<b>!</b> |Integer | Returns the result of the comparison with <c>0</c>.
95 * | Boolean |<b>!</b> |Float | Returns the result of the comparison with <c>0.0</c>.
96 * | Integer |<b>~</b> |Integer | Bitwise integral negation.
97 *
98 * <br>
99 * <b>Binary Operators:</b><br>
100 *
101 * | Return Type | Lhs Type | Operator | Rhs Type | Description|
102 * |-------------|----------|----------|----------|------------|
103 * |Integer | Boolean | <b>*</b> | Boolean | Multiplication with interpreting both boolean operands as integral value \c 0 or \c 1.
104 * |Integer | Boolean | <b>*</b> | Integer | Multiplication with interpreting the boolean operand as integral value \c 0 or \c 1.
105 * |Float | Boolean | <b>*</b> | Float | Multiplication with interpreting the boolean operand as floating point value \c 0.0 or \c 1.0.
106 * |Integer | Integer | <b>*</b> | Boolean | Multiplication with interpreting the boolean operand as integral value \c 0 or \c 1.
107 * |Integer | Integer | <b>*</b> | Integer | Multiplication.
108 * |Float | Integer | <b>*</b> | Float | Multiplication.
109 * |Float | Float | <b>*</b> | Boolean | Multiplication with interpreting the boolean operand as floating point value \c 0.0 or \c 1.0.
110 * |Float | Float | <b>*</b> | Integer | Multiplication.
111 * |Float | Float | <b>*</b> | Float | Multiplication.
112 * |Integer | Boolean | <b>/</b> | Integer | Division with interpreting the boolean operand as integral value \c 0 or \c 1.
113 * |Float | Boolean | <b>/</b> | Float | Division with interpreting the boolean operand as integral value \c 0 or \c 1.
114 * |Integer | Integer | <b>/</b> | Integer | Division.
115 * |Float | Integer | <b>/</b> | Float | Division.
116 * |Float | Float | <b>/</b> | Integer | Division.
117 * |Float | Float | <b>/</b> | Float | Division.
118 * |Integer | Boolean | <b>\%</b> | Integer | Modulo with interpreting the boolean operand as integral value \c 0 or \c 1.
119 * |Float | Boolean | <b>\%</b> | Float | Modulo with interpreting the boolean operand as integral value \c 0 or \c 1.
120 * |Integer | Integer | <b>\%</b> | Integer | Modulo.
121 * |Float | Integer | <b>\%</b> | Float | Modulo.
122 * |Float | Float | <b>\%</b> | Integer | Modulo.
123 * |Float | Float | <b>\%</b> | Float | Modulo.
124 * |Integer | Boolean | <b>+</b> | Boolean | Addition with interpreting both boolean operands as integral value \c 0 or \c 1.
125 * |Integer | Boolean | <b>+</b> | Integer | Addition with interpreting the boolean operand as integral value \c 0 or \c 1.
126 * |Float | Boolean | <b>+</b> | Float | Addition with interpreting the boolean operand as floating point value \c 0.0 or \c 1.0.
127 * |Integer | Integer | <b>+</b> | Boolean | Addition with interpreting the boolean operand as integral value \c 0 or \c 1.
128 * |Integer | Integer | <b>+</b> | Integer | Addition.
129 * |Float | Integer | <b>+</b> | Float | Addition.
130 * |Float | Float | <b>+</b> | Boolean | Addition with interpreting the boolean operand as floating point value \c 0.0 or \c 1.0.
131 * |Float | Float | <b>+</b> | Integer | Addition.
132 * |Float | Float | <b>+</b> | Float | Addition.
133 * |Integer | Boolean | <b>-</b> | Boolean | Subtraction with interpreting both boolean operands as integral value \c 0 or \c 1.
134 * |Integer | Boolean | <b>-</b> | Integer | Subtraction with interpreting the boolean operand as integral value \c 0 or \c 1.
135 * |Float | Boolean | <b>-</b> | Float | Subtraction with interpreting the boolean operand as floating point value \c 0.0 or \c 1.0.
136 * |Integer | Integer | <b>-</b> | Boolean | Subtraction with interpreting the boolean operand as integral value \c 0 or \c 1.
137 * |Integer | Integer | <b>-</b> | Integer | Subtraction.
138 * |Float | Integer | <b>-</b> | Float | Subtraction.
139 * |Float | Float | <b>-</b> | Boolean | Subtraction with interpreting the boolean operand as floating point value \c 0.0 or \c 1.0.
140 * |Float | Float | <b>-</b> | Integer | Subtraction.
141 * |Float | Float | <b>-</b> | Float | Subtraction.
142 * |Integer | Boolean | <b><<</b> | Integer | Bitwise shift left with interpreting the boolean operand as floating point value \c 0.0 or \c 1.0.
143 * |Integer | Integer | <b><<</b> | Boolean | Bitwise shift left with interpreting the boolean operand as floating point value \c 0.0 or \c 1.0.
144 * |Integer | Integer | <b><<</b> | Integer | Bitwise shift left.
145 * |Integer | Boolean | <b>>></b> | Integer | Bitwise shift right with interpreting the boolean operand as floating point value \c 0.0 or \c 1.0.
146 * |Integer | Integer | <b>>></b> | Boolean | Bitwise shift right with interpreting the boolean operand as floating point value \c 0.0 or \c 1.0.
147 * |Integer | Integer | <b>>></b> | Integer | Bitwise shift right.
148 * |Boolean | Boolean | <b><</b> | Boolean | Comparison operator with interpreting both boolean operands as integral value \c 0 or \c 1.
149 * |Boolean | Boolean | <b><</b> | Integer | Comparison operator with interpreting the boolean operand as integral value \c 0 or \c 1.
150 * |Boolean | Boolean | <b><</b> | Float | Comparison operator with interpreting the boolean operand as floating point value \c 0.0 or \c 1.0.
151 * |Boolean | Integer | <b><</b> | Boolean | Comparison operator with interpreting the boolean operand as integral value \c 0 or \c 1.
152 * |Boolean | Integer | <b><</b> | Integer | Comparison operator.
153 * |Boolean | Integer | <b><</b> | Float | Comparison operator.
154 * |Boolean | Float | <b><</b> | Boolean | Comparison operator with interpreting the boolean operand as floating point value \c 0.0 or \c 1.0.
155 * |Boolean | Float | <b><</b> | Integer | Comparison operator.
156 * |Boolean | Float | <b><</b> | Float | Comparison operator.
157 * |Boolean | Boolean | <b><=</b> | Boolean | Comparison operator with interpreting both boolean operands as integral value \c 0 or \c 1.
158 * |Boolean | Boolean | <b><=</b> | Integer | Comparison operator with interpreting the boolean operand as integral value \c 0 or \c 1.
159 * |Boolean | Boolean | <b><=</b> | Float | Comparison operator with interpreting the boolean operand as floating point value \c 0.0 or \c 1.0.
160 * |Boolean | Integer | <b><=</b> | Boolean | Comparison operator with interpreting the boolean operand as integral value \c 0 or \c 1.
161 * |Boolean | Integer | <b><=</b> | Integer | Comparison operator.
162 * |Boolean | Integer | <b><=</b> | Float | Comparison operator.
163 * |Boolean | Float | <b><=</b> | Boolean | Comparison operator with interpreting the boolean operand as floating point value \c 0.0 or \c 1.0.
164 * |Boolean | Float | <b><=</b> | Integer | Comparison operator.
165 * |Boolean | Float | <b><=</b> | Float | Comparison operator.
166 * |Boolean | Boolean | <b>></b> | Boolean | Comparison operator with interpreting both boolean operands as integral value \c 0 or \c 1.
167 * |Boolean | Boolean | <b>></b> | Integer | Comparison operator with interpreting the boolean operand as integral value \c 0 or \c 1.
168 * |Boolean | Boolean | <b>></b> | Float | Comparison operator with interpreting the boolean operand as floating point value \c 0.0 or \c 1.0.
169 * |Boolean | Integer | <b>></b> | Boolean | Comparison operator with interpreting the boolean operand as integral value \c 0 or \c 1.
170 * |Boolean | Integer | <b>></b> | Integer | Comparison operator.
171 * |Boolean | Integer | <b>></b> | Float | Comparison operator.
172 * |Boolean | Float | <b>></b> | Boolean | Comparison operator with interpreting the boolean operand as floating point value \c 0.0 or \c 1.0.
173 * |Boolean | Float | <b>></b> | Integer | Comparison operator.
174 * |Boolean | Float | <b>></b> | Float | Comparison operator.
175 * |Boolean | Boolean | <b>>=</b> | Boolean | Comparison operator with interpreting both boolean operands as integral value \c 0 or \c 1.
176 * |Boolean | Boolean | <b>>=</b> | Integer | Comparison operator with interpreting the boolean operand as integral value \c 0 or \c 1.
177 * |Boolean | Boolean | <b>>=</b> | Float | Comparison operator with interpreting the boolean operand as floating point value \c 0.0 or \c 1.0.
178 * |Boolean | Integer | <b>>=</b> | Boolean | Comparison operator with interpreting the boolean operand as integral value \c 0 or \c 1.
179 * |Boolean | Integer | <b>>=</b> | Integer | Comparison operator.
180 * |Boolean | Integer | <b>>=</b> | Float | Comparison operator.
181 * |Boolean | Float | <b>>=</b> | Boolean | Comparison operator with interpreting the boolean operand as floating point value \c 0.0 or \c 1.0.
182 * |Boolean | Float | <b>>=</b> | Integer | Comparison operator.
183 * |Boolean | Float | <b>>=</b> | Float | Comparison operator.
184 * |Boolean | Boolean | <b>==</b> | Boolean | Comparison operator with interpreting both boolean operands as integral value \c 0 or \c 1.
185 * |Boolean | Boolean | <b>==</b> | Integer | Comparison operator with interpreting the boolean operand as integral value \c 0 or \c 1.
186 * |Boolean | Boolean | <b>==</b> | Float | Comparison operator with interpreting the boolean operand as floating point value \c 0.0 or \c 1.0.
187 * |Boolean | Integer | <b>==</b> | Boolean | Comparison operator with interpreting the boolean operand as integral value \c 0 or \c 1.
188 * |Boolean | Integer | <b>==</b> | Integer | Comparison operator.
189 * |Boolean | Integer | <b>==</b> | Float | Comparison operator.
190 * |Boolean | Float | <b>==</b> | Boolean | Comparison operator with interpreting the boolean operand as floating point value \c 0.0 or \c 1.0.
191 * |Boolean | Float | <b>==</b> | Integer | Comparison operator.
192 * |Boolean | Float | <b>==</b> | Float | Comparison operator.
193 * |Boolean | Boolean | <b>!=</b> | Boolean | Comparison operator with interpreting both boolean operands as integral value \c 0 or \c 1.
194 * |Boolean | Boolean | <b>!=</b> | Integer | Comparison operator with interpreting the boolean operand as integral value \c 0 or \c 1.
195 * |Boolean | Boolean | <b>!=</b> | Float | Comparison operator with interpreting the boolean operand as floating point value \c 0.0 or \c 1.0.
196 * |Boolean | Integer | <b>!=</b> | Boolean | Comparison operator with interpreting the boolean operand as integral value \c 0 or \c 1.
197 * |Boolean | Integer | <b>!=</b> | Integer | Comparison operator.
198 * |Boolean | Integer | <b>!=</b> | Float | Comparison operator.
199 * |Boolean | Float | <b>!=</b> | Boolean | Comparison operator with interpreting the boolean operand as floating point value \c 0.0 or \c 1.0.
200 * |Boolean | Float | <b>!=</b> | Integer | Comparison operator.
201 * |Boolean | Float | <b>!=</b> | Float | Comparison operator.
202 * |Integer | Integer | <b>&</b> | Integer | Bitwise boolean "and" operator.
203 * |Integer | Integer | <b>\|</b> | Integer | Bitwise boolean "or" operator.
204 * |Integer | Integer | <b>^</b> | Integer | Bitwise boolean "xor" operator.
205 * |Boolean | Boolean | <b>&&</b> | Boolean | Logical boolean "and" operator.
206 * |Boolean | Boolean | <b>&&</b> | Integer | Logical boolean "and" operator. The integral operand is tested for a non-zero value.
207 * |Boolean | Boolean | <b>&&</b> | Float | Logical boolean "and" operator. The floating point operand is tested for a non-zero value.
208 * |Boolean | Integer | <b>&&</b> | Boolean | Logical boolean "and" operator. The integral operand is tested for a non-zero value.
209 * |Boolean | Integer | <b>&&</b> | Integer | Logical boolean "and" operator. The integral operands are tested for a non-zero value.
210 * |Boolean | Integer | <b>&&</b> | Float | Logical boolean "and" operator. The integral and floating point operands are tested for a non-zero value.
211 * |Boolean | Float | <b>&&</b> | Boolean | Logical boolean "and" operator. The floating point operand is tested for a non-zero value.
212 * |Boolean | Float | <b>&&</b> | Integer | Logical boolean "and" operator. The integral and floating point operands are tested for a non-zero value.
213 * |Boolean | Float | <b>&&</b> | Float | Logical boolean "and" operator. The floating point operands are tested for a non-zero value.
214 * |Boolean | Boolean | <b>\|\|</b> | Boolean | Logical boolean "or" operator.
215 * |Boolean | Boolean | <b>\|\|</b> | Integer | Logical boolean "or" operator. The integral operand is tested for a non-zero value.
216 * |Boolean | Boolean | <b>\|\|</b> | Float | Logical boolean "or" operator. The floating point operand is tested for a non-zero value.
217 * |Boolean | Integer | <b>\|\|</b> | Boolean | Logical boolean "or" operator. The integral operand is tested for a non-zero value.
218 * |Boolean | Integer | <b>\|\|</b> | Integer | Logical boolean "or" operator. The integral operands are tested for a non-zero value.
219 * |Boolean | Integer | <b>\|\|</b> | Float | Logical boolean "or" operator. The integral and floating point operands are tested for a non-zero value.
220 * |Boolean | Float | <b>\|\|</b> | Boolean | Logical boolean "or" operator. The floating point operand is tested for a non-zero value.
221 * |Boolean | Float | <b>\|\|</b> | Integer | Logical boolean "or" operator. The integral and floating point operands are tested for a non-zero value.
222 * |Boolean | Float | <b>\|\|</b> | Float | Logical boolean "or" operator. The floating point operands are tested for a non-zero value.
223 *
224 **************************************************************************************************/
226{
227 //==============================================================================================
228 /// Constructor. Creates the hash maps.
229 /// @param compiler The compiler we will get attached to.
230 //==============================================================================================
232
233 //==============================================================================================
234 /// Virtual destructor
235 //==============================================================================================
236 virtual ~Arithmetics() override
237 {}
238
239 //==============================================================================================
240 /// Invokes parent's method. On failure, tries to compile function <b>%Length(array)</b>.
241 ///
242 /// @param[in,out] ciFunction The compilation information.
243 /// @return \c true if an entry was found. \c false otherwise.
244 //==============================================================================================
246 virtual bool TryCompilation( CIFunction& ciFunction ) override;
247
248};
249
250//==================================================================================================
251/// This is the callback method for function \b %Boolean, which converts arbitrary types to boolean
252/// values.
253/// As an exception to the rule, this function is not defined in an anonymous namespace, but
254/// exposed through the header of struct \alib{expressions;plugins::Arithmetics}.
255/// The rationale for this is that the function is also used for auto-casting custom types to
256/// boolean values, which is performed with compiler plug-in \alib{expressions;plugins::AutoCast}.
257///
258/// The function is compile-time invokable and uses box-function \alib{boxing;FIsTrue} to
259/// determine if a boxed value represents \c true or \c false.
260///
261/// @param scope The scope.
262/// @param args The single argument.
263/// @return The boxed boolean result.
264//==================================================================================================
267
268
269}}} // namespace [alib::expressions::detail]
270
#define ALIB_DLL
Definition alib.inl:496
#define ALIB_EXPORT
Definition alib.inl:488
ALIB_DLL Box ToBoolean(Scope &scope, ArgIterator args, ArgIterator)
StdVectorMono< Box >::iterator ArgIterator
boxing::Box Box
Type alias in namespace alib.
Definition box.inl:1216
virtual ~Arithmetics() override
Virtual destructor.
ALIB_DLL Arithmetics(Compiler &compiler)
virtual ALIB_DLL bool TryCompilation(CIFunction &ciFunction) override