Class: CZMQ::FFI::Zchunk

Inherits:
Object
  • Object
show all
Defined in:
lib/czmq-ffi-gen/czmq/ffi/zchunk.rb

Overview

Note:

This class is 100% generated using zproject.

work with memory chunks

Defined Under Namespace

Classes: DestroyedError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ptr, finalize = true) ⇒ Zchunk

Attaches the pointer ptr to this instance and defines a finalizer for it if necessary.

Parameters:

  • ptr (::FFI::Pointer)
  • finalize (Boolean) (defaults to: true)


24
25
26
27
28
29
30
31
32
# File 'lib/czmq-ffi-gen/czmq/ffi/zchunk.rb', line 24

def initialize(ptr, finalize = true)
  @ptr = ptr
  if @ptr.null?
    @ptr = nil # Remove null pointers so we don't have to test for them.
  elsif finalize
    @finalizer = self.class.create_finalizer_for @ptr
    ObjectSpace.define_finalizer self, @finalizer
  end
end

Class Method Details

.__newObject



18
# File 'lib/czmq-ffi-gen/czmq/ffi/zchunk.rb', line 18

alias :__new :new

.create_finalizer_for(ptr) ⇒ Proc

Parameters:

  • ptr (::FFI::Pointer)

Returns:

  • (Proc)


35
36
37
38
39
40
41
42
# File 'lib/czmq-ffi-gen/czmq/ffi/zchunk.rb', line 35

def self.create_finalizer_for(ptr)
  ptr_ptr = ::FFI::MemoryPointer.new :pointer

  Proc.new do
    ptr_ptr.write_pointer ptr
    ::CZMQ::FFI.zchunk_destroy ptr_ptr
  end
end

.destructor_fnObject

Note:

WARNING: If your Ruby code doesn’t retain a reference to the FFI::Function object after passing it to a C function call, it may be garbage collected while C still holds the pointer, potentially resulting in a segmentation fault.

Create a new callback of the following type: Destroy an item

typedef void (zchunk_destructor_fn) (
    void **hint);


86
87
88
89
90
91
# File 'lib/czmq-ffi-gen/czmq/ffi/zchunk.rb', line 86

def self.destructor_fn
  ::FFI::Function.new :void, [:pointer], blocking: true do |hint|
    result = yield hint
    result
  end
end

.frommem(data, size, destructor, hint) ⇒ CZMQ::Zchunk

Create a new chunk from memory. Take ownership of the memory and calling the destructor on destroy.

