1 | /* |
2 | Copyright (c) 2009 Volker Krause <vkrause@kde.org> |
3 | |
4 | This library is free software; you can redistribute it and/or modify it |
5 | under the terms of the GNU Library General Public License as published by |
6 | the Free Software Foundation; either version 2 of the License, or (at your |
7 | option) any later version. |
8 | |
9 | This library is distributed in the hope that it will be useful, but WITHOUT |
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
11 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public |
12 | License for more details. |
13 | |
14 | You should have received a copy of the GNU Library General Public License |
15 | along with this library; see the file COPYING.LIB. If not, write to the |
16 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
17 | 02110-1301, USA. |
18 | */ |
19 | |
20 | #include "exception.h" |
21 | |
22 | #include <KDebug> |
23 | #include <QString> |
24 | |
25 | #include <memory> |
26 | |
27 | using namespace Akonadi; |
28 | |
29 | class Exception::Private |
30 | { |
31 | public: |
32 | QByteArray what; |
33 | QByteArray assembledWhat; |
34 | }; |
35 | |
36 | Exception::Exception(const char *what) throw() |
37 | : d(0) |
38 | { |
39 | try { |
40 | std::auto_ptr<Private> nd(new Private); |
41 | nd->what = what; |
42 | d = nd.release(); |
43 | } catch (...) {} |
44 | } |
45 | |
46 | Exception::Exception(const QByteArray &what) throw() |
47 | : d(0) |
48 | { |
49 | try { |
50 | std::auto_ptr<Private> nd(new Private); |
51 | nd->what = what; |
52 | d = nd.release(); |
53 | } catch (...) {} |
54 | } |
55 | |
56 | Exception::Exception(const QString &what) throw() |
57 | : d(0) |
58 | { |
59 | try { |
60 | std::auto_ptr<Private> nd(new Private); |
61 | nd->what = what.toUtf8(); |
62 | d = nd.release(); |
63 | } catch (...) {} |
64 | } |
65 | |
66 | Exception::Exception(const Akonadi::Exception &other) throw() |
67 | : std::exception(other) |
68 | , d(0) |
69 | { |
70 | if (!other.d) { |
71 | return; |
72 | } |
73 | try { |
74 | std::auto_ptr<Private> nd(new Private(*other.d)); |
75 | d = nd.release(); |
76 | } catch (...) {} |
77 | } |
78 | |
79 | Exception::~Exception() throw() |
80 | { |
81 | delete d; |
82 | } |
83 | |
84 | QByteArray Exception::type() const throw() |
85 | { |
86 | static const char mytype[] = "Akonadi::Exception" ; |
87 | try { |
88 | return QByteArray::fromRawData("Akonadi::Exception" , sizeof (mytype) - 1); |
89 | } catch (...) { |
90 | return QByteArray(); |
91 | } |
92 | } |
93 | |
94 | const char *Exception::what() const throw() |
95 | { |
96 | static const char fallback[] = "<some exception was thrown during construction: message lost>" ; |
97 | if (!d) { |
98 | return fallback; |
99 | } |
100 | if (d->assembledWhat.isEmpty()) { |
101 | try { |
102 | d->assembledWhat = QByteArray(type() + ": " + d->what); |
103 | } catch (...) { |
104 | return "caught some exception while assembling Akonadi::Exception::what() return value" ; |
105 | } |
106 | } |
107 | return d->assembledWhat.constData(); |
108 | } |
109 | |
110 | #define AKONADI_EXCEPTION_IMPLEMENT_TRIVIAL_INSTANCE( classname ) \ |
111 | Akonadi::classname::~classname() throw() {} \ |
112 | QByteArray Akonadi::classname::type() const throw() { \ |
113 | static const char mytype[] = "Akonadi::" #classname ; \ |
114 | try { \ |
115 | return QByteArray::fromRawData( mytype, sizeof (mytype)-1 ); \ |
116 | } catch ( ... ) { \ |
117 | return QByteArray(); \ |
118 | } \ |
119 | } |
120 | |
121 | AKONADI_EXCEPTION_IMPLEMENT_TRIVIAL_INSTANCE(PayloadException) |
122 | |
123 | #undef AKONADI_EXCEPTION_IMPLEMENT_TRIVIAL_INSTANCE |
124 | |