File Coverage

blib/lib/UUID/FFI.pm
Criterion Covered Total %
statement 67 68 98.5
branch 5 8 62.5
condition n/a
subroutine 22 23 95.6
pod 11 11 100.0
total 105 110 95.4


line stmt bran cond sub pod time code
1             package UUID::FFI;
2              
3 1     1   231007 use strict;
  1         6  
  1         31  
4 1     1   5 use warnings;
  1         2  
  1         23  
5 1     1   28 use 5.008001;
  1         4  
6 1     1   740 use FFI::Platypus 1.00;
  1         7395  
  1         29  
7 1     1   529 use FFI::Platypus::Memory ();
  1         13229  
  1         28  
8 1     1   528 use FFI::CheckLib ();
  1         2916  
  1         30  
9 1     1   8 use Carp qw( croak );
  1         2  
  1         99  
10 24     24   70 use overload '<=>' => sub { $_[0]->compare($_[1]) },
11 1     1   400 '""' => sub { shift->as_hex },
12 1     1   7 bool => sub { 1 }, fallback => 1;
  1     0   2  
  1         11  
  0         0  
13              
14             # TODO: as_bin or similar
15              
16             # ABSTRACT: Universally Unique Identifiers FFI style
17             our $VERSION = '0.10'; # VERSION
18              
19              
20             my $ffi = FFI::Platypus->new( api => 1 );
21              
22             $ffi->lib(sub {
23             my @lib = eval {
24             require Alien::libuuid;
25             Alien::libuuid->VERSION('0.05');
26             Alien::libuuid->dynamic_libs;
27             };
28             return @lib if @lib;
29             @lib = FFI::CheckLib::find_lib(
30             lib => 'uuid',
31             symbol => [
32             'uuid_generate_random',
33             'uuid_generate_time',
34             'uuid_unparse',
35             'uuid_parse',
36             'uuid_copy',
37             'uuid_clear',
38             'uuid_type',
39             'uuid_variant',
40             'uuid_time',
41             'uuid_is_null',
42             'uuid_compare',
43             ]
44             );
45             die "Unable to find system libuuid with required symbols. Try installing or upgrading Alien::libuuid"
46             unless @lib;
47             return @lib;
48             });
49              
50             $ffi->attach( [uuid_generate_random => '_generate_random'] => ['opaque'] => 'void' => '$' );
51             $ffi->attach( [uuid_generate_time => '_generate_time'] => ['opaque'] => 'void' => '$' );
52             $ffi->attach( [uuid_unparse => '_unparse'] => ['opaque', 'opaque'] => 'void' => '$$' );
53             $ffi->attach( [uuid_parse => '_parse'] => ['string', 'opaque'] => 'int' => '$$' );
54             $ffi->attach( [uuid_copy => '_copy'] => ['opaque', 'opaque'] => 'void' => '$$' );
55             $ffi->attach( [uuid_clear => '_clear'] => ['opaque'] => 'void' => '$' );
56             $ffi->attach( [uuid_type => '_type'] => ['opaque'] => 'int' => '$' );
57             $ffi->attach( [uuid_variant => '_variant'] => ['opaque'] => 'int' => '$' );
58             $ffi->attach( [uuid_time => '_time'] => ['opaque', 'opaque'] => 'time_t' => '$$' );
59             $ffi->attach( [uuid_is_null => '_is_null'] => ['opaque'] => 'int' => '$' );
60             $ffi->attach( [uuid_compare => '_compare'] => ['opaque', 'opaque'] => 'int' => '$$' );
61              
62              
63             sub new
64             {
65 14     14 1 5644 my($class, $hex) = @_;
66 14 50       63 croak "usage: UUID::FFI->new($hex)" unless $hex;
67 14         52 my $self = bless \FFI::Platypus::Memory::malloc(16), $class;
68 14         60 my $r = _parse($hex, $$self);
69 14 100       189 croak "$hex is not a valid hex UUID" if $r != 0;
70 13         28 $self;
71             }
72              
73              
74             sub new_random
75             {
76 7     7 1 13956 my($class) = @_;
77 7         46 my $self = bless \FFI::Platypus::Memory::malloc(16), $class;
78 7         382 _generate_random->($$self);
79 7         50 $self;
80             }
81              
82              
83             sub new_time
84             {
85 2     2 1 1603 my($class) = @_;
86 2         14 my $self = bless \FFI::Platypus::Memory::malloc(16), $class;
87 2         294 _generate_time($$self);
88 2         17 $self;
89             }
90              
91              
92             sub new_null
93             {
94 2     2 1 3393 my($class) = @_;
95 2         13 my $self = bless \FFI::Platypus::Memory::malloc(16), $class;
96 2         8 _clear($$self);
97 2         6 $self;
98             }
99              
100              
101 3     3 1 7 sub is_null { _is_null(${$_[0]}) }
  3         30  
102              
103              
104             sub clone
105             {
106 1     1 1 414 my($self) = @_;
107 1         8 my $other = bless \FFI::Platypus::Memory::malloc(16), ref $self;
108 1         5 _copy($$other, $$self);
109 1         3 $other;
110             }
111              
112              
113             sub as_hex
114             {
115 35     35 1 2348 my($self) = @_;
116 35         55 my $data = "x" x 36;
117 35         94 my $ptr = unpack 'L!', pack 'P', $data;
118 35         175 _unparse($$self, $ptr);
119 35         149 $data;
120             }
121              
122              
123 49     49 1 94 sub compare { _compare( ${$_[0]}, ${$_[1]} ) }
  49         68  
  49         122  
124              
125             my %type_map = (
126             1 => 'time',
127             4 => 'random',
128             );
129              
130              
131             sub type
132             {
133 4     4 1 10 my($self) = @_;
134 4         15 my $r = _type($$self);
135 4 50       31 $type_map{$r} || croak "illegal type: $r";
136             }
137              
138             my @variant = qw( ncs dce microsoft other );
139              
140              
141             sub variant
142             {
143 2     2 1 10 my($self) = @_;
144 2         9 my $r = _variant($$self);
145 2 50       18 $variant[$r] || croak "illegal varient: $r";
146             }
147              
148              
149             sub time
150             {
151 1     1 1 6 my($self) = @_;
152 1         7 _time($$self, undef);
153             }
154              
155             sub DESTROY
156             {
157 26     26   7267 my($self) = @_;
158 26         120 FFI::Platypus::Memory::free($$self);
159             }
160              
161             1;
162              
163             __END__