Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Allow creating components from gtype, not only gi.info

Also update component.clone to accept 'type' which is stored into
'_type' field of mt and can be used for runtime type identification of
repotables.
  • Loading branch information...
commit 0f9f33f76ac3f635b3183e97603717f1d7c359ff 1 parent c84dffe
@pavouk authored
View
12 lgi/class.lua
@@ -19,12 +19,12 @@ local record = require 'lgi.record'
-- Implementation of class and interface component loading.
local class = {
- class_mt = component.mt:clone {
- '_virtual', '_property', '_signal', '_method', '_constant', '_field'
- },
- interface_mt = component.mt:clone {
- '_virtual', '_property', '_signal', '_method', '_constant'
- },
+ class_mt = component.mt:clone(
+ 'class', { '_virtual', '_property', '_signal',
+ '_method', '_constant', '_field' }),
+ interface_mt = component.mt:clone(
+ 'interface', { '_virtual', '_property', '_signal',
+ '_method', '_constant' }),
}
-- Checks whether given argument is type of this class.
View
21 lgi/component.lua
@@ -120,9 +120,10 @@ end
-- Creates new component table by cloning all contents and setting
-- categories table.
-function component.mt:clone(categories)
+function component.mt:clone(type, categories)
local new_component = {}
for key, value in pairs(self) do new_component[key] = value end
+ new_component._type = type
if categories then
table.insert(categories, 1, '_attribute')
new_component._categories = categories
@@ -269,13 +270,21 @@ end
-- Creates new component and sets up common parts according to given
-- info.
-function component.create(info, mt)
+function component.create(info, mt, name)
+ local gtype
+ if core.gi.isinfo(info) then
+ gtype = info.gtype
+ name = info.fullname
+ else
+ gtype = info and core.gtype(info)
+ end
+
-- Fill in meta of the compound.
- local component = { _name = info.fullname }
- if info.gtype then
+ local component = { _name = name }
+ if gtype then
-- Bind component in repo, make the relation using GType.
- component._gtype = info.gtype
- core.index[info.gtype] = component
+ component._gtype = gtype
+ core.index[gtype] = component
end
return setmetatable(component, mt)
end
View
4 lgi/enum.lua
@@ -16,8 +16,8 @@ local component = require 'lgi.component'
local band, bor = core.band, core.bor
local enum = {
- enum_mt = component.mt:clone { '_method' },
- bitflags_mt = component.mt:clone { '_method' }
+ enum_mt = component.mt:clone('enum', { '_method' }),
+ bitflags_mt = component.mt:clone('flags', { '_method' }),
}
function enum.load(info, meta)
View
23 lgi/record.lua
@@ -18,10 +18,12 @@ local component = require 'lgi.component'
-- Implementation of record_mt, which is inherited from component
-- and provides customizations for structures and unions.
-local record = { mt = component.mt:clone { '_method', '_field' } }
+local record = {
+ struct_mt = component.mt:clone('struct', { '_method', '_field' }),
+}
-- Checks whether given argument is type of this class.
-function record.mt:is_type_of(instance)
+function record.struct_mt:is_type_of(instance)
if type(instance) == 'userdata' then
local instance_type = core.record.query(instance, 'repo')
while instance_type do
@@ -32,7 +34,7 @@ function record.mt:is_type_of(instance)
return false
end
-function record.mt:_element(instance, symbol)
+function record.struct_mt:_element(instance, symbol)
-- First of all, try normal inherited functionality.
local element, category = component.mt._element(self, instance, symbol)
if element then return element, category end
@@ -59,7 +61,7 @@ function record.mt:_element(instance, symbol)
end
-- Add accessor for handling fields.
-function record.mt:_access_field(instance, element, ...)
+function record.struct_mt:_access_field(instance, element, ...)
assert(gi.isinfo(element) and element.is_field)
-- Check the type of the field.
local ii = element.typeinfo.interface
@@ -80,13 +82,13 @@ function record.mt:_access_field(instance, element, ...)
end
-- Add accessor for 'internal' fields handling.
-function record.mt:_access_internal(instance, element, ...)
+function record.struct_mt:_access_internal(instance, element, ...)
if select('#', ...) ~= 0 or element ~= '_native' then return end
return core.record.query(instance, 'addr')
end
-- Add accessor for accessing inherited elements.
-function record.mt:_access_inherited(instance, element, ...)
+function record.struct_mt:_access_inherited(instance, element, ...)
-- Cast instance to inherited type.
instance = core.record.cast(instance, element.type)
@@ -96,7 +98,7 @@ function record.mt:_access_inherited(instance, element, ...)
end
-- Create structure instance and initialize it with given fields.
-function record.mt:_new(param, owns)
+function record.struct_mt:_new(param, owns)
-- Find baseinfo of requested record.
local info, struct
if self._gtype then
@@ -126,7 +128,8 @@ end
-- Loads structure information into table representing the structure
function record.load(info)
- local record = component.create(info, record.mt)
+ local record = component.create(
+ info, info.is_struct and record.struct_mt or record.union_mt)
record._size = info.size
record._method = component.get_category(info.methods, core.callable.new)
record._field = component.get_category(info.fields)
@@ -149,4 +152,8 @@ function record.load(info)
return record
end
+-- Union metatable is the same as struct one, but has different name
+-- to differentiate unions.
+record.union_mt = record.struct_mt:clone('union')
+
return record
Please sign in to comment.
Something went wrong with that request. Please try again.