You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by ro...@apache.org on 2014/09/10 00:13:16 UTC

git commit: THRIFT-2691 - C++ tutorial: printfs removed, generated operator<< used

Repository: thrift
Updated Branches:
  refs/heads/master 6bbbf1946 -> a8eec715d


THRIFT-2691 - C++ tutorial: printfs removed, generated operator<< used


Project: http://git-wip-us.apache.org/repos/asf/thrift/repo
Commit: http://git-wip-us.apache.org/repos/asf/thrift/commit/a8eec715
Tree: http://git-wip-us.apache.org/repos/asf/thrift/tree/a8eec715
Diff: http://git-wip-us.apache.org/repos/asf/thrift/diff/a8eec715

Branch: refs/heads/master
Commit: a8eec715d827a973d963edb4f348ff4fb2a48a81
Parents: 6bbbf19
Author: Konrad Grochowski <hc...@minions.org.pl>
Authored: Thu Sep 4 00:56:27 2014 +0200
Committer: Roger Meier <ro...@apache.org>
Committed: Mon Sep 8 23:13:08 2014 +0200

----------------------------------------------------------------------
 tutorial/cpp/CppClient.cpp | 24 +++++++++++-------------
 tutorial/cpp/CppServer.cpp | 21 ++++++++++-----------
 2 files changed, 21 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/thrift/blob/a8eec715/tutorial/cpp/CppClient.cpp
----------------------------------------------------------------------
diff --git a/tutorial/cpp/CppClient.cpp b/tutorial/cpp/CppClient.cpp
index 4218019..8ef976b 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;
   }
 
 }

http://git-wip-us.apache.org/repos/asf/thrift/blob/a8eec715/tutorial/cpp/CppServer.cpp
----------------------------------------------------------------------
diff --git a/tutorial/cpp/CppServer.cpp b/tutorial/cpp/CppServer.cpp
index d75b553..653caaa 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;
 }