| 1 | package CBOR::Free::Decoder; | ||||||||||
| 2 | |||||||||||
| 3 | =encoding utf8 | ||||||||||
| 4 | |||||||||||
| 5 | =head1 NAME | ||||||||||
| 6 | |||||||||||
| 7 | CBOR::Free::Decoder | ||||||||||
| 8 | |||||||||||
| 9 | =head1 SYNOPSIS | ||||||||||
| 10 | |||||||||||
| 11 | my $decoder = CBOR::Free::Decoder->new()->set_tag_handlers( | ||||||||||
| 12 | 2 => sub { DateTime->from_epoch( epoch => shift() ) }, | ||||||||||
| 13 | ); | ||||||||||
| 14 | |||||||||||
| 15 | # Enable shared/circular references: | ||||||||||
| 16 | $decoder->preserve_references(); | ||||||||||
| 17 | |||||||||||
| 18 | =head1 DESCRIPTION | ||||||||||
| 19 | |||||||||||
| 20 | This class provides an object-oriented interface to L<CBOR::Free>’s | ||||||||||
| 21 | decoder. This interface allows interpretation of tagged values. | ||||||||||
| 22 | |||||||||||
| 23 | =cut | ||||||||||
| 24 | |||||||||||
| 25 | #---------------------------------------------------------------------- | ||||||||||
| 26 | |||||||||||
| 27 | 2 | ▶ | use CBOR::Free (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | |||||||||||
| 29 | #---------------------------------------------------------------------- | ||||||||||
| 30 | |||||||||||
| 31 | =head1 METHODS | ||||||||||
| 32 | |||||||||||
| 33 | =head2 $obj = I<CLASS>->new() | ||||||||||
| 34 | |||||||||||
| 35 | Creates a new CBOR decoder object. | ||||||||||
| 36 | |||||||||||
| 37 | =cut | ||||||||||
| 38 | |||||||||||
| 39 | 102 | ▶ | sub new { bless {} } # TODO: implement in XS, and store a context. | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 40 | |||||||||||
| 41 | #---------------------------------------------------------------------- | ||||||||||
| 42 | |||||||||||
| 43 | =head2 $data = I<OBJ>->decode( $CBOR ) | ||||||||||
| 44 | |||||||||||
| 45 | Same as L<CBOR::Free>’s static function of the same name but applies | ||||||||||
| 46 | any tag handlers configured in C<set_tag_handlers()>. | ||||||||||
| 47 | |||||||||||
| 48 | As in L<CBOR::Free>, any unrecognized tags prompt a warning but are | ||||||||||
| 49 | otherwise ignored. | ||||||||||
| 50 | |||||||||||
| 51 | =cut | ||||||||||
| 52 | |||||||||||
| 53 | #---------------------------------------------------------------------- | ||||||||||
| 54 | |||||||||||
| 55 | =head2 $enabled_yn = I<OBJ>->preserve_references( [$ENABLE] ) | ||||||||||
| 56 | |||||||||||
| 57 | Enables/disables recognition of CBOR’s shared references. (If no | ||||||||||
| 58 | argument is given, shared references wil be enabled.) | ||||||||||
| 59 | |||||||||||
| 60 | B<HANDLE WITH CARE.> This option can cause CBOR::Free to create circular | ||||||||||
| 61 | references, which can cause memory leaks if not handled properly. | ||||||||||
| 62 | |||||||||||
| 63 | =cut | ||||||||||
| 64 | |||||||||||
| 65 | sub preserve_references { | ||||||||||
| 66 | 1 | ▶ | return $_[0]{'_preserve_references'} = (@_ > 1 ? !!$_[1] : 1); | ||||||||
|
Subroutine
preserve_references: called CC=2 SCAR=7.6
Pod
documented
Branch: 1 branch
| |||||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | #---------------------------------------------------------------------- | ||||||||||
| 70 | |||||||||||
| 71 | =head2 I<OBJ>->set_tag_handlers( %TAG_CALLBACK ) | ||||||||||
| 72 | |||||||||||
| 73 | Takes a list of key/value pairs where each key is a tag (i.e., number) | ||||||||||
| 74 | and each value is a coderef that CBOR::Free will run when that tag is | ||||||||||
| 75 | seen during a decode operation. The coderef will receive the tagged value, | ||||||||||
| 76 | and its (scalar) return will be inserted into the decoded data structure. | ||||||||||
| 77 | |||||||||||
| 78 | To unset a tag handler, assign undef to it. | ||||||||||
| 79 | |||||||||||
| 80 | This returns the I<OBJ>. | ||||||||||
| 81 | |||||||||||
| 82 | B<NOTE:> Handlers assigned here will only fire if CBOR::Free itself | ||||||||||
| 83 | doesn’t decode the tag. For example, a handler for the “indirection” tag | ||||||||||
| 84 | here will be ignored. | ||||||||||
| 85 | |||||||||||
| 86 | =cut | ||||||||||
| 87 | |||||||||||
| 88 | 2 | ▶ | use constant _TAG_PACK_TMPL => eval { pack 'Q' } ? 'Q' : 'L'; | ||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.0
Branch: 1 branch
| |||||||||||
| 89 | |||||||||||
| 90 | sub set_tag_handlers { | ||||||||||
| 91 | 101 | ▶ | my ($self, @tag_cb) = @_; | ||||||||
|
Subroutine
set_tag_handlers: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 92 | |||||||||||
| 93 | 101 | ▶ | while (my ($tag, $cb) = splice @tag_cb, 0, 2) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 94 | 101 | $self->{'_tag_decode_callback'}{ pack( _TAG_PACK_TMPL(), $tag ) } = $cb; | |||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | 101 | return $self; | |||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | 2 | 1; | |||||||||