Advertisement
clevershovel

Untitled

Jul 20th, 2019
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.16 KB | None | 0 0
  1. auto GetStringPtrs(Runtime::Object* lhs, Runtime::Object* rhs) {
  2.   using namespace Runtime;
  3.   return pair(dynamic_cast<String*>(lhs), dynamic_cast<String*>(rhs));
  4. }
  5.  
  6. auto GetNumberPtrs(Runtime::Object* lhs, Runtime::Object* rhs) {
  7.   using namespace Runtime;
  8.   return pair(dynamic_cast<Number*>(lhs), dynamic_cast<Number*>(rhs));
  9. }
  10.  
  11. auto GetBoolPtrs(Runtime::Object* lhs, Runtime::Object* rhs) {
  12.   using namespace Runtime;
  13.   return pair(dynamic_cast<Bool*>(lhs), dynamic_cast<Bool*>(rhs));
  14. }
  15.  
  16. template <typename BinOp>
  17. ObjectHolder ArithOpOrError(Runtime::Object* lhs, Runtime::Object* rhs,
  18.                             BinOp op) {
  19.   using namespace Runtime;
  20.   auto [str1, str2] = GetStringPtrs(lhs, rhs);
  21.   if (str1 && str2)
  22.     return ObjectHolder::Own(String(op(str1->GetValue(), str2->GetValue())));
  23.   auto [num1, num2] = GetNumberPtrs(lhs, rhs);
  24.   if (num1 && num2)
  25.     return ObjectHolder::Own(Number(op(num1->GetValue(), num2->GetValue())));
  26.   auto [bool1, bool2] = GetBoolPtrs(lhs, rhs);
  27.   if (bool1 && bool2)
  28.     return ObjectHolder::Own(Bool(op(bool1->GetValue(), bool2->GetValue())));
  29.   throw runtime_error("not the same type or not ValueObject");
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement