MayaChemTools

   1 package ObjectProperty;
   2 #
   3 # $RCSfile: ObjectProperty.pm,v $
   4 # $Date: 2008/04/25 00:00:46 $
   5 # $Revision: 1.10 $
   6 #
   7 # Author: Manish Sud <msud@san.rr.com>
   8 #
   9 # Copyright (C) 2004-2008 Manish Sud. All rights reserved.
  10 #
  11 # This file is part of MayaChemTools.
  12 #
  13 # MayaChemTools is free software; you can redistribute it and/or modify it under
  14 # the terms of the GNU Lesser General Public License as published by the Free
  15 # Software Foundation; either version 3 of the License, or (at your option) any
  16 # later version.
  17 #
  18 # MayaChemTools is distributed in the hope that it will be useful, but without
  19 # any warranty; without even the implied warranty of merchantability of fitness
  20 # for a particular purpose.  See the GNU Lesser General Public License for more
  21 # details.
  22 #
  23 # You should have received a copy of the GNU Lesser General Public License
  24 # along with MayaChemTools; if not, see <http://www.gnu.org/licenses/> or
  25 # write to the Free Software Foundation Inc., 59 Temple Place, Suite 330,
  26 # Boston, MA, 02111-1307, USA.
  27 #
  28 use 5.006;
  29 use strict;
  30 use Carp;
  31 
  32 use vars qw($VERSION $AUTOLOAD);
  33 
  34 $VERSION = '1.00';
  35 
  36 # Set property for an object...
  37 sub SetProperty {
  38   my($This, $Name, $Value) = @_;
  39 
  40   if (!(defined($Name) && defined($Value))) {
  41     return undef;
  42   }
  43   return $This->_SetProperty($Name, $Value);
  44 }
  45 
  46 # Set properties for an object...
  47 sub SetProperties {
  48   my($This, %NamesAndValues) = @_;
  49   my($Name, $Value);
  50 
  51   while (($Name, $Value) = each  %NamesAndValues) {
  52     $This->_SetProperty($Name, $Value);
  53   }
  54 
  55   return $This;
  56 }
  57 
  58 # Set object property...
  59 sub _SetProperty {
  60   my($This, $Name, $Value) = @_;
  61 
  62   $This->{$Name} = $Value;
  63 }
  64 
  65 # Get property for an object...
  66 sub GetProperty {
  67   my($This, $Name) = @_;
  68 
  69   if (!defined $Name) {
  70     return undef;
  71   }
  72   return $This->_GetProperty($Name);
  73 }
  74 
  75 # Get object property...
  76 sub _GetProperty {
  77   my($This, $Name) = @_;
  78 
  79   if (exists $This->{$Name}) {
  80     return $This->{$Name};
  81   }
  82   else {
  83     return undef;
  84   }
  85 }
  86 
  87 # Does this property exist?
  88 sub HasProperty {
  89   my($This, $Name) = @_;
  90 
  91   if (!defined $Name) {
  92     return 0;
  93   }
  94   return (exists $This->{$Name}) ? 1 : 0;
  95 }
  96 
  97 # Delete object property...
  98 sub DeleteProperty {
  99   my($This, $Name) = @_;
 100 
 101   if (!defined $Name) {
 102     return undef;
 103   }
 104   return $This->_DeleteProperty($Name);
 105 }
 106 
 107 # Delete object property...
 108 sub _DeleteProperty {
 109   my($This, $Name) = @_;
 110 
 111   if (exists $This->{$Name}) {
 112     delete $This->{$Name};
 113   }
 114   return $This;
 115 }
 116 
 117 # Implements Set<PropertyName> and Get<PropertyName> methods...
 118 sub AUTOLOAD {
 119   my($This, $PropertyValue) = @_;
 120   my($PackageName, $MethodName, $PropertyName, $ThisType);
 121 
 122   ($PackageName, $MethodName) = $AUTOLOAD =~ /^(.*?)::(.*?)$/;
 123 
 124   if ($MethodName =~ /^(BEGIN|DESTROY)$/) {
 125     return;
 126   }
 127 
 128   $ThisType = ref($This) or croak "Error: Invocation of function ${PackageName}::${MethodName} invocation is not supported: It must be invoked using an object reference...";
 129 
 130   if (!($MethodName =~ /^Get/ || $MethodName =~ /^Set/ || $MethodName =~ /^Delete/)) {
 131     croak "Error: Can't locate object method \"$MethodName\" via package \"$ThisType\": This method is not automatically implemented by AUTOLOAD: Only Get<PropertyName>, Set<PropertyName> and Delete<PropertyName> functions are implemented via AUTOLOAD...";
 132   }
 133   if ($MethodName =~ /^Delete/) {
 134     ($PropertyName) = $MethodName =~ /^Delete(.*?)$/;
 135   }
 136   else {
 137     ($PropertyName) = $MethodName =~ /^[SG]et(.*?)$/;
 138   }
 139   if ($MethodName =~ /^Set/ && !defined($PropertyValue)) {
 140     carp "Warning:  ${PackageName}::${MethodName}: Didn't set value for property $PropertyName: Property value for must be specified...\n";
 141     return undef;
 142   }
 143 
 144   if ($MethodName =~ /^Get/) {
 145     return $This->_GetProperty($PropertyName);
 146   }
 147   elsif ($MethodName =~ /^Set/) {
 148     return $This->_SetProperty($PropertyName, $PropertyValue);
 149   }
 150   elsif ($MethodName =~ /^Delete/) {
 151     return $This->_DeleteProperty($PropertyName);
 152   }
 153 
 154 }
 155