More gracefully handle type mismatch errors when doing playlist things (just ignore...
[ardour.git] / libs / glibmm2 / glib / glibmm / error.cc
1 // -*- c++ -*-
2 /* $Id: error.cc 291 2006-05-12 08:08:45Z murrayc $ */
3
4 /* error.cc
5  *
6  * Copyright 2002 The gtkmm Development Team
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22
23 #include <glib/gerror.h>
24 #include <glib.h>
25
26 #include <map>
27 #include <glibmmconfig.h>
28 #include <glibmm/error.h>
29 #include <glibmm/wrap_init.h>
30
31 GLIBMM_USING_STD(map)
32
33 namespace
34 {
35
36 typedef std::map<GQuark,Glib::Error::ThrowFunc> ThrowFuncTable;
37
38 static ThrowFuncTable* throw_func_table = 0;
39
40 } // anonymous namespace
41
42
43 namespace Glib
44 {
45
46 Error::Error()
47 :
48   gobject_ (0)
49 {}
50
51 Error::Error(GQuark domain, int code, const Glib::ustring& message)
52 :
53   gobject_ (g_error_new_literal(domain, code, message.c_str()))
54 {}
55
56 Error::Error(GError* gobject, bool take_copy)
57 :
58   gobject_ ((take_copy && gobject) ? g_error_copy(gobject) : gobject)
59 {}
60
61 Error::Error(const Error& other)
62 :
63   Exception(other),
64   gobject_ ((other.gobject_) ? g_error_copy(other.gobject_) : 0)
65 {}
66
67 Error& Error::operator=(const Error& other)
68 {
69   if(gobject_ != other.gobject_)
70   {
71     if(gobject_)
72     {
73       g_error_free(gobject_);
74       gobject_ = 0;
75     }
76     if(other.gobject_)
77     {
78       gobject_ = g_error_copy(other.gobject_);
79     }
80   }
81   return *this;
82 }
83
84 Error::~Error() throw()
85 {
86   if(gobject_)
87     g_error_free(gobject_);
88 }
89
90 GQuark Error::domain() const
91 {
92   g_return_val_if_fail(gobject_ != 0, 0);
93
94   return gobject_->domain;
95 }
96
97 int Error::code() const
98 {
99   g_return_val_if_fail(gobject_ != 0, -1);
100
101   return gobject_->code;
102 }
103
104 Glib::ustring Error::what() const
105 {
106   g_return_val_if_fail(gobject_ != 0, "");
107   g_return_val_if_fail(gobject_->message != 0, "");
108
109   return gobject_->message;
110 }
111
112 bool Error::matches(GQuark domain, int code) const
113 {
114   return g_error_matches(gobject_, domain, code);
115 }
116
117 GError* Error::gobj()
118 {
119   return gobject_;
120 }
121
122 const GError* Error::gobj() const
123 {
124   return gobject_;
125 }
126
127 void Error::propagate(GError** dest)
128 {
129   g_propagate_error(dest, gobject_);
130   gobject_ = 0;
131 }
132
133 // static
134 void Error::register_init()
135 {
136   if(!throw_func_table)
137   {
138     throw_func_table = new ThrowFuncTable();
139     Glib::wrap_init(); // make sure that at least the Glib exceptions are registered
140   }
141 }
142
143 // static
144 void Error::register_cleanup()
145 {
146   if(throw_func_table)
147   {
148     delete throw_func_table;
149     throw_func_table = 0;
150   }
151 }
152
153 // static
154 void Error::register_domain(GQuark domain, Error::ThrowFunc throw_func)
155 {
156   g_assert(throw_func_table != 0);
157
158   (*throw_func_table)[domain] = throw_func;
159 }
160
161 #ifdef GLIBMM_EXCEPTIONS_ENABLED
162 // static, noreturn
163 void Error::throw_exception(GError* gobject)
164 #else
165 std::auto_ptr<Glib::Error> Error::throw_exception(GError* gobject)
166 #endif //GLIBMM_EXCEPTIONS_ENABLED
167 {
168   g_assert(gobject != 0);
169
170   // Just in case Gtk::Main hasn't been instantiated yet.
171   if(!throw_func_table)
172     register_init();
173
174   if(const ThrowFunc throw_func = (*throw_func_table)[gobject->domain])
175   {
176     #ifdef GLIBMM_EXCEPTIONS_ENABLED
177     (*throw_func)(gobject);
178     #else
179     return (*throw_func)(gobject);
180     #endif //GLIBMM_EXCEPTIONS_ENABLED
181     g_assert_not_reached();
182   }
183
184   g_warning("Glib::Error::throw_exception():\n  "
185             "unknown error domain '%s': throwing generic Glib::Error exception\n",
186             (gobject->domain) ? g_quark_to_string(gobject->domain) : "(null)");
187
188 #ifdef GLIBMM_EXCEPTIONS_ENABLED
189   // Doesn't copy, because error-returning functions return a newly allocated GError for us.
190   throw Glib::Error(gobject);
191 #else
192   return std::auto_ptr<Glib::Error>(new Glib::Error(gobject));
193 #endif //GLIBMM_EXCEPTIONS_ENABLED
194 }
195
196
197 } // namespace Glib
198