Merged revisions 6293,6296-6306,6308 via svnmerge from
[ardour.git] / libs / glibmm2 / glib / glibmm / value.cc
1 // -*- c++ -*-
2 /* $Id: value.cc 292 2006-05-14 12:12:41Z murrayc $ */
3
4 /* Copyright 2002 The gtkmm Development Team
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20
21 #include <glibmm/value.h>
22 #include <glibmm/objectbase.h>
23 #include <glibmm/utility.h>
24 #include <glibmm/wrap.h>
25
26
27 namespace Glib
28 {
29
30 /**** Glib::ValueBase ******************************************************/
31
32 ValueBase::ValueBase()
33 {
34   GLIBMM_INITIALIZE_STRUCT(gobject_, GValue);
35 }
36
37 void ValueBase::init(GType type)
38 {
39   g_value_init(&gobject_, type);
40 }
41
42 void ValueBase::init(const GValue* value)
43 {
44   g_value_init(&gobject_, G_VALUE_TYPE(value));
45
46   if(value)
47     g_value_copy(value, &gobject_);
48 }
49
50 ValueBase::ValueBase(const ValueBase& other)
51 {
52   GLIBMM_INITIALIZE_STRUCT(gobject_, GValue);
53
54   g_value_init(&gobject_, G_VALUE_TYPE(&other.gobject_));
55   g_value_copy(&other.gobject_, &gobject_);
56 }
57
58 ValueBase& ValueBase::operator=(const ValueBase& other)
59 {
60   // g_value_copy() prevents self-assignment and deletes the destination.
61   g_value_copy(&other.gobject_, &gobject_);
62   return *this;
63 }
64
65 ValueBase::~ValueBase()
66 {
67   g_value_unset(&gobject_);
68 }
69
70 void ValueBase::reset()
71 {
72   g_value_reset(&gobject_);
73 }
74
75
76 /**** Glib::ValueBase_Boxed ************************************************/
77
78 // static
79 GType ValueBase_Boxed::value_type()
80 {
81   return G_TYPE_BOXED;
82 }
83
84 void ValueBase_Boxed::set_boxed(const void* data)
85 {
86   g_value_set_boxed(&gobject_, data);
87 }
88
89 void* ValueBase_Boxed::get_boxed() const
90 {
91   return g_value_get_boxed(&gobject_);
92 }
93
94 GParamSpec* ValueBase_Boxed::create_param_spec(const Glib::ustring& name) const
95 {
96   return g_param_spec_boxed(
97       name.c_str(), 0, 0, G_VALUE_TYPE(&gobject_),
98       GParamFlags(G_PARAM_READABLE | G_PARAM_WRITABLE));
99 }
100
101
102 /**** Glib::ValueBase_Object ***********************************************/
103
104 // static
105 GType ValueBase_Object::value_type()
106 {
107   return G_TYPE_OBJECT;
108 }
109
110 void ValueBase_Object::set_object(Glib::ObjectBase* data)
111 {
112   g_value_set_object(&gobject_, (data) ? data->gobj() : 0);
113 }
114
115 Glib::ObjectBase* ValueBase_Object::get_object() const
116 {
117   GObject *const data = static_cast<GObject*>(g_value_get_object(&gobject_));
118   return Glib::wrap_auto(data, false);
119 }
120
121 Glib::RefPtr<Glib::ObjectBase> ValueBase_Object::get_object_copy() const
122 {
123   GObject *const data = static_cast<GObject*>(g_value_get_object(&gobject_));
124   return Glib::RefPtr<Glib::ObjectBase>(Glib::wrap_auto(data, true));
125 }
126
127 GParamSpec* ValueBase_Object::create_param_spec(const Glib::ustring& name) const
128 {
129   // Glib::Value_Pointer<> derives from Glib::ValueBase_Object, because
130   // we don't know beforehand whether a certain type is derived from
131   // Glib::Object or not.  To keep create_param_spec() out of the template
132   // struggle, we dispatch here at runtime.
133
134   if(G_VALUE_HOLDS_OBJECT(&gobject_))
135   {
136     return g_param_spec_object(
137         name.c_str(), 0, 0, G_VALUE_TYPE(&gobject_),
138         GParamFlags(G_PARAM_READABLE | G_PARAM_WRITABLE));
139   }
140   else
141   {
142     g_return_val_if_fail(G_VALUE_HOLDS_POINTER(&gobject_), 0);
143
144     return g_param_spec_pointer(
145         name.c_str(), 0, 0,
146         GParamFlags(G_PARAM_READABLE | G_PARAM_WRITABLE));
147   }
148 }
149
150
151 /**** Glib::ValueBase_Enum *************************************************/
152
153 // static
154 GType ValueBase_Enum::value_type()
155 {
156   return G_TYPE_ENUM;
157 }
158
159 void ValueBase_Enum::set_enum(int data)
160 {
161   g_value_set_enum(&gobject_, data);
162 }
163
164 int ValueBase_Enum::get_enum() const
165 {
166   return g_value_get_enum(&gobject_);
167 }
168
169 GParamSpec* ValueBase_Enum::create_param_spec(const Glib::ustring& name) const
170 {
171   return g_param_spec_enum(
172       name.c_str(), 0, 0,
173       G_VALUE_TYPE(&gobject_), g_value_get_enum(&gobject_),
174       GParamFlags(G_PARAM_READABLE | G_PARAM_WRITABLE));
175 }
176
177
178 /**** Glib::ValueBase_Flags ************************************************/
179
180 // static
181 GType ValueBase_Flags::value_type()
182 {
183   return G_TYPE_FLAGS;
184 }
185
186 void ValueBase_Flags::set_flags(unsigned int data)
187 {
188   g_value_set_flags(&gobject_, data);
189 }
190
191 unsigned int ValueBase_Flags::get_flags() const
192 {
193   return g_value_get_flags(&gobject_);
194 }
195
196 GParamSpec* ValueBase_Flags::create_param_spec(const Glib::ustring& name) const
197 {
198   return g_param_spec_flags(
199       name.c_str(), 0, 0,
200       G_VALUE_TYPE(&gobject_), g_value_get_flags(&gobject_),
201       GParamFlags(G_PARAM_READABLE | G_PARAM_WRITABLE));
202 }
203
204
205 /**** Glib::ValueBase_String ***********************************************/
206
207 // static
208 GType ValueBase_String::value_type()
209 {
210   return G_TYPE_STRING;
211 }
212
213 void ValueBase_String::set_cstring(const char* data)
214 {
215   g_value_set_string(&gobject_, data);
216 }
217
218 const char* ValueBase_String::get_cstring() const
219 {
220   if(const char *const data = g_value_get_string(&gobject_))
221     return data;
222   else
223     return "";
224 }
225
226 GParamSpec* ValueBase_String::create_param_spec(const Glib::ustring& name) const
227 {
228   return g_param_spec_string(
229       name.c_str(), 0, 0, get_cstring(),
230       GParamFlags(G_PARAM_READABLE | G_PARAM_WRITABLE));
231 }
232
233
234 /**** Glib::Value<std::string> *********************************************/
235
236 void Value<std::string>::set(const std::string& data)
237 {
238   g_value_set_string(&gobject_, data.c_str());
239 }
240
241
242 /**** Glib::Value<Glib::ustring> *******************************************/
243
244 void Value<Glib::ustring>::set(const Glib::ustring& data)
245 {
246   g_value_set_string(&gobject_, data.c_str());
247 }
248
249 } // namespace Glib
250