Class: GObject::GValue

Inherits:
FFI::ManagedStruct
  • Object
show all
Defined in:
lib/vips/gvalue.rb

Overview

Represent a GValue. Example use:

gvalue = GValue::alloc
gvalue.init GObject::GDOUBLE_TYPE
gvalue.set 3.1415
value = gvalue.get

Lifetime is managed automatically. It doesn't know about all GType values, but it does know the ones that libvips uses.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.allocGValue

Allocate memory for a GValue and return a class wrapper. Memory will be freed automatically when it goes out of scope. The GValue is inited to 0, use #init to set a type.

Returns:

  • (GValue)

    a new gvalue set to 0



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/vips/gvalue.rb', line 36

def self.alloc
    # allocate memory
    memory = FFI::MemoryPointer.new GValue

    # make this alloc autorelease ... we mustn't release in 
    # GValue::release, since we are used to wrap GValue pointers 
    # made by other people
    pointer = FFI::Pointer.new GValue, memory

    # ... and wrap in a GValue
    return GValue.new pointer
end

.release(ptr) ⇒ Object



26
27
28
29
# File 'lib/vips/gvalue.rb', line 26

def self.release ptr
    # GLib::logger.debug("GObject::GValue::release") {"ptr = #{ptr}"}
    ::GObject::g_value_unset ptr 
end

Instance Method Details

#getAny

Get the value of a GValue. The value is converted to a Ruby type in the obvious way.

Returns:

  • (Any)

    the value held by the GValue



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/vips/gvalue.rb', line 151

def get
    gtype = self[:gtype]
    fundamental = ::GObject::g_type_fundamental gtype
    result = nil

    case gtype
    when GBOOL_TYPE
        result = ::GObject::g_value_get_boolean(self) != 0 ? true : false

    when GINT_TYPE
        result = ::GObject::g_value_get_int self

    when GDOUBLE_TYPE
        result = ::GObject::g_value_get_double self

    when GSTR_TYPE
        result = ::GObject::g_value_get_string self

    when Vips::REFSTR_TYPE
        len = Vips::SizeStruct.new
        result = ::Vips::vips_value_get_ref_string self, len

    when Vips::ARRAY_INT_TYPE
        len = Vips::IntStruct.new
        array = Vips::vips_value_get_array_int self, len
        result = array.get_array_of_int32 0, len[:value]

    when Vips::ARRAY_DOUBLE_TYPE
        len = Vips::IntStruct.new
        array = Vips::vips_value_get_array_double self, len
        result = array.get_array_of_double 0, len[:value]

    when Vips::ARRAY_IMAGE_TYPE
        len = Vips::IntStruct.new
        array = Vips::vips_value_get_array_image self, len
        result = array.get_array_of_pointer 0, len[:value]
        result.map! do |pointer| 
            ::GObject::g_object_ref pointer
            Vips::Image.new pointer
        end

    when Vips::BLOB_TYPE
        len = Vips::SizeStruct.new
        array = Vips::vips_value_get_blob self, len
        result = array.get_bytes 0, len[:value]

    else
        case fundamental
        when GFLAGS_TYPE
            result = ::GObject::g_value_get_flags self

        when GENUM_TYPE
            enum_value = ::GObject::g_value_get_enum self
            # this returns a static string, no need to worry about 
            # lifetime
            enum_name = Vips::vips_enum_nick self[:gtype], enum_value
            if enum_name == nil
                raise Vips::Error
            end
            result = enum_name.to_sym

        when GOBJECT_TYPE
            obj = ::GObject::g_value_get_object self
            # g_value_get_object() does not add a ref ... we need to add
            # one to match the unref in gobject release
            ::GObject::g_object_ref obj
            result = Vips::Image.new obj

        else
            raise Vips::Error, "unimplemented gtype for get: " +
                "#{::GObject::g_type_name gtype} (#{gtype})"

        end
    end

    # GLib::logger.debug("GObject::GValue.get") {
    #     "result = #{result.inspect[0..50]}"
    # }

    return result

end

#init(gtype) ⇒ Object

Set the type of thing a gvalue can hold.

Parameters:

  • gtype (GType)

    the type of thing this GValue can hold.



52
53
54
# File 'lib/vips/gvalue.rb', line 52

def init gtype
    ::GObject::g_value_init self, gtype
end

#set(value) ⇒ Object

Set the value of a GValue. The value is converted to the type of the GValue, if possible.

Parameters:

  • value (Any)

    The value to set



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/vips/gvalue.rb', line 60

def set value
    # GLib::logger.debug("GObject::GValue.set") {
    #     "value = #{value.inspect[0..50]}"
    # }

    gtype = self[:gtype]
    fundamental = ::GObject::g_type_fundamental gtype

    case gtype
    when GBOOL_TYPE
        ::GObject::g_value_set_boolean self, (value ? 1 : 0)

    when GINT_TYPE
        ::GObject::g_value_set_int self, value

    when GDOUBLE_TYPE
        ::GObject::g_value_set_double self, value

    when GSTR_TYPE
        ::GObject::g_value_set_string self, value

    when Vips::REFSTR_TYPE
        ::Vips::vips_value_set_ref_string self, value

    when Vips::ARRAY_INT_TYPE
        value = [value] if not value.is_a? Array

        Vips::vips_value_set_array_int self, nil, value.length
        ptr = Vips::vips_value_get_array_int self, nil
        ptr.write_array_of_int32 value

    when Vips::ARRAY_DOUBLE_TYPE
        value = [value] if not value.is_a? Array

        # this will allocate an array in the gvalue
        Vips::vips_value_set_array_double self, nil, value.length

        # pull the array out and fill it
        ptr = Vips::vips_value_get_array_double self, nil

        ptr.write_array_of_double value

    when Vips::ARRAY_IMAGE_TYPE
        value = [value] if not value.is_a? Array

        Vips::vips_value_set_array_image self, value.length
        ptr = Vips::vips_value_get_array_image self, nil
        ptr.write_array_of_pointer value

        # the gvalue needs a ref on each of the images
        value.each {|image| ::GObject::g_object_ref image}

    when Vips::BLOB_TYPE
        len = value.length
        ptr = GLib::g_malloc len
        Vips::vips_value_set_blob self, GLib::G_FREE, ptr, len
        ptr.write_bytes value

    else
        case fundamental
        when GFLAGS_TYPE
            ::GObject::g_value_set_flags self, value

        when GENUM_TYPE
            value = value.to_s if value.is_a? Symbol

            if value.is_a? String
                value = Vips::vips_enum_from_nick "ruby-vips", 
                    self[:gtype], value
                if value == -1
                    raise Vips::Error
                end
            end

            ::GObject::g_value_set_enum self, value

        when GOBJECT_TYPE
            ::GObject::g_value_set_object self, value

        else
            raise Vips::Error, "unimplemented gtype for set: " +
                "#{::GObject::g_type_name gtype} (#{gtype})"

        end
    end
end