ALib C++ Library
Library Version: 2510 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
elvisoperator.cpp
1// #################################################################################################
2// ALib C++ Library
3//
4// Copyright 2013-2025 A-Worx GmbH, Germany
5// Published under 'Boost Software License' (a free software license, see LICENSE.txt)
6// #################################################################################################
7#include "alib_precompile.hpp"
8#if !defined(ALIB_C20_MODULES) || ((ALIB_C20_MODULES != 0) && (ALIB_C20_MODULES != 1))
9# error "Symbol ALIB_C20_MODULES has to be given to the compiler as either 0 or 1"
10#endif
11#if ALIB_C20_MODULES
12 module;
13#endif
14// ====================================== Global Fragment ======================================
16
17// =========================================== Module ==========================================
18#if ALIB_C20_MODULES
21 import ALib.Strings;
22#else
24# include "ALib.Strings.H"
26#endif
27// ====================================== Implementation =======================================
28//! @cond NO_DOX
29namespace alib { namespace expressions { namespace plugins {
30
32: CompilerPlugin("ALib Elvis", compiler, CompilePriorities::ElvisOperator )
33{}
34
35namespace {
36
37Box elvis( Scope&, ArgIterator arg, ArgIterator )
38{
39 return (*arg).Call<FIsTrue>() ? *arg : *(arg + 1);
40}
41
42} // anonymous namespace
43
44bool ElvisOperator::TryCompilation( CIBinaryOp& ciBinaryOp )
45{
46 Box& lhs= * ciBinaryOp.ArgsBegin;
47 Box& rhs= *(ciBinaryOp.ArgsBegin + 1);
48
49 // not Elvis operator A_CHAR("A ?: B") ?
50 if( ciBinaryOp.Operator != A_CHAR("?:") || !lhs.IsSameType(rhs) )
51 return false;
52
53 ALIB_DBG( ciBinaryOp.DbgCallbackName = "elvis"; )
54
55 // constant A?
56 if( ciBinaryOp.LhsIsConst )
57 {
58 if( lhs.Call<FIsTrue>() )
59 ciBinaryOp.TypeOrValue= lhs;
60 else
61 {
62 // constant B?
63 if( ciBinaryOp.RhsIsConst )
64 ciBinaryOp.TypeOrValue= rhs;
65 else
66 ciBinaryOp.NonConstArgIsResult= true;
67 }
68 return true;
69 }
70
71 // non constant A
72 ciBinaryOp.Callback = elvis;
73 ciBinaryOp.TypeOrValue= rhs;
74
75 return true;
76}
77
78}}} // namespace [alib::expressions::detail]
79//! @endcond
decltype(std::declval< typename TFDecl::Signature >()(std::declval< Box & >(), std::declval< TArgs >()...)) Call(TArgs &&... args) const
Definition box.inl:1039
#define A_CHAR(STR)
#define ALIB_DBG(...)
Definition alib.inl:836
expressions::CompilerPlugin CompilerPlugin
Type alias in namespace alib.
boxing::Box Box
Type alias in namespace alib.
Definition box.inl:1216
expressions::Compiler Compiler
Type alias in namespace alib.
Definition compiler.inl:574
ALIB_DLL ElvisOperator(Compiler &compiler)