ALib C++ Library
by
Library Version:
2402 R1
Documentation generated by
Loading...
Searching...
No Matches
home
dev
A-Worx
ALib
src
alib
expressions
plugins
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
12
# include "
alib/expressions/compilerplugin.hpp
"
13
#endif
14
15
16
namespace
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
**************************************************************************************************/
45
struct
ElvisOperator
:
public
CompilerPlugin
46
{
47
/** ********************************************************************************************
48
* Constructor.
49
* @param compiler The compiler we will get attached to.
50
**********************************************************************************************/
51
ALIB_API
ElvisOperator
(
Compiler
& compiler );
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
**********************************************************************************************/
65
ALIB_API
66
virtual
bool
TryCompilation
(
CIBinaryOp
& ciBinaryOp )
override
;
67
68
};
69
70
}}}
// namespace [alib::expressions::detail]
71
72
#endif
// HPP_ALIB_EXPRESSIONS_PLUGINS_ELVIS
alib::expressions::Compiler
Definition
compiler.hpp:90
compilerplugin.hpp
ALIB_API
#define ALIB_API
Definition
alib.hpp:538
alib
Definition
alib.cpp:57
alib::expressions::CompilerPlugin::CIBinaryOp
Definition
compilerplugin.hpp:389
alib::expressions::CompilerPlugin
Definition
compilerplugin.hpp:115
alib::expressions::plugins::ElvisOperator
Definition
elvisoperator.hpp:46
alib::expressions::plugins::ElvisOperator::~ElvisOperator
virtual ~ElvisOperator() override
Definition
elvisoperator.hpp:56
alib::expressions::plugins::ElvisOperator::TryCompilation
virtual ALIB_API bool TryCompilation(CIBinaryOp &ciBinaryOp) override
alib::expressions::plugins::ElvisOperator::ElvisOperator
ALIB_API ElvisOperator(Compiler &compiler)