Gaia
gaiaexception.h
1 /*
2  * Copyright (C) 2006-2013 Music Technology Group - Universitat Pompeu Fabra
3  *
4  * This file is part of Gaia
5  *
6  * Gaia is free software: you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License as published by the Free
8  * Software Foundation (FSF), either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the Affero GNU General Public License
17  * version 3 along with this program. If not, see http://www.gnu.org/licenses/
18  */
19 
20 #ifndef GAIA_GAIAEXCEPTION_H
21 #define GAIA_GAIAEXCEPTION_H
22 
23 #include <QString>
24 #include <QStringList>
25 #include <QTextStream>
26 #include <QPair>
27 #include "baseexception.h"
28 
29 
30 namespace gaia2 {
31 
32 inline QTextStream& operator<<(QTextStream& out, const QPair<int, int>& p) {
33  return out << '<' << p.first << ", " << p.second << '>';
34 }
35 
36 
37 inline QTextStream& operator<<(QTextStream& out, const QStringList& slist) {
38  if (slist.empty()) return out << "[]";
39  return out << "[ " << slist.join(", ") << " ]";
40 }
41 
47  protected:
48  QString _msg;
49 
50  public:
51  GaiaException() {}
52  GaiaException(const QString& msg) : _msg(msg) {}
53  GaiaException(const char* msg) : gaia2std::GException(), _msg(msg) {}
54  GaiaException(const std::string& msg) : gaia2std::GException(), _msg(QString::fromUtf8(msg.c_str(),
55  (int)msg.size())) {}
56  GaiaException(const QStringList& msg) : gaia2std::GException(), _msg(msg.join("")) {}
57 
58  template <typename T, typename U>
59  GaiaException(const T& a, const U& b) : gaia2std::GException() {
60  QTextStream(&_msg) << a << b;
61  }
62 
63  template <typename T, typename U, typename V>
64  GaiaException(const T& a, const U& b, const V& c) : gaia2std::GException() {
65  QTextStream(&_msg) << a << b << c;
66  }
67 
68  template <typename T, typename U, typename V, typename W>
69  GaiaException(const T& a, const U& b, const V& c, const W& d) : gaia2std::GException() {
70  QTextStream(&_msg) << a << b << c << d;
71  }
72 
73  virtual ~GaiaException() throw() {}
74  virtual const char* what() const throw() {
75  // NB: we need to store the ascii message permanently inside the exception
76  // instance, otherwise it gets destroyed when we come out and the pointer is
77  // invalid
78 
79  // if message was set from a gaia2::GaiaException, copy it into the
80  // std::string error message variable
81  if (_msg.size() > 0) {
82  const_cast<GaiaException*>(this)->_message = _msg.toUtf8().data();
83  }
84  return _message.c_str();
85  }
86 
87  const QString& msg() const throw () { return _msg; }
88  QString& msg() throw () { return _msg; }
89 };
90 
91 
95 #define GAIA_DEFINE_EXCEPTION(UserException) \
96 class UserException : public gaia2::GaiaException { \
97  public: \
98  UserException() {} \
99  UserException(const QString& msg) : gaia2::GaiaException(msg) {} \
100  UserException(const char* msg) : gaia2::GaiaException(msg) {} \
101  UserException(const std::string& msg) : gaia2::GaiaException(msg) {} \
102  UserException(const QStringList& msg) : gaia2::GaiaException(msg) {} \
103  template <typename T, typename U> \
104  UserException(const T& a, const U& b) : gaia2::GaiaException(a, b) {} \
105  template <typename T, typename U, typename V> \
106  UserException(const T& a, const U& b, const V& c) : gaia2::GaiaException(a, b, c) {} \
107  template <typename T, typename U, typename V, typename W> \
108  UserException(const T& a, const U& b, const V& c, const W& d) : \
109  gaia2::GaiaException(a, b, c, d) {} \
110 };
111 
112 
113 // Exception hierarchy
114 GAIA_DEFINE_EXCEPTION(FixedLengthException);
115 
116 } // namespace gaia2
117 
118 
119 #endif // GAIA_GAIAEXCEPTION_H
Main Gaia namespace, which contains all the library functions.
Definition: addfield.cpp:22
Definition: baseexception.h:28
Exception class that can take up to 3 arguments of any type, which will be serialized into a QString ...
Definition: gaiaexception.h:46