| Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 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/ | 
 | 6 |  | 
| Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 7 | #ifndef _THRIFT_THRIFT_H_ | 
 | 8 | #define _THRIFT_THRIFT_H_ 1 | 
| Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 9 |  | 
| Mark Slee | 4f261c5 | 2007-04-13 00:33:24 +0000 | [diff] [blame] | 10 | #ifdef HAVE_CONFIG_H | 
 | 11 | #include "config.h" | 
 | 12 | #endif | 
 | 13 |  | 
| Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 14 | #include <netinet/in.h> | 
| Mark Slee | 4f261c5 | 2007-04-13 00:33:24 +0000 | [diff] [blame] | 15 | #ifdef HAVE_INTTYPES_H | 
| Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 16 | #include <inttypes.h> | 
| Mark Slee | 4f261c5 | 2007-04-13 00:33:24 +0000 | [diff] [blame] | 17 | #endif | 
| Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 18 | #include <string> | 
 | 19 | #include <map> | 
 | 20 | #include <list> | 
 | 21 | #include <set> | 
| Mark Slee | 4ecbebc | 2006-09-05 00:14:21 +0000 | [diff] [blame] | 22 | #include <vector> | 
| Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 23 | #include <exception> | 
 | 24 |  | 
| Aditya Agarwal | d622e96 | 2006-10-11 02:42:49 +0000 | [diff] [blame] | 25 | #include "TLogging.h" | 
 | 26 |  | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 27 | namespace facebook { namespace thrift { | 
| Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 28 |  | 
| boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 29 | class TOutput{ | 
 | 30 | public: | 
 | 31 |   TOutput() : f_(perror) {} | 
 | 32 |  | 
 | 33 |   inline void setOutputFunction(void (*function)(const char *)){ | 
 | 34 |     f_ = function; | 
 | 35 |   } | 
 | 36 |  | 
 | 37 |   inline void operator()(const char *message){ | 
 | 38 |     f_(message); | 
 | 39 |   } | 
 | 40 |  | 
 | 41 | private: | 
 | 42 |   void (*f_)(const char *); | 
 | 43 | }; | 
 | 44 |  | 
 | 45 | extern TOutput GlobalOutput; | 
 | 46 |  | 
| Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 47 | namespace protocol { | 
 | 48 |   class TProtocol; | 
 | 49 | } | 
 | 50 |  | 
| Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 51 | class TException : public std::exception { | 
| Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 52 | public: | 
| Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 53 |   TException() {} | 
 | 54 |  | 
| Mark Slee | 82a6c0f | 2007-04-04 21:08:21 +0000 | [diff] [blame] | 55 |   TException(const std::string& message) : | 
| Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 56 |     message_(message) {} | 
 | 57 |  | 
| Martin Kraemer | 8196a61 | 2006-12-09 01:57:58 +0000 | [diff] [blame] | 58 |   virtual ~TException() throw() {} | 
| Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 59 |  | 
| Martin Kraemer | 92a2eac | 2007-02-05 20:58:41 +0000 | [diff] [blame] | 60 |   virtual const char* what() const throw() { | 
| Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 61 |     if (message_.empty()) { | 
 | 62 |       return "Default TException."; | 
 | 63 |     } else { | 
 | 64 |       return message_.c_str(); | 
 | 65 |     } | 
| Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 66 |   } | 
 | 67 |  | 
| Martin Kraemer | 92a2eac | 2007-02-05 20:58:41 +0000 | [diff] [blame] | 68 | protected: | 
| Mark Slee | 2abc9df | 2006-12-16 01:06:49 +0000 | [diff] [blame] | 69 |   std::string message_; | 
| Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 70 |  | 
| Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 71 | }; | 
 | 72 |  | 
| Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 73 | class TApplicationException : public TException { | 
 | 74 | public: | 
 | 75 |  | 
 | 76 |   /** | 
 | 77 |    * Error codes for the various types of exceptions. | 
 | 78 |    */ | 
 | 79 |   enum TApplicationExceptionType { | 
 | 80 |     UNKNOWN = 0, | 
| Mark Slee | f3d3363 | 2007-02-21 01:39:23 +0000 | [diff] [blame] | 81 |     UNKNOWN_METHOD = 1, | 
 | 82 |     INVALID_MESSAGE_TYPE = 2, | 
 | 83 |     WRONG_METHOD_NAME = 3, | 
 | 84 |     BAD_SEQUENCE_ID = 4, | 
 | 85 |     MISSING_RESULT = 5, | 
| Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 86 |   }; | 
 | 87 |  | 
 | 88 |   TApplicationException() : | 
 | 89 |     TException(), | 
 | 90 |     type_(UNKNOWN) {} | 
 | 91 |  | 
 | 92 |   TApplicationException(TApplicationExceptionType type) : | 
 | 93 |     TException(),  | 
 | 94 |     type_(type) {} | 
 | 95 |  | 
| Mark Slee | 82a6c0f | 2007-04-04 21:08:21 +0000 | [diff] [blame] | 96 |   TApplicationException(const std::string& message) : | 
| Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 97 |     TException(message), | 
 | 98 |     type_(UNKNOWN) {} | 
 | 99 |  | 
 | 100 |   TApplicationException(TApplicationExceptionType type, | 
| Mark Slee | 82a6c0f | 2007-04-04 21:08:21 +0000 | [diff] [blame] | 101 |                         const std::string& message) : | 
| Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 102 |     TException(message), | 
 | 103 |     type_(type) {} | 
 | 104 |  | 
 | 105 |   virtual ~TApplicationException() throw() {} | 
 | 106 |  | 
 | 107 |   /** | 
 | 108 |    * Returns an error code that provides information about the type of error | 
 | 109 |    * that has occurred. | 
 | 110 |    * | 
 | 111 |    * @return Error code | 
 | 112 |    */ | 
 | 113 |   TApplicationExceptionType getType() { | 
 | 114 |     return type_; | 
 | 115 |   } | 
 | 116 |  | 
 | 117 |   virtual const char* what() const throw() { | 
 | 118 |     if (message_.empty()) { | 
 | 119 |       return "Default TApplicationException."; | 
 | 120 |     } else { | 
 | 121 |       return message_.c_str(); | 
 | 122 |     } | 
 | 123 |   } | 
 | 124 |  | 
| Mark Slee | ba8f8d7 | 2007-04-03 00:34:00 +0000 | [diff] [blame] | 125 |   uint32_t read(protocol::TProtocol* iprot); | 
 | 126 |   uint32_t write(protocol::TProtocol* oprot) const; | 
| Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 127 |  | 
 | 128 | protected: | 
 | 129 |   /** | 
 | 130 |    * Error code | 
 | 131 |    */ | 
 | 132 |   TApplicationExceptionType type_; | 
 | 133 |  | 
 | 134 | }; | 
 | 135 |  | 
 | 136 |  | 
| Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 137 | }} // facebook::thrift | 
| Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 138 |  | 
| Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 139 | #endif // #ifndef _THRIFT_THRIFT_H_ |