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