[glibmm/gmmproc-refactor: 16/23] Move Base and Store class elsewhere.



commit 9ea90d04223a2a5c3efd2fb5141277cb033a6c22
Author: Krzesimir Nowak <qdlacz gmail com>
Date:   Mon Sep 26 14:49:41 2011 +0200

    Move Base and Store class elsewhere.
    
    Also, Misc.pm has some contents of Common.pm, so the latter is
    removed.

 tools/pm/Gir/Handlers/Base.pm                   |   60 --------
 tools/pm/Gir/Handlers/Common.pm                 |  100 -------------
 tools/pm/Gir/Handlers/Generated/Common/Base.pm  |  179 +++++++++++++++++++++++
 tools/pm/Gir/Handlers/Generated/Common/Misc.pm  |  116 +++++++++++++++
 tools/pm/Gir/Handlers/Generated/Common/Store.pm |   59 ++++++++
 tools/pm/Gir/Handlers/Store.pm                  |   42 ------
 6 files changed, 354 insertions(+), 202 deletions(-)
---
diff --git a/tools/pm/Gir/Handlers/Generated/Common/Base.pm b/tools/pm/Gir/Handlers/Generated/Common/Base.pm
new file mode 100644
index 0000000..093ee06
--- /dev/null
+++ b/tools/pm/Gir/Handlers/Generated/Common/Base.pm
@@ -0,0 +1,179 @@
+## Copyright 2011 Krzesimir Nowak
+##
+## 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 Gir::Handlers::Generated::Common::Base;
+
+use strict;
+use warnings;
+
+use Gir::Handlers::Generated::Common::Misc;
+use Gir::Handlers::Generated::Common::Store;
+
+##
+## private
+##
+sub _base_gen_subhandlers_impl ($$)
+{
+  my (undef, $tags) = @_;
+  my %subhandlers = map { $_ => 'Gir::Handlers::Generated::' . Gir::Handlers::Generated::Common::Misc::module_from_tag ($_) } @{$tags};
+
+  return \%subhandlers;
+}
+
+##
+## private virtuals:
+##
+sub _setup_handlers ($)
+{
+  #TODO: throw an error;
+  print STDERR 'Gir::Handlers::Generated::Common::Base::_setup_handlers ($) is not implemented.' . "\n";
+  exit 1;
+}
+
+sub _setup_subhandlers ($)
+{
+  #TODO: throw an error;
+  print STDERR 'Gir::Handlers::Generated::Common::Base::_setup_subhandlers ($) is not implemented.' . "\n";
+  exit 1;
+}
+
+sub _generate_subhandlers ($$)
+{
+  my ($self, $tags) = @_;
+  my $generator = $self->{'generator'};
+
+  return $self->$generator ($tags);
+}
+
+##
+## protected:
+##
+sub _set_handlers ($$$)
+{
+  my ($self, $start_handlers, $end_handlers) = @_;
+
+  $self->{'start_handlers'} = $start_handlers;
+  $self->{'end_handlers'} = $end_handlers;
+}
+
+sub _set_subhandlers ($$)
+{
+  my ($self, $subhandlers) = @_;
+
+  $self->{'subhandlers'} = $subhandlers;
+}
+
+sub _set_generator ($$)
+{
+  my ($self, $generator) = @_;
+
+  $self->{'generator'} = $generator;
+}
+
+sub _set_start_ignores ($$)
+{
+  my ($self, $ignores) = @_;
+
+  $self->{'start_ignored'} = $ignores;
+}
+
+sub _set_end_ignores ($$)
+{
+  my ($self, $ignores) = @_;
+
+  $self->{'end_ignored'} = $ignores;
+}
+
+sub _is_start_ignored ($$)
+{
+  my ($self, $tag) = @_;
+  my $ignores = $self->{'start_ignored'};
+
+  return (exists ($ignores->{$tag}) or exists ($ignores->{'*'}));
+}
+
+sub _is_end_ignored ($$)
+{
+  my ($self, $tag) = @_;
+  my $ignores = $self->{'end_ignored'};
+
+  return (exists ($ignores->{$tag}) or exists ($ignores->{'*'}));
+}
+
+##
+## public:
+##
+sub new ($)
+{
+  my $type = shift;
+  my $class = (ref ($type) or $type or 'Gir::Handlers::Generated::Common::Base');
+  my $self =
+  {
+    'start_handlers' => {},
+    'end_handlers' => {},
+    'start_ignored' => {},
+    'end_ignored' => {},
+    'subhandlers' => {},
+    'generator' => \&_base_gen_subhandlers_impl
+  };
+
+  return bless ($self, $class);
+}
+
+sub get_start_handlers ($)
+{
+  my $self = shift;
+
+  return $self->{'start_handlers'};
+}
+
+sub get_end_handlers ($)
+{
+  my $self = shift;
+
+  return $self->{'end_handlers'};
+}
+
+sub get_subhandlers_for ($$)
+{
+  my ($self, $elem) = @_;
+  my $subhandlers = $self->{'subhandlers'};
+  my $package = undef;
+
+  if (exists ($subhandlers->{$elem}))
+  {
+    $package = $subhandlers->{$elem};
+  }
+  elsif (exists ($subhandlers->{'*'}))
+  {
+    $package = $subhandlers->{'*'};
+  }
+
+  if (defined ($package))
+  {
+    my $generator = $self->{'generator'};
+    my $instance = $package->new ();
+
+    $instance->_set_generator ($generator);
+    $instance->_setup_handlers ();
+    $instance->_setup_subhandlers ();
+    return $instance;
+  }
+  return undef;
+}
+
+1; # indicate proper module load.
diff --git a/tools/pm/Gir/Handlers/Generated/Common/Misc.pm b/tools/pm/Gir/Handlers/Generated/Common/Misc.pm
new file mode 100644
index 0000000..da32ce8
--- /dev/null
+++ b/tools/pm/Gir/Handlers/Generated/Common/Misc.pm
@@ -0,0 +1,116 @@
+## Copyright 2011 Krzesimir Nowak
+##
+## 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 Gir::Handlers::Generated::Common::Misc;
+
+use strict;
+use warnings;
+
+##
+## public:
+##
+sub extract_values($$$)
+{
+  my ($keys, $optional_keys, $atts_vals) = @_;
+  my $params = {};
+  my %check = ();
+  my %leftovers = ();
+  my $leftover = undef;
+  my $att = undef;
+
+  foreach my $key (@{$keys})
+  {
+    $params->{$key} = undef;
+    $check{$key} = undef;
+  }
+  foreach my $pair (@{$optional_keys})
+  {
+    $params->{$pair->[0]} = $pair->[1];
+  }
+
+  foreach my $entry (@{$atts_vals})
+  {
+    if (defined ($leftover))
+    {
+      $leftovers{$leftover} = $entry;
+      $leftover = undef;
+    }
+    elsif (not defined ($att))
+    {
+      if (exists ($params->{$entry}))
+      {
+        $att = $entry;
+        delete ($check{$att});
+      }
+      else
+      {
+        $leftover = $entry;
+      }
+    }
+    else
+    {
+      $params->{$att} = $entry;
+      $att = undef;
+    }
+  }
+
+  my @check_keys = sort keys (%check);
+  my $message = '';
+
+  if (@check_keys > 0)
+  {
+    $message .= 'Missing attributes:' . "\n";
+
+    foreach my $key (@check_keys)
+    {
+      $message .= '  ' . $key . "\n";
+    }
+  }
+
+  my @leftover_keys = sort keys %leftovers;
+
+  if (@leftover_keys > 0)
+  {
+    $message .= 'Leftover attributes:' . "\n";
+
+    foreach $leftover (@leftover_keys)
+    {
+      $message .= "  " . $leftover . " => " . $leftovers{$leftover} . "\n";
+    }
+  }
+
+  if ($message)
+  {
+    # TODO: throw an error.
+    print STDERR $message;
+    exit 1;
+  }
+
+  return $params;
+}
+
+sub module_from_tag ($)
+{
+  # unreadable, huh?
+  # - splits 'foo-BAR:bAz' to 'foo', 'BAR' and 'bAz'
+  # - changes 'foo' to 'Foo', 'BAR' to 'Bar' and 'bAz' to 'Baz'
+  # - joins 'Foo', 'Bar' and 'Baz' into one string 'FooBarBaz'
+  # - returns the joined string
+  return join ('', map { ucfirst lc } split (/\W+/, shift));
+}
+
+1; # indicate proper module load.
diff --git a/tools/pm/Gir/Handlers/Generated/Common/Store.pm b/tools/pm/Gir/Handlers/Generated/Common/Store.pm
new file mode 100644
index 0000000..bad52a7
--- /dev/null
+++ b/tools/pm/Gir/Handlers/Generated/Common/Store.pm
@@ -0,0 +1,59 @@
+## Copyright 2011 Krzesimir Nowak
+##
+## 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 Gir::Handlers::Generated::Common::Store;
+
+use strict;
+use warnings;
+
+##
+## public:
+##
+sub new ($$)
+{
+  my ($type, $methods) = @_;
+  my $class = (ref ($type) or $type or 'Gir::Handlers::Stores::Store');
+  my $self =
+  {
+    'methods' => $methods
+  };
+
+  return bless ($self, $class);
+}
+
+sub has_method_for ($$)
+{
+  my ($self, $elem) = @_;
+  my $methods = $self->{'methods'};
+
+  return exists ($methods->{$elem});
+}
+
+sub get_method_for ($$)
+{
+  my ($self, $elem) = @_;
+
+  if ($self->has_method_for ($elem))
+  {
+    my $methods = $self->{'methods'};
+
+    return $methods->{$elem};
+  }
+  # TODO: error.
+}
+
+1;



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