More gracefully handle type mismatch errors when doing playlist things (just ignore...
[ardour.git] / libs / glibmm2 / glib / glibmm / value_custom.cc
1 // -*- c++ -*-
2 /* $Id: value_custom.cc 260 2006-04-12 08:12:12Z 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/utility.h>
23 #include <glib.h>
24
25
26 namespace
27 {
28
29 static void warn_already_registered(const char* location, const std::string& full_name)
30 {
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());
35 }
36
37 } // anonymous namespace
38
39
40 namespace Glib
41 {
42
43 GType custom_boxed_type_register(const char*   type_name,
44                                  ValueInitFunc init_func,
45                                  ValueFreeFunc free_func,
46                                  ValueCopyFunc copy_func)
47 {
48   std::string full_name ("glibmm__CustomBoxed_");
49   Glib::append_canonical_typename(full_name, type_name);
50
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.
55   //
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
58   // should be removed.
59
60   if(const GType existing_type = g_type_from_name(full_name.c_str()))
61   {
62     warn_already_registered("Glib::custom_boxed_type_register", full_name);
63     return existing_type;
64   }
65
66   // Via GTypeValueTable, we can teach GValue how to instantiate,
67   // destroy, and copy arbitrary objects of the C++ type.
68
69   const GTypeValueTable value_table =
70   {
71     init_func,
72     free_func,
73     copy_func,
74     0, // value_peek_pointer
75     0, // collect_format
76     0, // collect_value
77     0, // lcopy_format
78     0, // lcopy_value
79   };
80
81   const GTypeInfo type_info =
82   {
83     0, // class_size
84     0, // base_init
85     0, // base_finalize
86     0, // class_init_func
87     0, // class_finalize
88     0, // class_data
89     0, // instance_size
90     0, // n_preallocs
91     0, // instance_init
92     &value_table,
93   };
94
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.
98
99   return g_type_register_static(G_TYPE_BOXED, full_name.c_str(), &type_info, GTypeFlags(0));
100 }
101
102 GType custom_pointer_type_register(const char* type_name)
103 {
104   std::string full_name ("glibmm__CustomPointer_");
105   Glib::append_canonical_typename(full_name, type_name);
106
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.
111   //
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.
115
116   if(const GType existing_type = g_type_from_name(full_name.c_str()))
117   {
118     warn_already_registered("Glib::custom_pointer_type_register", full_name);
119     return existing_type;
120   }
121
122   const GTypeInfo type_info =
123   {
124     0, // class_size
125     0, // base_init
126     0, // base_finalize
127     0, // class_init_func
128     0, // class_finalize
129     0, // class_data
130     0, // instance_size
131     0, // n_preallocs
132     0, // instance_init
133     0, // value_table
134   };
135
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.
139
140   return g_type_register_static(G_TYPE_POINTER, full_name.c_str(), &type_info, GTypeFlags(0));
141 }
142
143
144 } // namespace Glib
145