summaryrefslogtreecommitdiff
path: root/libs/glibmm2/glibmm/value_custom.cc
blob: f27747771238c5c2810431d2f94a10620a79c1ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// -*- c++ -*-
/* $Id$ */

/* Copyright 2002 The gtkmm Development Team
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <glibmm/value.h>
#include <glibmm/utility.h>
#include <glib.h>


namespace
{

void warn_already_registered(const char* location, const std::string& full_name)
{
  g_warning("file %s: (%s): The type name `%s' has been registered already.\n"
            "This is not supposed to happen -- please send a mail with detailed "
            "information about your platform to gtkmm-list@gnome.org.  Thanks.\n",
            __FILE__, location, full_name.c_str());
}

} // anonymous namespace


namespace Glib
{

GType custom_boxed_type_register(const char*   type_name,
                                 ValueInitFunc init_func,
                                 ValueFreeFunc free_func,
                                 ValueCopyFunc copy_func)
{
  std::string full_name ("glibmm__CustomBoxed_");
  Glib::append_canonical_typename(full_name, type_name);

  // Templates of the same type _might_ be duplicated when instantiated in
  // multiple translation units -- I'm not sure whether this is true.  If the
  // static custom_type_ variable in Value<> is duplicated, then the type
  // would be registered more than once.
  //
  // Lookup the type name to see whether this scenario actually happens.
  // If this turns out to be common behaviour on some platform the warning
  // should be removed.

  if(const GType existing_type = g_type_from_name(full_name.c_str()))
  {
    warn_already_registered("Glib::custom_boxed_type_register", full_name);
    return existing_type;
  }

  // Via GTypeValueTable, we can teach GValue how to instantiate,
  // destroy, and copy arbitrary objects of the C++ type.

  const GTypeValueTable value_table =
  {
    init_func,
    free_func,
    copy_func,
    0, // value_peek_pointer
    0, // collect_format
    0, // collect_value
    0, // lcopy_format
    0, // lcopy_value
  };

  const GTypeInfo type_info =
  {
    0, // class_size
    0, // base_init
    0, // base_finalize
    0, // class_init_func
    0, // class_finalize
    0, // class_data
    0, // instance_size
    0, // n_preallocs
    0, // instance_init
    &value_table,
  };

  // Don't use g_boxed_type_register_static(), because that wouldn't allow
  // for a non-NULL default value.  The implementation of g_boxed_copy() will
  // use our custom GTypeValueTable automatically.

  return g_type_register_static(G_TYPE_BOXED, full_name.c_str(), &type_info, GTypeFlags(0));
}

GType custom_pointer_type_register(const char* type_name)
{
  std::string full_name ("glibmm__CustomPointer_");
  Glib::append_canonical_typename(full_name, type_name);

  // Templates of the same type _might_ be duplicated when instantiated in
  // multiple translation units -- I'm not sure whether this is true.  If the
  // static custom_type variable in Value<>::value_type_() is duplicated, then
  // the type would be registered more than once.
  //
  // Lookup the type name to see whether this scenario actually happens.
  // If this turns out to be common behaviour on some platform the warning
  // should be removed.

  if(const GType existing_type = g_type_from_name(full_name.c_str()))
  {
    warn_already_registered("Glib::custom_pointer_type_register", full_name);
    return existing_type;
  }

  const GTypeInfo type_info =
  {
    0, // class_size
    0, // base_init
    0, // base_finalize
    0, // class_init_func
    0, // class_finalize
    0, // class_data
    0, // instance_size
    0, // n_preallocs
    0, // instance_init
    0, // value_table
  };

  // We could probably use g_pointer_type_register_static(), but I want
  // to keep this function symmetric to custom_boxed_type_register().  Also,
  // g_pointer_type_register_static() would lookup the type name once again.

  return g_type_register_static(G_TYPE_POINTER, full_name.c_str(), &type_info, GTypeFlags(0));
}


} // namespace Glib