1 /** 2 * Copyright (c) 2021-2022 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 LIBPANDAFILE_PROTO_DATA_ACCESSOR_H 17 #define LIBPANDAFILE_PROTO_DATA_ACCESSOR_H 18 19 #include "file.h" 20 #include "file_items.h" 21 22 namespace panda::panda_file { 23 24 // NOLINTNEXTLINE(cppcoreguidelines-special-member-functions, hicpp-special-member-functions) 25 class ProtoDataAccessor { 26 public: ProtoDataAccessor(const File & panda_file,File::EntityId proto_id)27 ProtoDataAccessor(const File &panda_file, File::EntityId proto_id) : panda_file_(panda_file), proto_id_(proto_id) {} 28 29 ~ProtoDataAccessor() = default; 30 GetPandaFile()31 const File &GetPandaFile() const 32 { 33 return panda_file_; 34 } 35 GetProtoId()36 File::EntityId GetProtoId() const 37 { 38 return proto_id_; 39 } 40 GetShorty()41 Span<const uint8_t> GetShorty() const 42 { 43 return panda_file_.GetSpanFromId(proto_id_); 44 } 45 46 template <class Callback> 47 void EnumerateTypes(const Callback &c); 48 49 uint32_t GetNumArgs(); 50 51 Type GetArgType(size_t idx) const; 52 53 Type GetReturnType() const; 54 55 File::EntityId GetReferenceType(size_t i); 56 GetRefNum()57 size_t GetRefNum() 58 { 59 if (ref_types_sp_.data() == nullptr) { 60 SkipShorty(); 61 } 62 63 return ref_types_num_; 64 } 65 GetSize()66 size_t GetSize() 67 { 68 if (size_ == 0) { 69 SkipShorty(); 70 } 71 72 return size_; 73 } 74 75 bool IsEqual(ProtoDataAccessor *other); 76 77 private: 78 void SkipShorty(); 79 80 Type GetType(size_t idx) const; 81 82 const File &panda_file_; 83 File::EntityId proto_id_; 84 85 size_t elem_num_ {0}; 86 Span<const uint8_t> ref_types_sp_ {nullptr, nullptr}; 87 size_t ref_types_num_ {0}; 88 size_t size_ {0}; 89 }; 90 91 } // namespace panda::panda_file 92 93 #endif // LIBPANDAFILE_PROTO_DATA_ACCESSOR_H 94