Class: Babeltrace2Gen::BTValueCLass::Scalar

Inherits:
Babeltrace2Gen::BTValueCLass show all
Includes:
Babeltrace2Gen::BTPrinter
Defined in:
lib/metababel/bt2_values_generator.rb

Direct Known Subclasses

Bool, IntegerUnsigned, String

Constant Summary

Constants included from Babeltrace2Gen::BTPrinter

Babeltrace2Gen::BTPrinter::INDENT_INCREMENT

Instance Attribute Summary collapse

Attributes inherited from Babeltrace2Gen::BTValueCLass

#cast_type

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Babeltrace2Gen::BTPrinter

context, #name_sanitized, pr, #scope

Constructor Details

#initialize(name, usr_default_value) ⇒ Scalar



34
35
36
37
# File 'lib/metababel/bt2_values_generator.rb', line 34

def initialize(name, usr_default_value)
  @name = name
  @usr_default_value = usr_default_value
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



25
26
27
# File 'lib/metababel/bt2_values_generator.rb', line 25

def name
  @name
end

#usr_default_valueObject

Returns the value of attribute usr_default_value.



25
26
27
# File 'lib/metababel/bt2_values_generator.rb', line 25

def usr_default_value
  @usr_default_value
end

Class Method Details

.from_h(model) ⇒ Object

Scalars are leaves, avoid recursion



30
31
32
# File 'lib/metababel/bt2_values_generator.rb', line 30

def self.from_h(model)
  new(model[:name], model.fetch(:default_value, nil))
end

Instance Method Details

#get(name, val) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/metababel/bt2_values_generator.rb', line 39

def get(name, val)
  cast_func = @cast_type ? "(#{@cast_type})" : ''
  bt_default_value = self.class.instance_variable_get(:@bt_default_value)
  bt_type = self.class.instance_variable_get(:@bt_type)
  bt_type_is = self.class.instance_variable_get(:@bt_type_is)

  default_value = @usr_default_value || bt_default_value

  pr "if (#{val} != NULL) {"
  pr "  if (!#{bt_type_is}(#{val})) {"
  pr "    fprintf(stderr,\"Bad value for command line argument '%s' the value must be '%s'. \\n\",\"#{@name}\",\"#{bt_type}\");"
  pr '    exit(1);'
  pr '  }'
  pr "  #{name} = #{cast_func}bt_value_#{bt_type}_get(#{val});"
  pr '} else {'
  pr "  #{name} = #{cast_func}#{default_value};"
  pr '}'
end