• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2021 Huawei Device Co., Ltd.
2# Licensed under the Apache License, Version 2.0 (the "License");
3# you may not use this file except in compliance with the License.
4# You may obtain a copy of the License at
5#
6# http://www.apache.org/licenses/LICENSE-2.0
7#
8# Unless required by applicable law or agreed to in writing, software
9# distributed under the License is distributed on an "AS IS" BASIS,
10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11# See the License for the specific language governing permissions and
12# limitations under the License.
13
14require 'ostruct'
15require 'delegate'
16
17class Option < SimpleDelegator
18  def field_name
19    name.tr('-', '_').tr('.', '_') + '_'
20  end
21
22  def getter_name
23    r = name.split(Regexp.union(['-', '.'])).map(&:capitalize).join
24    type == 'bool' ? 'Is' + r : 'Get' + r
25  end
26
27  def setter_name
28    'Set' + name.split(Regexp.union(['-', '.'])).map(&:capitalize).join
29  end
30
31  def deprecated?
32    respond_to?(:deprecated) && deprecated
33  end
34
35  def default_value
36    return default_constant_name if need_default_constant
37    return '{' + default.map { |e| expand_string(e) }.join(', ') + '}' if type == 'arg_list_t'
38    return expand_string(default) if type == 'std::string'
39    default
40  end
41
42  def full_description
43    full_desc = description
44    full_desc.prepend("[DEPRECATED] ") if deprecated?
45    if defined? possible_values
46      full_desc += '. Possible values: ' + possible_values.inspect
47    end
48    Common::to_raw(full_desc + '. Default: ' + default.inspect)
49  end
50
51  def expand_string(s)
52    @expansion_map ||= {
53      '$ORIGIN' => 'exe_dir_'
54    }
55    @expansion_map.each do |k, v|
56      ret = ""
57      if s.include?(k)
58        splitted = s.split(k);
59        for i in 1..splitted.length() - 1
60          ret += v + ' + ' + Common::to_raw(splitted[i])+ ' + '
61        end
62        return ret.delete_suffix(' + ')
63      end
64    end
65    Common::to_raw(s)
66  end
67
68  def need_default_constant
69    type == 'int' || type == 'double' || type == 'uint32_t' || type == 'uint64_t'
70  end
71
72  def default_constant_name
73    name.tr('-', '_').tr('.', '_').upcase + '_DEFAULT'
74  end
75end
76
77class Event < SimpleDelegator
78  def method_name
79    'Event' + name.split('-').map(&:capitalize).join
80  end
81
82  def args_list
83    args = ''
84    delim = ''
85    fields.each do |field|
86      args.concat(delim, field.type, ' ', field.name)
87      delim = ', '
88    end
89    return args
90  end
91
92  def print_line
93    qoute = '"'
94    line = 'events_file'
95    delim = ' << '
96    fields.each do |field|
97      line.concat(delim, qoute, field.name, qoute)
98      delim = ' << ":" << '
99      line.concat(delim, field.name)
100      delim = ' << "," << '
101    end
102    return line
103  end
104
105end
106
107module Common
108  module_function
109
110  def options
111    @data.options.each_with_object([]) do |option, options|
112      option_hash = option.to_h
113      if option_hash.include?(:lang)
114        new_option = option_hash.clone
115        new_option.delete(:lang)
116        option.lang.each do |lang|
117          new_option[:name] = "#{lang}.#{option.name}"
118          options << Option.new(OpenStruct.new(new_option))
119        end
120        new_option[:name] = "#{option.name}"
121        options << Option.new(OpenStruct.new(new_option))
122      else
123        options << Option.new(option)
124      end
125    end
126  end
127
128  def events
129    @data.events.map do |op|
130      Event.new(op)
131    end
132  end
133
134  def module
135    @data.module
136  end
137
138  def to_raw(s)
139    'R"(' + s + ')"'
140  end
141
142  def wrap_data(data)
143    @data = data
144  end
145end
146
147def Gen.on_require(data)
148  Common.wrap_data(data)
149end
150