1 /* 2 * Copyright 2019 The Android Open Source Project 3 * 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 */ 16 17 #pragma once 18 19 #include <map> 20 #include <variant> 21 22 #include "enum_def.h" 23 #include "field_list.h" 24 #include "fields/all_fields.h" 25 #include "fields/packet_field.h" 26 #include "parse_location.h" 27 #include "type_def.h" 28 29 class ParentDef : public TypeDef { 30 public: 31 ParentDef(std::string name, FieldList fields); 32 ParentDef(std::string name, FieldList fields, ParentDef* parent); 33 34 void AddParentConstraint(std::string field_name, std::variant<int64_t, std::string> value); 35 36 // Assign all size fields to their corresponding variable length fields. 37 // Will crash if 38 // - there aren't any fields that don't match up to a field. 39 // - the size field points to a fixed size field. 40 // - if the size field comes after the variable length field. 41 void AssignSizeFields(); 42 43 void SetEndianness(bool is_little_endian); 44 45 // Get the size. You scan specify without_payload to exclude payload and body fields as children override them. 46 Size GetSize(bool without_payload = false) const; 47 48 // Get the offset until the field is reached, if there is no field 49 // returns an empty Size. from_end requests the offset to the field 50 // starting from the end() iterator. If there is a field with an unknown 51 // size along the traversal, then an empty size is returned. 52 Size GetOffsetForField(std::string field_name, bool from_end = false) const; 53 54 FieldList GetParamList() const; 55 56 void GenMembers(std::ostream& s) const; 57 58 void GenSize(std::ostream& s) const; 59 60 void GenSerialize(std::ostream& s) const; 61 62 void GenInstanceOf(std::ostream& s) const; 63 64 FieldList fields_; 65 66 ParentDef* parent_{nullptr}; 67 68 std::map<std::string, std::variant<int64_t, std::string>> parent_constraints_; 69 bool is_little_endian_; 70 }; 71