2 /* $Id: value_custom.cc 260 2006-04-12 08:12:12Z 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/utility.h>
29 static void warn_already_registered(const char* location, const std::string& full_name)
31 g_warning("file %s: (%s): The type name `%s' has been registered already.\n"
32 "This is not supposed to happen -- please send a mail with detailed "
33 "information about your platform to gtkmm-list@gnome.org. Thanks.\n",
34 __FILE__, location, full_name.c_str());
37 } // anonymous namespace
43 GType custom_boxed_type_register(const char* type_name,
44 ValueInitFunc init_func,
45 ValueFreeFunc free_func,
46 ValueCopyFunc copy_func)
48 std::string full_name ("glibmm__CustomBoxed_");
49 Glib::append_canonical_typename(full_name, type_name);
51 // Templates of the same type _might_ be duplicated when instantiated in
52 // multiple translation units -- I'm not sure whether this is true. If the
53 // static custom_type_ variable in Value<> is duplicated, then the type
54 // would be registered more than once.
56 // Lookup the type name to see whether this scenario actually happens.
57 // If this turns out to be common behaviour on some platform the warning
60 if(const GType existing_type = g_type_from_name(full_name.c_str()))
62 warn_already_registered("Glib::custom_boxed_type_register", full_name);
66 // Via GTypeValueTable, we can teach GValue how to instantiate,
67 // destroy, and copy arbitrary objects of the C++ type.
69 const GTypeValueTable value_table =
74 0, // value_peek_pointer
81 const GTypeInfo type_info =
95 // Don't use g_boxed_type_register_static(), because that wouldn't allow
96 // for a non-NULL default value. The implementation of g_boxed_copy() will
97 // use our custom GTypeValueTable automatically.
99 return g_type_register_static(G_TYPE_BOXED, full_name.c_str(), &type_info, GTypeFlags(0));
102 GType custom_pointer_type_register(const char* type_name)
104 std::string full_name ("glibmm__CustomPointer_");
105 Glib::append_canonical_typename(full_name, type_name);
107 // Templates of the same type _might_ be duplicated when instantiated in
108 // multiple translation units -- I'm not sure whether this is true. If the
109 // static custom_type variable in Value<>::value_type_() is duplicated, then
110 // the type would be registered more than once.
112 // Lookup the type name to see whether this scenario actually happens.
113 // If this turns out to be common behaviour on some platform the warning
114 // should be removed.
116 if(const GType existing_type = g_type_from_name(full_name.c_str()))
118 warn_already_registered("Glib::custom_pointer_type_register", full_name);
119 return existing_type;
122 const GTypeInfo type_info =
127 0, // class_init_func
136 // We could probably use g_pointer_type_register_static(), but I want
137 // to keep this function symmetric to custom_boxed_type_register(). Also,
138 // g_pointer_type_register_static() would lookup the type name once again.
140 return g_type_register_static(G_TYPE_POINTER, full_name.c_str(), &type_info, GTypeFlags(0));