Om
choose_operation.cpp
Go to the documentation of this file.
1 
15 #ifndef Om_Language_Operation_ChooseOperation_
16 
18 
19  #ifdef Om_Macro_Test_
20 
21  #include "om/language/system.hpp"
22 
23  #ifndef Om_Macro_Precompilation_
24 
25  #include "boost/test/unit_test.hpp"
26 
27  #endif
28 
29 namespace Om {
30 
31  namespace Language {
32 
33  namespace Operation {
34 
35  BOOST_AUTO_TEST_SUITE(ChooseOperationTest)
36 
37  BOOST_AUTO_TEST_CASE(DefinitionTest) {
38  BOOST_CHECK_EQUAL(
39  "{choose}",
40  System::Get().Evaluate("drop find {choose} system")
41  );
42  }
43 
44  BOOST_AUTO_TEST_CASE(GeneralTest) {
45  BOOST_CHECK_EQUAL(
46  "{empty}",
47  System::Get().Evaluate("choose{empty}{non-empty}{}")
48  );
49 
50  BOOST_CHECK_EQUAL(
51  "{non-empty}",
52  System::Get().Evaluate("choose{empty}{non-empty}{some operators}")
53  );
54  }
55 
56  BOOST_AUTO_TEST_SUITE_END()
57 
58  }
59 
60  }
61 
62 }
63 
64  #endif
65 
66 #else
67 
68  #include "om/language/literal.hpp"
69 
70 // MARK: - Om::Language::Operation::ChooseOperation
71 
72  #define Type_ \
73  Om::Language::Operation::ChooseOperation
74 
75 // MARK: public (static)
76 
77 inline char const * Type_::GetName() {
79 }
80 
81 template <typename TheChooseOperation>
82 inline void Type_::GiveElements(
83  TheChooseOperation & theChooseOperation,
84  Consumer & theConsumer
85 ) {
86  theConsumer.TakeElement(
87  GetOperator()
88  );
89  if (0 < theChooseOperation.thisOperandCount) {
90  theConsumer.TakeElement(theChooseOperation.thisEmptyCase);
91  if (1 < theChooseOperation.thisOperandCount) {
92  theConsumer.TakeElement(theChooseOperation.thisNonEmptyCase);
93  }
94  }
95 }
96 
97 // MARK: public (non-static)
98 
99 inline Type_::ChooseOperation():
100 thisEmptyCase(),
101 thisNonEmptyCase(),
102 thisOperandCount() {}
103 
104 template <typename TheProducer>
105 inline bool Type_::TakeQuotedProducer(
106  Evaluation & theEvaluation,
107  TheProducer & theProducer
108 ) {
109  Operand theOperand(
110  theProducer.GiveProgram()
111  );
112  return this->TakeOperand(
113  theEvaluation,
114  theOperand
115  );
116 }
117 
118 template <typename TheOperand>
119 inline bool Type_::TakeOperand(
120  Evaluation & theEvaluation,
121  TheOperand & theOperand
122 ) {
123  assert(
124  !theOperand.IsEmpty()
125  );
126  assert(this->thisOperandCount < 3);
127  switch (this->thisOperandCount++) {
128  case 0:
129  this->thisEmptyCase.Take(theOperand);
130  return false;
131  case 1:
132  this->thisNonEmptyCase.Take(theOperand);
133  return false;
134  default:
135  {
136  TheOperand const & theConstOperand = theOperand;
137  theEvaluation.TakeOperand(
138  theConstOperand.GetProgram()->IsEmpty() ?
139  this->thisEmptyCase :
140  this->thisNonEmptyCase
141  );
142  }
143  return true;
144  }
145 }
146 
147  #undef Type_
148 
149 #endif
Om header file.
#define Om_Language_Operation_ChooseOperation_GetName_()
static System & Get()
Om header file.
The Om library.
Definition: code_point.hpp:26
Om header file.