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