[glibmm/gmmproc-refactor: 159/164] Implement _GMMPROC_WRAP_CONDITIONALLY and _INCLUDE_IN_WRAP_INIT.



commit 684d66195147bb22ff8fb21cf9558ea05a7b3690
Author: Krzesimir Nowak <qdlacz gmail com>
Date:   Fri Jul 13 17:56:10 2012 +0200

    Implement _GMMPROC_WRAP_CONDITIONALLY and _INCLUDE_IN_WRAP_INIT.
    
    I will probably want to get rid of them anyway at some point in
    favour of passing those values as parameters of _CLASS_FOO and
    _WRAP_FOO macros.

 tools/pm/Common/Gmmproc.pm          |   93 ++++++++++++++++++++++-------------
 tools/pm/Common/WrapInit/Base.pm    |   34 ++++++++-----
 tools/pm/Common/WrapInit/GError.pm  |    4 +-
 tools/pm/Common/WrapInit/GObject.pm |    7 +--
 tools/pm/Common/WrapParser.pm       |   77 +++++++++++++++++++++++++----
 5 files changed, 152 insertions(+), 63 deletions(-)
---
diff --git a/tools/pm/Common/Gmmproc.pm b/tools/pm/Common/Gmmproc.pm
index df8f447..b6ed96b 100644
--- a/tools/pm/Common/Gmmproc.pm
+++ b/tools/pm/Common/Gmmproc.pm
@@ -150,13 +150,13 @@ sub _read_all_bases
     $fd = IO::File->new ($ccg, 'r');
     if (defined $fd)
     {
-      my $str = join '',
+      my $str = join "\n",
                      '_INSERT_SECTION(SECTION_CCG_BEGIN)',
-                     "\n",
+                     '',
                      $fd->getlines,
-                     "\n",
+                     '',
                      '_INSERT_SECTION(SECTION_CCG_END)',
-                     "\n";
+                     '';
       $tokens_store->set_ccg_tokens (_tokenize_contents_ ($str));
       $fd->close;
     }
