Fix three minor memory leaks in the Editor by using Gtk::manage
[ardour.git] / libs / glibmm2 / glibmm / error.cc
1 // -*- c++ -*-
2 /* $Id$ */
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/gmessages.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
34 namespace
35 {
36
37 typedef std::map<GQuark,Glib::Error::ThrowFunc> ThrowFuncTable;
38
39 static ThrowFuncTable* throw_func_table = 0;
40
41 } // anonymous namespace
42
43
44 namespace Glib
45 {
46
47 Error::Error()
48 :
49   gobject_ (0)
50 {}
51
52 Error::Error(GQuark domain, int code, const Glib::ustring& message)
53 :
54   gobject_ (g_error_new_literal(domain, code, message.c_str()))
55 {}
56
57 Error::Error(GError* gobject, bool take_copy)
58 :
59   gobject_ ((take_copy && gobject) ? g_error_copy(gobject) : gobject)
60 {}
61
62 Error::Error(const Error& other)
63 :
64   Exception(other),
65   gobject_ ((other.gobject_) ? g_error_copy(other.gobject_) : 0)
66 {}
67
68 Error& Error::operator=(const Error& other)
69 {
70   if(gobject_ != other.gobject_)
71   {
72     if(gobject_)
73     {
74       g_error_free(gobject_);
75       gobject_ = 0;
76     }
77     if(other.gobject_)
78     {
79       gobject_ = g_error_copy(other.gobject_);
80     }
81   }
82   return *this;
83 }
84
85 Error::~Error() throw()
86 {
87   if(gobject_)
88     g_error_free(gobject_);
89 }
90
91 GQuark Error::domain() const
92 {
93   g_return_val_if_fail(gobject_ != 0, 0);
94
95   return gobject_->domain;
96 }
97
98 int Error::code() const
99 {
100   g_return_val_if_fail(gobject_ != 0, -1);
101
102   return gobject_->code;
103 }
104
105 Glib::ustring Error::what() const
106 {
107   g_return_val_if_fail(gobject_ != 0, "");
108   g_return_val_if_fail(gobject_->message != 0, "");
109
110   return gobject_->message;
111 }
112
113 bool Error::matches(GQuark domain, int code) const
114 {
115   return g_error_matches(gobject_, domain, code);
116 }
117
118 GError* Error::gobj()
119 {
120   return gobject_;
121 }
122
123 const GError* Error::gobj() const
124 {
125   return gobject_;
126 }
127
128 void Error::propagate(GError** dest)
129 {
130   g_propagate_error(dest, gobject_);
131   gobject_ = 0;
132 }
133
134
135 // static
136 void Error::register_init()
137 {
138   if(!throw_func_table)
139   {
140     throw_func_table = new ThrowFuncTable();
141     Glib::wrap_init(); // make sure that at least the Glib exceptions are registered
142   }
143 }
144
145 // static
146 void Error::register_cleanup()
147 {
148   if(throw_func_table)
149   {
150     delete throw_func_table;
151     throw_func_table = 0;
152   }
153 }
154
155 // static
156 void Error::register_domain(GQuark domain, Error::ThrowFunc throw_func)
157 {
158   g_assert(throw_func_table != 0);
159
160   (*throw_func_table)[domain] = throw_func;
161 }
162
163 // static, noreturn
164 void Error::throw_exception(GError* gobject)
165 {
166   g_assert(gobject != 0);
167
168   // Just in case Gtk::Main hasn't been instantiated yet.
169   if(!throw_func_table)
170     register_init();
171
172   if(const ThrowFunc throw_func = (*throw_func_table)[gobject->domain])
173   {
174     (*throw_func)(gobject);
175     g_assert_not_reached();
176   }
177
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)");
181
182   // Doesn't copy, because error-returning functions return a newly allocated GError for us.
183   throw Glib::Error(gobject);
184 }
185
186 } // namespace Glib
187