1#!/usr/bin/env ruby
2
3# Copyright (c) 2021-2024 Huawei Device Co., Ltd.
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16class Regmap
17  attr_reader :data
18
19  def initialize(data, direct: false)
20    if direct
21      @data = data
22    else
23      @data = data[Options.arch]
24      raise "Regmap doesn't contain data for required arch" unless @data
25    end
26
27  end
28
29  def self.from_hash(data)
30    Regmap.new(data, direct: true)
31  end
32
33  def [](v)
34    @data[v]
35  end
36
37  def +(other)
38    if other.is_a? Regmap
39      Regmap.new(@data.merge(other.data), direct: true)
40    elsif other.is_a? Hash
41      Regmap.new(@data.merge(other), direct: true)
42    else
43      raise "Unsupported type: #{other.class}"
44    end
45  end
46
47  def -(other)
48    if other.is_a? Regmap
49      Regmap.new(@data.select { |k, _| !other.data.key?(k) }, direct: true)
50    elsif other.is_a? Hash
51      Regmap.new(@data.select { |k, _| !other.key?(k) }, direct: true)
52    else
53      raise "Unsupported type: #{other.class}"
54    end
55  end
56
57  def ==(other)
58    if other.is_a? Regmap
59      @data == other.data
60    elsif other.is_a? Hash
61      @data == other
62    else
63      false
64    end
65  end
66
67  def to_s
68    @data.to_s
69  end
70
71  def inspect
72    @data.inspect
73  end
74end
75