summaryrefslogtreecommitdiff
path: root/tools/pm/Common/Output/OpaqueCopyable.pm
blob: 415b03a12395af0e509e4414553d0190a5734cb8 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# -*- mode: perl; perl-indent-level: 2; indent-tabs-mode: nil -*-
# gmmproc - Common::Output::OpaqueCopyable module
#
# Copyright 2012 glibmm development team
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
#

package Common::Output::OpaqueCopyable;

use strict;
use warnings;

sub nl
{
  return Common::Output::Shared::nl @_;
}

sub _output_h_in_class ($$$)
{
  my ($wrap_parser, $c_type, $cxx_type) = @_;
  my $section_manager = $wrap_parser->get_section_manager;
  my $code_string = nl ('public:') .
                    nl (Common::Output::Shared::doxy_skip_begin) .
                    nl ('  typedef ' . $cxx_type . ' CppObjectType;') .
                    nl ('  typedef ' . $c_type . ' BaseObjectType;') .
                    nl (Common::Output::Shared::doxy_skip_end) .
                    nl ();
  my $main_section = $wrap_parser->get_main_section;

  $section_manager->push_section ($main_section);
  $section_manager->append_string ($code_string);

  my $conditional = Common::Output::Shared::default_ctor_proto $wrap_parser, $cxx_type;

  $section_manager->append_conditional ($conditional);

  my $copy_proto = 'const';
  my $reinterpret = 0;
  my $definitions = 1;
  my $virtual_dtor = 0;

  $code_string = nl ('  // Use make_a_copy = true when getting it directly from a struct.') .
                 nl ('  explicit ' . $cxx_type . '(' . $c_type . '* castitem, bool make_a_copy = false);') .
                 nl () .
                 nl (Common::Output::Shared::copy_protos_str $cxx_type) .
                 nl () .
                 nl (Common::Output::Shared::dtor_proto_str $cxx_type, $virtual_dtor) .
                 nl () .
                 nl (Common::Output::Shared::gobj_protos_str $c_type, $copy_proto, $reinterpret, $definitions) .
                 nl () .
                 nl ('protected:') .
                 nl ('  ' . $c_type . '* gobject_;') .
                 nl () .
                 nl ('private:');
  $section_manager->append_string ($code_string);
  $section_manager->pop_entry;
}

sub _output_h_after_first_namespace ($$)
{
  my ($wrap_parser, $c_type) = @_;
  my $section_manager = $wrap_parser->get_section_manager;
  my $result_type = 'plain';
  my $take_copy_by_default = 'no';
  my $open_glib_namespace = 1;
  my $const_function = 0;
  my $conditional = Common::Output::Shared::wrap_proto $wrap_parser, $c_type, $result_type, $take_copy_by_default, $open_glib_namespace, $const_function;
  my $section = Common::Output::Shared::get_section $wrap_parser, Common::Sections::H_AFTER_FIRST_NAMESPACE;

  $section_manager->append_conditional_to_section ($conditional, $section);
}

