r1403@opsdev009 (orig r74388): dreiss | 2007-12-14 12:56:04 -0800
[amiethrift.git] / lib / cpp / src / Thrift.h
blob338dbdebf76527d4e366067bccd80e2227681fc1
1 // Copyright (c) 2006- Facebook
2 // Distributed under the Thrift Software License
3 //
4 // See accompanying file LICENSE or visit the Thrift site at:
5 // http://developers.facebook.com/thrift/
7 #ifndef _THRIFT_THRIFT_H_
8 #define _THRIFT_THRIFT_H_ 1
10 #ifdef HAVE_CONFIG_H
11 #include "config.h"
12 #endif
14 #include <netinet/in.h>
15 #ifdef HAVE_INTTYPES_H
16 #include <inttypes.h>
17 #endif
18 #include <string>
19 #include <map>
20 #include <list>
21 #include <set>
22 #include <vector>
23 #include <exception>
25 #include "TLogging.h"
27 namespace facebook { namespace thrift {
29 class TOutput {
30 public:
31 TOutput() : f_(&perrorTimeWrapper) {}
33 inline void setOutputFunction(void (*function)(const char *)){
34 f_ = function;
37 inline void operator()(const char *message){
38 f_(message);
41 inline static void perrorTimeWrapper(const char* msg) {
42 time_t now;
43 char dbgtime[25];
44 time(&now);
45 ctime_r(&now, dbgtime);
46 dbgtime[24] = 0;
47 fprintf(stderr, "%s ", dbgtime);
48 perror(msg);
50 private:
51 void (*f_)(const char *);
54 extern TOutput GlobalOutput;
56 namespace protocol {
57 class TProtocol;
60 class TException : public std::exception {
61 public:
62 TException() {}
64 TException(const std::string& message) :
65 message_(message) {}
67 virtual ~TException() throw() {}
69 virtual const char* what() const throw() {
70 if (message_.empty()) {
71 return "Default TException.";
72 } else {
73 return message_.c_str();
77 protected:
78 std::string message_;
82 class TApplicationException : public TException {
83 public:
85 /**
86 * Error codes for the various types of exceptions.
88 enum TApplicationExceptionType {
89 UNKNOWN = 0,
90 UNKNOWN_METHOD = 1,
91 INVALID_MESSAGE_TYPE = 2,
92 WRONG_METHOD_NAME = 3,
93 BAD_SEQUENCE_ID = 4,
94 MISSING_RESULT = 5,
97 TApplicationException() :
98 TException(),
99 type_(UNKNOWN) {}
101 TApplicationException(TApplicationExceptionType type) :
102 TException(),
103 type_(type) {}
105 TApplicationException(const std::string& message) :
106 TException(message),
107 type_(UNKNOWN) {}
109 TApplicationException(TApplicationExceptionType type,
110 const std::string& message) :
111 TException(message),
112 type_(type) {}
114 virtual ~TApplicationException() throw() {}
117 * Returns an error code that provides information about the type of error
118 * that has occurred.
120 * @return Error code
122 TApplicationExceptionType getType() {
123 return type_;
126 virtual const char* what() const throw() {
127 if (message_.empty()) {
128 switch (type_) {
129 case UNKNOWN : return "TApplicationException: Unknown application exception";
130 case UNKNOWN_METHOD : return "TApplicationException: Unknown method";
131 case INVALID_MESSAGE_TYPE : return "TApplicationException: Invalid message type";
132 case WRONG_METHOD_NAME : return "TApplicationException: Wrong method name";
133 case BAD_SEQUENCE_ID : return "TApplicationException: Bad sequence identifier";
134 case MISSING_RESULT : return "TApplicationException: Missing result";
135 default : return "TApplicationException: (Invalid exception type)";
137 } else {
138 return message_.c_str();
142 uint32_t read(protocol::TProtocol* iprot);
143 uint32_t write(protocol::TProtocol* oprot) const;
145 protected:
147 * Error code
149 TApplicationExceptionType type_;
154 // Forward declare this structure used by TDenseProtocol
155 namespace reflection { namespace local {
156 struct TypeSpec;
160 }} // facebook::thrift
162 #endif // #ifndef _THRIFT_THRIFT_H_