FFI/ZMQ3/Context.pm untested

filter: Statementstmt 0.0% 0 / 54 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 77 CC 3 SCAR 13.9
CC
3
Cov
0%
BUILD
12.0
get
2.0
set
2.0
CRAP 4.0
1 package ZMQ::FFI::ZMQ3::Context;
2 $ZMQ::FFI::ZMQ3::Context::VERSION = '0.16';
3 use Moo;
4 use namespace::autoclean;
5  
6 use FFI::Raw;
7 use Carp;
8 use Try::Tiny;
9  
10 use ZMQ::FFI::ZMQ3::Socket;
11 use ZMQ::FFI::Constants qw(ZMQ_IO_THREADS ZMQ_MAX_SOCKETS);
12  
13 extends q(ZMQ::FFI::ContextBase);
14  
15 has _ffi => (
16 is => 'ro',
17 lazy => 1,
18 builder => '_init_ffi',
19 );
20  
21 sub BUILD {
22 my $self = shift;
23  
24 try {
25 $self->_ctx( $self->_ffi->{zmq_ctx_new}->() );
26 $self->check_null('zmq_ctx_new', $self->_ctx);
27 }
28 catch {
29 $self->_ctx(-1);
30 die $_;
31 };
32  
33 if ( $self->has_threads ) {
34 $self->set(ZMQ_IO_THREADS, $self->threads);
35 }
36  
37 if ( $self->has_max_sockets ) {
38 $self->set(ZMQ_MAX_SOCKETS, $self->max_sockets);
39 }
40 }
41  
42 sub get {
43 my ($self, $option) = @_;
44  
45 my $option_val = $self->_ffi->{zmq_ctx_get}->($self->_ctx, $option);
46 $self->check_error('zmq_ctx_get', $option_val);
47  
48 return $option_val;
49 }
50  
51 sub set {
52 my ($self, $option, $option_val) = @_;
53  
54 $self->check_error(
55 'zmq_ctx_set',
56 $self->_ffi->{zmq_ctx_set}->($self->_ctx, $option, $option_val)
57 );
58 }
59  
60 sub socket {
61 my ($self, $type) = @_;
62  
63 return ZMQ::FFI::ZMQ3::Socket->new(
64 ctx => $self,
65 type => $type,
66 soname => $self->soname,
67 error_helper => $self->error_helper,
68 );
69 }
70  
71 sub destroy {
72 my $self = shift;
73  
74 $self->check_error(
75 'zmq_ctx_destroy',
76 $self->_ffi->{zmq_ctx_destroy}->($self->_ctx)
77 );
78  
79 $self->_ctx(-1);
80 };
81  
82 sub _init_ffi {
83 my $self = shift;
84  
85 my $ffi = {};
86 my $soname = $self->soname;
87  
88 $ffi->{zmq_ctx_new} = FFI::Raw->new(
89 $soname => 'zmq_ctx_new',
90 FFI::Raw::ptr, # returns ctx ptr
91 # void
92 );
93  
94 $ffi->{zmq_ctx_set} = FFI::Raw->new(
95 $soname => 'zmq_ctx_set',
96 FFI::Raw::int, # error code,
97 FFI::Raw::ptr, # ctx
98 FFI::Raw::int, # opt constant
99 FFI::Raw::int # opt value
100 );
101  
102 $ffi->{zmq_ctx_get} = FFI::Raw->new(
103 $soname => 'zmq_ctx_get',
104 FFI::Raw::int, # opt value,
105 FFI::Raw::ptr, # ctx
106 FFI::Raw::int # opt constant
107 );
108  
109 $ffi->{zmq_ctx_destroy} = FFI::Raw->new(
110 $soname => 'zmq_ctx_destroy',
111 FFI::Raw::int, # retval
112 FFI::Raw::ptr # ctx to destroy
113 );
114  
115 return $ffi;
116 }
117  
118 __PACKAGE__->meta->make_immutable();
119  
120 __END__