FFI/ZMQ3/Context.pm untested

filter: Statementstmt 0.0% 0 / 58 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 85 CC 4 SCAR 14.4
CC
4
Cov
0%
BUILD
12.0
proxy
6.0
get
2.0
CRAP 4.2
1 package ZMQ::FFI::ZMQ3::Context;
2 $ZMQ::FFI::ZMQ3::Context::VERSION = '0.18';
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 proxy {
72 my ($self, $front, $back, $capture) = @_;
73  
74 $self->check_error(
75 'zmq_proxy',
76 $self->_ffi->{zmq_proxy}->(
77 $front->_socket,
78 $back->_socket,
79 defined $capture ? $capture->_socket : undef,
80 )
81 );
82 }
83  
84 sub destroy {
85 my $self = shift;
86  
87 $self->check_error(
88 'zmq_ctx_destroy',
89 $self->_ffi->{zmq_ctx_destroy}->($self->_ctx)
90 );
91  
92 $self->_ctx(-1);
93 };
94  
95 sub _init_ffi {
96 my $self = shift;
97  
98 my $ffi = {};
99 my $soname = $self->soname;
100  
101 $ffi->{zmq_ctx_new} = FFI::Raw->new(
102 $soname => 'zmq_ctx_new',
103 FFI::Raw::ptr, # returns ctx ptr
104 # void
105 );
106  
107 $ffi->{zmq_ctx_set} = FFI::Raw->new(
108 $soname => 'zmq_ctx_set',
109 FFI::Raw::int, # error code,
110 FFI::Raw::ptr, # ctx
111 FFI::Raw::int, # opt constant
112 FFI::Raw::int # opt value
113 );
114  
115 $ffi->{zmq_ctx_get} = FFI::Raw->new(
116 $soname => 'zmq_ctx_get',
117 FFI::Raw::int, # opt value,
118 FFI::Raw::ptr, # ctx
119 FFI::Raw::int # opt constant
120 );
121  
122 $ffi->{zmq_proxy} = FFI::Raw->new(
123 $soname => 'zmq_proxy',
124 FFI::Raw::int, # error code
125 FFI::Raw::ptr, # frontend
126 FFI::Raw::ptr, # backend
127 FFI::Raw::ptr, # captuer
128 );
129  
130 $ffi->{zmq_device} = FFI::Raw->new(
131 $soname => 'zmq_device',
132 FFI::Raw::int, # error code
133 FFI::Raw::int, # type
134 FFI::Raw::ptr, # frontend
135 FFI::Raw::ptr, # backend
136 );
137  
138 $ffi->{zmq_ctx_destroy} = FFI::Raw->new(
139 $soname => 'zmq_ctx_destroy',
140 FFI::Raw::int, # retval
141 FFI::Raw::ptr # ctx to destroy
142 );
143  
144 return $ffi;
145 }
146  
147 __PACKAGE__->meta->make_immutable();
148  
149 __END__