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