ALib C++ Library
Library Version: 2412 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
elvisoperator.cpp
1// #################################################################################################
2// ALib C++ Library
3//
4// Copyright 2013-2024 A-Worx GmbH, Germany
5// Published under 'Boost Software License' (a free software license, see LICENSE.txt)
6// #################################################################################################
8
9#if !DOXYGEN
11#endif // !DOXYGEN
12
13//! @cond NO_DOX
14
15namespace alib { namespace expressions { namespace plugins {
16
18: CompilerPlugin("ALib Elvis", compiler, CompilePriorities::ElvisOperator )
19{}
20
21namespace {
22
23Box elvis( Scope&, ArgIterator arg, ArgIterator )
24{
25 return (*arg).Call<FIsTrue>() ? *arg : *(arg + 1);
26}
27
28} // anonymous namespace
29
30bool ElvisOperator::TryCompilation( CIBinaryOp& ciBinaryOp )
31{
32 Box& lhs= * ciBinaryOp.ArgsBegin;
33 Box& rhs= *(ciBinaryOp.ArgsBegin + 1);
34
35 // not Elvis operator A_CHAR("A ?: B") ?
36 if( ciBinaryOp.Operator != A_CHAR("?:") || !lhs.IsSameType(rhs) )
37 return false;
38
39 ALIB_DBG( ciBinaryOp.DbgCallbackName = "elvis"; )
40
41 // constant A?
42 if( ciBinaryOp.LhsIsConst )
43 {
44 if( lhs.Call<FIsTrue>() )
45 ciBinaryOp.TypeOrValue= lhs;
46 else
47 {
48 // constant B?
49 if( ciBinaryOp.RhsIsConst )
50 ciBinaryOp.TypeOrValue= rhs;
51 else
52 ciBinaryOp.NonConstArgIsResult= true;
53 }
54 return true;
55 }
56
57 // non constant A
58 ciBinaryOp.Callback = elvis;
59 ciBinaryOp.TypeOrValue= rhs;
60
61 return true;
62}
63
64}}} // namespace [alib::expressions::detail]
65
66//! @endcond
decltype(std::declval< typename TFDecl::Signature >()(std::declval< Box & >(), std::declval< TArgs >()...)) Call(TArgs &&... args) const
Definition box.inl:1175
#define A_CHAR(STR)
#define ALIB_DBG(...)
Definition alib.hpp:390
Definition alib.cpp:69
expressions::CompilerPlugin CompilerPlugin
Type alias in namespace alib.
boxing::Box Box
Type alias in namespace alib.
expressions::Compiler Compiler
Type alias in namespace alib.
Definition compiler.hpp:553
ALIB_API ElvisOperator(Compiler &compiler)