PCL
Exception.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.1.19
6 // ----------------------------------------------------------------------------
7 // pcl/Exception.h - Released 2019-11-07T10:59:34Z
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-2019 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 (http://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 
150  static bool IsConsoleOutputEnabled();
151 
158  static void EnableConsoleOutput( bool = true );
159 
166  static void DisableConsoleOutput( bool disable = true )
167  {
168  EnableConsoleOutput( !disable );
169  }
170 
180  static bool IsGUIOutputEnabled();
181 
188  static void EnableGUIOutput( bool = true );
189 
196  static void DisableGUIOutput( bool disable = true )
197  {
198  EnableGUIOutput( !disable );
199  }
200 };
201 
202 // ----------------------------------------------------------------------------
203 
213 class PCL_CLASS Error : public pcl::Exception
214 {
215 public:
216 
220  Error() = default;
221 
225  Error( const Error& ) = default;
226 
230  Error( const String& message ) : m_message( message )
231  {
232  }
233 
241  String Message() const override
242  {
243  return m_message;
244  }
245 
248  String Caption() const override
249  {
250  return "Error";
251  }
252 
253 protected:
254 
255  String m_message;
256 };
257 
258 // ----------------------------------------------------------------------------
259 
269 class PCL_CLASS FatalError : public Error
270 {
271 public:
272 
276  FatalError() = default;
277 
281  FatalError( const String& message ) : Error( message )
282  {
283  }
284 
288  FatalError( const pcl::Exception& x ) : Error( x.Message() )
289  {
290  }
291 
295  FatalError( const pcl::FatalError& ) = default;
296 
299  String Caption() const override
300  {
301  return "Fatal Error";
302  }
303 };
304 
305 // ----------------------------------------------------------------------------
306 
315 class PCL_CLASS NotImplemented : public Error
316 {
317 public:
318 
327  template <typename T>
328  NotImplemented( const T& object, const String& notImplemented ) :
329  Error( String( IsoString( typeid( object ).name() )
330 #ifdef __PCL_WINDOWS
331  .MBSToWCS()
332 #else
333  .UTF8ToUTF16()
334 #endif
335  ) + ": Not implemented: " + notImplemented )
336  {
337  }
338 
342  NotImplemented( const NotImplemented& ) = default;
343 };
344 
345 // ----------------------------------------------------------------------------
346 
359 class PCL_CLASS ParseError : public Error
360 {
361 public:
362 
367  ParseError() = default;
368 
372  ParseError( const ParseError& ) = default;
373 
391  ParseError( const String& message, const String& beingParsed = String(), int errorPosition = -1 ) :
392  Error( message ), m_beingParsed( beingParsed ), m_errorPosition( errorPosition )
393  {
394  }
395 
398  String Message() const override;
399 
402  void Show() const override;
403 
404 protected:
405 
406  String m_beingParsed;
407  int m_errorPosition = -1;
408 };
409 
410 // ----------------------------------------------------------------------------
411 
425 class PCL_CLASS SourceCodeError : public Error
426 {
427 public:
428 
433  SourceCodeError() = default;
434 
438  SourceCodeError( const SourceCodeError& ) = default;
439 
462  template <typename T1>
463  SourceCodeError( const String& message, T1 line = -1, T1 column = -1 ) :
464  Error( message ),
465  m_lineNumber( Range( int( line ), -1, int_max ) ),
466  m_columnNumber( Range( int( column ), -1, int_max ) )
467  {
468  }
469 
480  int LineNumber() const
481  {
482  return m_lineNumber;
483  }
484 
495  int ColumnNumber() const
496  {
497  return m_columnNumber;
498  }
499 
513  template <typename T1>
514  void SetPosition( T1 line, T1 column = -1 )
515  {
516  m_lineNumber = Range( int( line ), -1, int_max );
517  m_columnNumber = Range( int( column ), -1, int_max );
518  }
519 
533  template <typename T1>
534  void AddToPosition( T1 lines, T1 columns = 0 )
535  {
536  SetPosition( m_lineNumber+int( lines ), m_columnNumber+int( columns ) );
537  }
538 
541  String Message() const override;
542 
545  void Show() const override;
546 
547 protected:
548 
549  int m_lineNumber = -1;
550  int m_columnNumber = -1;
551 };
552 
553 // ----------------------------------------------------------------------------
554 
566 class PCL_CLASS CaughtException : public pcl::Exception
567 {
568 public:
569 
573  CaughtException() = default;
574 
578  CaughtException( const pcl::CaughtException& ) = default;
579 };
580 
581 } // pcl
582 
583 // ----------------------------------------------------------------------------
584 
604 #define PCL_DECLARE_EXCEPTION_CLASS( className, exceptionClass, message ) \
605  class PCL_CLASS className : public pcl::Exception \
606  { \
607  public: \
608  className() = default; \
609  className( const className& ) = default; \
610  pcl::String ExceptionClass() const override \
611  { \
612  return exceptionClass; \
613  } \
614  pcl::String Message() const override \
615  { \
616  return message; \
617  } \
618  }
619 
620 // ----------------------------------------------------------------------------
621 
622 namespace pcl
623 {
624 
634 PCL_DECLARE_EXCEPTION_CLASS( ProcessAborted, "Process aborted", String() );
635 
636 } // pcl
637 
638 #endif // __PCL_Exception_h
639 
640 // ----------------------------------------------------------------------------
641 // EOF pcl/Exception.h - Released 2019-11-07T10:59:34Z
Base class for exceptions thrown by source code interpreters.
Definition: Exception.h:425
String Message() const override
Definition: Exception.h:241
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:604
Base class for exceptions thrown by parsing routines.
Definition: Exception.h:359
void SetPosition(T1 line, T1 column=-1)
Definition: Exception.h:514
NotImplemented(const T &object, const String &notImplemented)
Definition: Exception.h:328
An exception that has already been handled.
Definition: Exception.h:566
An exception that indicates an unsupported feature.
Definition: Exception.h:315
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:166
Errors that cause immediate program termination.
Definition: Exception.h:269
Error(const String &message)
Definition: Exception.h:230
int LineNumber() const
Definition: Exception.h:480
void AddToPosition(T1 lines, T1 columns=0)
Definition: Exception.h:534
int ColumnNumber() const
Definition: Exception.h:495
String Caption() const override
Definition: Exception.h:248
Unicode (UTF-16) string.
Definition: String.h:7911
FatalError(const String &message)
Definition: Exception.h:281
static void DisableGUIOutput(bool disable=true)
Definition: Exception.h:196
virtual String Message() const
Definition: Exception.h:113
FatalError(const pcl::Exception &x)
Definition: Exception.h:288
ParseError(const String &message, const String &beingParsed=String(), int errorPosition=-1)
Definition: Exception.h:391
A simple exception with an associated error message.
Definition: Exception.h:213
virtual ~Exception()
Definition: Exception.h:98
String Caption() const override
Definition: Exception.h:299
SourceCodeError(const String &message, T1 line=-1, T1 column=-1)
Definition: Exception.h:463
virtual String ExceptionClass() const
Definition: Exception.h:105
Eight-bit string (ISO/IEC-8859-1 or UTF-8 string)
Definition: String.h:5387