6 * Copyright 2002 The gtkmm Development Team
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.
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.
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.
23 #include <glib/gerror.h>
24 #include <glib/gmessages.h>
27 #include <glibmmconfig.h>
28 #include <glibmm/error.h>
29 #include <glibmm/wrap_init.h>
37 typedef std::map<GQuark,Glib::Error::ThrowFunc> ThrowFuncTable;
39 static ThrowFuncTable* throw_func_table = 0;
41 } // anonymous namespace
52 Error::Error(GQuark domain, int code, const Glib::ustring& message)
54 gobject_ (g_error_new_literal(domain, code, message.c_str()))
57 Error::Error(GError* gobject, bool take_copy)
59 gobject_ ((take_copy && gobject) ? g_error_copy(gobject) : gobject)
62 Error::Error(const Error& other)
65 gobject_ ((other.gobject_) ? g_error_copy(other.gobject_) : 0)
68 Error& Error::operator=(const Error& other)
70 if(gobject_ != other.gobject_)
74 g_error_free(gobject_);
79 gobject_ = g_error_copy(other.gobject_);
85 Error::~Error() throw()
88 g_error_free(gobject_);
91 GQuark Error::domain() const
93 g_return_val_if_fail(gobject_ != 0, 0);
95 return gobject_->domain;
98 int Error::code() const
100 g_return_val_if_fail(gobject_ != 0, -1);
102 return gobject_->code;
105 Glib::ustring Error::what() const
107 g_return_val_if_fail(gobject_ != 0, "");
108 g_return_val_if_fail(gobject_->message != 0, "");
110 return gobject_->message;
113 bool Error::matches(GQuark domain, int code) const
115 return g_error_matches(gobject_, domain, code);
118 GError* Error::gobj()
123 const GError* Error::gobj() const
128 void Error::propagate(GError** dest)
130 g_propagate_error(dest, gobject_);
136 void Error::register_init()
138 if(!throw_func_table)
140 throw_func_table = new ThrowFuncTable();
141 Glib::wrap_init(); // make sure that at least the Glib exceptions are registered
146 void Error::register_cleanup()
150 delete throw_func_table;
151 throw_func_table = 0;
156 void Error::register_domain(GQuark domain, Error::ThrowFunc throw_func)
158 g_assert(throw_func_table != 0);
160 (*throw_func_table)[domain] = throw_func;
164 void Error::throw_exception(GError* gobject)
166 g_assert(gobject != 0);
168 // Just in case Gtk::Main hasn't been instantiated yet.
169 if(!throw_func_table)
172 if(const ThrowFunc throw_func = (*throw_func_table)[gobject->domain])
174 (*throw_func)(gobject);
175 g_assert_not_reached();
178 g_warning("Glib::Error::throw_exception():\n "
179 "unknown error domain '%s': throwing generic Glib::Error exception\n",
180 (gobject->domain) ? g_quark_to_string(gobject->domain) : "(null)");
182 // Doesn't copy, because error-returning functions return a newly allocated GError for us.
183 throw Glib::Error(gobject);