Class: Rbind::RTemplateClass

Inherits:
RClass show all
Extended by:
Logger
Defined in:
lib/rbind/core/rtemplate_class.rb

Direct Known Subclasses

StdMap, StdVector

Instance Attribute Summary collapse

Attributes included from Logger

#log

Attributes inherited from RClass

#attributes, #parent_classes, #polymorphic

Attributes inherited from RNamespace

#consts, #operation_alias, #operations, #root, #type_alias, #used_namespaces

Attributes inherited from RDataType

#cdelete_method, #check_type, #invalid_value, #typedef

Attributes inherited from RBase

#alias, #auto_alias, #cname, #csignature, #doc, #extern_package_name, #ignore, #name, #namespace, #owner, #signature, #version

Instance Method Summary collapse

Methods included from Logger

extend_object

Methods inherited from RClass

#add_attribute, #add_child, #add_parent, #attribute, #basic_type?, #cdelete_method, #child?, #child_class, #child_classes, #constructor?, #empty?, #operation, #operations, #parent?, #parent_class, #polymorphic?, #pretty_print, #pretty_print_name, #used_namespaces

Methods inherited from RNamespace

#add_const, #add_const_val, #add_default_types, #add_namespace, #add_operation, #add_simple_type, #add_simple_types, #add_type_alias, #const, #constructor?, #container?, default_operators, #delete_type, #each_const, #each_container, #each_operation, #each_type, #empty?, #enum_hash, #extern?, #method_missing, #operation, #operation?, #pretty_print, #pretty_print_name, #root?, #type, #type?, #types, #use_namespace, #used_extern_types

Methods included from Hooks

included

Methods inherited from RDataType

#==, #basic_type?, #check_type?, #cname, #const?, #container?, #ownership?, #ptr?, #raw?, #ref?, #remove_const, #remove_ownership, #remove_ptr, #remove_ref, #to_const, #to_ownership, #to_ptr, #to_raw, #to_ref, #to_single_ptr, #typedef?

Methods inherited from RBase

basename, #binding, #delete!, #doc?, #extern?, #full_name, #generate_signatures, #ignore?, #map_to_namespace, namespace, #namespace?, normalize, #overwrite_c, #overwrite_ruby, #pretty_print, #rename, #specialize_ruby, split_name, to_cname, #to_s

Constructor Details

#initialize(name, *parent_classes) ⇒ RTemplateClass

Returns a new instance of RTemplateClass.



30
31
32
33
34
# File 'lib/rbind/core/rtemplate_class.rb', line 30

def initialize(name,*parent_classes)
    raise "parent classes for template classes are not supported!" if !parent_classes.empty?
    @template_parameters =[]
    super
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Rbind::RNamespace

Instance Attribute Details

#template_parametersObject (readonly)

template parameters in the right order



28
29
30
# File 'lib/rbind/core/rtemplate_class.rb', line 28

def template_parameters
  @template_parameters
end

Instance Method Details

#add_type(klass) ⇒ Object



104
105
106
107
108
109
# File 'lib/rbind/core/rtemplate_class.rb', line 104

def add_type(klass)
    if klass.kind_of?(RTemplateParameter)
        @template_parameters << klass
    end
    super
end

#do_specialize(name, *parameters) ⇒ Object

called by RNamespace



41
42
43
44
# File 'lib/rbind/core/rtemplate_class.rb', line 41

def do_specialize(name,*parameters)
    klass = RTemplateClassSpecialization.new(name,self,*parameters)
    specialize(klass,*parameters)
end

#resolve_type(parameters, param_name) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rbind/core/rtemplate_class.rb', line 85

def resolve_type(parameters, param_name)
    # This resolves
    resolved_type = type(param_name, false)
    if not resolved_type
        raise ArgumentError, "RTemplateClass: template #{name} could not resolve template parameter #{param_name}"
    end

    if index = @template_parameters.index(resolved_type)
        resolved_type = type(parameters[index])
    end

    resolved_type
end

#specialize(klass, *parameters) ⇒ Object

hook for implementing the specialization



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
78
79
80
81
82
83
# File 'lib/rbind/core/rtemplate_class.rb', line 47

def specialize(klass,*parameters)
    RTemplateClass.log.info "RTemplateClass: #{name} default specialized with #{klass} -- specialization parameters: #{parameters}, template parameters: #{template_parameters}"
    # by default create a dummy implementation without additional arguments
    if parameters.size != @template_parameters.size
        raise ArgumentError, "RTemplateClass: template #{name} expects #{@template_parameters.size} parameters"
    end

    operations.each do |ops|
        ops.each do |o|
            begin
                RTemplateClass.log.debug "RTemplateClass: #{name} handle operation: #{o.name}"
                op = o.dup

                if op.kind_of?(RGetter) or op.kind_of?(RSetter)
                    attr = RAttribute.new(op.attribute.name, resolve_type(parameters, op.attribute.type))
                    op = op.class.new(attr)
                    klass.add_operation(op)
                    next
                else
                    op.return_type = resolve_type(parameters, op.return_type.name)
                end

                op.parameters.each do |p|
                    rtype = resolve_type(parameters, p.type.name)
                    RTemplateClass.log.debug "RTemplateClass: #{name} specialized with #{klass} -- resolved #{p.type.name} -> #{rtype}"
                    p.type = rtype
                end

                klass.add_operation(op)
            rescue => e
                RTemplateClass.log.warn "RTemplateClass: #{name} could not add parameter #{e} #{e.backtrace}"

            end
        end
    end
    klass
end

#specialize_ruby_specialization(klass) ⇒ Object

hook for generating additional ruby code going to be embedded into the class definition



101
102
# File 'lib/rbind/core/rtemplate_class.rb', line 101

def specialize_ruby_specialization(klass)
end

#template?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/rbind/core/rtemplate_class.rb', line 36

def template?
    true
end