2 /* $Id: value.cc 292 2006-05-14 12:12:41Z murrayc $ */
4 /* Copyright 2002 The gtkmm Development Team
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.
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.
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.
21 #include <glibmm/value.h>
22 #include <glibmm/objectbase.h>
23 #include <glibmm/utility.h>
24 #include <glibmm/wrap.h>
30 /**** Glib::ValueBase ******************************************************/
32 ValueBase::ValueBase()
34 GLIBMM_INITIALIZE_STRUCT(gobject_, GValue);
37 void ValueBase::init(GType type)
39 g_value_init(&gobject_, type);
42 void ValueBase::init(const GValue* value)
44 g_value_init(&gobject_, G_VALUE_TYPE(value));
47 g_value_copy(value, &gobject_);
50 ValueBase::ValueBase(const ValueBase& other)
52 GLIBMM_INITIALIZE_STRUCT(gobject_, GValue);
54 g_value_init(&gobject_, G_VALUE_TYPE(&other.gobject_));
55 g_value_copy(&other.gobject_, &gobject_);
58 ValueBase& ValueBase::operator=(const ValueBase& other)
60 // g_value_copy() prevents self-assignment and deletes the destination.
61 g_value_copy(&other.gobject_, &gobject_);
65 ValueBase::~ValueBase()
67 g_value_unset(&gobject_);
70 void ValueBase::reset()
72 g_value_reset(&gobject_);
76 /**** Glib::ValueBase_Boxed ************************************************/
79 GType ValueBase_Boxed::value_type()
84 void ValueBase_Boxed::set_boxed(const void* data)
86 g_value_set_boxed(&gobject_, data);
89 void* ValueBase_Boxed::get_boxed() const
91 return g_value_get_boxed(&gobject_);
94 GParamSpec* ValueBase_Boxed::create_param_spec(const Glib::ustring& name) const
96 return g_param_spec_boxed(
97 name.c_str(), 0, 0, G_VALUE_TYPE(&gobject_),
98 GParamFlags(G_PARAM_READABLE | G_PARAM_WRITABLE));
102 /**** Glib::ValueBase_Object ***********************************************/
105 GType ValueBase_Object::value_type()
107 return G_TYPE_OBJECT;
110 void ValueBase_Object::set_object(Glib::ObjectBase* data)
112 g_value_set_object(&gobject_, (data) ? data->gobj() : 0);
115 Glib::ObjectBase* ValueBase_Object::get_object() const
117 GObject *const data = static_cast<GObject*>(g_value_get_object(&gobject_));
118 return Glib::wrap_auto(data, false);
121 Glib::RefPtr<Glib::ObjectBase> ValueBase_Object::get_object_copy() const
123 GObject *const data = static_cast<GObject*>(g_value_get_object(&gobject_));
124 return Glib::RefPtr<Glib::ObjectBase>(Glib::wrap_auto(data, true));
127 GParamSpec* ValueBase_Object::create_param_spec(const Glib::ustring& name) const
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.
134 if(G_VALUE_HOLDS_OBJECT(&gobject_))
136 return g_param_spec_object(
137 name.c_str(), 0, 0, G_VALUE_TYPE(&gobject_),
138 GParamFlags(G_PARAM_READABLE | G_PARAM_WRITABLE));
142 g_return_val_if_fail(G_VALUE_HOLDS_POINTER(&gobject_), 0);
144 return g_param_spec_pointer(
146 GParamFlags(G_PARAM_READABLE | G_PARAM_WRITABLE));
151 /**** Glib::ValueBase_Enum *************************************************/
154 GType ValueBase_Enum::value_type()
159 void ValueBase_Enum::set_enum(int data)
161 g_value_set_enum(&gobject_, data);
164 int ValueBase_Enum::get_enum() const
166 return g_value_get_enum(&gobject_);
169 GParamSpec* ValueBase_Enum::create_param_spec(const Glib::ustring& name) const
171 return g_param_spec_enum(
173 G_VALUE_TYPE(&gobject_), g_value_get_enum(&gobject_),
174 GParamFlags(G_PARAM_READABLE | G_PARAM_WRITABLE));
178 /**** Glib::ValueBase_Flags ************************************************/
181 GType ValueBase_Flags::value_type()
186 void ValueBase_Flags::set_flags(unsigned int data)
188 g_value_set_flags(&gobject_, data);
191 unsigned int ValueBase_Flags::get_flags() const
193 return g_value_get_flags(&gobject_);
196 GParamSpec* ValueBase_Flags::create_param_spec(const Glib::ustring& name) const
198 return g_param_spec_flags(
200 G_VALUE_TYPE(&gobject_), g_value_get_flags(&gobject_),
201 GParamFlags(G_PARAM_READABLE | G_PARAM_WRITABLE));
205 /**** Glib::ValueBase_String ***********************************************/
208 GType ValueBase_String::value_type()
210 return G_TYPE_STRING;
213 void ValueBase_String::set_cstring(const char* data)
215 g_value_set_string(&gobject_, data);
218 const char* ValueBase_String::get_cstring() const
220 if(const char *const data = g_value_get_string(&gobject_))
226 GParamSpec* ValueBase_String::create_param_spec(const Glib::ustring& name) const
228 return g_param_spec_string(
229 name.c_str(), 0, 0, get_cstring(),
230 GParamFlags(G_PARAM_READABLE | G_PARAM_WRITABLE));
234 /**** Glib::Value<std::string> *********************************************/
236 void Value<std::string>::set(const std::string& data)
238 g_value_set_string(&gobject_, data.c_str());
242 /**** Glib::Value<Glib::ustring> *******************************************/
244 void Value<Glib::ustring>::set(const Glib::ustring& data)
246 g_value_set_string(&gobject_, data.c_str());