libyui
 
Loading...
Searching...
No Matches
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
39class YWidget;
40
41
42//
43// Macros for application use
44//
45
79
80
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{
231public:
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
276private:
277 std::string _file;
278 std::string _func;
279 int _line;
280
281}; // YCodeLocation
282
283
287std::ostream & operator<<( std::ostream & str, const YCodeLocation & obj );
288
289
297class YUIException : public std::exception
298{
299public:
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
373protected:
374
378 virtual std::ostream & dumpOn( std::ostream & str ) const;
379
380
381private:
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
400std::ostream & operator<<( std::ostream & str, const YUIException & obj );
401
402
407class YUINullPointerException: public YUIException
408{
409public:
410 YUINullPointerException()
411 : YUIException( "Null pointer" )
412 {}
413
414 virtual ~YUINullPointerException() throw()
415 {}
416};
417
418
423class YUIOutOfMemoryException: public YUIException
424{
425public:
426 YUIOutOfMemoryException()
427 : YUIException( "Out of memory" )
428 {}
429
430 virtual ~YUIOutOfMemoryException() throw()
431 {}
432
433};
434
440class YUIInvalidWidgetException: public YUIException
441{
442public:
443 YUIInvalidWidgetException()
444 : YUIException( "Invalid widget" )
445 {}
446
447 virtual ~YUIInvalidWidgetException() throw()
448 {}
449};
450
451
455class YUIWidgetNotFoundException: public YUIException
456{
457public:
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
467class YUINoDialogException: public YUIException
468{
469public:
470 YUINoDialogException()
471 : YUIException( "No dialog existing" )
472 {}
473
474 virtual ~YUINoDialogException() throw()
475 {}
476};
477
478
479class YUIDialogStackingOrderException: public YUIException
480{
481public:
482 YUIDialogStackingOrderException()
483 : YUIException( "Dialog stacking order violated" )
484 {}
485
486 virtual ~YUIDialogStackingOrderException() throw()
487 {}
488};
489
490
491class YUISyntaxErrorException: public YUIException
492{
493public:
494 YUISyntaxErrorException( const std::string & msg )
495 : YUIException( msg )
496 {}
497
498 virtual ~YUISyntaxErrorException() throw()
499 {}
500};
501
502
506class YUIPropertyException: public YUIException
507{
508protected:
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
519public:
523 YProperty property() const { return _property; }
524
528 YWidget * widget() const { return _widget; }
529
533 void setWidget( YWidget * w ) { _widget = w; }
534
535protected:
536
541 virtual std::ostream & dumpOn( std::ostream & str ) const = 0;
542
543private:
544 YProperty _property;
545 YWidget * _widget;
546};
547
548
553class YUIUnknownPropertyException: public YUIPropertyException
554{
555public:
556 YUIUnknownPropertyException( const std::string & propertyName,
557 YWidget * widget = 0 )
558 : YUIPropertyException( YProperty( propertyName, YUnknownPropertyType ), widget )
559 {}
560
561 virtual ~YUIUnknownPropertyException() throw()
562 {}
563
564protected:
565
570 virtual std::ostream & dumpOn( std::ostream & str ) const;
571};
572
573
578class YUIPropertyTypeMismatchException: public YUIPropertyException
579{
580public:
581
582 YUIPropertyTypeMismatchException( const YProperty & property,
583 YPropertyType type,
584 YWidget * widget = 0 )
585 : YUIPropertyException( property, widget )
586 , _type( type )
587 {}
588
589 virtual ~YUIPropertyTypeMismatchException() throw()
590 {}
591
595 YPropertyType type() const { return _type; }
596
597protected:
598
603 virtual std::ostream & dumpOn( std::ostream & str ) const;
604
605private:
606 YPropertyType _type;
607};
608
609
613class YUISetReadOnlyPropertyException: public YUIPropertyException
614{
615public:
616
617 YUISetReadOnlyPropertyException( const YProperty & property,
618 YWidget * widget = 0 )
619 : YUIPropertyException( property, widget )
620 {}
621
622 virtual ~YUISetReadOnlyPropertyException() throw()
623 {}
624
625protected:
626
631 virtual std::ostream & dumpOn( std::ostream & str ) const;
632};
633
634
635class YUIBadPropertyArgException: public YUIPropertyException
636{
637public:
638
639 YUIBadPropertyArgException( const YProperty & property,
640 YWidget * widget,
641 const std::string & message = "" )
642 : YUIPropertyException( property, widget )
643 { setMsg( message ); }
644
645 virtual ~YUIBadPropertyArgException() throw()
646 {}
647
648protected:
649
654 virtual std::ostream & dumpOn( std::ostream & str ) const;
655};
656
657
663template<class YWidget> class YUITooManyChildrenException: public YUIException
664{
665public:
666
667 YUITooManyChildrenException( YWidget * container )
668 : YUIException( "Too many children" )
669 , _container( container )
670 {}
671
672 virtual ~YUITooManyChildrenException() throw()
673 {}
674
678 YWidget * container() const { return _container; }
679
680protected:
681
686 virtual std::ostream & dumpOn( std::ostream & str ) const
687 {
688 std::string widgetClass =
690 "widget";
691
692 return str << "Too many children for "
693 << widgetClass
694 << std::endl;
695 }
696
697private:
698
699 YWidget * _container;
700};
701
702
712template<class YWidget> class YUIInvalidChildException: public YUIException
713{
714public:
715
716 YUIInvalidChildException( YWidget * container,
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
736protected:
737
742 virtual std::ostream & dumpOn( std::ostream & str ) const
743 {
744 std::string containerWidgetClass =
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
758private:
759
760 YWidget * _container;
761 YWidget * _child;
762};
763
764
765
775class YUIUnsupportedWidgetException: public YUIException
776{
777public:
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
792class YUIInvalidDimensionException: public YUIException
793{
794public:
795 YUIInvalidDimensionException()
796 : YUIException( "Invalid dimension (neither YD_HORIZ nor YD_VERT)" )
797 {}
798
799 virtual ~YUIInvalidDimensionException() throw()
800 {}
801};
802
803
808{
809public:
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
846protected:
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
864private:
865
866 int _invalidIndex;
867 int _validMin;
868 int _validMax;
869};
870
871
875class YUIPluginException: public YUIException
876{
877public:
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
890class YUICantLoadAnyUIException: public YUIException
891{
892public:
893 YUICantLoadAnyUIException()
894 : YUIException( "No $DISPLAY and stdout is not a tty" )
895 {}
896
897 virtual ~YUICantLoadAnyUIException() throw()
898 {}
899};
900
901
905class YUIButtonRoleMismatchException: public YUIException
906{
907public:
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
926template<class _Exception>
927void _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
938template<class _Exception>
939void _YUI_CAUGHT( const _Exception & exception_r, const YCodeLocation & where_r )
940{
941 YUIException::log( exception_r, where_r, "CAUGHT: " );
942}
943
944
948template<class _Exception>
949void _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
Definition YUIException.h:230
std::string asString() const
Definition YUIException.cc:42
std::string func() const
Definition YUIException.h:259
std::string file() const
Definition YUIException.h:254
friend std::ostream & operator<<(std::ostream &str, const YCodeLocation &obj)
int line() const
Definition YUIException.h:264
YCodeLocation(const std::string &file_r, const std::string &func_r, int line_r)
Definition YUIException.h:236
YCodeLocation()
Definition YUIException.h:247
Definition YProperty.h:52
virtual std::ostream & dumpOn(std::ostream &str) const
Definition YUIException.cc:197
Definition YUIException.h:298
const YCodeLocation & where() const
Definition YUIException.h:320
static std::string strErrno(int errno_r, const std::string &msg)
static std::string strErrno(int errno_r)
Definition YUIException.cc:112
void relocate(const YCodeLocation &newLocation) const
Definition YUIException.h:326
virtual ~YUIException()
Definition YUIException.cc:75
std::string asString() const
Definition YUIException.cc:82
virtual const char * what() const
Definition YUIException.h:370
virtual std::ostream & dumpOn(std::ostream &str) const
Definition YUIException.cc:91
void setMsg(const std::string &msg)
Definition YUIException.h:340
const std::string & msg() const
Definition YUIException.h:334
YUIException(const std::string &msg_r)
friend std::ostream & operator<<(std::ostream &str, const YUIException &obj)
YUIException()
Definition YUIException.cc:63
static void log(const YUIException &exception, const YCodeLocation &location, const char *const prefix)
Definition YUIException.cc:128
Definition YUIException.h:808
int validMin() const
Definition YUIException.h:839
virtual std::ostream & dumpOn(std::ostream &str) const
Definition YUIException.h:852
int validMax() const
Definition YUIException.h:844
int invalidIndex() const
Definition YUIException.h:834
YUIIndexOutOfRangeException(int invalidIndex, int validMin, int validMax, const std::string &msg="")
Definition YUIException.h:818
YWidget * container() const
Definition YUIException.h:729
YWidget * child() const
Definition YUIException.h:734
virtual std::ostream & dumpOn(std::ostream &str) const
Definition YUIException.h:742
YProperty property() const
Definition YUIException.h:523
void setWidget(YWidget *w)
Definition YUIException.h:533
virtual std::ostream & dumpOn(std::ostream &str) const =0
YWidget * widget() const
Definition YUIException.h:528
YPropertyType type() const
Definition YUIException.h:595
virtual std::ostream & dumpOn(std::ostream &str) const
Definition YUIException.cc:162
virtual std::ostream & dumpOn(std::ostream &str) const
Definition YUIException.cc:181
virtual std::ostream & dumpOn(std::ostream &str) const
Definition YUIException.h:686
YWidget * container() const
Definition YUIException.h:678
virtual std::ostream & dumpOn(std::ostream &str) const
Definition YUIException.cc:141
Definition YWidget.h:55
virtual const char * widgetClass() const
Definition YWidget.h:72