ALib C++ Library
Library Version: 2402 R1
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 !defined(ALIB_DOX)
10#if !defined (HPP_ALIB_EXPRESSIONS_PLUGINS_ELVIS)
12#endif
13#endif // !defined(ALIB_DOX)
14
15
16//! @cond NO_DOX
17
18namespace alib { namespace expressions { namespace plugins {
19
21: CompilerPlugin("ALib Elvis", compiler )
22{}
23
24namespace {
25
26Box elvis( Scope&, ArgIterator arg, ArgIterator )
27{
28 return (*arg).Call<FIsTrue>() ? *arg : *(arg + 1);
29}
30
31} // anonymous namespace
32
33bool ElvisOperator::TryCompilation( CIBinaryOp& ciBinaryOp )
34{
35 Box& lhs= * ciBinaryOp.ArgsBegin;
36 Box& rhs= *(ciBinaryOp.ArgsBegin + 1);
37
38 // not Elvis operator A_CHAR("A ?: B") ?
39 if( ciBinaryOp.Operator != A_CHAR("?:") || !lhs.IsSameType(rhs) )
40 return false;
41
42 ALIB_DBG( ciBinaryOp.DbgCallbackName = "elvis"; )
43
44 // constant A?
45 if( ciBinaryOp.LhsIsConst )
46 {
47 if( lhs.Call<FIsTrue>() )
48 ciBinaryOp.TypeOrValue= lhs;
49 else
50 {
51 // constant B?
52 if( ciBinaryOp.RhsIsConst )
53 ciBinaryOp.TypeOrValue= rhs;
54 else
55 ciBinaryOp.NonConstArgIsResult= true;
56 }
57 return true;
58 }
59
60 // non constant A
61 ciBinaryOp.Callback = elvis;
62 ciBinaryOp.TypeOrValue= rhs;
63
64 return true;
65}
66
67}}} // namespace [alib::expressions::detail]
68
69//! @endcond
decltype(std::declval< typename TFDecl::Signature >()(std::declval< Box & >(), std::declval< TArgs >()...)) Call(TArgs &&... args) const
Definition box.inl:1135
#define A_CHAR(STR)
#define ALIB_DBG(...)
Definition alib.hpp:457
Definition alib.cpp:57
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:596
ALIB_API ElvisOperator(Compiler &compiler)