diff options
author | Konrad Grochowski <hcorg@minions.org.pl> | 2014-09-04 00:56:27 +0200 |
---|---|---|
committer | Roger Meier <roger@apache.org> | 2014-09-08 23:13:08 +0200 |
commit | a8eec715d827a973d963edb4f348ff4fb2a48a81 (patch) | |
tree | 58f64309c4c5ed357778da8fae1b6dd917b197ef /tutorial/cpp | |
parent | 6bbbf1946e55f91f063251679f479daab62e51df (diff) | |
download | thrift-a8eec715d827a973d963edb4f348ff4fb2a48a81.tar.gz |
THRIFT-2691 - C++ tutorial: printfs removed, generated operator<< used
Diffstat (limited to 'tutorial/cpp')
-rw-r--r-- | tutorial/cpp/CppClient.cpp | 24 | ||||
-rw-r--r-- | tutorial/cpp/CppServer.cpp | 21 |
2 files changed, 21 insertions, 24 deletions
diff --git a/tutorial/cpp/CppClient.cpp b/tutorial/cpp/CppClient.cpp index 4218019b5..8ef976b2b 100644 --- a/tutorial/cpp/CppClient.cpp +++ b/tutorial/cpp/CppClient.cpp @@ -17,9 +17,7 @@ * under the License. */ -#include <stdio.h> -#include <unistd.h> -#include <sys/time.h> +#include <iostream> #include <thrift/protocol/TBinaryProtocol.h> #include <thrift/transport/TSocket.h> @@ -45,10 +43,9 @@ int main(int argc, char** argv) { transport->open(); client.ping(); - printf("ping()\n"); + cout << "ping()" << endl; - int32_t sum = client.add(1,1); - printf("1+1=%d\n", sum); + cout << "1 + 1 = " << client.add(1, 1) << endl; Work work; work.op = Operation::DIVIDE; @@ -57,26 +54,27 @@ int main(int argc, char** argv) { try { client.calculate(1, work); - printf("Whoa? We can divide by zero!\n"); - } catch (InvalidOperation &io) { - printf("InvalidOperation: %s\n", io.why.c_str()); + cout << "Whoa? We can divide by zero!" << endl; + } catch (InvalidOperation& io) { + cout << "InvalidOperation: " << io.why << endl; + // or using generated operator<<: cout << io << endl; } work.op = Operation::SUBTRACT; work.num1 = 15; work.num2 = 10; int32_t diff = client.calculate(1, work); - printf("15-10=%d\n", diff); + cout << "15 - 10 = " << diff << endl; // Note that C++ uses return by reference for complex types to avoid // costly copy construction SharedStruct ss; client.getStruct(ss, 1); - printf("Check log: %s\n", ss.value.c_str()); + cout << "Received log: " << ss << endl; transport->close(); - } catch (TException &tx) { - printf("ERROR: %s\n", tx.what()); + } catch (TException& tx) { + cout << "ERROR: " << tx.what() << endl; } } diff --git a/tutorial/cpp/CppServer.cpp b/tutorial/cpp/CppServer.cpp index d75b5532f..653caaa83 100644 --- a/tutorial/cpp/CppServer.cpp +++ b/tutorial/cpp/CppServer.cpp @@ -25,6 +25,7 @@ #include <thrift/server/TThreadedServer.h> #include <thrift/transport/TServerSocket.h> #include <thrift/transport/TTransportUtils.h> +#include <thrift/TToString.h> #include <iostream> #include <stdexcept> @@ -46,16 +47,16 @@ class CalculatorHandler : public CalculatorIf { CalculatorHandler() {} void ping() { - printf("ping()\n"); + cout << "ping()" << endl; } int32_t add(const int32_t n1, const int32_t n2) { - printf("add(%d,%d)\n", n1, n2); + cout << "add(" << n1 << ", " << n2 << ")" << endl; return n1 + n2; } - int32_t calculate(const int32_t logid, const Work &work) { - printf("calculate(%d,{%d,%d,%d})\n", logid, work.op, work.num1, work.num2); + int32_t calculate(const int32_t logid, const Work& work) { + cout << "calculate(" << logid << ", " << work << ")" << endl; int32_t val; switch (work.op) { @@ -86,9 +87,7 @@ class CalculatorHandler : public CalculatorIf { SharedStruct ss; ss.key = logid; - char buffer[12]; - snprintf(buffer, sizeof(buffer), "%d", val); - ss.value = buffer; + ss.value = to_string(val); log[logid] = ss; @@ -96,12 +95,12 @@ class CalculatorHandler : public CalculatorIf { } void getStruct(SharedStruct &ret, const int32_t logid) { - printf("getStruct(%d)\n", logid); + cout << "getStruct(" << logid << ")" << endl; ret = log[logid]; } void zip() { - printf("zip()\n"); + cout << "zip()" << endl; } protected: @@ -145,8 +144,8 @@ int main(int argc, char **argv) { */ - printf("Starting the server...\n"); + cout << "Starting the server..." << endl; server.serve(); - printf("done.\n"); + cout << "Done." << endl; return 0; } |