PCL
Exception.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.4.1
6 // ----------------------------------------------------------------------------
7 // pcl/Exception.h - Released 2020-10-12T19:24:41Z
8 // ----------------------------------------------------------------------------
9 // This file is part of the PixInsight Class Library (PCL).
10 // PCL is a multiplatform C++ framework for development of PixInsight modules.
11 //
12 // Copyright (c) 2003-2020 Pleiades Astrophoto S.L. All Rights Reserved.
13 //
14 // Redistribution and use in both source and binary forms, with or without
15 // modification, is permitted provided that the following conditions are met:
16 //
17 // 1. All redistributions of source code must retain the above copyright
18 // notice, this list of conditions and the following disclaimer.
19 //
20 // 2. All redistributions in binary form must reproduce the above copyright
21 // notice, this list of conditions and the following disclaimer in the
22 // documentation and/or other materials provided with the distribution.
23 //
24 // 3. Neither the names "PixInsight" and "Pleiades Astrophoto", nor the names
25 // of their contributors, may be used to endorse or promote products derived
26 // from this software without specific prior written permission. For written
27 // permission, please contact info@pixinsight.com.
28 //
29 // 4. All products derived from this software, in any form whatsoever, must
30 // reproduce the following acknowledgment in the end-user documentation
31 // and/or other materials provided with the product:
32 //
33 // "This product is based on software from the PixInsight project, developed
34 // by Pleiades Astrophoto and its contributors (https://pixinsight.com/)."
35 //
36 // Alternatively, if that is where third-party acknowledgments normally
37 // appear, this acknowledgment must be reproduced in the product itself.
38 //
39 // THIS SOFTWARE IS PROVIDED BY PLEIADES ASTROPHOTO AND ITS CONTRIBUTORS
40 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
41 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PLEIADES ASTROPHOTO OR ITS
43 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
44 // EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, BUSINESS
45 // INTERRUPTION; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; AND LOSS OF USE,
46 // DATA OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49 // POSSIBILITY OF SUCH DAMAGE.
50 // ----------------------------------------------------------------------------
51 
52 #ifndef __PCL_Exception_h
53 #define __PCL_Exception_h
54 
56 
57 #include <pcl/Defs.h>
58 
59 #include <pcl/String.h>
60 
61 #include <typeinfo>
62 
63 namespace pcl
64 {
65 
66 // ----------------------------------------------------------------------------
67 
81 class PCL_CLASS Exception
82 {
83 public:
84 
88  Exception() = default;
89 
93  Exception( const pcl::Exception& ) = default;
94 
98  virtual ~Exception()
99  {
100  }
101 
105  virtual String ExceptionClass() const
106  {
107  return String();
108  }
109 
113  virtual String Message() const
114  {
115  return String();
116  }
117 
122  virtual String FormatInfo() const;
123 
128  virtual String Caption() const;
129 
139  virtual void Show() const;
140 
148  PCL_FORCE_INLINE void ShowOnConsole() const
149  {
150  /*
151  * N.B.: This function must always be expanded inline. Otherwise, neither
152  * UNIX synchronous signal handlers nor Win32 structured exception
153  * handlers can throw C++ exceptions.
154  */
155  bool wasConsoleOutput = IsConsoleOutputEnabled();
156  bool wasGUIOutput = IsGUIOutputEnabled();
157  EnableConsoleOutput();
158  DisableGUIOutput();
159 
160  Exception::Show();
161 
162  EnableConsoleOutput( wasConsoleOutput );
163  EnableGUIOutput( wasGUIOutput );
164  }
165 
175  static bool IsConsoleOutputEnabled();
176 
183  static void EnableConsoleOutput( bool = true );
184 
191  static void DisableConsoleOutput( bool disable = true )
192  {
193  EnableConsoleOutput( !disable );
194  }
195 
205  static bool IsGUIOutputEnabled();
206 
213  static void EnableGUIOutput( bool = true );
214 
221  static void DisableGUIOutput( bool disable = true )
222  {
223  EnableGUIOutput( !disable );
224  }
225 };
226 
227 // ----------------------------------------------------------------------------
228 
238 class PCL_CLASS Error : public pcl::Exception
239 {
240 public:
241 
245  Error() = default;
246 
250  Error( const Error& ) = default;
251 
255  Error( const String& message )
256  : m_message( message )
257  {
258  }
259 
267  String Message() const override
268  {
269  return m_message;
270  }
271 
274  String Caption() const override
275  {
276  return "Error";
277  }
278 
279 protected:
280 
281  String m_message;
282 };
283 
284 // ----------------------------------------------------------------------------
285 
295 class PCL_CLASS FatalError : public Error
296 {
297 public:
298 
302  FatalError() = default;
303 
307  FatalError( const String& message )
308  : Error( message )
309  {
310  }
311 
316  : Error( x.Message() )
317  {
318  }
319 
323  FatalError( const pcl::FatalError& ) = default;
324 
327  String Caption() const override
328  {
329  return "Fatal Error";
330  }
331 };
332 
333 // ----------------------------------------------------------------------------
334 
343 class PCL_CLASS NotImplemented : public Error
344 {
345 public:
346 
355  template <typename T>
356  NotImplemented( const T& object, const String& notImplemented )
357  : Error( String( IsoString( typeid( object ).name() )
358 #ifdef __PCL_WINDOWS
359  .MBSToWCS()
360 #else
361  .UTF8ToUTF16()
362 #endif
363  ) + ": Not implemented: " + notImplemented )
364  {
365  }
366 
370  NotImplemented( const NotImplemented& ) = default;
371 };
372 
373 // ----------------------------------------------------------------------------
374 
387 class PCL_CLASS ParseError : public Error
388 {
389 public:
390 
395  ParseError() = default;
396 
400  ParseError( const ParseError& ) = default;
401 
419  ParseError( const String& message, const String& beingParsed = String(), int errorPosition = -1 )
420  : Error( message )
421  , m_beingParsed( beingParsed )
422  , m_errorPosition( errorPosition )
423  {
424  }
425 
428  String Message() const override;
429 
432  void Show() const override;
433 
434 protected:
435 
436  String m_beingParsed;
437  int m_errorPosition = -1;
438 };
439 
440 // ----------------------------------------------------------------------------
441 
455 class PCL_CLASS SourceCodeError : public Error
456 {
457 public:
458 
463  SourceCodeError() = default;
464 
468  SourceCodeError( const SourceCodeError& ) = default;
469 
492  template <typename T1>
493  SourceCodeError( const String& message, T1 line = -1, T1 column = -1 )
494  : Error( message )
495  , m_lineNumber( Range( int( line ), -1, int_max ) )
496  , m_columnNumber( Range( int( column ), -1, int_max ) )
497  {
498  }
499 
510  int LineNumber() const
511  {
512  return m_lineNumber;
513  }
514 
525  int ColumnNumber() const
526  {
527  return m_columnNumber;
528  }
529 
543  template <typename T1>
544  void SetPosition( T1 line, T1 column = -1 )
545  {
546  m_lineNumber = Range( int( line ), -1, int_max );
547  m_columnNumber = Range( int( column ), -1, int_max );
548  }
549 
563  template <typename T1>
564  void AddToPosition( T1 lines, T1 columns = 0 )
565  {
566  SetPosition( m_lineNumber+int( lines ), m_columnNumber+int( columns ) );
567  }
568 
571  String Message() const override;
572 
575  void Show() const override;
576 
577 protected:
578 
579  int m_lineNumber = -1;
580  int m_columnNumber = -1;
581 };
582 
583 // ----------------------------------------------------------------------------
584 
596 class PCL_CLASS CaughtException : public pcl::Exception
597 {
598 public:
599 
603  CaughtException() = default;
604 
608  CaughtException( const pcl::CaughtException& ) = default;
609 };
610 
611 } // pcl
612 
613 // ----------------------------------------------------------------------------
614 
634 #define PCL_DECLARE_EXCEPTION_CLASS( className, exceptionClass, message ) \
635  class PCL_CLASS className : public pcl::Exception \
636  { \
637  public: \
638  className() = default; \
639  className( const className& ) = default; \
640  pcl::String ExceptionClass() const override \
641  { \
642  return exceptionClass; \
643  } \
644  pcl::String Message() const override \
645  { \
646  return message; \
647  } \
648  }
649 
650 // ----------------------------------------------------------------------------
651 
652 namespace pcl
653 {
654 
664 PCL_DECLARE_EXCEPTION_CLASS( ProcessAborted, "Process aborted", String() );
665 
666 } // pcl
667 
668 #endif // __PCL_Exception_h
669 
670 // ----------------------------------------------------------------------------
671 // EOF pcl/Exception.h - Released 2020-10-12T19:24:41Z
Base class for exceptions thrown by source code interpreters.
Definition: Exception.h:455
String Message() const override
Definition: Exception.h:267
Root base class for all PCL exception classes.
Definition: Exception.h:81
PCL root namespace.
Definition: AbstractImage.h:76
#define PCL_DECLARE_EXCEPTION_CLASS(className, exceptionClass, message)
A macro to implement simple exception classes derived from Exception.
Definition: Exception.h:634
Base class for exceptions thrown by parsing routines.
Definition: Exception.h:387
void SetPosition(T1 line, T1 column=-1)
Definition: Exception.h:544
NotImplemented(const T &object, const String &notImplemented)
Definition: Exception.h:356
An exception that has already been handled.
Definition: Exception.h:596
An exception that indicates an unsupported feature.
Definition: Exception.h:343
virtual void Show() const
constexpr const T & Range(const T &x, const T &a, const T &b)
Definition: Utility.h:190
static void DisableConsoleOutput(bool disable=true)
Definition: Exception.h:191
Errors that cause immediate program termination.
Definition: Exception.h:295
Error(const String &message)
Definition: Exception.h:255
int LineNumber() const
Definition: Exception.h:510
void AddToPosition(T1 lines, T1 columns=0)
Definition: Exception.h:564
int ColumnNumber() const
Definition: Exception.h:525
String Caption() const override
Definition: Exception.h:274
Unicode (UTF-16) string.
Definition: String.h:7916
FatalError(const String &message)
Definition: Exception.h:307
static void DisableGUIOutput(bool disable=true)
Definition: Exception.h:221
virtual String Message() const
Definition: Exception.h:113
FatalError(const pcl::Exception &x)
Definition: Exception.h:315
ParseError(const String &message, const String &beingParsed=String(), int errorPosition=-1)
Definition: Exception.h:419
A simple exception with an associated error message.
Definition: Exception.h:238
virtual ~Exception()
Definition: Exception.h:98
String Caption() const override
Definition: Exception.h:327
SourceCodeError(const String &message, T1 line=-1, T1 column=-1)
Definition: Exception.h:493
virtual String ExceptionClass() const
Definition: Exception.h:105
Eight-bit string (ISO/IEC-8859-1 or UTF-8 string)
Definition: String.h:5390