2 /* $Id: error.cc 291 2006-05-12 08:08:45Z murrayc $ */
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>
27 #include <glibmmconfig.h>
28 #include <glibmm/error.h>
29 #include <glibmm/wrap_init.h>
36 typedef std::map<GQuark,Glib::Error::ThrowFunc> ThrowFuncTable;
38 static ThrowFuncTable* throw_func_table = 0;
40 } // anonymous namespace
51 Error::Error(GQuark domain, int code, const Glib::ustring& message)
53 gobject_ (g_error_new_literal(domain, code, message.c_str()))
56 Error::Error(GError* gobject, bool take_copy)
58 gobject_ ((take_copy && gobject) ? g_error_copy(gobject) : gobject)
61 Error::Error(const Error& other)
64 gobject_ ((other.gobject_) ? g_error_copy(other.gobject_) : 0)
67 Error& Error::operator=(const Error& other)
69 if(gobject_ != other.gobject_)
73 g_error_free(gobject_);
78 gobject_ = g_error_copy(other.gobject_);
84 Error::~Error() throw()
87 g_error_free(gobject_);
90 GQuark Error::domain() const
92 g_return_val_if_fail(gobject_ != 0, 0);
94 return gobject_->domain;
97 int Error::code() const
99 g_return_val_if_fail(gobject_ != 0, -1);
101 return gobject_->code;
104 Glib::ustring Error::what() const
106 g_return_val_if_fail(gobject_ != 0, "");
107 g_return_val_if_fail(gobject_->message != 0, "");
109 return gobject_->message;
112 bool Error::matches(GQuark domain, int code) const
114 return g_error_matches(gobject_, domain, code);
117 GError* Error::gobj()
122 const GError* Error::gobj() const
127 void Error::propagate(GError** dest)
129 g_propagate_error(dest, gobject_);
134 void Error::register_init()
136 if(!throw_func_table)
138 throw_func_table = new ThrowFuncTable();
139 Glib::wrap_init(); // make sure that at least the Glib exceptions are registered
144 void Error::register_cleanup()
148 delete throw_func_table;
149 throw_func_table = 0;
154 void Error::register_domain(GQuark domain, Error::ThrowFunc throw_func)
156 g_assert(throw_func_table != 0);
158 (*throw_func_table)[domain] = throw_func;
161 #ifdef GLIBMM_EXCEPTIONS_ENABLED
163 void Error::throw_exception(GError* gobject)
165 std::auto_ptr<Glib::Error> Error::throw_exception(GError* gobject)
166 #endif //GLIBMM_EXCEPTIONS_ENABLED
168 g_assert(gobject != 0);
170 // Just in case Gtk::Main hasn't been instantiated yet.
171 if(!throw_func_table)
174 if(const ThrowFunc throw_func = (*throw_func_table)[gobject->domain])
176 #ifdef GLIBMM_EXCEPTIONS_ENABLED
177 (*throw_func)(gobject);
179 return (*throw_func)(gobject);
180 #endif //GLIBMM_EXCEPTIONS_ENABLED
181 g_assert_not_reached();
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)");
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);
192 return std::auto_ptr<Glib::Error>(new Glib::Error(gobject));
193 #endif //GLIBMM_EXCEPTIONS_ENABLED