Thrift Binary protocol improvements and application exceptions

Summary: Add application exceptions for unknown methods etc, and also let binary protocol support size limits on containers and strings

Reviewed By: aditya, xp-wayne


git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665003 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/cpp/src/Thrift.h b/lib/cpp/src/Thrift.h
index 2268c34..61edf5b 100644
--- a/lib/cpp/src/Thrift.h
+++ b/lib/cpp/src/Thrift.h
@@ -14,6 +14,10 @@
 
 namespace facebook { namespace thrift {
 
+namespace protocol {
+  class TProtocol;
+}
+
 class TException : public std::exception {
 public:
   TException() {}
@@ -36,6 +40,66 @@
 
 };
 
+class TApplicationException : public TException {
+public:
+
+  /**
+   * Error codes for the various types of exceptions.
+   */
+  enum TApplicationExceptionType {
+    UNKNOWN = 0,
+    INVALID_METHOD = 1,
+  };
+
+  TApplicationException() :
+    TException(),
+    type_(UNKNOWN) {}
+
+  TApplicationException(TApplicationExceptionType type) :
+    TException(), 
+    type_(type) {}
+
+  TApplicationException(const std::string message) :
+    TException(message),
+    type_(UNKNOWN) {}
+
+  TApplicationException(TApplicationExceptionType type,
+                        const std::string message) :
+    TException(message),
+    type_(type) {}
+
+  virtual ~TApplicationException() throw() {}
+
+  /**
+   * Returns an error code that provides information about the type of error
+   * that has occurred.
+   *
+   * @return Error code
+   */
+  TApplicationExceptionType getType() {
+    return type_;
+  }
+
+  virtual const char* what() const throw() {
+    if (message_.empty()) {
+      return "Default TApplicationException.";
+    } else {
+      return message_.c_str();
+    }
+  }
+
+  uint32_t TApplicationException::read(facebook::thrift::protocol::TProtocol* iprot);
+  uint32_t TApplicationException::write(facebook::thrift::protocol::TProtocol* oprot) const;
+
+protected:
+  /**
+   * Error code
+   */
+  TApplicationExceptionType type_;
+
+};
+
+
 }} // facebook::thrift
 
 #endif // #ifndef _THRIFT_THRIFT_H_