Parameters:

  • data (::FFI::Pointer, #to_ptr)
  • size (Integer, #to_int, #to_i)
  • destructor (::FFI::Pointer, #to_ptr)
  • hint (::FFI::Pointer, #to_ptr)

Returns:

  • (CZMQ::Zchunk)


112
113
114
115
116
# File 'lib/czmq-ffi-gen/czmq/ffi/zchunk.rb', line 112

def self.frommem(data, size, destructor, hint)
  size = Integer(size)
  ptr = ::CZMQ::FFI.zchunk_frommem(data, size, destructor, hint)
  __new ptr
end

.is(self_) ⇒ Boolean

Probe the supplied object, and report if it looks like a zchunk_t.

Parameters:

  • self_ (::FFI::Pointer, #to_ptr)

Returns:

  • (Boolean)


409
410
411
412
# File 'lib/czmq-ffi-gen/czmq/ffi/zchunk.rb', line 409

def self.is(self_)
  result = ::CZMQ::FFI.zchunk_is(self_)
  result
end

.new(data, size) ⇒ CZMQ::Zchunk

Create a new chunk of the specified size. If you specify the data, it is copied into the chunk. If you do not specify the data, the chunk is allocated and left empty, and you can then add data using zchunk_append.

Parameters:

  • data (::FFI::Pointer, #to_ptr)
  • size (Integer, #to_int, #to_i)

Returns:

  • (CZMQ::Zchunk)


99
100
101
102
103
# File 'lib/czmq-ffi-gen/czmq/ffi/zchunk.rb', line 99

def self.new(data, size)
  size = Integer(size)
  ptr = ::CZMQ::FFI.zchunk_new(data, size)
  __new ptr
end

.packx(self_p) ⇒ Zframe

Transform zchunk into a zframe that can be sent in a message. Take ownership of the chunk.

Parameters:

Returns:



355
356
357
358
359
360
# File 'lib/czmq-ffi-gen/czmq/ffi/zchunk.rb', line 355

def self.packx(self_p)
  self_p = self_p.__ptr_give_ref
  result = ::CZMQ::FFI.zchunk_packx(self_p)
  result = Zframe.__new result, true
  result
end

.read(handle, bytes) ⇒ Zchunk

Read chunk from an open file descriptor

Parameters:

  • handle (::FFI::Pointer, #to_ptr)
  • bytes (Integer, #to_int, #to_i)

Returns:



258
259
260
261
262
263
# File 'lib/czmq-ffi-gen/czmq/ffi/zchunk.rb', line 258

def self.read(handle, bytes)
  bytes = Integer(bytes)
  result = ::CZMQ::FFI.zchunk_read(handle, bytes)
  result = Zchunk.__new result, true
  result
end

.slurp(filename, maxsize) ⇒ Zchunk

Try to slurp an entire file into a chunk. Will read up to maxsize of the file. If maxsize is 0, will attempt to read the entire file and fail with an assertion if that cannot fit into memory. Returns a new chunk containing the file data, or NULL if the file could not be read.

Parameters:

  • filename (String, #to_s, nil)
  • maxsize (Integer, #to_int, #to_i)

Returns:



284
285
286
287
288
289
# File 'lib/czmq-ffi-gen/czmq/ffi/zchunk.rb', line 284

def self.slurp(filename, maxsize)
  maxsize = Integer(maxsize)
  result = ::CZMQ::FFI.zchunk_slurp(filename, maxsize)
  result = Zchunk.__new result, true
  result
end

.test(verbose) ⇒ void

This method returns an undefined value.

Self test of this class.

Parameters:

  • verbose (Boolean)


418
419
420
421
422
# File 'lib/czmq-ffi-gen/czmq/ffi/zchunk.rb', line 418

def self.test(verbose)
  verbose = !(0==verbose||!verbose) # boolean
  result = ::CZMQ::FFI.zchunk_test(verbose)
  result
end

.unpack(frame) ⇒ Zchunk

Transform a zframe into a zchunk.

Parameters:

Returns:



366
367
368
369
370
371
# File 'lib/czmq-ffi-gen/czmq/ffi/zchunk.rb', line 366

def self.unpack(frame)
  frame = frame.__ptr if frame
  result = ::CZMQ::FFI.zchunk_unpack(frame)
  result = Zchunk.__new result, true
  result
end

Instance Method Details

#__ptr::FFI::Pointer Also known as: to_ptr

Return internal pointer

Returns:

  • (::FFI::Pointer)

Raises:



49
50
51
52
# File 'lib/czmq-ffi-gen/czmq/ffi/zchunk.rb', line 49

def __ptr
  raise DestroyedError unless @ptr
  @ptr
end

#__ptr_give_ref::FFI::MemoryPointer

Note:

This detaches the current instance from the native object and thus makes it unusable.

Nullify internal pointer and return pointer pointer.

Returns:

  • (::FFI::MemoryPointer)

    the pointer pointing to a pointer pointing to the native object

Raises:



60
61
62
63
64
65
66
67
# File 'lib/czmq-ffi-gen/czmq/ffi/zchunk.rb', line 60

def __ptr_give_ref
  raise DestroyedError unless @ptr
  ptr_ptr = ::FFI::MemoryPointer.new :pointer
  ptr_ptr.write_pointer @ptr
  __undef_finalizer if @finalizer
  @ptr = nil
  ptr_ptr
end

#__undef_finalizervoid

Note:

Only use this if you need to and can guarantee that the native object will be freed by other means.

This method returns an undefined value.

Undefines the finalizer for this object.



72
73
74
75
# File 'lib/czmq-ffi-gen/czmq/ffi/zchunk.rb', line 72

def __undef_finalizer
  ObjectSpace.undefine_finalizer self
  @finalizer = nil
end

#append(data, size) ⇒ Integer

Append user-supplied data to chunk, return resulting chunk size. If the data would exceeded the available space, it is truncated. If you want to grow the chunk to accommodate new data, use the zchunk_extend method.

Parameters:

  • data (::FFI::Pointer, #to_ptr)
  • size (Integer, #to_int, #to_i)

Returns:

  • (Integer)

Raises:



205
206
207
208
209
210
211
# File 'lib/czmq-ffi-gen/czmq/ffi/zchunk.rb', line 205

def append(data, size)
  raise DestroyedError unless @ptr
  self_p = @ptr
  size = Integer(size)
  result = ::CZMQ::FFI.zchunk_append(self_p, data, size)
  result
end

#consume(source) ⇒ Integer

Copy as much data from ‘source’ into the chunk as possible; returns the new size of chunk. If all data from ‘source’ is used, returns exhausted on the source chunk. Source can be consumed as many times as needed until it is exhausted. If source was already exhausted, does not change chunk.

Parameters:

Returns:

  • (Integer)

Raises:



234
235
236
237
238
239
240
# File 'lib/czmq-ffi-gen/czmq/ffi/zchunk.rb', line 234

def consume(source)
  raise DestroyedError unless @ptr
  self_p = @ptr
  source = source.__ptr if source
  result = ::CZMQ::FFI.zchunk_consume(self_p, source)
  result
end

#data::FFI::Pointer

Return chunk data

Returns:

  • (::FFI::Pointer)

Raises:



163
164
165
166
167
168
# File 'lib/czmq-ffi-gen/czmq/ffi/zchunk.rb', line 163

def data()
  raise DestroyedError unless @ptr
  self_p = @ptr
  result = ::CZMQ::FFI.zchunk_data(self_p)
  result
end

#destroyvoid

This method returns an undefined value.

Destroy a chunk



121
122
123
124
125
126
# File 'lib/czmq-ffi-gen/czmq/ffi/zchunk.rb', line 121

def destroy()
  return unless @ptr
  self_p = __ptr_give_ref
  result = ::CZMQ::FFI.zchunk_destroy(self_p)
  result
end

#digestString

Calculate SHA1 digest for chunk, using zdigest class.

Returns:

  • (String)

Raises:



376
377
378
379
380
381
# File 'lib/czmq-ffi-gen/czmq/ffi/zchunk.rb', line 376

def digest()
  raise DestroyedError unless @ptr
  self_p = @ptr
  result = ::CZMQ::FFI.zchunk_digest(self_p)
  result
end

#dupZchunk

Create copy of chunk, as new chunk object. Returns a fresh zchunk_t object, or null if there was not enough heap memory. If chunk is null, returns null.

Returns:

Raises:



296
297
298
299
300
301
302
# File 'lib/czmq-ffi-gen/czmq/ffi/zchunk.rb', line 296

def dup()
  raise DestroyedError unless @ptr
  self_p = @ptr
  result = ::CZMQ::FFI.zchunk_dup(self_p)
  result = Zchunk.__new result, true
  result
end

#exhaustedBoolean

Returns true if the chunk was exhausted by consume methods, or if the chunk has a size of zero.

Returns:

  • (Boolean)

Raises:



246
247
248
249
250
251
# File 'lib/czmq-ffi-gen/czmq/ffi/zchunk.rb', line 246

def exhausted()
  raise DestroyedError unless @ptr
  self_p = @ptr
  result = ::CZMQ::FFI.zchunk_exhausted(self_p)
  result
end

#extend(data, size) ⇒ Integer

Append user-supplied data to chunk, return resulting chunk size. If the data would exceeded the available space, the chunk grows in size.

Parameters:

  • data (::FFI::Pointer, #to_ptr)
  • size (Integer, #to_int, #to_i)

Returns:

  • (Integer)

Raises:



219
220
221
222
223
224
225
# File 'lib/czmq-ffi-gen/czmq/ffi/zchunk.rb', line 219

def extend(data, size)
  raise DestroyedError unless @ptr
  self_p = @ptr
  size = Integer(size)
  result = ::CZMQ::FFI.zchunk_extend(self_p, data, size)
  result
end

#fill(filler, size) ⇒ Integer

Fill chunk data from user-supplied octet

Parameters:

  • filler (Integer, #to_int, #to_i)
  • size (Integer, #to_int, #to_i)

Returns:

  • (Integer)

Raises:



189
190
191
192
193
194
195
196
# File 'lib/czmq-ffi-gen/czmq/ffi/zchunk.rb', line 189

def fill(filler, size)
  raise DestroyedError unless @ptr
  self_p = @ptr
  filler = Integer(filler)
  size = Integer(size)
  result = ::CZMQ::FFI.zchunk_fill(self_p, filler, size)
  result
end

#fprint(file) ⇒ void

This method returns an undefined value.

Dump chunk to FILE stream, for debugging and tracing.

Parameters:

  • file (::FFI::Pointer, #to_ptr)

Raises:



387
388
389
390
391
392
# File 'lib/czmq-ffi-gen/czmq/ffi/zchunk.rb', line 387

def fprint(file)
  raise DestroyedError unless @ptr
  self_p = @ptr
  result = ::CZMQ::FFI.zchunk_fprint(self_p, file)
  result
end

#max_sizeInteger

Return chunk max size

Returns:

  • (Integer)

Raises:



153
154
155
156
157
158
# File 'lib/czmq-ffi-gen/czmq/ffi/zchunk.rb', line 153

def max_size()
  raise DestroyedError unless @ptr
  self_p = @ptr
  result = ::CZMQ::FFI.zchunk_max_size(self_p)
  result
end

#null?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/czmq-ffi-gen/czmq/ffi/zchunk.rb', line 44

def null?
  !@ptr or @ptr.null?
end

#packZframe

Transform zchunk into a zframe that can be sent in a message.

Returns:

Raises:



342
343
344
345
346
347
348
# File 'lib/czmq-ffi-gen/czmq/ffi/zchunk.rb', line 342

def pack()
  raise DestroyedError unless @ptr
  self_p = @ptr
  result = ::CZMQ::FFI.zchunk_pack(self_p)
  result = Zframe.__new result, true
  result
end

This method returns an undefined value.

Dump message to stderr, for debugging and tracing. See zchunk_fprint for details

Raises:



398
399
400
401
402
403
# File 'lib/czmq-ffi-gen/czmq/ffi/zchunk.rb', line 398

def print()
  raise DestroyedError unless @ptr
  self_p = @ptr
  result = ::CZMQ::FFI.zchunk_print(self_p)
  result
end

#resize(size) ⇒ void

This method returns an undefined value.

Resizes chunk max_size as requested; chunk_cur size is set to zero

Parameters:

  • size (Integer, #to_int, #to_i)

Raises:



132
133
134
135
136
137
138
# File 'lib/czmq-ffi-gen/czmq/ffi/zchunk.rb', line 132

def resize(size)
  raise DestroyedError unless @ptr
  self_p = @ptr
  size = Integer(size)
  result = ::CZMQ::FFI.zchunk_resize(self_p, size)
  result
end

#set(data, size) ⇒ Integer

Set chunk data from user-supplied data; truncate if too large. Data may be null. Returns actual size of chunk

Parameters:

  • data (::FFI::Pointer, #to_ptr)
  • size (Integer, #to_int, #to_i)

Returns:

  • (Integer)

Raises:



176
177
178
179
180
181
182
# File 'lib/czmq-ffi-gen/czmq/ffi/zchunk.rb', line 176

def set(data, size)
  raise DestroyedError unless @ptr
  self_p = @ptr
  size = Integer(size)
  result = ::CZMQ::FFI.zchunk_set(self_p, data, size)
  result
end

#sizeInteger

Return chunk cur size

Returns:

  • (Integer)

Raises:



143
144
145
146
147
148
# File 'lib/czmq-ffi-gen/czmq/ffi/zchunk.rb', line 143

def size()
  raise DestroyedError unless @ptr
  self_p = @ptr
  result = ::CZMQ::FFI.zchunk_size(self_p)
  result
end

#strdup::FFI::AutoPointer

Return chunk data copied into freshly allocated string Caller must free string when finished with it.

Returns:

  • (::FFI::AutoPointer)

Raises:



320
321
322
323
324
325
326
# File 'lib/czmq-ffi-gen/czmq/ffi/zchunk.rb', line 320

def strdup()
  raise DestroyedError unless @ptr
  self_p = @ptr
  result = ::CZMQ::FFI.zchunk_strdup(self_p)
  result = ::FFI::AutoPointer.new(result, LibC.method(:free))
  result
end

#streq(string) ⇒ Boolean

Return TRUE if chunk body is equal to string, excluding terminator

Parameters:

  • string (String, #to_s, nil)

Returns:

  • (Boolean)

Raises:



332
333
334
335
336
337
# File 'lib/czmq-ffi-gen/czmq/ffi/zchunk.rb', line 332

def streq(string)
  raise DestroyedError unless @ptr
  self_p = @ptr
  result = ::CZMQ::FFI.zchunk_streq(self_p, string)
  result
end

#strhex::FFI::AutoPointer

Return chunk data encoded as printable hex string. Caller must free string when finished with it.

Returns:

  • (::FFI::AutoPointer)

Raises:



308
309
310
311
312
313
314
# File 'lib/czmq-ffi-gen/czmq/ffi/zchunk.rb', line 308

def strhex()
  raise DestroyedError unless @ptr
  self_p = @ptr
  result = ::CZMQ::FFI.zchunk_strhex(self_p)
  result = ::FFI::AutoPointer.new(result, LibC.method(:free))
  result
end

#write(handle) ⇒ Integer

Write chunk to an open file descriptor

Parameters:

  • handle (::FFI::Pointer, #to_ptr)

Returns:

  • (Integer)

Raises:



269
270
271
272
273
274
# File 'lib/czmq-ffi-gen/czmq/ffi/zchunk.rb', line 269

def write(handle)
  raise DestroyedError unless @ptr
  self_p = @ptr
  result = ::CZMQ::FFI.zchunk_write(self_p, handle)
  result
end