Imported Upstream version 15.5.0
[platform/upstream/libzypp.git] / zypp / base / UserRequestException.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/base/UserRequestException.h
10  *
11 */
12 #ifndef ZYPP_BASE_USERREQUESTEXCEPTION_H
13 #define ZYPP_BASE_USERREQUESTEXCEPTION_H
14
15 #include <iosfwd>
16
17 #include "zypp/base/Exception.h"
18
19 ///////////////////////////////////////////////////////////////////
20 namespace zypp
21 { /////////////////////////////////////////////////////////////////
22
23   ///////////////////////////////////////////////////////////////////
24   //
25   //    CLASS NAME : UserRequestException
26   //
27   /** Base for exceptions caused by explicit user request.
28    *
29    * Use the derived convenience classes to throw exceptions
30    * of a certain kind.
31    * \code
32    *     ProgressData ticks( makeProgressData( input_r ) );
33    *     ticks.sendTo( fnc_r );
34    *     ticks.toMin(); // start sending min (0)
35    *
36    *     iostr::EachLine line( input_r );
37    *     for( ; line; line.next() )
38    *     {
39    *       // process the line
40    *
41    *       if ( ! ticks.set( input_r.stream().tellg() ) )
42    *         ZYPP_THROW( AbortRequestException( "" ) );
43    *     }
44    * \endcode
45    * \code
46    * // either this way
47    * catch ( const AbortRequestException & excpt_r )
48    * {
49    *   ...
50    * }
51    *
52    * // or that
53    * catch ( const UserRequestException & excpt_r )
54    * {
55    *   switch ( excpt_r.kind() )
56    *   {
57    *     case UserRequestException::ABORT:
58    *       ...
59    *       break;
60    *   }
61    * }
62    * \endcode
63   */
64   class UserRequestException : public Exception
65   {
66     public:
67       enum Kind { UNSPECIFIED, IGNORE, SKIP, RETRY, ABORT };
68     public:
69       explicit
70       UserRequestException( const std::string & msg_r = std::string() );
71       UserRequestException( const std::string & msg_r, const Exception & history_r );
72       explicit
73       UserRequestException( Kind kind_r, const std::string & msg_r = std::string() );
74       UserRequestException( Kind kind_r, const std::string & msg_r, const Exception & history_r );
75     public:
76       Kind kind() const
77       { return _kind; }
78     protected:
79       virtual std::ostream & dumpOn( std::ostream & str ) const;
80     private:
81       Kind _kind;
82   };
83   ///////////////////////////////////////////////////////////////////
84
85   /** Convenience macro to declare more specific PluginScriptExceptions. */
86 #define declException( EXCP, KIND )                                     \
87   struct EXCP : public UserRequestException {                           \
88     explicit                                                            \
89     EXCP( const std::string & msg_r = std::string() )                   \
90       : UserRequestException( KIND, msg_r )                             \
91     {}                                                                  \
92     EXCP( const std::string & msg_r, const Exception & history_r )      \
93       : UserRequestException( KIND, msg_r, history_r )                  \
94     {}                                                                  \
95   }
96
97   declException( IgnoreRequestException, IGNORE );
98   declException( SkipRequestException, SKIP );
99   declException( RetryRequestException, RETRY );
100   declException( AbortRequestException, ABORT );
101
102 #undef declException
103
104   /////////////////////////////////////////////////////////////////
105 } // namespace zypp
106 ///////////////////////////////////////////////////////////////////
107 #endif // ZYPP_BASE_USERREQUESTEXCEPTION_H