sub _output_cc ($$$$$$)
{
  my ($wrap_parser, $c_type, $cxx_type, $new_func, $copy_func, $free_func) = @_;
  my $section_manager = $wrap_parser->get_section_manager;
  my $custom_default_ctor_var = Common::Output::Shared::get_variable $wrap_parser, Common::Variables::CUSTOM_DEFAULT_CTOR;
  my $conditional = Common::Output::Shared::generate_conditional $wrap_parser;
  my $full_cxx_type = Common::Output::Shared::get_full_cxx_type $wrap_parser;
  my $code_string = nl ($full_cxx_type . '::' . $cxx_type . '()') .
                    nl (':');
  my $section = Common::Output::Shared::get_section $wrap_parser, Common::Sections::CC_GENERATED;

  if (defined $new_func and $new_func ne '' and $new_func ne 'NONE')
  {
    $code_string .= nl ('  gobject_(' . $new_func . '())');
  }
  else
  {
    $code_string .= nl ('  gobject_(0) // Allows creation of invalid wrapper, e.g. for output arguments to methods.');
  }
  $code_string .= nl ();
  $section_manager->append_string_to_conditional ($code_string, $conditional, 0);
  $section_manager->push_section ($section);
  $section_manager->append_conditional ($conditional);
  $section_manager->set_variable_for_conditional ($custom_default_ctor_var, $conditional);
# TODO: we probably have to assume that copy func must be provided.
  $code_string = nl ($full_cxx_type . '::' . $cxx_type . '(const ' . $cxx_type . '& src)') .
                 nl (':') .
                 nl ('  gobject_((src.gobject_) ? ' . $copy_func . '(src.gobject_) : 0)') .
                 nl ('{}') .
                 nl () .
                 nl ($full_cxx_type . '::' . $cxx_type . '(' . $c_type . '* castitem, bool make_a_copy /* = false */)') .
                 nl ('{') .
                 nl ('  if (!make_a_copy)') .
                 nl ('  {') .
                 nl ('    // It was given to use by a function which has already made a copy for us to keep.') .
                 nl ('    gobject_ = castitem;') .
                 nl ('  }') .
                 nl ('  else') .
                 nl ('  {') .
                 nl ('    // We are probably getting it via direct access to a struct,') .
                 nl ('    // so we can not just take ut - we have to take a copy if it.') .
                 nl ('    if (castitem)') .
                 nl ('    {') .
                 nl ('      gobject_ = ' . $copy_func . '(castitem);') .
                 nl ('    }') .
                 nl ('    else') .
                 nl ('    {') .
                 nl ('      gobject_ = 0;') .
                 nl ('    }') .
                 nl ('  }') .
                 nl ('}') .
                 nl ();
  if (defined $copy_func and $copy_func ne '' and $copy_func ne 'NONE')
  {
    $code_string .= nl ($full_cxx_type . '& ' . $full_cxx_type . '::operator=(const ' . $full_cxx_type . '& src)') .
                    nl ('{') .
                    nl ('  ' . $c_type . '* const new_gobject = (src.gobject_) ? ' . $copy_func . '(src.gobject_) : 0;') .
                    nl () .
                    nl ('  if (gobject_)') .
                    nl ('  {') .
                    nl ('    ' . $free_func . '(gobject_);') .
                    nl ('  }') .
                    nl () .
                    nl ('  gobject_ = new_gobject;') .
                    nl () .
                    nl ('  return *this') .
                    nl ('}') .
                    nl ();
  }
  $code_string .= nl ($full_cxx_type . '::~' . $cxx_type . '()') .
                  nl ('{') .
                  nl ('  if (gobject_)') .
                  nl ('  {') .
                  nl ('    ' . $free_func . '(gobject_);') .
                  nl ('  }') .
                  nl ('}') .
                  nl () .
                  nl ($c_type . '* ' . $full_cxx_type . '::gobj_copy() const') .
                  nl ('{') .
                  nl ('  return ' . $copy_func . '(gobject_);') .
                  nl ('}') .
                  nl ();
  $section_manager->append_string ($code_string);

  my $cc_namespace_section = Common::Output::Shared::get_section $wrap_parser, Common::Sections::CC_NAMESPACE;

  $section_manager->append_section ($cc_namespace_section);
  $code_string = nl () .
                 Common::Output::Shared::close_namespaces $wrap_parser;
  $section_manager->append_string ($code_string);
  $section_manager->pop_entry;
}

sub output ($$$$$$)
{
  my ($wrap_parser, $c_type, $cxx_type, $new_func, $copy_func, $free_func) = @_;

  _output_h_in_class $wrap_parser, $c_type, $cxx_type;
  _output_h_after_first_namespace $wrap_parser, $c_type;
  _output_cc $wrap_parser, $c_type, $cxx_type, $new_func, $copy_func, $free_func;
}

1; # indicate proper module load.