Class: Fiddle::CUnionEntity (Ruby 2.4.2)
Description: Class : Fiddle::CUnionEntity - Ruby 2.4.2

Support for the Ruby 2.4 series has ended. See here for reference.

### Home Classes Methods

### In Files

- fiddle/lib/fiddle/struct.rb

### Parent

Fiddle::CStructEntity

### Methods

- ::malloc
- ::size
- #set_ctypes

### Class/Module Index

Quicksearch

- Fiddle
- Fiddle::BasicTypes
- Fiddle::CParser
- Fiddle::CStruct
- Fiddle::CStructBuilder
- Fiddle::CStructEntity
- Fiddle::CUnion
- Fiddle::CUnionEntity
- Fiddle::Closure
- Fiddle::Closure::BlockCaller
- Fiddle::CompositeHandler
- Fiddle::DLError
- Fiddle::Function
- Fiddle::Handle
- Fiddle::Importer
- Fiddle::Pointer
- Fiddle::Win32Types

No matching classes.

# Fiddle::CUnionEntity

A C union wrapper

### Public Class Methods

malloc(types, func=nil) click to toggle source

Allocates a C union the `types` provided.

When the instance is garbage collected, the C function `func` is called.

                   # File fiddle/lib/fiddle/struct.rb, line 217
    def CUnionEntity.malloc(types, func=nil)
      addr = Fiddle.malloc(CUnionEntity.size(types))
      CUnionEntity.new(addr, types, func)
    end

size(types) click to toggle source

Returns the size needed for the union with the given `types`.

    Fiddle::CUnionEntity.size(
      [ Fiddle::TYPE_DOUBLE,
        Fiddle::TYPE_INT,
        Fiddle::TYPE_CHAR,
        Fiddle::TYPE_VOIDP ]) #=> 8

                   # File fiddle/lib/fiddle/struct.rb, line 229
    def CUnionEntity.size(types)
      types.map { |type, count = 1|
        PackInfo::SIZE_MAP[type] * count
      }.max
    end

### Public Instance Methods

set_ctypes(types) click to toggle source

Calculate the necessary offset and for each union member with the given `types`

                   # File fiddle/lib/fiddle/struct.rb, line 237
    def set_ctypes(types)
      @ctypes = types
      @offset = Array.new(types.length, 0)
      @size   = self.class.size types
    end