Akonadi

exception.cpp
1 /*
2  Copyright (c) 2009 Volker Krause <[email protected]>
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 "exceptionbase.h"
21 
22 #include <QString>
23 
24 #include <memory>
25 
26 using namespace Akonadi;
27 
28 class Exception::Private
29 {
30 public:
31  explicit Private(const QByteArray &what): what(what) {}
32 
34  QByteArray assembledWhat;
35 };
36 
38 {
39  try {
40  d = std::make_unique<Private>(what);
41  } catch (...) {
42  }
43 }
44 
46 {
47  try {
48  d = std::make_unique<Private>(what);
49  } catch (...) {
50  }
51 }
52 
54 {
55  try {
56  d = std::make_unique<Private>(what.toUtf8());
57  } catch (...) {
58  }
59 }
60 
62  : std::exception(other)
63 {
64  if (!other.d) {
65  return;
66  }
67  try {
68  d = std::make_unique<Private>(*other.d);
69  } catch (...) {
70  }
71 }
72 
73 Exception::Exception(Exception &&other) = default;
74 Exception::~Exception() = default;
75 
77 {
78  static constexpr char mytype[] = "Akonadi::Exception";
79  try {
80  return QByteArray::fromRawData("Akonadi::Exception", sizeof(mytype) - 1);
81  } catch (...) {
82  return QByteArray();
83  }
84 }
85 
86 const char *Exception::what() const noexcept
87 {
88  static constexpr char fallback[] = "<some exception was thrown during construction: message lost>";
89  if (!d) {
90  return fallback;
91  }
92  if (d->assembledWhat.isEmpty()) {
93  try {
94  d->assembledWhat = QByteArray(type() + ": " + d->what);
95  } catch (...) {
96  return "caught some exception while assembling Akonadi::Exception::what() return value";
97  }
98  }
99  return d->assembledWhat.constData();
100 }
101 
102 #define AKONADI_EXCEPTION_IMPLEMENT_TRIVIAL_INSTANCE(classname) \
103  Akonadi::classname::~classname() = default; \
104  QByteArray Akonadi::classname::type() const { \
105  static constexpr char mytype[] = "Akonadi::" #classname ; \
106  try { \
107  return QByteArray::fromRawData( mytype, sizeof (mytype)-1 ); \
108  } catch ( ... ) { \
109  return QByteArray(); \
110  } \
111  }
112 
113 AKONADI_EXCEPTION_IMPLEMENT_TRIVIAL_INSTANCE(PayloadException)
114 
115 #undef AKONADI_EXCEPTION_IMPLEMENT_TRIVIAL_INSTANCE
116 
Exception(const char *what)
Creates a new exception with the error message what.
Definition: exception.cpp:37
QByteArray fromRawData(const char *data, int size)
A glue between Qt and the standard library.
~Exception() override
Destructor.
const char * what() const noexcept override
Returns the error message associated with this exception.
Definition: exception.cpp:86
Helper integration between Akonadi and Qt.
Base class for exceptions used by the Akonadi library.
Definition: exceptionbase.h:43
virtual QByteArray type() const
Returns the type of this exception.
Definition: exception.cpp:76
QByteArray toUtf8() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat May 30 2020 22:46:09 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.