summaryrefslogtreecommitdiff
path: root/glib/glibmm/error.cc
blob: 57844142c5dd5159d55cc32b9fa63926934d1a87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* error.cc
 *
 * Copyright 2002 The gtkmm Development Team
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <glibmmconfig.h>
#include <glibmm/error.h>
#include <glibmm/wrap.h>
#include <glibmm/wrap_init.h>
#include <glib.h>
#include <map>

namespace
{

typedef std::map<GQuark,Glib::Error::ThrowFunc> ThrowFuncTable;

static ThrowFuncTable* throw_func_table = 0;

} // anonymous namespace


namespace Glib
{

Error::Error()
:
  gobject_ (0)
{}

Error::Error(GQuark domain, int code, const Glib::ustring& message)
:
  gobject_ (g_error_new_literal(domain, code, message.c_str()))
{}

Error::Error(GError* gobject, bool take_copy)
:
  gobject_ ((take_copy && gobject) ? g_error_copy(gobject) : gobject)
{}

Error::Error(const Error& other)
:
  Exception(other),
  gobject_ ((other.gobject_) ? g_error_copy(other.gobject_) : 0)
{}

Error& Error::operator=(const Error& other)
{
  if(gobject_ != other.gobject_)
  {
    if(gobject_)
    {
      g_error_free(gobject_);
      gobject_ = 0;
    }
    if(other.gobject_)
    {
      gobject_ = g_error_copy(other.gobject_);
    }
  }
  return *this;
}

Error::~Error() throw()
{
  if(gobject_)
    g_error_free(gobject_);
}

GQuark Error::domain() const
{
  g_return_val_if_fail(gobject_ != 0, 0);

  return gobject_->domain;
}

int Error::code() const
{
  g_return_val_if_fail(gobject_ != 0, -1);

  return gobject_->code;
}

Glib::ustring Error::what() const
{
  g_return_val_if_fail(gobject_ != 0, "");
  g_return_val_if_fail(gobject_->message != 0, "");

  return gobject_->message;
}

bool Error::matches(GQuark domain, int code) const
{
  return g_error_matches(gobject_, domain, code);
}

GError* Error::gobj()
{
  return gobject_;
}

const GError* Error::gobj() const
{
  return gobject_;
}

void Error::propagate(GError** dest)
{
  g_propagate_error(dest, gobject_);
  gobject_ = 0;
}

// static
void Error::register_init()
{
  if(!throw_func_table)
  {
    throw_func_table = new ThrowFuncTable();
    Glib::wrap_register_init();
    Glib::wrap_init(); // make sure that at least the Glib exceptions are registered
  }
}

// static
void Error::register_cleanup()
{
  if(throw_func_table)
  {
    delete throw_func_table;
    throw_func_table = 0;
  }
}

// static
void Error::register_domain(GQuark domain, Error::ThrowFunc throw_func)
{
  g_assert(throw_func_table != 0);

  (*throw_func_table)[domain] = throw_func;
}

// static, noreturn
void Error::throw_exception(GError* gobject)
{
  g_assert(gobject != 0);

  // Just in case Gtk::Main hasn't been instantiated yet.
  if(!throw_func_table)
    register_init();

  if(const ThrowFunc throw_func = (*throw_func_table)[gobject->domain])
  {
    (*throw_func)(gobject);
    g_assert_not_reached();
  }

  g_warning("Glib::Error::throw_exception():\n  "
            "unknown error domain '%s': throwing generic Glib::Error exception\n",
            (gobject->domain) ? g_quark_to_string(gobject->domain) : "(null)");

  // Doesn't copy, because error-returning functions return a newly allocated GError for us.
  throw Glib::Error(gobject);
}

} // namespace Glib