ALib C++ Library
Library Version: 2402 R1
Documentation generated by doxygen
Loading...
Searching...
No Matches
elvisoperator.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_ELVIS
9#define HPP_ALIB_EXPRESSIONS_PLUGINS_ELVIS
10
11#ifndef HPP_ALIB_EXPRESSIONS_COMPILERPLUGIN
13#endif
14
15
16namespace alib { namespace expressions { namespace plugins {
17
18/** ************************************************************************************************
19 * The binary variant of ternary operator <c>"Q ? T : F"</c> is created by leaving out
20 * term \c 'T', which results in <c>"A ? : B"</c>. This operator is often called
21 * <b>"Elvis Operator"</b> because <c>?:</c> reminds people to an emoticon of singer Elvis Presley.
22 *
23 * Usually the operator chooses \c 'A' in the case that \c 'A' can be interpreted as something like
24 * not null, not \e nulled or \c true in any means. Otherwise it selects \c 'B'.
25 * Due to the type-safe approach of \alib_expressions_nl, the type of \c 'A' and \c 'B'
26 * must be equal, or at least an auto-cast has to exist that casts the types to a pair of
27 * equal types.
28 *
29 * This built-in compiler plug-in covers the operator for virtually any type, including custom ones.
30 * The implementation invokes box-function \alib{boxing;FIsTrue} on term \c 'A' and chooses
31 * \c 'A' if the interface returned \c true, otherwise it chooses \c 'B'.
32 *
33 * Often, the elvis operator is used with strings. Because the default implementation of boxing
34 * interface \alib{boxing;FIsTrue} returns \c true if the boxed value is an array type and the
35 * array length is not zero, with strings given (which are arrays of characters) only empty strings
36 * evaluate to \c false. This way the expression:
37 *
38 * "" ?: "Elvis"
39 *
40 * rightfully evaluates to string "Elvis".
41 *
42 * With this default implementation, it should be very seldom needed to define a custom
43 * Elvis-Operator for custom types.
44 **************************************************************************************************/
46{
47 /** ********************************************************************************************
48 * Constructor.
49 * @param compiler The compiler we will get attached to.
50 **********************************************************************************************/
52
53 /** ********************************************************************************************
54 * Virtual destructor.
55 **********************************************************************************************/
56 virtual ~ElvisOperator() override
57 {}
58
59 /** ********************************************************************************************
60 * Compiles binary elvis operator <c>A ?: B</c> if terms \c 'A' and \c 'B' share the same type.
61 *
62 * @param[in,out] ciBinaryOp The compilation info struct.
63 * @return \c true if an entry was found. \c false otherwise.
64 **********************************************************************************************/
66 virtual bool TryCompilation( CIBinaryOp& ciBinaryOp ) override;
67
68};
69
70}}} // namespace [alib::expressions::detail]
71
72#endif // HPP_ALIB_EXPRESSIONS_PLUGINS_ELVIS
#define ALIB_API
Definition alib.hpp:538
Definition alib.cpp:57
virtual ALIB_API bool TryCompilation(CIBinaryOp &ciBinaryOp) override
ALIB_API ElvisOperator(Compiler &compiler)