blob: 4a3df658b70e116ae6497ec8908f72994029f9d6 [file] [log] [blame]
Mark Slee9f0c6512007-02-28 23:58:26 +00001// 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 Sleef5f2be42006-09-05 21:05:31 +00007#ifndef _THRIFT_THRIFT_H_
8#define _THRIFT_THRIFT_H_ 1
Mark Sleee8540632006-05-30 09:24:40 +00009
Mark Slee4f261c52007-04-13 00:33:24 +000010#ifdef HAVE_CONFIG_H
11#include "config.h"
12#endif
13
Mark Slee8d7e1f62006-06-07 06:48:56 +000014#include <netinet/in.h>
Mark Slee4f261c52007-04-13 00:33:24 +000015#ifdef HAVE_INTTYPES_H
Mark Slee8d7e1f62006-06-07 06:48:56 +000016#include <inttypes.h>
Mark Slee4f261c52007-04-13 00:33:24 +000017#endif
Mark Sleee8540632006-05-30 09:24:40 +000018#include <string>
19#include <map>
20#include <list>
21#include <set>
Mark Slee4ecbebc2006-09-05 00:14:21 +000022#include <vector>
Marc Slemko5b126d62006-08-11 23:03:42 +000023#include <exception>
24
Aditya Agarwald622e962006-10-11 02:42:49 +000025#include "TLogging.h"
26
Mark Sleecfc01932006-09-01 22:18:16 +000027namespace facebook { namespace thrift {
Marc Slemko5b126d62006-08-11 23:03:42 +000028
boz6ded7752007-06-05 22:41:18 +000029class TOutput{
30public:
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
41private:
42 void (*f_)(const char *);
43};
44
45extern TOutput GlobalOutput;
46
Mark Sleef9831082007-02-20 20:59:21 +000047namespace protocol {
48 class TProtocol;
49}
50
Mark Sleeb9ff32a2006-11-16 01:00:24 +000051class TException : public std::exception {
Marc Slemko5b126d62006-08-11 23:03:42 +000052public:
Mark Sleeb9ff32a2006-11-16 01:00:24 +000053 TException() {}
54
Mark Slee82a6c0f2007-04-04 21:08:21 +000055 TException(const std::string& message) :
Mark Slee2f6404d2006-10-10 01:37:40 +000056 message_(message) {}
57
Martin Kraemer8196a612006-12-09 01:57:58 +000058 virtual ~TException() throw() {}
Mark Slee2f6404d2006-10-10 01:37:40 +000059
Martin Kraemer92a2eac2007-02-05 20:58:41 +000060 virtual const char* what() const throw() {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000061 if (message_.empty()) {
62 return "Default TException.";
63 } else {
64 return message_.c_str();
65 }
Mark Slee2f6404d2006-10-10 01:37:40 +000066 }
67
Martin Kraemer92a2eac2007-02-05 20:58:41 +000068protected:
Mark Slee2abc9df2006-12-16 01:06:49 +000069 std::string message_;
Mark Slee2f6404d2006-10-10 01:37:40 +000070
Marc Slemko5b126d62006-08-11 23:03:42 +000071};
72
Mark Sleef9831082007-02-20 20:59:21 +000073class TApplicationException : public TException {
74public:
75
76 /**
77 * Error codes for the various types of exceptions.
78 */
79 enum TApplicationExceptionType {
80 UNKNOWN = 0,
Mark Sleef3d33632007-02-21 01:39:23 +000081 UNKNOWN_METHOD = 1,
82 INVALID_MESSAGE_TYPE = 2,
83 WRONG_METHOD_NAME = 3,
84 BAD_SEQUENCE_ID = 4,
85 MISSING_RESULT = 5,
Mark Sleef9831082007-02-20 20:59:21 +000086 };
87
88 TApplicationException() :
89 TException(),
90 type_(UNKNOWN) {}
91
92 TApplicationException(TApplicationExceptionType type) :
93 TException(),
94 type_(type) {}
95
Mark Slee82a6c0f2007-04-04 21:08:21 +000096 TApplicationException(const std::string& message) :
Mark Sleef9831082007-02-20 20:59:21 +000097 TException(message),
98 type_(UNKNOWN) {}
99
100 TApplicationException(TApplicationExceptionType type,
Mark Slee82a6c0f2007-04-04 21:08:21 +0000101 const std::string& message) :
Mark Sleef9831082007-02-20 20:59:21 +0000102 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 Sleeba8f8d72007-04-03 00:34:00 +0000125 uint32_t read(protocol::TProtocol* iprot);
126 uint32_t write(protocol::TProtocol* oprot) const;
Mark Sleef9831082007-02-20 20:59:21 +0000127
128protected:
129 /**
130 * Error code
131 */
132 TApplicationExceptionType type_;
133
134};
135
136
David Reissd779cbe2007-08-31 01:42:55 +0000137// Forward declare this structure used by TDenseProtocol
138namespace reflection { namespace local {
139struct TypeSpec;
140}}
141
142
Marc Slemko5b126d62006-08-11 23:03:42 +0000143}} // facebook::thrift
Mark Sleee8540632006-05-30 09:24:40 +0000144
Mark Sleef5f2be42006-09-05 21:05:31 +0000145#endif // #ifndef _THRIFT_THRIFT_H_