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 #ifndef DOXYGEN_SHOULD_SKIP_THIS
22 #ifndef _GLIBMM_VALUE_H_INCLUDE_VALUE_CUSTOM_H
23 #error "glibmm/value_custom.h cannot be included directly"
29 #include <glibmmconfig.h>
31 GLIBMM_USING_STD(nothrow)
37 #ifndef DOXYGEN_SHOULD_SKIP_THIS
41 typedef void (* ValueInitFunc) (GValue*);
42 typedef void (* ValueFreeFunc) (GValue*);
43 typedef void (* ValueCopyFunc) (const GValue*, GValue*);
46 /* When using Glib::Value<T> with custom types, each T will be registered
47 * as subtype of G_TYPE_BOXED, via this function. The type_name argument
48 * should be the C++ RTTI name.
50 GType custom_boxed_type_register(const char* type_name,
51 ValueInitFunc init_func,
52 ValueFreeFunc free_func,
53 ValueCopyFunc copy_func);
55 /* When using Glib::Value<T*> or Glib::Value<const T*> with custom types,
56 * each T* or const T* will be registered as a subtype of G_TYPE_POINTER,
57 * via this function. The type_name argument should be the C++ RTTI name.
59 GType custom_pointer_type_register(const char* type_name);
61 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
65 * @ingroup glibmmValue
67 template <class T, class PtrT>
68 class Value_Pointer : public ValueBase_Object
74 static inline GType value_type() G_GNUC_CONST;
76 inline void set(CppType data);
77 inline CppType get() const;
81 static GType value_type_(Glib::Object*);
82 static GType value_type_(void*);
84 inline void set_(CppType data, Glib::Object*);
85 inline void set_(CppType data, void*);
87 inline CppType get_(Glib::Object*) const;
88 inline CppType get_(void*) const;
92 /** Generic value implementation for custom types.
93 * @ingroup glibmmValue
94 * Any type to be used with this template must implement:
95 * - default constructor
97 * - assignment operator
100 * Compiler-generated implementations are OK, provided they do the
101 * right thing for the type. In other words, any type that works with
102 * <tt>std::vector</tt> will work with Glib::Value<>.
104 * @note None of the operations listed above are allowed to throw. If you
105 * cannot ensure that no exceptions will be thrown, consider using either
106 * a normal pointer or a smart pointer to hold your objects indirectly.
109 class Value : public ValueBase_Boxed
115 static GType value_type() G_GNUC_CONST;
117 inline void set(const CppType& data);
118 inline CppType get() const;
121 static GType custom_type_;
123 static void value_init_func(GValue* value);
124 static void value_free_func(GValue* value);
125 static void value_copy_func(const GValue* src_value, GValue* dest_value);
129 /** Specialization for pointers to instances of any type.
130 * @ingroup glibmmValue
131 * No attempt is made to manage the memory associated with the
132 * pointer, you must take care of that yourself.
135 class Value<T*> : public Value_Pointer<T,T*>
138 /** Specialization for pointers to const instances of any type.
139 * @ingroup glibmmValue
140 * No attempt is made to manage the memory associated with the
141 * pointer, you must take care of that yourself.
144 class Value<const T*> : public Value_Pointer<T,const T*>
148 #ifndef DOXYGEN_SHOULD_SKIP_THIS
150 /**** Glib::Value_Pointer<T, PtrT> *****************************************/
152 /** Implementation for Glib::Object pointers **/
155 template <class T, class PtrT> inline
156 GType Value_Pointer<T,PtrT>::value_type_(Glib::Object*)
158 return T::get_base_type();
161 template <class T, class PtrT> inline
162 void Value_Pointer<T,PtrT>::set_(PtrT data, Glib::Object*)
164 set_object(const_cast<T*>(data));
167 //More spec-compliant compilers (such as Tru64) need this to be near Glib::Object instead.
168 #ifdef GLIBMM_CAN_USE_DYNAMIC_CAST_IN_UNUSED_TEMPLATE_WITHOUT_DEFINITION
169 template <class T, class PtrT> inline
170 PtrT Value_Pointer<T,PtrT>::get_(Glib::Object*) const
172 return dynamic_cast<T*>(get_object());
174 #endif //GLIBMM_CAN_USE_DYNAMIC_CAST_IN_UNUSED_TEMPLATE_WITHOUT_DEFINITION
176 /** Implementation for custom pointers **/
179 template <class T, class PtrT>
180 GType Value_Pointer<T,PtrT>::value_type_(void*)
182 static GType custom_type = 0;
185 custom_type = Glib::custom_pointer_type_register(typeid(PtrT).name());
190 template <class T, class PtrT> inline
191 void Value_Pointer<T,PtrT>::set_(PtrT data, void*)
193 gobject_.data[0].v_pointer = const_cast<T*>(data);
196 template <class T, class PtrT> inline
197 PtrT Value_Pointer<T,PtrT>::get_(void*) const
199 return static_cast<T*>(gobject_.data[0].v_pointer);
202 /** Public forwarding interface **/
205 template <class T, class PtrT> inline
206 GType Value_Pointer<T,PtrT>::value_type()
208 // Dispatch to the specific value_type_() overload.
209 return Value_Pointer<T,PtrT>::value_type_(static_cast<T*>(0));
212 template <class T, class PtrT> inline
213 void Value_Pointer<T,PtrT>::set(PtrT data)
215 // Dispatch to the specific set_() overload.
216 this->set_(data, static_cast<T*>(0));
219 template <class T, class PtrT> inline
220 PtrT Value_Pointer<T,PtrT>::get() const
222 // Dispatch to the specific get_() overload.
223 return this->get_(static_cast<T*>(0));
227 /**** Glib::Value<T> *******************************************************/
229 // Static data, specific to each template instantiation.
231 GType Value<T>::custom_type_ = 0;
233 template <class T> inline
234 void Value<T>::set(const typename Value<T>::CppType& data)
236 // Assume the value is already default-initialized. See value_init_func().
237 *static_cast<T*>(gobject_.data[0].v_pointer) = data;
240 template <class T> inline
241 typename Value<T>::CppType Value<T>::get() const
243 // Assume the pointer is not NULL. See value_init_func().
244 return *static_cast<T*>(gobject_.data[0].v_pointer);
249 GType Value<T>::value_type()
253 custom_type_ = Glib::custom_boxed_type_register(
254 typeid(CppType).name(),
255 &Value<T>::value_init_func,
256 &Value<T>::value_free_func,
257 &Value<T>::value_copy_func);
264 void Value<T>::value_init_func(GValue* value)
266 // Never store a NULL pointer (unless we're out of memory).
267 value->data[0].v_pointer = new(std::nothrow) T();
272 void Value<T>::value_free_func(GValue* value)
274 delete static_cast<T*>(value->data[0].v_pointer);
279 void Value<T>::value_copy_func(const GValue* src_value, GValue* dest_value)
281 // Assume the source is not NULL. See value_init_func().
282 const T& source = *static_cast<T*>(src_value->data[0].v_pointer);
283 dest_value->data[0].v_pointer = new(std::nothrow) T(source);
286 #endif /* DOXYGEN_SHOULD_SKIP_THIS */