@@ -234,6 +234,7 @@ sub _generate_wrap_init
 {
   my ($self) = @_;
   my $bases = $self->_get_bases ();
+  my %total_extra_includes = ();
   my %total_c_includes = ();
   my %total_cxx_includes = ();
   my %total_entries = ();
@@ -246,9 +247,10 @@ sub _generate_wrap_init
     foreach my $entry (@{$wrap_init_entries})
     {
       my $deprecated = $entry->get_deprecated ();
-      my $not_for_windows = $entry->get_not_for_windows ();
+      my $cpp_condition = $entry->get_cpp_condition ();
       my $c_includes = $entry->get_c_includes ();
       my $cxx_includes = $entry->get_cxx_includes ();
+      my $extra_includes = $entry->get_extra_includes ();
       my $ref = ref ($entry);
 
       if (exists ($total_entries{$ref}))
@@ -260,6 +262,11 @@ sub _generate_wrap_init
         $total_entries{$ref} = [$entry];
       }
 
+      foreach my $extra_include (@{$extra_includes})
+      {
+        $total_extra_includes{$extra_include} = undef;
+      }
+
       foreach my $pair ([$c_includes, \%total_c_includes], [$cxx_includes, \%total_cxx_includes])
       {
         my $includes = $pair->[0];
@@ -271,7 +278,7 @@ sub _generate_wrap_init
           {
             my $include_entry = $total->{$include};
 
-            foreach my $another_pair ([0, $deprecated], [1, $not_for_windows])
+            foreach my $another_pair ([0, $deprecated], [1, $cpp_condition])
             {
               my $index = $another_pair->[0];
               my $trait = $another_pair->[1];
@@ -284,7 +291,7 @@ sub _generate_wrap_init
           }
           else
           {
-            $total->{$include} = [$deprecated, $not_for_windows];
+            $total->{$include} = [$deprecated, $cpp_condition];
           }
         }
       }
@@ -305,44 +312,62 @@ sub _generate_wrap_init
   $wrap_init_cc->say ('#include <glibmm/object.h>');
   $wrap_init_cc->say ();
 
+  my @extra_includes = keys (%total_extra_includes);
+
+  if (@extra_includes > 0)
+  {
+    $wrap_init_cc->say ('// extra includes');
+
+    foreach my $extra_include (sort (@extra_includes))
+    {
+      $wrap_init_cc->say ('#include <' . $extra_include . '>');
+    }
+    $wrap_init_cc->say ();
+  }
+
   foreach my $pair (['C includes', \%total_c_includes], ['C++ includes', \%total_cxx_includes])
   {
-    my $comment = '// ' . $pair->[0];
-    my $includes = $pair->[1];
+    my $total_includes = $pair->[1];
+    my @includes = keys (%{$total_includes});
 
-    $wrap_init_cc->say ($comment);
-    foreach my $include (sort (keys (%{$includes})))
+    if (@includes > 0)
     {
-      my $traits = $includes->{$include};
-      my $deprecated = $traits->[0];
-      my $not_for_windows = $traits->[1];
+      my $comment = '// ' . $pair->[0];
 
-      if ($deprecated)
+      $wrap_init_cc->say ($comment);
+      foreach my $include (sort (@includes))
       {
-        $wrap_init_cc->say ('#ifndef ' . $deprecation_guard);
-      }
-      if ($not_for_windows)
-      {
-        $wrap_init_cc->say ('#ifndef G_OS_WIN32');
-      }
-      $wrap_init_cc->say ('#include ' . $include);
-      if ($not_for_windows)
-      {
-        $wrap_init_cc->say ('#endif // G_OS_WIN32');
-      }
-      if ($deprecated)
-      {
-        $wrap_init_cc->say ('#endif // ' . $deprecation_guard);
+        my $traits = $total_includes->{$include};
+        my $deprecated = $traits->[0];
+        my $cpp_condition = $traits->[1];
+
+        if ($deprecated)
+        {
+          $wrap_init_cc->say ('#ifndef ' . $deprecation_guard);
+        }
+        if (defined $cpp_condition and $cpp_condition ne '')
+        {
+          $wrap_init_cc->say ('#' . $cpp_condition);
+        }
+        $wrap_init_cc->say ('#include ' . $include);
+        if (defined $cpp_condition and $cpp_condition ne '')
+        {
+          $wrap_init_cc->say ('#endif // ' . $cpp_condition);
+        }
+        if ($deprecated)
+        {
+          $wrap_init_cc->say ('#endif // ' . $deprecation_guard);
+        }
       }
+      $wrap_init_cc->say ();
     }
-    $wrap_init_cc->say ();
   }
 
   my @namespaces = split (/::/, $self->_get_wrap_init_namespace ());
 
   foreach my $namespace (@namespaces)
   {
-    $wrap_init_cc->say (join ('', 'namespace ', $namespace));
+    $wrap_init_cc->say ('namespace ' . $namespace);
     $wrap_init_cc->say ('{');
     $wrap_init_cc->say ();
   }
@@ -353,9 +378,9 @@ sub _generate_wrap_init
   foreach my $entry_type (sort (keys (%total_entries)))
   {
     my $entries = $total_entries{$entry_type};
-    my $comment = join ('', '  // ', (split (/::/, $entry_type))[-1]);
+    my $entry_type_comment = (split (/::/, $entry_type))[-1];
 
-    $wrap_init_cc->say ($comment);
+    $wrap_init_cc->say ('  // ' . $entry_type_comment);
     foreach my $entry (@{$entries})
     {
       $wrap_init_cc->say ($entry->get_main_line ());
@@ -367,7 +392,7 @@ sub _generate_wrap_init
 
   foreach my $namespace (reverse (@namespaces))
   {
-    $wrap_init_cc->say (join ('', '} // namespace ', $namespace));
+    $wrap_init_cc->say ('} // namespace ' . $namespace);
     $wrap_init_cc->say ();
   }
   $wrap_init_cc->say ('// end of generated file');
diff --git a/tools/pm/Common/WrapInit/Base.pm b/tools/pm/Common/WrapInit/Base.pm
index d784367..2c31431 100644
--- a/tools/pm/Common/WrapInit/Base.pm
+++ b/tools/pm/Common/WrapInit/Base.pm
@@ -31,18 +31,26 @@ sub _get_line
 
 sub new
 {
-  my ($type, $c_includes, $cxx_includes, $deprecated, $not_for_windows, $mm_module) = @_;
-  my $class = (ref $type or $type or 'Common::WrapInit::Base');
+  my ($type, $extra_includes, $c_includes, $cxx_includes, $deprecated, $cpp_condition, $mm_module) = @_;
+  my $class = (ref ($type) or $type or 'Common::WrapInit::Base');
   my $self =
   {
+    'extra_includes' => $extra_includes,
     'c_includes' => $c_includes,
     'cxx_includes' => $cxx_includes,
     'deprecated' => $deprecated,
-    'not_for_windows' => $not_for_windows,
+    'cpp_condition' => $cpp_condition,
     'mm_module' => $mm_module
   };
 
-  return bless $self, $class;
+  return bless ($self, $class);
+}
+
+sub get_extra_includes
+{
+  my ($self) = @_;
+
+  return $self->{'extra_includes'};
 }
 
 sub get_c_includes
@@ -66,18 +74,18 @@ sub get_deprecated
   return $self->{'deprecated'};
 }
 
-sub get_not_for_windows
+sub get_cpp_condition
 {
   my ($self) = @_;
 
-  return $self->{'not_for_windows'};
+  return $self->{'cpp_condition'};
 }
 
 sub get_main_line
 {
   my ($self) = @_;
   my $deprecated = $self->get_deprecated ();
-  my $not_for_windows = $self->get_not_for_windows ();
+  my $cpp_condition = $self->get_cpp_condition ();
   my $mm_module = $self->{'mm_module'};
   my $deprecation_macro = join ('', uc ($mm_module), '_DISABLE_DEPRECATED');
   my @lines = ();
@@ -85,20 +93,20 @@ sub get_main_line
 
   if ($deprecated)
   {
-    push (@lines, join ('', '#ifndef ', $deprecation_macro, "\n"));
+    push (@lines, '#ifndef ' . $deprecation_macro);
   }
-  if ($not_for_windows)
+  if (defined $cpp_condition and $cpp_condition ne '')
   {
-    push (@lines, '#ifndef G_OS_WIN32');
+    push (@lines, '#' . $cpp_condition);
   }
   push (@lines, $self->_get_line ());
-  if ($not_for_windows)
+  if (defined $cpp_condition and $cpp_condition ne '')
   {
-    push (@lines, '#endif // G_OS_WIN32');
+    push (@lines, '#endif // ' . $cpp_condition);
   }
   if ($deprecated)
   {
-    push (@lines, join ('', '#endif // ', $deprecation_macro));
+    push (@lines, '#endif // ' . $deprecation_macro);
   }
 
   return join ("\n", @lines);
diff --git a/tools/pm/Common/WrapInit/GError.pm b/tools/pm/Common/WrapInit/GError.pm
index 21c10b3..dba07eb 100644
--- a/tools/pm/Common/WrapInit/GError.pm
+++ b/tools/pm/Common/WrapInit/GError.pm
@@ -36,9 +36,9 @@ sub _get_line
 
 sub new
 {
-  my ($type, $c_includes, $cxx_includes, $deprecated, $not_for_windows, $mm_module, $cxx_type, $error_domain) = @_;
+  my ($type, $extra_includes, $c_includes, $cxx_includes, $deprecated, $not_for_windows, $mm_module, $cxx_type, $error_domain) = @_;
   my $class = (ref $type or $type or 'Common::WrapInit::GError');
-  my $self = $class->SUPER::new ($c_includes, $cxx_includes, $deprecated, $not_for_windows, $mm_module);
+  my $self = $class->SUPER::new ($extra_includes, $c_includes, $cxx_includes, $deprecated, $not_for_windows, $mm_module);
 
   $self->{'cxx_type'} = $cxx_type;
   $self->{'error_domain'} = $error_domain;
diff --git a/tools/pm/Common/WrapInit/GObject.pm b/tools/pm/Common/WrapInit/GObject.pm
index ae64299..ecf29b3 100644
--- a/tools/pm/Common/WrapInit/GObject.pm
+++ b/tools/pm/Common/WrapInit/GObject.pm
@@ -34,8 +34,7 @@ sub _get_line
   my @lines =
   (
     join ('', '  Glib::wrap_register(', $get_type_func, '(), &::', $cxx_class_type, '::wrap_new);'),
-# TODO: use the g_ensure_registered or something from GLib.
-    join ('', '  ', $cxx_type, '::get_type();')
+    join ('', '  g_type_ensure(', $cxx_type, '::get_type());')
   );
 
   return join ("\n", @lines);
@@ -43,9 +42,9 @@ sub _get_line
 
 sub new
 {
-  my ($type, $c_includes, $cxx_includes, $deprecated, $not_for_windows, $mm_module, $get_type_func, $cxx_class_type, $cxx_type) = @_;
+  my ($type, $extra_includes, $c_includes, $cxx_includes, $deprecated, $cpp_condition, $mm_module, $get_type_func, $cxx_class_type, $cxx_type) = @_;
   my $class = (ref $type or $type or 'Common::WrapInit::GObject');
-  my $self = $class->SUPER::new ($c_includes, $cxx_includes, $deprecated, $not_for_windows, $mm_module);
+  my $self = $class->SUPER::new ($extra_includes, $c_includes, $cxx_includes, $deprecated, $cpp_condition, $mm_module);
 
   $self->{'get_type_func'} = $get_type_func;
   $self->{'cxx_class_type'} = $cxx_class_type;
diff --git a/tools/pm/Common/WrapParser.pm b/tools/pm/Common/WrapParser.pm
index 57bdb06..5b6b986 100644
--- a/tools/pm/Common/WrapParser.pm
+++ b/tools/pm/Common/WrapParser.pm
@@ -37,12 +37,18 @@ use Common::TypeInfo::Local;
 use Common::WrapInit;
 use constant
 {
+  # stages
   'STAGE_HG' => 0,
   'STAGE_CCG' => 1,
   'STAGE_INVALID' => 2,
+  # gir entry
   'GIR_RECORD' => 0,
   'GIR_CLASS' => 1,
-  'GIR_ANY' => 2
+  'GIR_ANY' => 2,
+  # temp wrap init
+  'TEMP_WRAP_INIT_EXTRA_INCLUDES' => 1,
+  'TEMP_WRAP_INIT_DEPRECATED' => 4,
+  'TEMP_WRAP_INIT_CPP_CONDITION' => 5
 };
 
 ###
@@ -1396,13 +1402,16 @@ sub _on_wrap_gerror ($)
   my $cxx_includes = [join ('', '"', $self->get_base (), '.h"')];
 # TODO: Add deprecated option to _WRAP_GERROR
   my $deprecated = 0;
-# TODO: Add "not for windows" option to _WRAP_GERROR
-  my $not_for_windows = 0;
+# TODO: Add "C preprocessor condition" option to _WRAP_GERROR
+  my $cpp_condition = 0;
+# TODO: Add "Extra include" option to _WRAP_GERROR
+  my $extra_includes = [];
   my $complete_cxx_type = Common::Output::Shared::get_complete_cxx_type ($self);
-  my $wrap_init_entry = Common::WrapInit::GError->new ($c_includes,
+  my $wrap_init_entry = Common::WrapInit::GError->new ($extra_includes,
+                                                       $c_includes,
                                                        $cxx_includes,
                                                        $deprecated,
-                                                       $not_for_windows,
+                                                       $cpp_condition,
                                                        $self->get_mm_module (),
                                                        $gir_domain,
                                                        $complete_cxx_type);
@@ -1502,6 +1511,7 @@ sub push_temp_wrap_init
   push (@{$temp_wrap_init_stack},
         [
           $level,
+          [],
           $c_includes,
           $cxx_includes,
           $deprecated,
@@ -2789,14 +2799,15 @@ sub _on_is_deprecated
 
     if ($temp_wrap_init->[0] == $level)
     {
-      $temp_wrap_init->[3] = 1;
+      $temp_wrap_init->[TEMP_WRAP_INIT_DEPRECATED] = 1;
     }
   }
 }
 
-sub _on_gtkmmproc_win32_no_wrap
+# TODO: move it elsewhere in the file.
+sub _add_wrap_init_condition
 {
-  my ($self) = @_;
+  my ($self, $cpp_condition) = @_;
   my $temp_wrap_init_stack = $self->_get_temp_wrap_init_stack ();
 
   if (@{$temp_wrap_init_stack})
@@ -2806,11 +2817,19 @@ sub _on_gtkmmproc_win32_no_wrap
 
     if ($temp_wrap_init->[0] == $level)
     {
-      $temp_wrap_init->[4] = 1;
+      $temp_wrap_init->[TEMP_WRAP_INIT_CPP_CONDITION] = $cpp_condition;
     }
   }
 }
 
+sub _on_gtkmmproc_win32_no_wrap
+{
+  my ($self) = @_;
+
+  $self->fixed_warning ('Deprecated. Use _GMMPROC_WRAP_CONDITIONALLY instead.');
+  $self->_add_wrap_init_condition ('ifndef G_OS_WIN32');
+}
+
 sub _on_ascii_func
 {
   my ($self) = @_;
@@ -3057,6 +3076,42 @@ sub _on_gmmproc_extra_namespace
   $self->_extract_bracketed_text ();
 }
 
+sub _on_gmmproc_wrap_conditionally
+{
+  my ($self) = @_;
+  my $cpp_condition = Common::Util::string_trim ($self->_extract_bracketed_text());
+
+  if ($cpp_condition =~ /^#/)
+  {
+    $cpp_condition =~ s/^#//;
+  }
+
+  if ($cpp_condition !~ /^(?:(?:ifndef)|(?:ifdef)|(?:if))/)
+  {
+    $self->fixed_error ('Expected C preprocessor conditional (if, ifdef, ifndef))');
+  }
+
+  $self->_add_wrap_init_condition ($cpp_condition);
+}
+
+sub _on_include_in_wrap_init
+{
+  my ($self) = @_;
+  my $temp_wrap_init_stack = $self->_get_temp_wrap_init_stack ();
+  my $extra_include = Common::Util::string_trim ($self->_extract_bracketed_text());
+
+  if (@{$temp_wrap_init_stack})
+  {
+    my $temp_wrap_init = $temp_wrap_init_stack->[-1];
+    my $level = $self->get_level ();
+
+    if ($temp_wrap_init->[0] == $level)
+    {
+      push (@{$temp_wrap_init->[TEMP_WRAP_INIT_EXTRA_INCLUDES]}, $extra_include);
+    }
+  }
+}
+
 ###
 ### HANDLERS ABOVE
 ###
@@ -3232,7 +3287,9 @@ sub new ($$$$$$)
     '_MEMBER_GET_PTR' => sub { $self->_on_member_get_ptr (@_); },
     '_MEMBER_GET_GOBJECT' => sub { $self->_on_member_get_gobject (@_); },
     '_MEMBER_GET_REF_PTR' => sub { $self->_on_member_get_ref_ptr (@_); },
-    '_GMMPROC_EXTRA_NAMESPACE' => sub { $self->_on_gmmproc_extra_namespace (@_); }
+    '_GMMPROC_EXTRA_NAMESPACE' => sub { $self->_on_gmmproc_extra_namespace (@_); },
+    '_GMMPROC_WRAP_CONDITIONALLY' => sub { $self->_on_gmmproc_wrap_conditionally (@_); },
+    '_INCLUDE_IN_WRAP_INIT' => sub { $self->_on_include_in_wrap_init (@_); }
   };
 
   return $self;



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]