czmq-ffi-gen 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,595 @@
1
+ ################################################################################
2
+ # THIS FILE IS 100% GENERATED BY ZPROJECT; DO NOT EDIT EXCEPT EXPERIMENTALLY #
3
+ # Please refer to the README for information about making permanent changes. #
4
+ ################################################################################
5
+
6
+ module CZMQ
7
+ module FFI
8
+
9
+ # extended generic type-free hash container
10
+ # @note This class is 100% generated using zproject.
11
+ class Zhashx
12
+ # Raised when one tries to use an instance of {Zhashx} after
13
+ # the internal pointer to the native object has been nullified.
14
+ class DestroyedError < RuntimeError; end
15
+
16
+ # Boilerplate for self pointer, initializer, and finalizer
17
+ class << self
18
+ alias :__new :new
19
+ end
20
+ # Attaches the pointer _ptr_ to this instance and defines a finalizer for
21
+ # it if necessary.
22
+ # @param ptr [::FFI::Pointer]
23
+ # @param finalize [Boolean]
24
+ def initialize(ptr, finalize = true)
25
+ @ptr = ptr
26
+ if @ptr.null?
27
+ @ptr = nil # Remove null pointers so we don't have to test for them.
28
+ elsif finalize
29
+ @finalizer = self.class.create_finalizer_for @ptr
30
+ ObjectSpace.define_finalizer self, @finalizer
31
+ end
32
+ end
33
+ # @param ptr [::FFI::Pointer]
34
+ # @return [Proc]
35
+ def self.create_finalizer_for(ptr)
36
+ Proc.new do
37
+ ptr_ptr = ::FFI::MemoryPointer.new :pointer
38
+ ptr_ptr.write_pointer ptr
39
+ ::CZMQ::FFI.zhashx_destroy ptr_ptr
40
+ end
41
+ end
42
+ # @return [Boolean]
43
+ def null?
44
+ !@ptr or @ptr.null?
45
+ end
46
+ # Return internal pointer
47
+ # @return [::FFI::Pointer]
48
+ def __ptr
49
+ raise DestroyedError unless @ptr
50
+ @ptr
51
+ end
52
+ # So external Libraries can just pass the Object to a FFI function which expects a :pointer
53
+ alias_method :to_ptr, :__ptr
54
+ # Nullify internal pointer and return pointer pointer.
55
+ # @note This detaches the current instance from the native object
56
+ # and thus makes it unusable.
57
+ # @return [::FFI::MemoryPointer] the pointer pointing to a pointer
58
+ # pointing to the native object
59
+ def __ptr_give_ref
60
+ raise DestroyedError unless @ptr
61
+ ptr_ptr = ::FFI::MemoryPointer.new :pointer
62
+ ptr_ptr.write_pointer @ptr
63
+ ObjectSpace.undefine_finalizer self if @finalizer
64
+ @finalizer = nil
65
+ @ptr = nil
66
+ ptr_ptr
67
+ end
68
+
69
+ # Create a new callback of the following type:
70
+ # Destroy an item
71
+ # typedef void (zhashx_destructor_fn) (
72
+ # void **item);
73
+ #
74
+ # @note WARNING: If your Ruby code doesn't retain a reference to the
75
+ # FFI::Function object after passing it to a C function call,
76
+ # it may be garbage collected while C still holds the pointer,
77
+ # potentially resulting in a segmentation fault.
78
+ def self.destructor_fn
79
+ ::FFI::Function.new :void, [:pointer], blocking: true do |item|
80
+ result = yield item
81
+ result
82
+ end
83
+ end
84
+
85
+ # Create a new callback of the following type:
86
+ # Duplicate an item
87
+ # typedef void * (zhashx_duplicator_fn) (
88
+ # const void *item);
89
+ #
90
+ # @note WARNING: If your Ruby code doesn't retain a reference to the
91
+ # FFI::Function object after passing it to a C function call,
92
+ # it may be garbage collected while C still holds the pointer,
93
+ # potentially resulting in a segmentation fault.
94
+ def self.duplicator_fn
95
+ ::FFI::Function.new :pointer, [:pointer], blocking: true do |item|
96
+ result = yield item
97
+ result
98
+ end
99
+ end
100
+
101
+ # Create a new callback of the following type:
102
+ # Compare two items, for sorting
103
+ # typedef int (zhashx_comparator_fn) (
104
+ # const void *item1, const void *item2);
105
+ #
106
+ # @note WARNING: If your Ruby code doesn't retain a reference to the
107
+ # FFI::Function object after passing it to a C function call,
108
+ # it may be garbage collected while C still holds the pointer,
109
+ # potentially resulting in a segmentation fault.
110
+ def self.comparator_fn
111
+ ::FFI::Function.new :int, [:pointer, :pointer], blocking: true do |item1, item2|
112
+ result = yield item1, item2
113
+ result = Integer(result)
114
+ result
115
+ end
116
+ end
117
+
118
+ # Create a new callback of the following type:
119
+ # compare two items, for sorting
120
+ # typedef void (zhashx_free_fn) (
121
+ # void *data);
122
+ #
123
+ # @note WARNING: If your Ruby code doesn't retain a reference to the
124
+ # FFI::Function object after passing it to a C function call,
125
+ # it may be garbage collected while C still holds the pointer,
126
+ # potentially resulting in a segmentation fault.
127
+ def self.free_fn
128
+ ::FFI::Function.new :void, [:pointer], blocking: true do |data|
129
+ result = yield data
130
+ result
131
+ end
132
+ end
133
+
134
+ # Create a new callback of the following type:
135
+ # compare two items, for sorting
136
+ # typedef size_t (zhashx_hash_fn) (
137
+ # const void *key);
138
+ #
139
+ # @note WARNING: If your Ruby code doesn't retain a reference to the
140
+ # FFI::Function object after passing it to a C function call,
141
+ # it may be garbage collected while C still holds the pointer,
142
+ # potentially resulting in a segmentation fault.
143
+ def self.hash_fn
144
+ ::FFI::Function.new :size_t, [:pointer], blocking: true do |key|
145
+ result = yield key
146
+ result = Integer(result)
147
+ result
148
+ end
149
+ end
150
+
151
+ # Create a new callback of the following type:
152
+ # DEPRECATED as clumsy -- use zhashx_first/_next instead
153
+ # typedef int (zhashx_foreach_fn) (
154
+ # const char *key, void *item, void *argument);
155
+ #
156
+ # @note WARNING: If your Ruby code doesn't retain a reference to the
157
+ # FFI::Function object after passing it to a C function call,
158
+ # it may be garbage collected while C still holds the pointer,
159
+ # potentially resulting in a segmentation fault.
160
+ def self.foreach_fn
161
+ ::FFI::Function.new :int, [:string, :pointer, :pointer], blocking: true do |key, item, argument|
162
+ result = yield key, item, argument
163
+ result = Integer(result)
164
+ result
165
+ end
166
+ end
167
+
168
+ # Create a new, empty hash container
169
+ # @return [CZMQ::Zhashx]
170
+ def self.new()
171
+ ptr = ::CZMQ::FFI.zhashx_new()
172
+ __new ptr
173
+ end
174
+
175
+ # Unpack binary frame into a new hash table. Packed data must follow format
176
+ # defined by zhashx_pack. Hash table is set to autofree. An empty frame
177
+ # unpacks to an empty hash table.
178
+ # @param frame [Zframe, #__ptr]
179
+ # @return [CZMQ::Zhashx]
180
+ def self.unpack(frame)
181
+ frame = frame.__ptr if frame
182
+ ptr = ::CZMQ::FFI.zhashx_unpack(frame)
183
+ __new ptr
184
+ end
185
+
186
+ # Destroy a hash container and all items in it
187
+ #
188
+ # @return [void]
189
+ def destroy()
190
+ return unless @ptr
191
+ self_p = __ptr_give_ref
192
+ result = ::CZMQ::FFI.zhashx_destroy(self_p)
193
+ result
194
+ end
195
+
196
+ # Insert item into hash table with specified key and item.
197
+ # If key is already present returns -1 and leaves existing item unchanged
198
+ # Returns 0 on success.
199
+ #
200
+ # @param key [::FFI::Pointer, #to_ptr]
201
+ # @param item [::FFI::Pointer, #to_ptr]
202
+ # @return [Integer]
203
+ def insert(key, item)
204
+ raise DestroyedError unless @ptr
205
+ self_p = @ptr
206
+ result = ::CZMQ::FFI.zhashx_insert(self_p, key, item)
207
+ result
208
+ end
209
+
210
+ # Update or insert item into hash table with specified key and item. If the
211
+ # key is already present, destroys old item and inserts new one. If you set
212
+ # a container item destructor, this is called on the old value. If the key
213
+ # was not already present, inserts a new item. Sets the hash cursor to the
214
+ # new item.
215
+ #
216
+ # @param key [::FFI::Pointer, #to_ptr]
217
+ # @param item [::FFI::Pointer, #to_ptr]
218
+ # @return [void]
219
+ def update(key, item)
220
+ raise DestroyedError unless @ptr
221
+ self_p = @ptr
222
+ result = ::CZMQ::FFI.zhashx_update(self_p, key, item)
223
+ result
224
+ end
225
+
226
+ # Remove an item specified by key from the hash table. If there was no such
227
+ # item, this function does nothing.
228
+ #
229
+ # @param key [::FFI::Pointer, #to_ptr]
230
+ # @return [void]
231
+ def delete(key)
232
+ raise DestroyedError unless @ptr
233
+ self_p = @ptr
234
+ result = ::CZMQ::FFI.zhashx_delete(self_p, key)
235
+ result
236
+ end
237
+
238
+ # Delete all items from the hash table. If the key destructor is
239
+ # set, calls it on every key. If the item destructor is set, calls
240
+ # it on every item.
241
+ #
242
+ # @return [void]
243
+ def purge()
244
+ raise DestroyedError unless @ptr
245
+ self_p = @ptr
246
+ result = ::CZMQ::FFI.zhashx_purge(self_p)
247
+ result
248
+ end
249
+
250
+ # Return the item at the specified key, or null
251
+ #
252
+ # @param key [::FFI::Pointer, #to_ptr]
253
+ # @return [::FFI::Pointer]
254
+ def lookup(key)
255
+ raise DestroyedError unless @ptr
256
+ self_p = @ptr
257
+ result = ::CZMQ::FFI.zhashx_lookup(self_p, key)
258
+ result
259
+ end
260
+
261
+ # Reindexes an item from an old key to a new key. If there was no such
262
+ # item, does nothing. Returns 0 if successful, else -1.
263
+ #
264
+ # @param old_key [::FFI::Pointer, #to_ptr]
265
+ # @param new_key [::FFI::Pointer, #to_ptr]
266
+ # @return [Integer]
267
+ def rename(old_key, new_key)
268
+ raise DestroyedError unless @ptr
269
+ self_p = @ptr
270
+ result = ::CZMQ::FFI.zhashx_rename(self_p, old_key, new_key)
271
+ result
272
+ end
273
+
274
+ # Set a free function for the specified hash table item. When the item is
275
+ # destroyed, the free function, if any, is called on that item.
276
+ # Use this when hash items are dynamically allocated, to ensure that
277
+ # you don't have memory leaks. You can pass 'free' or NULL as a free_fn.
278
+ # Returns the item, or NULL if there is no such item.
279
+ #
280
+ # @param key [::FFI::Pointer, #to_ptr]
281
+ # @param free_fn [::FFI::Pointer, #to_ptr]
282
+ # @return [::FFI::Pointer]
283
+ def freefn(key, free_fn)
284
+ raise DestroyedError unless @ptr
285
+ self_p = @ptr
286
+ result = ::CZMQ::FFI.zhashx_freefn(self_p, key, free_fn)
287
+ result
288
+ end
289
+
290
+ # Return the number of keys/items in the hash table
291
+ #
292
+ # @return [Integer]
293
+ def size()
294
+ raise DestroyedError unless @ptr
295
+ self_p = @ptr
296
+ result = ::CZMQ::FFI.zhashx_size(self_p)
297
+ result
298
+ end
299
+
300
+ # Return a zlistx_t containing the keys for the items in the
301
+ # table. Uses the key_duplicator to duplicate all keys and sets the
302
+ # key_destructor as destructor for the list.
303
+ #
304
+ # @return [::FFI::Pointer]
305
+ def keys()
306
+ raise DestroyedError unless @ptr
307
+ self_p = @ptr
308
+ result = ::CZMQ::FFI.zhashx_keys(self_p)
309
+ result
310
+ end
311
+
312
+ # Return a zlistx_t containing the values for the items in the
313
+ # table. Uses the duplicator to duplicate all items and sets the
314
+ # destructor as destructor for the list.
315
+ #
316
+ # @return [::FFI::Pointer]
317
+ def values()
318
+ raise DestroyedError unless @ptr
319
+ self_p = @ptr
320
+ result = ::CZMQ::FFI.zhashx_values(self_p)
321
+ result
322
+ end
323
+
324
+ # Simple iterator; returns first item in hash table, in no given order,
325
+ # or NULL if the table is empty. This method is simpler to use than the
326
+ # foreach() method, which is deprecated. To access the key for this item
327
+ # use zhashx_cursor(). NOTE: do NOT modify the table while iterating.
328
+ #
329
+ # @return [::FFI::Pointer]
330
+ def first()
331
+ raise DestroyedError unless @ptr
332
+ self_p = @ptr
333
+ result = ::CZMQ::FFI.zhashx_first(self_p)
334
+ result
335
+ end
336
+
337
+ # Simple iterator; returns next item in hash table, in no given order,
338
+ # or NULL if the last item was already returned. Use this together with
339
+ # zhashx_first() to process all items in a hash table. If you need the
340
+ # items in sorted order, use zhashx_keys() and then zlistx_sort(). To
341
+ # access the key for this item use zhashx_cursor(). NOTE: do NOT modify
342
+ # the table while iterating.
343
+ #
344
+ # @return [::FFI::Pointer]
345
+ def next()
346
+ raise DestroyedError unless @ptr
347
+ self_p = @ptr
348
+ result = ::CZMQ::FFI.zhashx_next(self_p)
349
+ result
350
+ end
351
+
352
+ # After a successful first/next method, returns the key for the item that
353
+ # was returned. This is a constant string that you may not modify or
354
+ # deallocate, and which lasts as long as the item in the hash. After an
355
+ # unsuccessful first/next, returns NULL.
356
+ #
357
+ # @return [::FFI::Pointer]
358
+ def cursor()
359
+ raise DestroyedError unless @ptr
360
+ self_p = @ptr
361
+ result = ::CZMQ::FFI.zhashx_cursor(self_p)
362
+ result
363
+ end
364
+
365
+ # Add a comment to hash table before saving to disk. You can add as many
366
+ # comment lines as you like. These comment lines are discarded when loading
367
+ # the file. If you use a null format, all comments are deleted.
368
+ #
369
+ # @param format [String, #to_str, #to_s]
370
+ # @param args [Array<Object>] see https://github.com/ffi/ffi/wiki/examples#using-varargs
371
+ # @return [void]
372
+ def comment(format, *args)
373
+ raise DestroyedError unless @ptr
374
+ self_p = @ptr
375
+ format = String(format)
376
+ result = ::CZMQ::FFI.zhashx_comment(self_p, format, *args)
377
+ result
378
+ end
379
+
380
+ # Save hash table to a text file in name=value format. Hash values must be
381
+ # printable strings; keys may not contain '=' character. Returns 0 if OK,
382
+ # else -1 if a file error occurred.
383
+ #
384
+ # @param filename [String, #to_str, #to_s]
385
+ # @return [Integer]
386
+ def save(filename)
387
+ raise DestroyedError unless @ptr
388
+ self_p = @ptr
389
+ filename = String(filename)
390
+ result = ::CZMQ::FFI.zhashx_save(self_p, filename)
391
+ result
392
+ end
393
+
394
+ # Load hash table from a text file in name=value format; hash table must
395
+ # already exist. Hash values must printable strings; keys may not contain
396
+ # '=' character. Returns 0 if OK, else -1 if a file was not readable.
397
+ #
398
+ # @param filename [String, #to_str, #to_s]
399
+ # @return [Integer]
400
+ def load(filename)
401
+ raise DestroyedError unless @ptr
402
+ self_p = @ptr
403
+ filename = String(filename)
404
+ result = ::CZMQ::FFI.zhashx_load(self_p, filename)
405
+ result
406
+ end
407
+
408
+ # When a hash table was loaded from a file by zhashx_load, this method will
409
+ # reload the file if it has been modified since, and is "stable", i.e. not
410
+ # still changing. Returns 0 if OK, -1 if there was an error reloading the
411
+ # file.
412
+ #
413
+ # @return [Integer]
414
+ def refresh()
415
+ raise DestroyedError unless @ptr
416
+ self_p = @ptr
417
+ result = ::CZMQ::FFI.zhashx_refresh(self_p)
418
+ result
419
+ end
420
+
421
+ # Serialize hash table to a binary frame that can be sent in a message.
422
+ # The packed format is compatible with the 'dictionary' type defined in
423
+ # http://rfc.zeromq.org/spec:35/FILEMQ, and implemented by zproto:
424
+ #
425
+ # ; A list of name/value pairs
426
+ # dictionary = dict-count *( dict-name dict-value )
427
+ # dict-count = number-4
428
+ # dict-value = longstr
429
+ # dict-name = string
430
+ #
431
+ # ; Strings are always length + text contents
432
+ # longstr = number-4 *VCHAR
433
+ # string = number-1 *VCHAR
434
+ #
435
+ # ; Numbers are unsigned integers in network byte order
436
+ # number-1 = 1OCTET
437
+ # number-4 = 4OCTET
438
+ #
439
+ # Comments are not included in the packed data. Item values MUST be
440
+ # strings.
441
+ #
442
+ # @return [Zframe]
443
+ def pack()
444
+ raise DestroyedError unless @ptr
445
+ self_p = @ptr
446
+ result = ::CZMQ::FFI.zhashx_pack(self_p)
447
+ result = Zframe.__new result, true
448
+ result
449
+ end
450
+
451
+ # Make a copy of the list; items are duplicated if you set a duplicator
452
+ # for the list, otherwise not. Copying a null reference returns a null
453
+ # reference. Note that this method's behavior changed slightly for CZMQ
454
+ # v3.x, as it does not set nor respect autofree. It does however let you
455
+ # duplicate any hash table safely. The old behavior is in zhashx_dup_v2.
456
+ #
457
+ # @return [Zhashx]
458
+ def dup()
459
+ raise DestroyedError unless @ptr
460
+ self_p = @ptr
461
+ result = ::CZMQ::FFI.zhashx_dup(self_p)
462
+ result = Zhashx.__new result, true
463
+ result
464
+ end
465
+
466
+ # Set a user-defined deallocator for hash items; by default items are not
467
+ # freed when the hash is destroyed.
468
+ #
469
+ # @param destructor [::FFI::Pointer, #to_ptr]
470
+ # @return [void]
471
+ def set_destructor(destructor)
472
+ raise DestroyedError unless @ptr
473
+ self_p = @ptr
474
+ result = ::CZMQ::FFI.zhashx_set_destructor(self_p, destructor)
475
+ result
476
+ end
477
+
478
+ # Set a user-defined duplicator for hash items; by default items are not
479
+ # copied when the hash is duplicated.
480
+ #
481
+ # @param duplicator [::FFI::Pointer, #to_ptr]
482
+ # @return [void]
483
+ def set_duplicator(duplicator)
484
+ raise DestroyedError unless @ptr
485
+ self_p = @ptr
486
+ result = ::CZMQ::FFI.zhashx_set_duplicator(self_p, duplicator)
487
+ result
488
+ end
489
+
490
+ # Set a user-defined deallocator for keys; by default keys are freed
491
+ # when the hash is destroyed using free().
492
+ #
493
+ # @param destructor [::FFI::Pointer, #to_ptr]
494
+ # @return [void]
495
+ def set_key_destructor(destructor)
496
+ raise DestroyedError unless @ptr
497
+ self_p = @ptr
498
+ result = ::CZMQ::FFI.zhashx_set_key_destructor(self_p, destructor)
499
+ result
500
+ end
501
+
502
+ # Set a user-defined duplicator for keys; by default keys are duplicated
503
+ # using strdup.
504
+ #
505
+ # @param duplicator [::FFI::Pointer, #to_ptr]
506
+ # @return [void]
507
+ def set_key_duplicator(duplicator)
508
+ raise DestroyedError unless @ptr
509
+ self_p = @ptr
510
+ result = ::CZMQ::FFI.zhashx_set_key_duplicator(self_p, duplicator)
511
+ result
512
+ end
513
+
514
+ # Set a user-defined comparator for keys; by default keys are
515
+ # compared using strcmp.
516
+ #
517
+ # @param comparator [::FFI::Pointer, #to_ptr]
518
+ # @return [void]
519
+ def set_key_comparator(comparator)
520
+ raise DestroyedError unless @ptr
521
+ self_p = @ptr
522
+ result = ::CZMQ::FFI.zhashx_set_key_comparator(self_p, comparator)
523
+ result
524
+ end
525
+
526
+ # Set a user-defined comparator for keys; by default keys are
527
+ # compared using strcmp.
528
+ #
529
+ # @param hasher [::FFI::Pointer, #to_ptr]
530
+ # @return [void]
531
+ def set_key_hasher(hasher)
532
+ raise DestroyedError unless @ptr
533
+ self_p = @ptr
534
+ result = ::CZMQ::FFI.zhashx_set_key_hasher(self_p, hasher)
535
+ result
536
+ end
537
+
538
+ # Make copy of hash table; if supplied table is null, returns null.
539
+ # Does not copy items themselves. Rebuilds new table so may be slow on
540
+ # very large tables. NOTE: only works with item values that are strings
541
+ # since there's no other way to know how to duplicate the item value.
542
+ #
543
+ # @return [Zhashx]
544
+ def dup_v2()
545
+ raise DestroyedError unless @ptr
546
+ self_p = @ptr
547
+ result = ::CZMQ::FFI.zhashx_dup_v2(self_p)
548
+ result = Zhashx.__new result, false
549
+ result
550
+ end
551
+
552
+ # DEPRECATED as clumsy -- use set_destructor instead
553
+ # Set hash for automatic value destruction
554
+ #
555
+ # @return [void]
556
+ def autofree()
557
+ raise DestroyedError unless @ptr
558
+ self_p = @ptr
559
+ result = ::CZMQ::FFI.zhashx_autofree(self_p)
560
+ result
561
+ end
562
+
563
+ # DEPRECATED as clumsy -- use zhashx_first/_next instead
564
+ # Apply function to each item in the hash table. Items are iterated in no
565
+ # defined order. Stops if callback function returns non-zero and returns
566
+ # final return code from callback function (zero = success).
567
+ # Callback function for zhashx_foreach method
568
+ #
569
+ # @param callback [::FFI::Pointer, #to_ptr]
570
+ # @param argument [::FFI::Pointer, #to_ptr]
571
+ # @return [Integer]
572
+ def foreach(callback, argument)
573
+ raise DestroyedError unless @ptr
574
+ self_p = @ptr
575
+ result = ::CZMQ::FFI.zhashx_foreach(self_p, callback, argument)
576
+ result
577
+ end
578
+
579
+ # Self test of this class.
580
+ #
581
+ # @param verbose [Boolean]
582
+ # @return [void]
583
+ def self.test(verbose)
584
+ verbose = !(0==verbose||!verbose) # boolean
585
+ result = ::CZMQ::FFI.zhashx_test(verbose)
586
+ result
587
+ end
588
+ end
589
+ end
590
+ end
591
+
592
+ ################################################################################
593
+ # THIS FILE IS 100% GENERATED BY ZPROJECT; DO NOT EDIT EXCEPT EXPERIMENTALLY #
594
+ # Please refer to the README for information about making permanent changes. #
595
+ ################################################################################