blob: a6fd8da33309b3c31b1638aea8c8001392a2a04f [file] [log] [blame]
// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
/// A tool that connects to a calculator engine over FIDL to perform
/// arithmetic operations.
#include <fuchsia/examples/calculator/cpp/fidl.h>
#include <lib/async-loop/cpp/loop.h>
#include <lib/async-loop/default.h>
#include <iostream>
#include <string>
#include "client.h"
namespace calculator = fuchsia::examples::calculator;
namespace calculator_cli {
/// Structured request parameters for Calculator interface
struct Request {
int a;
int b;
calculator::BinaryOp op;
};
std::ostream& operator << (std::ostream& os, const Request& arg)
{
os << arg.a;
switch (arg.op) {
case calculator::BinaryOp::ADDITION:
os << " + ";
break;
case calculator::BinaryOp::SUBTRACTION:
os << " - ";
break;
case calculator::BinaryOp::MULTIPLICATION:
os << " x ";
break;
case calculator::BinaryOp::DIVISION:
os << " / ";
break;
}
os << arg.b;
return os;
}
} // namespace calculator_cli
/// Entry point for the calculator CLI.
int main(int argc, char **argv) {
async::Loop loop(&kAsyncLoopConfigAttachToCurrentThread);
calculator_cli::CalculatorClient app;
app.Start();
// 2 + 2 = ?
calculator_cli::Request args = {
2, 2,
calculator::BinaryOp::ADDITION,
};
std::cout << "Request: " << args << std::endl;
app.calculator()->DoBinaryOp(args.op, args.a, args.b, [&loop](calculator::Result value) {
if (value.is_error()) {
std::cerr << "Error: " << value.error().message << std::endl;
} else {
std::cout << "Result: " << value.number() << std::endl;
}
loop.Quit();
});
return loop.Run();
}