1b1994897Sopenharmony_ci/**
2b1994897Sopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3b1994897Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4b1994897Sopenharmony_ci * you may not use this file except in compliance with the License.
5b1994897Sopenharmony_ci * You may obtain a copy of the License at
6b1994897Sopenharmony_ci *
7b1994897Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8b1994897Sopenharmony_ci *
9b1994897Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10b1994897Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11b1994897Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12b1994897Sopenharmony_ci * See the License for the specific language governing permissions and
13b1994897Sopenharmony_ci * limitations under the License.
14b1994897Sopenharmony_ci */
15b1994897Sopenharmony_ci
16b1994897Sopenharmony_ci#ifndef LIBPANDAFILE_VALUE_H
17b1994897Sopenharmony_ci#define LIBPANDAFILE_VALUE_H
18b1994897Sopenharmony_ci
19b1994897Sopenharmony_ci#include <type_traits>
20b1994897Sopenharmony_ci
21b1994897Sopenharmony_ci#include "file.h"
22b1994897Sopenharmony_ci#include "helpers.h"
23b1994897Sopenharmony_ci
24b1994897Sopenharmony_cinamespace panda::panda_file {
25b1994897Sopenharmony_ci
26b1994897Sopenharmony_ciclass ScalarValue {
27b1994897Sopenharmony_cipublic:
28b1994897Sopenharmony_ci    ScalarValue(const File &panda_file, uint32_t value) : panda_file_(panda_file), value_(value) {}
29b1994897Sopenharmony_ci
30b1994897Sopenharmony_ci    ~ScalarValue() = default;
31b1994897Sopenharmony_ci
32b1994897Sopenharmony_ci    NO_COPY_SEMANTIC(ScalarValue);
33b1994897Sopenharmony_ci    NO_MOVE_SEMANTIC(ScalarValue);
34b1994897Sopenharmony_ci
35b1994897Sopenharmony_ci    template <class T>
36b1994897Sopenharmony_ci    T Get() const
37b1994897Sopenharmony_ci    {
38b1994897Sopenharmony_ci        static_assert(std::is_arithmetic_v<T> || std::is_same_v<T, File::EntityId>);
39b1994897Sopenharmony_ci
40b1994897Sopenharmony_ci        if constexpr (std::is_same_v<T, float>) {  // NOLINT
41b1994897Sopenharmony_ci            return bit_cast<float>(value_);
42b1994897Sopenharmony_ci        }
43b1994897Sopenharmony_ci
44b1994897Sopenharmony_ci        constexpr size_t T_SIZE = sizeof(T);
45b1994897Sopenharmony_ci
46b1994897Sopenharmony_ci        // NOLINTNEXTLINE(readability-braces-around-statements, bugprone-suspicious-semicolon)
47b1994897Sopenharmony_ci        if constexpr (T_SIZE <= sizeof(uint32_t)) {
48b1994897Sopenharmony_ci            return static_cast<T>(value_);
49b1994897Sopenharmony_ci        }
50b1994897Sopenharmony_ci
51b1994897Sopenharmony_ci        File::EntityId id(value_);
52b1994897Sopenharmony_ci        auto sp = panda_file_.GetSpanFromId(id);
53b1994897Sopenharmony_ci        auto res = helpers::Read<T_SIZE>(&sp);
54b1994897Sopenharmony_ci
55b1994897Sopenharmony_ci        if constexpr (std::is_floating_point_v<T>) {  // NOLINT
56b1994897Sopenharmony_ci            return bit_cast<T>(res);
57b1994897Sopenharmony_ci        }
58b1994897Sopenharmony_ci
59b1994897Sopenharmony_ci        return static_cast<T>(res);
60b1994897Sopenharmony_ci    }
61b1994897Sopenharmony_ci
62b1994897Sopenharmony_ci    uint32_t GetValue() const
63b1994897Sopenharmony_ci    {
64b1994897Sopenharmony_ci        return value_;
65b1994897Sopenharmony_ci    }
66b1994897Sopenharmony_ci
67b1994897Sopenharmony_ciprivate:
68b1994897Sopenharmony_ci    const File &panda_file_;
69b1994897Sopenharmony_ci    uint32_t value_;
70b1994897Sopenharmony_ci};
71b1994897Sopenharmony_ci
72b1994897Sopenharmony_ciclass ArrayValue {
73b1994897Sopenharmony_cipublic:
74b1994897Sopenharmony_ci    ArrayValue(const File &panda_file, File::EntityId id) : panda_file_(panda_file), id_(id)
75b1994897Sopenharmony_ci    {
76b1994897Sopenharmony_ci        auto sp = panda_file_.GetSpanFromId(id_);
77b1994897Sopenharmony_ci        count_ = helpers::ReadULeb128(&sp);
78b1994897Sopenharmony_ci        data_ = sp;
79b1994897Sopenharmony_ci    }
80b1994897Sopenharmony_ci
81b1994897Sopenharmony_ci    ~ArrayValue() = default;
82b1994897Sopenharmony_ci
83b1994897Sopenharmony_ci    NO_COPY_SEMANTIC(ArrayValue);
84b1994897Sopenharmony_ci    NO_MOVE_SEMANTIC(ArrayValue);
85b1994897Sopenharmony_ci
86b1994897Sopenharmony_ci    template <class T>
87b1994897Sopenharmony_ci    T Get(size_t idx) const
88b1994897Sopenharmony_ci    {
89b1994897Sopenharmony_ci        static_assert(std::is_arithmetic_v<T> || std::is_same_v<T, File::EntityId>);
90b1994897Sopenharmony_ci
91b1994897Sopenharmony_ci        constexpr size_t T_SIZE = sizeof(T);
92b1994897Sopenharmony_ci
93b1994897Sopenharmony_ci        auto sp = data_.SubSpan(T_SIZE * idx);
94b1994897Sopenharmony_ci        auto res = helpers::Read<T_SIZE>(&sp);
95b1994897Sopenharmony_ci
96b1994897Sopenharmony_ci        if constexpr (std::is_floating_point_v<T>) {  // NOLINT
97b1994897Sopenharmony_ci            return bit_cast<T>(res);
98b1994897Sopenharmony_ci        }
99b1994897Sopenharmony_ci
100b1994897Sopenharmony_ci        return static_cast<T>(res);
101b1994897Sopenharmony_ci    }
102b1994897Sopenharmony_ci
103b1994897Sopenharmony_ci    uint32_t GetCount() const
104b1994897Sopenharmony_ci    {
105b1994897Sopenharmony_ci        return count_;
106b1994897Sopenharmony_ci    }
107b1994897Sopenharmony_ci
108b1994897Sopenharmony_ci    File::EntityId GetId() const
109b1994897Sopenharmony_ci    {
110b1994897Sopenharmony_ci        return id_;
111b1994897Sopenharmony_ci    }
112b1994897Sopenharmony_ci
113b1994897Sopenharmony_ciprivate:
114b1994897Sopenharmony_ci    static constexpr size_t COUNT_SIZE = sizeof(uint32_t);
115b1994897Sopenharmony_ci
116b1994897Sopenharmony_ci    const File &panda_file_;
117b1994897Sopenharmony_ci    File::EntityId id_;
118b1994897Sopenharmony_ci    uint32_t count_;
119b1994897Sopenharmony_ci    Span<const uint8_t> data_ {nullptr, nullptr};
120b1994897Sopenharmony_ci};
121b1994897Sopenharmony_ci
122b1994897Sopenharmony_ci}  // namespace panda::panda_file
123b1994897Sopenharmony_ci
124b1994897Sopenharmony_ci#endif  // LIBPANDAFILE_VALUE_H
125