Skip to content

Commit

Permalink
Add proper documentation to FFI::Generator and ::Task
Browse files Browse the repository at this point in the history
  • Loading branch information
larskanis committed May 17, 2019
1 parent 17e13e2 commit bc99b7e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
44 changes: 43 additions & 1 deletion lib/ffi/tools/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,49 @@

module FFI

# @private
##
# Generate files with C structs for FFI::Struct and C constants.
#
# == A simple example
#
# In file +zlib.rb.ffi+:
# module Zlib
# @@@
# constants do |c|
# c.include "zlib.h"
# c.const :ZLIB_VERNUM
# end
# @@@
#
# class ZStream < FFI::Struct
#
# struct do |s|
# s.name "struct z_stream_s"
# s.include "zlib.h"
#
# s.field :next_in, :pointer
# s.field :avail_in, :uint
# s.field :total_in, :ulong
# end
# @@@
# end
# end
#
# Translate the file:
# require "ffi/tools/generator"
# FFI::Generator.new "zlib.rb.ffi", "zlib.rb"
#
# Generates the file +zlib.rb+ with constant values and offsets:
# module Zlib
# ZLIB_VERNUM = 4784
#
# class ZStream < FFI::Struct
# layout :next_in, :pointer, 0,
# :avail_in, :uint, 8,
# :total_in, :ulong, 16
# end
#
# @see FFI::Generator::Task for easy integration in a Rakefile
class Generator

def initialize(ffi_name, rb_name, options = {})
Expand Down
13 changes: 10 additions & 3 deletions lib/ffi/tools/generator_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@
require 'rake/tasklib'

##
# Rake task that calculates C structs for FFI::Struct.

# @private
# Add Rake tasks that generate files with C structs for FFI::Struct and C constants.
#
# @example a simple example for your Rakefile
# require "ffi/tools/generator_task"
# # Add a task to generate my_object.rb out of my_object.rb.ffi
# FFI::Generator::Task.new ["my_object.rb"], cflags: "-I/usr/local/mylibrary"
#
# The generated files are also added to the 'clear' task.
#
# @see FFI::Generator for a description of the file content
class FFI::Generator::Task < Rake::TaskLib

def initialize(rb_names)
Expand Down

0 comments on commit bc99b7e

Please sign in to comment.