1# Copyright (c) 2021-2022 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 String 18 def camelize 19 self.split('_').collect(&:capitalize).join 20 end 21end 22 23class Field 24 attr_reader :name 25 attr_reader :type 26 27 def initialize(event, dscr) 28 @dscr = dscr 29 @name = dscr['name'] 30 if dscr['type'] =='enum' 31 @type = event.name.camelize + dscr['name'].camelize 32 else 33 @type = dscr['type'] 34 end 35 @need_move = ['std::string'].include? @type 36 end 37 38 def arg_type 39 @need_move ? "const #{@type}&" : @type 40 end 41 42 def is_enum? 43 @dscr['type'] =='enum' 44 end 45 46 def enums 47 return @dscr['enum'] 48 end 49end 50 51class Event 52 attr_reader :name 53 54 def initialize(dscr) 55 @dscr = dscr 56 @name = dscr['name'] 57 end 58 59 def fields 60 @fields ||= @dscr['fields'].map { |field| Field.new(self, field) } 61 end 62 63 def enable? 64 @dscr.respond_to?('enable') ? @dscr['enable'] : true 65 end 66 67end 68 69module EventsData 70 module_function 71 72 def events 73 @events 74 end 75 76 def wrap_data(data) 77 @events = data.events.map { |op| Event.new(op) } 78 end 79end 80 81def Gen.on_require(data) 82 EventsData.wrap_data(data) 83end 84