Class: AutoC::Box

Inherits:
Composite show all
Defined in:
lib/autoc/box.rb

Overview

C union wrapper with managed fields

Constant Summary

Constants inherited from Composite

Composite::DEFINITIONS, Composite::PRIVATE

Constants included from Entity

Entity::ReferenceSet

Constants included from STD

STD::ASSERT_H, STD::BOOL, STD::CHAR, STD::COMPLEX, STD::COMPLEX_H, STD::DOUBLE, STD::DOUBLE_COMPLEX, STD::DOUBLE_T, STD::FLOAT, STD::FLOAT_COMPLEX, STD::FLOAT_T, STD::INT, STD::INTMAX_T, STD::INTPTR_T, STD::INTTYPES_H, STD::LONG, STD::LONG_DOUBLE, STD::LONG_DOUBLE_COMPLEX, STD::LONG_LONG, STD::MALLOC_H, STD::MATH_H, STD::PTRDIFF_T, STD::SHORT, STD::SIGNED_CHAR, STD::SIZE_T, STD::STDBOOL_H, STD::STDDEF_H, STD::STDLIB_H, STD::STRING_H, STD::UINTMAX_T, STD::UINTPTR_T, STD::UNSIGNED, STD::UNSIGNED_CHAR, STD::UNSIGNED_LONG, STD::UNSIGNED_LONG_LONG, STD::UNSIGNED_SHORT, STD::WCHAR_T

Instance Attribute Summary collapse

Attributes inherited from Composite

#_master, #visibility

Attributes inherited from Type

#signature

Instance Method Summary collapse

Methods inherited from Composite

allocator, allocator=, #const_lvalue, #const_rvalue, decorator, decorator=, #defgroup, #hasher, hasher, hasher=, #identifier, #ingroup, #inspect, #internal?, #lvalue, #memory, new, #prefix, #private?, #public?, #respond_to_missing?, #rvalue, #to_value

Methods included from Entity

#<=>, #complexity, #dependencies, #forward_declarations, #implementation, #interface, #position, #references, #total_dependencies, #total_references

Methods inherited from Type

abstract, #constructible?, #copy, #custom_create, #default_create, #destroy, #inspect, #to_s, #to_type

Constructor Details

#initialize(type, variants, visibility: :public) ⇒ Box

Returns a new instance of Box.



29
30
31
32
33
34
# File 'lib/autoc/box.rb', line 29

def initialize(type, variants, visibility: :public)
  super(type, visibility:)
  setup_variants(variants)
  @tag_ = "#{signature}_";
  @default = 'abort();'
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class AutoC::Composite

Instance Attribute Details

#tag_Object (readonly)

Returns the value of attribute tag_.



19
20
21
# File 'lib/autoc/box.rb', line 19

def tag_
  @tag_
end

#variantsObject (readonly)

Returns the value of attribute variants.



17
18
19
# File 'lib/autoc/box.rb', line 17

def variants
  @variants
end

Instance Method Details

#comparable?Boolean

Returns:

  • (Boolean)


24
# File 'lib/autoc/box.rb', line 24

def comparable? = variants.values.all? { |t| t.comparable? }

#copyable?Boolean

Returns:

  • (Boolean)


26
# File 'lib/autoc/box.rb', line 26

def copyable? = variants.values.all? { |t| t.copyable? }

#custom_constructible?Boolean

Returns:

  • (Boolean)


22
# File 'lib/autoc/box.rb', line 22

def custom_constructible? = false

#default_constructible?Boolean

Returns:

  • (Boolean)


21
# File 'lib/autoc/box.rb', line 21

def default_constructible? = true

#destructible?Boolean

Returns:

  • (Boolean)


23
# File 'lib/autoc/box.rb', line 23

def destructible? = variants.values.any? { |t| t.destructible? }

#hashable?Boolean

Returns:

  • (Boolean)


27
# File 'lib/autoc/box.rb', line 27

def hashable? = variants.values.all? { |t| t.hashable? }

#orderable?Boolean

Returns:

  • (Boolean)


25
# File 'lib/autoc/box.rb', line 25

def orderable? = variants.values.all? { |t| t.orderable? }

#render_interface(stream) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/autoc/box.rb', line 36

def render_interface(stream)
  if public?
    stream << %{
      /**
        #{defgroup}
        @brief Value type wrapper of the C union
      */
    }
    stream << %{
      /**
        #{ingroup}

        @brief Box tag set

        Use @ref #{identifier(:tag)} to query current box contents. Empty box is always tagged as 0.

        @since 2.0
      */
    }
  else
    stream << PRIVATE
  end
  stream << 'typedef enum {'
  i = 0; stream << (variants.collect { |name, type| "#{identifier(name)} = #{i+=1}" }).join(',')
  stream << "} #{tag_};"
  if public?
    stream << %{
      /**
        #{ingroup}

        @brief Opaque struct holding state of the box

        @since 2.0
      */
    }
  else
    stream << PRIVATE
  end
  stream << 'typedef struct {union {'
    variants.each { |name, type| stream << field_declaration(type, name) }
  stream << "} variant; /**< @private */ #{tag_} tag; /**< @private */} #{signature};"
end

#type_tagObject



79
# File 'lib/autoc/box.rb', line 79

def type_tag = "#{signature}<#{variants.values.join(',')}>"