• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef PANDA_LIBPANDAFILE_LITERAL_DATA_ACCESSOR_H_
17 #define PANDA_LIBPANDAFILE_LITERAL_DATA_ACCESSOR_H_
18 
19 #include "field_data_accessor.h"
20 #include "file.h"
21 #include "utils/span.h"
22 
23 namespace panda::panda_file {
24 using StringData = File::StringData;
25 
26 /* LiteralTag could be extended by different language
27 // For example, JAVA could use it to represent Array of Integer
28 // by adding `INTARRAY` in the future
29 */
30 enum class LiteralTag : uint8_t {
31     TAGVALUE = 0x00,
32     BOOL = 0x01,
33     INTEGER = 0x02,
34     FLOAT = 0x03,
35     DOUBLE = 0x04,
36     STRING = 0x05,
37     METHOD = 0x06,
38     GENERATORMETHOD = 0x07,
39     ACCESSOR = 0x08,
40     METHODAFFILIATE = 0x09,
41     ARRAY_I8 = 0x0a,
42     ARRAY_I16 = 0x0b,
43     ARRAY_I32 = 0x0c,
44     ARRAY_I64 = 0x0d,
45     ARRAY_F32 = 0x0e,
46     ARRAY_F64 = 0x0f,
47     ARRAY_STRING = 0x10,
48     NULLVALUE = 0xff
49 };
50 
51 class LiteralDataAccessor {
52 public:
53     LiteralDataAccessor(const File &panda_file, File::EntityId literal_data_id);
54     ~LiteralDataAccessor() = default;
55     DEFAULT_MOVE_CTOR(LiteralDataAccessor)
56     DEFAULT_COPY_CTOR(LiteralDataAccessor)
57     NO_MOVE_OPERATOR(LiteralDataAccessor);
58     NO_COPY_OPERATOR(LiteralDataAccessor);
59 
60     template <class Callback>
61     void EnumerateObjectLiterals(size_t index, const Callback &cb);
62 
63     template <class Callback>
64     void EnumerateLiteralVals(size_t index, const Callback &cb);
65 
66     template <class Callback>
67     void EnumerateLiteralVals(File::EntityId id, const Callback &cb);
68 
69     size_t GetLiteralValsNum(size_t index);
70 
GetLiteralNum()71     uint32_t GetLiteralNum() const
72     {
73         return literal_num_;
74     }
75 
GetPandaFile()76     const File &GetPandaFile() const
77     {
78         return panda_file_;
79     }
80 
GetLiteralDataId()81     File::EntityId GetLiteralDataId() const
82     {
83         return literal_data_id_;
84     }
85 
86     using LiteralValue = std::variant<bool, void *, uint8_t, uint16_t, uint32_t, uint64_t, float, double, StringData>;
87 
88 private:
89     const File &panda_file_;
90     File::EntityId literal_data_id_;
91     uint32_t literal_num_;
92     Span<const uint8_t> literal_data_sp_ {nullptr, nullptr};
93 };
94 
95 }  // namespace panda::panda_file
96 
97 #endif  // PANDA_LIBPANDAFILE_LITERAL_DATA_ACCESSOR_H_
98