Exception.hpp
Go to the documentation of this file.
1 //==============================================================================
2 //
3 // This file is part of GNSSTk, the ARL:UT GNSS Toolkit.
4 //
5 // The GNSSTk is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published
7 // by the Free Software Foundation; either version 3.0 of the License, or
8 // any later version.
9 //
10 // The GNSSTk is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with GNSSTk; if not, write to the Free Software Foundation,
17 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
18 //
19 // This software was developed by Applied Research Laboratories at the
20 // University of Texas at Austin.
21 // Copyright 2004-2022, The Board of Regents of The University of Texas System
22 //
23 //==============================================================================
24 
25 //==============================================================================
26 //
27 // This software was developed by Applied Research Laboratories at the
28 // University of Texas at Austin, under contract to an agency or agencies
29 // within the U.S. Department of Defense. The U.S. Government retains all
30 // rights to use, duplicate, distribute, disclose, or release this software.
31 //
32 // Pursuant to DoD Directive 523024
33 //
34 // DISTRIBUTION STATEMENT A: This software has been approved for public
35 // release, distribution is unlimited.
36 //
37 //==============================================================================
38 
44 // The unusual include macro below is done this way because xerces
45 // #defines EXCEPTION_HPP in their own exception class header file.
46 #ifndef GNSSTK_EXCEPTION_HPP
47 #define GNSSTK_EXCEPTION_HPP
48 
49 #include <cstdlib>
50 #include <iostream>
51 #include <vector>
52 #include <string>
53 
54 namespace gnsstk
55 {
67  class ExceptionLocation
70  {
71  public:
78  ExceptionLocation(const std::string& filename = std::string(),
79  const std::string& funcName = std::string(),
80  const unsigned long& lineNum = 0)
81  : fileName(filename), functionName(funcName),
82  lineNumber(lineNum)
83  { }
84 
89 
91  std::string getFileName() const
92  { return fileName; }
94  std::string getFunctionName() const
95  { return functionName; }
97  unsigned long getLineNumber() const
98  { return lineNumber; }
99 
105  void dump(std::ostream& s) const;
106 
108  std::string what() const;
109 
119  friend std::ostream& operator<<( std::ostream& s,
120  const ExceptionLocation& e );
121 
122  private:
124  std::string fileName;
126  std::string functionName;
128  unsigned long lineNumber;
129  }; // class ExceptionLocation
130 
151  class Exception
152  {
153  public:
155  enum Severity
156  {
159  };
160 
165  Exception();
166 
174  Exception(const std::string& errorText,
175  const unsigned long& errorId = 0,
176  const Severity& severity = unrecoverable);
177 
178 
186  Exception(const char* errorText,
187  const unsigned long& errorId = 0,
188  const Severity& severity = unrecoverable);
189 
190 
192  Exception(const Exception& exception);
193 
196  {};
197 
199  Exception& operator=(const Exception& e);
200 
208  void terminate()
209  { exit(1); };
210 
212  unsigned long getErrorId() const
213  { return errorId; };
214 
220  Exception& setErrorId(const unsigned long& errId)
221  { errorId = errId; return *this; };
222 
235  Exception& addLocation(const ExceptionLocation& location);
236 
243  const ExceptionLocation getLocation(const size_t& index=0) const;
244 
247  size_t getLocationCount() const;
248 
254  bool isRecoverable() const
255  { return (severity == recoverable); }
256 
263  { severity = sever; return *this; };
264 
270  Exception& addText(const std::string& errorText);
271 
280  std::string getText(const size_t& index=0) const;
281 
283  size_t getTextCount() const;
284 
286  std::string getName() const
287  { return "Exception"; };
288 
294  void dump(std::ostream& s) const;
295 
297  std::string what() const;
298 
307  friend std::ostream& operator<<( std::ostream& s,
308  const Exception& e );
309 
310  protected:
312  unsigned long errorId;
314  std::vector<ExceptionLocation> locations;
318  std::vector<std::string> text;
319 
328  int overflow(int c);
329 
330  private:
332  std::string streamBuffer;
333  }; // class Exception
334 
335 
336 } // namespace gnsstk
337 
338 
349 #if defined ( __FUNCTION__ )
350 #define FILE_LOCATION gnsstk::ExceptionLocation(__FILE__, __FUNCTION__, __LINE__)
351 #else
352 #define FILE_LOCATION gnsstk::ExceptionLocation(__FILE__, "", __LINE__)
353 #endif
354 
355 // For compilers without exceptions, die if you get an exception.
356 #if defined (NO_EXCEPTIONS_SUPPORT)
357 #define GNSSTK_THROW(exc) { exc.addLocation(FILE_LOCATION); exc.terminate(); }
360 #define GNSSTK_RETHROW(exc) { exc.addLocation(FILE_LOCATION); exc.terminate(); }
363 #else
364 #define GNSSTK_THROW(exc) { exc.addLocation(FILE_LOCATION); throw exc; }
367 #define GNSSTK_RETHROW(exc) { exc.addLocation(FILE_LOCATION); throw; }
370 #endif
371 
373 #define GNSSTK_ASSERT(CONDITION) if (!(CONDITION)) { \
374  gnsstk::AssertionFailure exc("Assertion failed: " #CONDITION); \
375  GNSSTK_THROW(exc); \
376  }
377 
378 
388 #define NEW_EXCEPTION_CLASS(child, parent) \
389 class child : public parent \
390 { \
391 public: \
392  \
393  child() : parent() {} \
394  \
395  child(const child& a): parent(a) {} \
396  \
397  child(const gnsstk::Exception& a) : parent(a) {}; \
398  \
404  child(const std::string& a, unsigned long b = 0,\
405  gnsstk::Exception::Severity c = gnsstk::Exception::unrecoverable) \
406  : parent(a, b, c) \
407  {};\
408  \
414  child(const char* a, unsigned long b = 0,\
415  gnsstk::Exception::Severity c = gnsstk::Exception::unrecoverable) \
416  : parent(a, b, c) \
417  {};\
418  \
419  ~child() {} \
420  \
421  std::string getName() const {return ( # child);} \
422  \
423  child& operator=(const child& kid) \
424  { parent::operator=(kid); return *this; } \
425  \
426  friend std::ostream& operator<<(std::ostream& s, const child& c) \
427  { c.dump(s); return s; } \
428 }
429 
430 namespace gnsstk
431 {
434  NEW_EXCEPTION_CLASS(InvalidParameter, Exception);
435 
438  NEW_EXCEPTION_CLASS(InvalidRequest, Exception);
439 
442  NEW_EXCEPTION_CLASS(AssertionFailure, Exception);
443 
446  NEW_EXCEPTION_CLASS(AccessError, Exception);
447 
450  NEW_EXCEPTION_CLASS(IndexOutOfBoundsException, Exception);
451 
454  NEW_EXCEPTION_CLASS(InvalidArgumentException, Exception);
455 
458  NEW_EXCEPTION_CLASS(ConfigurationException, Exception);
459 
462  NEW_EXCEPTION_CLASS(FileMissingException, Exception);
463 
466  NEW_EXCEPTION_CLASS(SystemSemaphoreException, Exception);
467 
470  NEW_EXCEPTION_CLASS(SystemPipeException, Exception);
471 
474  NEW_EXCEPTION_CLASS(SystemQueueException, Exception);
475 
478  NEW_EXCEPTION_CLASS(OutOfMemory, Exception);
479 
482  NEW_EXCEPTION_CLASS(ObjectNotFound, AccessError);
483 
486  NEW_EXCEPTION_CLASS(NullPointerException, Exception);
487 
490  NEW_EXCEPTION_CLASS(UnimplementedException, Exception);
491 
496  NEW_EXCEPTION_CLASS(FFStreamError, gnsstk::Exception);
497 
500  NEW_EXCEPTION_CLASS(EndOfFile, gnsstk::FFStreamError);
501 
503  class StopIterator {};
504 
505 } // namespace gnsstk
506 #endif
gnsstk::Exception::setSeverity
Exception & setSeverity(const Severity &sever)
Definition: Exception.hpp:262
gnsstk::Exception::overflow
int overflow(int c)
Definition: Exception.cpp:170
gnsstk::Exception::isRecoverable
bool isRecoverable() const
Definition: Exception.hpp:254
gnsstk::Exception::errorId
unsigned long errorId
Error code.
Definition: Exception.hpp:312
gnsstk::Exception::operator=
Exception & operator=(const Exception &e)
Assignment operator.
Definition: Exception.cpp:95
gnsstk::ExceptionLocation::dump
void dump(std::ostream &s) const
Definition: Exception.cpp:56
gnsstk::Exception::locations
std::vector< ExceptionLocation > locations
Stack of exception locations (where it was thrown).
Definition: Exception.hpp:314
gnsstk::Exception::Exception
Exception()
Definition: Exception.cpp:65
gnsstk::Exception::what
std::string what() const
Dump to a string.
Definition: Exception.cpp:193
gnsstk::ExceptionLocation::ExceptionLocation
ExceptionLocation(const std::string &filename=std::string(), const std::string &funcName=std::string(), const unsigned long &lineNum=0)
Definition: Exception.hpp:78
gnsstk::Exception::getTextCount
size_t getTextCount() const
Returns the number of text strings in the exception text stack.
Definition: Exception.cpp:152
gnsstk::ExceptionLocation::getLineNumber
unsigned long getLineNumber() const
Accessor for line of source file where exception occurred.
Definition: Exception.hpp:97
gnsstk::Exception::addLocation
Exception & addLocation(const ExceptionLocation &location)
Definition: Exception.cpp:108
gnsstk
For Sinex::InputHistory.
Definition: BasicFramework.cpp:50
gnsstk::ExceptionLocation::lineNumber
unsigned long lineNumber
Line in source file where exception occurred.
Definition: Exception.hpp:128
gnsstk::Exception
Definition: Exception.hpp:151
gnsstk::NEW_EXCEPTION_CLASS
NEW_EXCEPTION_CLASS(FileSpecException, gnsstk::Exception)
gnsstk::Exception::getText
std::string getText(const size_t &index=0) const
Definition: Exception.cpp:139
gnsstk::Exception::getLocationCount
size_t getLocationCount() const
Definition: Exception.cpp:128
gnsstk::Exception::getName
std::string getName() const
Returns the name of the object's class.
Definition: Exception.hpp:286
gnsstk::ExceptionLocation::fileName
std::string fileName
Name of source file where exception occurred.
Definition: Exception.hpp:124
gnsstk::Exception::Severity
Severity
Exception severity classes.
Definition: Exception.hpp:155
gnsstk::Exception::terminate
void terminate()
Definition: Exception.hpp:208
gnsstk::Exception::~Exception
~Exception()
Destructor.
Definition: Exception.hpp:195
gnsstk::Exception::setErrorId
Exception & setErrorId(const unsigned long &errId)
Definition: Exception.hpp:220
gnsstk::Exception::recoverable
@ recoverable
Definition: Exception.hpp:158
gnsstk::Exception::dump
void dump(std::ostream &s) const
Definition: Exception.cpp:157
gnsstk::Exception::getErrorId
unsigned long getErrorId() const
Returns the error ID of the exception.
Definition: Exception.hpp:212
gnsstk::Exception::unrecoverable
@ unrecoverable
Definition: Exception.hpp:157
gnsstk::ExceptionLocation::what
std::string what() const
Dump to a string.
Definition: Exception.cpp:186
gnsstk::Exception::getLocation
const ExceptionLocation getLocation(const size_t &index=0) const
Definition: Exception.cpp:115
gnsstk::Exception::addText
Exception & addText(const std::string &errorText)
Definition: Exception.cpp:133
gnsstk::ExceptionLocation::getFileName
std::string getFileName() const
Accessor for name of source file where exception occurred.
Definition: Exception.hpp:91
gnsstk::Exception::streamBuffer
std::string streamBuffer
Buffer for stream output.
Definition: Exception.hpp:332
gnsstk::Exception::severity
Severity severity
Severity of exception.
Definition: Exception.hpp:316
gnsstk::ExceptionLocation::operator<<
friend std::ostream & operator<<(std::ostream &s, const ExceptionLocation &e)
gnsstk::Exception::text
std::vector< std::string > text
Text stack describing exception condition.
Definition: Exception.hpp:318
gnsstk::ExceptionLocation
Definition: Exception.hpp:69
gnsstk::ExceptionLocation::getFunctionName
std::string getFunctionName() const
Accessor for name of function where exception occurred.
Definition: Exception.hpp:94
gnsstk::ExceptionLocation::functionName
std::string functionName
Name of function where exception occurred.
Definition: Exception.hpp:126
gnsstk::Exception::operator<<
friend std::ostream & operator<<(std::ostream &s, const Exception &e)
gnsstk::ExceptionLocation::~ExceptionLocation
~ExceptionLocation()
Definition: Exception.hpp:88


gnsstk
Author(s):
autogenerated on Wed Oct 25 2023 02:40:39