libyui
YUIException.h
1 /*
2  Copyright (C) 2000-2012 Novell, Inc
3  This library is free software; you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as
5  published by the Free Software Foundation; either version 2.1 of the
6  License, or (at your option) version 3.0 of the License. This library
7  is distributed in the hope that it will be useful, but WITHOUT ANY
8  WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  License for more details. You should have received a copy of the GNU
11  Lesser General Public License along with this library; if not, write
12  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13  Floor, Boston, MA 02110-1301 USA
14 */
15 
16 
17 /*-/
18 
19  File: YUIException.h
20 
21  Stolen from zypp/libzypp/base/Exception.h
22 
23  Author: Michael Andres <ma@suse.de>
24  Maintainer: Stefan Hundhammer <shundhammer@suse.de>
25 
26 /-*/
27 
28 #ifndef YUIException_h
29 #define YUIException_h
30 
31 
32 #include <cerrno>
33 #include <iostream>
34 #include <stdexcept>
35 
36 #include "YProperty.h"
37 
38 
39 class YWidget;
40 
41 
42 //
43 // Macros for application use
44 //
45 
84 #define YUI_EXCEPTION_CODE_LOCATION YCodeLocation(__FILE__,__FUNCTION__,__LINE__)
85 
86 
90 #define YUI_THROW( EXCEPTION ) \
91  _YUI_THROW( ( EXCEPTION ), YUI_EXCEPTION_CODE_LOCATION )
92 
96 #define YUI_CAUGHT( EXCEPTION ) \
97  _YUI_CAUGHT( ( EXCEPTION ), YUI_EXCEPTION_CODE_LOCATION )
98 
99 
103 #define YUI_RETHROW( EXCEPTION ) \
104  _YUI_RETHROW( ( EXCEPTION ), YUI_EXCEPTION_CODE_LOCATION )
105 
106 
110 #define YUI_THROW_MSG( EXCEPTION_TYPE, MSG ) \
111  YUI_THROW( EXCEPTION_TYPE( MSG ) )
112 
113 
117 #define YUI_THROW_ERRNO( EXCEPTION_TYPE ) \
118  YUI_THROW( EXCEPTION_TYPE( YUIException::strErrno( errno ) ) )
119 
123 #define YUI_THROW_ERRNO1( EXCEPTION_TYPE, ERRNO ) \
124  YUI_THROW( EXCEPTION_TYPE( YUIException::strErrno( ERRNO ) ) )
125 
129 #define YUI_THROW_ERRNO_MSG( EXCEPTION_TYPE, MSG) \
130  YUI_THROW( EXCEPTION_TYPE( YUIException::strErrno( errno, MSG ) ) )
131 
135 #define YUI_THROW_ERRNO_MSG1( EXCEPTION_TYPE, ERRNO,MSG ) \
136  YUI_THROW( EXCEPTION_TYPE( YUIException::strErrno( ERRNO, MSG ) ) )
137 
138 
139 //
140 // Higher-level (UI specific) exception macros
141 //
142 
147 #define YUI_CHECK_NEW( PTR ) \
148  do \
149  { \
150  if ( ! (PTR) ) \
151  { \
152  YUI_THROW( YUIOutOfMemoryException() ); \
153  } \
154  } while( 0 )
155 
156 
157 
162 #define YUI_CHECK_PTR( PTR ) \
163  do \
164  { \
165  if ( ! (PTR) ) \
166  { \
167  YUI_THROW( YUINullPointerException() ); \
168  } \
169  } while( 0 )
170 
171 
191 #define YUI_CHECK_WIDGET( WIDGET ) \
192  do \
193  { \
194  if ( ! ( static_cast<bool> (WIDGET) ) || \
195  ! (WIDGET)->isValid() ) \
196  { \
197  YUI_THROW( YUIInvalidWidgetException() ); \
198  } \
199  } while( 0 )
200 
201 
208 #define YUI_CHECK_INDEX_MSG( INDEX, VALID_MIN, VALID_MAX, MSG ) \
209  do \
210  { \
211  if ( (INDEX) < (VALID_MIN) || \
212  (INDEX) > (VALID_MAX) ) \
213  { \
214  YUI_THROW( YUIIndexOutOfRangeException( (INDEX), (VALID_MIN), (VALID_MAX), (MSG) ) ); \
215  } \
216  } while( 0 )
217 
218 
219 #define YUI_CHECK_INDEX( INDEX, VALID_MIN, VALID_MAX ) \
220  YUI_CHECK_INDEX_MSG( (INDEX), (VALID_MIN), (VALID_MAX), "")
221 
222 
223 
224 
230 {
231 public:
236  YCodeLocation( const std::string & file_r,
237  const std::string & func_r,
238  int line_r )
239  : _file( file_r )
240  , _func( func_r )
241  , _line( line_r )
242  {}
243 
248  : _line( 0 )
249  {}
250 
254  std::string file() const { return _file; }
255 
259  std::string func() const { return _func; }
260 
264  int line() const { return _line; }
265 
269  std::string asString() const;
270 
274  friend std::ostream & operator<<( std::ostream & str, const YCodeLocation & obj );
275 
276 private:
277  std::string _file;
278  std::string _func;
279  int _line;
280 
281 }; // YCodeLocation
282 
283 
287 std::ostream & operator<<( std::ostream & str, const YCodeLocation & obj );
288 
289 
297 class YUIException : public std::exception
298 {
299 public:
304  YUIException();
305 
310  YUIException( const std::string & msg_r );
311 
315  virtual ~YUIException() throw();
316 
320  const YCodeLocation & where() const
321  { return _where; }
322 
326  void relocate( const YCodeLocation & newLocation ) const
327  { _where = newLocation; }
328 
334  const std::string & msg() const
335  { return _msg; }
336 
340  void setMsg( const std::string & msg )
341  { _msg = msg; }
342 
346  std::string asString() const;
347 
351  static std::string strErrno( int errno_r );
352 
356  static std::string strErrno( int errno_r, const std::string & msg );
357 
362  static void log( const YUIException & exception,
363  const YCodeLocation & location,
364  const char * const prefix );
370  virtual const char * what() const throw()
371  { return _msg.c_str(); }
372 
373 protected:
374 
378  virtual std::ostream & dumpOn( std::ostream & str ) const;
379 
380 
381 private:
382  friend std::ostream & operator<<( std::ostream & str, const YUIException & obj );
383 
384 
385  mutable YCodeLocation _where;
386  std::string _msg;
387 
392  std::ostream & dumpError( std::ostream & str ) const;
393 
394 }; // class YUIException
395 
396 
400 std::ostream & operator<<( std::ostream & str, const YUIException & obj );
401 
402 
408 {
409 public:
411  : YUIException( "Null pointer" )
412  {}
413 
414  virtual ~YUINullPointerException() throw()
415  {}
416 };
417 
418 
424 {
425 public:
427  : YUIException( "Out of memory" )
428  {}
429 
430  virtual ~YUIOutOfMemoryException() throw()
431  {}
432 
433 };
434 
441 {
442 public:
444  : YUIException( "Invalid widget" )
445  {}
446 
447  virtual ~YUIInvalidWidgetException() throw()
448  {}
449 };
450 
451 
456 {
457 public:
458  YUIWidgetNotFoundException( const std::string & idString )
459  : YUIException( std::string( "No widget with ID " ) + idString )
460  {}
461 
462  virtual ~YUIWidgetNotFoundException() throw()
463  {}
464 };
465 
466 
468 {
469 public:
471  : YUIException( "No dialog existing" )
472  {}
473 
474  virtual ~YUINoDialogException() throw()
475  {}
476 };
477 
478 
480 {
481 public:
483  : YUIException( "Dialog stacking order violated" )
484  {}
485 
486  virtual ~YUIDialogStackingOrderException() throw()
487  {}
488 };
489 
490 
492 {
493 public:
494  YUISyntaxErrorException( const std::string & msg )
495  : YUIException( msg )
496  {}
497 
498  virtual ~YUISyntaxErrorException() throw()
499  {}
500 };
501 
502 
507 {
508 protected:
509  YUIPropertyException( const YProperty & prop,
510  YWidget * widget = 0 )
511  : YUIException()
512  , _property( prop )
513  , _widget( widget )
514  {}
515 
516  virtual ~YUIPropertyException() throw()
517  {}
518 
519 public:
523  YProperty property() const { return _property; }
524 
528  YWidget * widget() const { return _widget; }
529 
533  void setWidget( YWidget * w ) { _widget = w; }
534 
535 protected:
536 
541  virtual std::ostream & dumpOn( std::ostream & str ) const = 0;
542 
543 private:
544  YProperty _property;
545  YWidget * _widget;
546 };
547 
548 
554 {
555 public:
556  YUIUnknownPropertyException( const std::string & propertyName,
557  YWidget * widget = 0 )
558  : YUIPropertyException( YProperty( propertyName, YUnknownPropertyType ), widget )
559  {}
560 
561  virtual ~YUIUnknownPropertyException() throw()
562  {}
563 
564 protected:
565 
570  virtual std::ostream & dumpOn( std::ostream & str ) const;
571 };
572 
573 
579 {
580 public:
581 
583  YPropertyType type,
584  YWidget * widget = 0 )
586  , _type( type )
587  {}
588 
589  virtual ~YUIPropertyTypeMismatchException() throw()
590  {}
591 
595  YPropertyType type() const { return _type; }
596 
597 protected:
598 
603  virtual std::ostream & dumpOn( std::ostream & str ) const;
604 
605 private:
606  YPropertyType _type;
607 };
608 
609 
614 {
615 public:
616 
618  YWidget * widget = 0 )
620  {}
621 
622  virtual ~YUISetReadOnlyPropertyException() throw()
623  {}
624 
625 protected:
626 
631  virtual std::ostream & dumpOn( std::ostream & str ) const;
632 };
633 
634 
636 {
637 public:
638 
640  YWidget * widget,
641  const std::string & message = "" )
643  { setMsg( message ); }
644 
645  virtual ~YUIBadPropertyArgException() throw()
646  {}
647 
648 protected:
649 
654  virtual std::ostream & dumpOn( std::ostream & str ) const;
655 };
656 
657 
663 template<class YWidget> class YUITooManyChildrenException: public YUIException
664 {
665 public:
666 
668  : YUIException( "Too many children" )
669  , _container( container )
670  {}
671 
672  virtual ~YUITooManyChildrenException() throw()
673  {}
674 
678  YWidget * container() const { return _container; }
679 
680 protected:
681 
686  virtual std::ostream & dumpOn( std::ostream & str ) const
687  {
688  std::string widgetClass =
689  container() ? container()->widgetClass() :
690  "widget";
691 
692  return str << "Too many children for "
693  << widgetClass
694  << std::endl;
695  }
696 
697 private:
698 
699  YWidget * _container;
700 };
701 
702 
712 template<class YWidget> class YUIInvalidChildException: public YUIException
713 {
714 public:
715 
717  YWidget * child = 0 )
718  : YUIException( "Invalid child" )
719  , _container( container )
720  , _child( child )
721  {}
722 
723  virtual ~YUIInvalidChildException() throw()
724  {}
725 
729  YWidget * container() const { return _container; }
730 
734  YWidget * child() const { return _child; }
735 
736 protected:
737 
742  virtual std::ostream & dumpOn( std::ostream & str ) const
743  {
744  std::string containerWidgetClass =
745  container() ? container()->widgetClass() :
746  "widget";
747 
748  std::string childWidgetClass =
749  child() ? child()->widgetClass() :
750  "<Null>";
751 
752  return str << childWidgetClass
753  << " is not a child of "
754  << containerWidgetClass
755  << std::endl;
756  }
757 
758 private:
759 
760  YWidget * _container;
761  YWidget * _child;
762 };
763 
764 
765 
776 {
777 public:
778 
779  YUIUnsupportedWidgetException( const std::string & widgetType )
780  : YUIException( std::string( "Unsupported optional widget type: " ) + widgetType )
781  {}
782 
783  virtual ~YUIUnsupportedWidgetException() throw()
784  {}
785 };
786 
787 
793 {
794 public:
796  : YUIException( "Invalid dimension (neither YD_HORIZ nor YD_VERT)" )
797  {}
798 
799  virtual ~YUIInvalidDimensionException() throw()
800  {}
801 };
802 
803 
808 {
809 public:
819  int validMin,
820  int validMax,
821  const std::string & msg = "" )
822  : YUIException( msg )
823  , _invalidIndex( invalidIndex )
824  , _validMin( validMin )
825  , _validMax( validMax )
826  {}
827 
828  virtual ~YUIIndexOutOfRangeException() throw()
829  {}
830 
834  int invalidIndex() const { return _invalidIndex; }
835 
839  int validMin() const { return _validMin; }
840 
844  int validMax() const { return _validMax; }
845 
846 protected:
847 
852  virtual std::ostream & dumpOn( std::ostream & str ) const
853  {
854  std::string prefix = msg();
855 
856  if ( prefix.empty() )
857  prefix = "Index out of range";
858 
859  return str << prefix << ": " << _invalidIndex
860  << " valid: " << _validMin << " .. " << _validMax
861  << std::endl;
862  }
863 
864 private:
865 
866  int _invalidIndex;
867  int _validMin;
868  int _validMax;
869 };
870 
871 
876 {
877 public:
878  YUIPluginException( const std::string & pluginName )
879  : YUIException( std::string( "Couldn't load plug-in " ) + pluginName )
880  {}
881 
882  virtual ~YUIPluginException() throw()
883  {}
884 };
885 
886 
891 {
892 public:
894  : YUIException( "No $DISPLAY and stdout is not a tty" )
895  {}
896 
897  virtual ~YUICantLoadAnyUIException() throw()
898  {}
899 };
900 
901 
906 {
907 public:
908 
909  YUIButtonRoleMismatchException( const std::string & msg )
910  : YUIException( msg )
911  {}
912 
913  virtual ~YUIButtonRoleMismatchException() throw()
914  {}
915 };
916 
917 
918 //
919 // Helper templates
920 //
921 
922 
926 template<class _Exception>
927 void _YUI_THROW( const _Exception & exception_r, const YCodeLocation & where_r )
928 {
929  exception_r.relocate( where_r );
930  YUIException::log( exception_r, where_r, "THROW: " );
931  throw( exception_r );
932 }
933 
934 
938 template<class _Exception>
939 void _YUI_CAUGHT( const _Exception & exception_r, const YCodeLocation & where_r )
940 {
941  YUIException::log( exception_r, where_r, "CAUGHT: " );
942 }
943 
944 
948 template<class _Exception>
949 void _YUI_RETHROW( const _Exception & exception_r, const YCodeLocation & where_r )
950 {
951  YUIException::log( exception_r, where_r, "RETHROW: " );
952  exception_r.relocate( where_r );
953  throw;
954 }
955 
956 
957 #endif // YUIException_h
virtual std::ostream & dumpOn(std::ostream &str) const
Definition: YUIException.cc:162
YProperty property() const
Definition: YUIException.h:523
virtual ~YUIException()
Definition: YUIException.cc:75
virtual std::ostream & dumpOn(std::ostream &str) const =0
std::string asString() const
Definition: YUIException.cc:82
YPropertyType type() const
Definition: YUIException.h:595
Definition: YUIException.h:807
void relocate(const YCodeLocation &newLocation) const
Definition: YUIException.h:326
Definition: YUIException.h:613
void setMsg(const std::string &msg)
Definition: YUIException.h:340
Definition: YUIException.h:663
virtual const char * widgetClass() const
Definition: YWidget.h:72
YWidget * child() const
Definition: YUIException.h:734
YWidget * container() const
Definition: YUIException.h:678
int line() const
Definition: YUIException.h:264
virtual std::ostream & dumpOn(std::ostream &str) const
Definition: YUIException.cc:141
std::string asString() const
Definition: YUIException.cc:42
int validMax() const
Definition: YUIException.h:844
void setWidget(YWidget *w)
Definition: YUIException.h:533
YCodeLocation(const std::string &file_r, const std::string &func_r, int line_r)
Definition: YUIException.h:236
virtual std::ostream & dumpOn(std::ostream &str) const
Definition: YUIException.h:852
static std::string strErrno(int errno_r)
Definition: YUIException.cc:112
Definition: YUIException.h:905
friend std::ostream & operator<<(std::ostream &str, const YCodeLocation &obj)
Definition: YUIException.h:792
friend std::ostream & operator<<(std::ostream &str, const YUIException &obj)
YWidget * container() const
Definition: YUIException.h:729
Definition: YUIException.h:467
std::string func() const
Definition: YUIException.h:259
virtual const char * what() const
Definition: YUIException.h:370
std::string file() const
Definition: YUIException.h:254
YUIIndexOutOfRangeException(int invalidIndex, int validMin, int validMax, const std::string &msg="")
Definition: YUIException.h:818
virtual std::ostream & dumpOn(std::ostream &str) const
Definition: YUIException.h:742
Definition: YUIException.h:491
Definition: YUIException.h:440
Definition: YUIException.h:875
int validMin() const
Definition: YUIException.h:839
Definition: YUIException.h:423
Definition: YUIException.h:635
Definition: YUIException.h:775
Definition: YUIException.h:455
virtual std::ostream & dumpOn(std::ostream &str) const
Definition: YUIException.cc:181
Definition: YUIException.h:553
YCodeLocation()
Definition: YUIException.h:247
YUIException()
Definition: YUIException.cc:63
Definition: YProperty.h:51
Definition: YUIException.h:229
const std::string & msg() const
Definition: YUIException.h:334
virtual std::ostream & dumpOn(std::ostream &str) const
Definition: YUIException.cc:197
Definition: YUIException.h:578
static void log(const YUIException &exception, const YCodeLocation &location, const char *const prefix)
Definition: YUIException.cc:128
Definition: YUIException.h:479
Definition: YUIException.h:407
Definition: YUIException.h:712
virtual std::ostream & dumpOn(std::ostream &str) const
Definition: YUIException.h:686
virtual std::ostream & dumpOn(std::ostream &str) const
Definition: YUIException.cc:91
Definition: YUIException.h:890
Definition: YUIException.h:506
int invalidIndex() const
Definition: YUIException.h:834
Definition: YWidget.h:54
const YCodeLocation & where() const
Definition: YUIException.h:320
Definition: YUIException.h:297
YWidget * widget() const
Definition: YUIException.h:528