source: xplra/src/client/c/hu/varadiistvan/xplra/Exception.h@ 40:ec5dde8a6ff6

Last change on this file since 40:ec5dde8a6ff6 was 40:ec5dde8a6ff6, checked in by István Váradi <ivaradi@…>, 11 years ago

Implemented the client support for the new commands and updated the basic test programs with tests showing messages

File size: 8.0 KB
Line 
1// Copyright (c) 2013 by István Váradi
2
3// This file is part of XPLRA, a remote-access plugin for X-Plane
4
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are met:
7
8// 1. Redistributions of source code must retain the above copyright notice, this
9// list of conditions and the following disclaimer.
10// 2. Redistributions in binary form must reproduce the above copyright notice,
11// this list of conditions and the following disclaimer in the documentation
12// and/or other materials provided with the distribution.
13
14// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24
25// The views and conclusions contained in the software and documentation are those
26// of the authors and should not be interpreted as representing official policies,
27// either expressed or implied, of the FreeBSD Project.
28
29#ifndef HU_VARADIISTVAN_XPLRA_EXCEPTION_H
30#define HU_VARADIISTVAN_XPLRA_EXCEPTION_H
31//------------------------------------------------------------------------------
32
33#include <exception>
34
35#include <string>
36
37//------------------------------------------------------------------------------
38
39namespace hu { namespace varadiistvan { namespace xplra {
40
41//------------------------------------------------------------------------------
42
43/**
44 * Base class for exceptions that can be thrown by the client.
45 */
46class Exception : public std::exception
47{
48protected:
49 /**
50 * The string representation of the error message.
51 */
52 std::string message;
53
54public:
55 /**
56 * Construct the exception with the given message.
57 */
58 Exception(const char* message) throw();
59
60protected:
61 /**
62 * Default constructor for children.
63 */
64 Exception() throw();
65
66public:
67 /**
68 * Destroy the exception
69 */
70 virtual ~Exception() throw();
71
72public:
73 /**
74 * Get a message.
75 */
76 virtual const char* what() const throw();
77};
78
79//------------------------------------------------------------------------------
80
81/**
82 * An I/O exception.
83 */
84class IOException : public Exception
85{
86public:
87 /**
88 * Type for the error code.
89 */
90 typedef unsigned long errorCode_t;
91
92private:
93 /**
94 * The error code.
95 */
96 errorCode_t errorCode;
97
98public:
99 /**
100 * Construct the exception.
101 */
102 IOException(errorCode_t errorCode) throw();
103
104
105 /**
106 * Get the error code.
107 */
108 errorCode_t getErrorCode() const throw();
109};
110
111//------------------------------------------------------------------------------
112
113/**
114 * An exception arising from the invalid use of the protocol, e.g. an
115 * invalid dataref.
116 */
117class ProtocolException : public Exception
118{
119public:
120 /**
121 * Type for the error codes.
122 */
123 typedef enum {
124 /// Invalid command issued
125 INVALID_COMMAND = 1,
126
127 /// Unknown dataref
128 UNKNOWN_DATAREF = 2,
129
130 /// Invalid type
131 INVALID_TYPE = 3,
132
133 /// Invalid length
134 INVALID_LENGTH = 4,
135
136 /// Invalid offset
137 INVALID_OFFSET = 5,
138
139 /// Invalid count
140 INVALID_COUNT = 6,
141
142 /// Invalid ID
143 INVALID_ID = 7,
144
145 /// Invalid duration
146 INVALID_DURATION = 8,
147
148 /// Other error
149 OTHER = 255
150 } errorCode_t;
151
152private:
153 /**
154 * The error code.
155 */
156 errorCode_t errorCode;
157
158 /**
159 * Indicate if the parameter is valid.
160 */
161 bool hasParameter;
162
163 /**
164 * Parameter of the error.
165 */
166 long parameter;
167
168public:
169 /**
170 * Construct the exception.
171 */
172 ProtocolException(errorCode_t errorCode,
173 bool hasParameter = false, long parameter = 0) throw();
174
175 /**
176 * Get the error code.
177 */
178 errorCode_t getErrorCode() const throw();
179
180 /**
181 * Get the value of the parameter and whether it is valid or not.
182 */
183 bool getParameter(long& param) const throw();
184};
185
186//------------------------------------------------------------------------------
187
188/**
189 * Exception to signify the fact the we are not connected to the
190 * simulator.
191 */
192class NotConnectedException : public Exception
193{
194public:
195 /**
196 * Construct the exception
197 */
198 NotConnectedException() throw();
199};
200
201//------------------------------------------------------------------------------
202
203/**
204 * Exception thrown when we want to get or set a dataref in a
205 * MultiBuffer into or from a value of the wrong type.
206 */
207class TypeMismatchException : public Exception
208{
209public:
210 /**
211 * Construct the exception
212 */
213 TypeMismatchException() throw();
214};
215
216//------------------------------------------------------------------------------
217
218/**
219 * Exception thrown when we want to get or set a dataref with an
220 * invalid ID.
221 */
222class InvalidIDException : public Exception
223{
224public:
225 /**
226 * Construct the exception
227 */
228 InvalidIDException() throw();
229};
230
231//------------------------------------------------------------------------------
232// Inline definitions
233//------------------------------------------------------------------------------
234
235inline Exception::Exception(const char* message) throw() :
236 message(message)
237{
238}
239
240//------------------------------------------------------------------------------
241
242inline Exception::Exception() throw()
243{
244}
245
246//------------------------------------------------------------------------------
247
248inline Exception::~Exception() throw()
249{
250}
251
252//------------------------------------------------------------------------------
253//------------------------------------------------------------------------------
254
255inline IOException::errorCode_t IOException::getErrorCode() const throw()
256{
257 return errorCode;
258}
259
260//------------------------------------------------------------------------------
261//------------------------------------------------------------------------------
262
263inline ProtocolException::errorCode_t
264ProtocolException::getErrorCode() const throw()
265{
266 return errorCode;
267}
268
269//------------------------------------------------------------------------------
270
271inline bool ProtocolException::getParameter(long& param) const throw()
272{
273 if (hasParameter) param = parameter;
274 return hasParameter;
275}
276
277//------------------------------------------------------------------------------
278//------------------------------------------------------------------------------
279
280inline NotConnectedException::NotConnectedException() throw() :
281 Exception("xplra::NotConnectedException: not connected to the simulator")
282{
283}
284
285//------------------------------------------------------------------------------
286//------------------------------------------------------------------------------
287
288inline TypeMismatchException::TypeMismatchException() throw() :
289 Exception("xplra::TypeMismatchException")
290{
291}
292
293//------------------------------------------------------------------------------
294//------------------------------------------------------------------------------
295
296inline InvalidIDException::InvalidIDException() throw() :
297 Exception("xplra::InvalidIDException")
298{
299}
300
301//------------------------------------------------------------------------------
302
303} /* namespace hu::varadiistvan::xplra */ } /* namespace hu::varadiistvan */ } /* namespace hu */
304
305//------------------------------------------------------------------------------
306#endif // HU_VARADIISTVAN_XPLRA_EXCEPTION_H
307
308// Local Variables:
309// mode: C++
310// c-basic-offset: 4
311// indent-tabs-mode: nil
312// End:
Note: See TracBrowser for help on using the repository browser.