| 1 | package SPVM::EqualityChecker; | ||
| 2 | |||
| 3 | 1; | ||
| 4 | |||
| 5 | =head1 Name | ||
| 6 | |||
| 7 | SPVM::EqualityChecker - Interface Type for Object Equality Checking Callback | ||
| 8 | |||
| 9 | =head1 Usage | ||
| 10 | |||
| 11 | use EqualityChecker; | ||
| 12 | use Point; | ||
| 13 | |||
| 14 | my $equality_checker = (EqualityChecker)method : int ($object1 : object, $object2 : object) { | ||
| 15 | my $point1 = (Point)$object1; | ||
| 16 | my $point2 = (Point)$object2; | ||
| 17 | |||
| 18 | if ($point1->x == $point2->x && $point1->y == $point2->y) { | ||
| 19 | return 1; | ||
| 20 | } | ||
| 21 | else { | ||
| 22 | return 0; | ||
| 23 | } | ||
| 24 | }; | ||
| 25 | |||
| 26 | my $point1 = Point->new(1, 2); | ||
| 27 | my $point2 = Point->new(5, 6); | ||
| 28 | |||
| 29 | my $is_equal = $equality_checker->($point1, $point2); | ||
| 30 | |||
| 31 | =head1 Description | ||
| 32 | |||
| 33 | L<EqualityChecker|SPVM::EqualityChecker> is the interface type for the object equality checking callback. | ||
| 34 | |||
| 35 | =head1 Interface Methods | ||
| 36 | |||
| 37 | C<required method : int ($object1 : object, $object2 : object)> | ||
| 38 | |||
| 39 | The implementation must receive two objects and if they are equal, return 1. Otherwise, return 0. | ||
| 40 | |||
| 41 | =head1 Class Methods | ||
| 42 | |||
| 43 | =head2 default_equality_checker | ||
| 44 | |||
| 45 | C<static method default_equality_checker : L<EqualityChecker|SPVM::EqualityChecker> ();> | ||
| 46 | |||
| 47 | Returns a default default equality checker. | ||
| 48 | |||
| 49 | The implementation of the default equality checker is | ||
| 50 | |||
| 51 | method : int ($object1 : EqualityCheckable, $object2 : EqualityCheckable) { | ||
| 52 | |||
| 53 | my $eq = 0; | ||
| 54 | if ($object1 && $object2) { | ||
| 55 | $eq = $object1->eq($object1, $object2); | ||
| 56 | } | ||
| 57 | elsif ($object1) { | ||
| 58 | $eq = 0; | ||
| 59 | } | ||
| 60 | elsif ($object2) { | ||
| 61 | $eq = 0; | ||
| 62 | } | ||
| 63 | else { | ||
| 64 | $eq = 1; | ||
| 65 | } | ||
| 66 | |||
| 67 | return $eq; | ||
| 68 | }; | ||
| 69 | |||
| 70 | If $object1 and $object2 are defined, the check of equality is perfermed by L<EqualityCheckable#eq|SPVM::EqualityCheckable/"eq"> method in the class of $object1, and returns its return value. | ||
| 71 | |||
| 72 | If only $object1 is defined, returns 0. | ||
| 73 | |||
| 74 | If only $object2 is defined, returns 0. | ||
| 75 | |||
| 76 | If both $object1 and $object2 are not defined, returns 1. | ||
| 77 | |||
| 78 | =head1 Copyright & License | ||
| 79 | |||
| 80 | Copyright (c) 2023 Yuki Kimoto | ||
| 81 | |||
| 82 | MIT License |