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