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 #include "fields/custom_field_fixed_size.h"
18
19 #include "util.h"
20
21 const std::string CustomFieldFixedSize::kFieldType = "CustomField";
22
CustomFieldFixedSize(std::string name,std::string type_name,int size,ParseLocation loc)23 CustomFieldFixedSize::CustomFieldFixedSize(std::string name, std::string type_name, int size,
24 ParseLocation loc)
25 : ScalarField(name, size, loc), type_name_(type_name) {}
26
GetFieldType() const27 const std::string& CustomFieldFixedSize::GetFieldType() const {
28 return CustomFieldFixedSize::kFieldType;
29 }
30
GetDataType() const31 std::string CustomFieldFixedSize::GetDataType() const { return type_name_; }
32
GenBounds(std::ostream & s,Size start_offset,Size end_offset,Size size) const33 int CustomFieldFixedSize::GenBounds(std::ostream& s, Size start_offset, Size end_offset,
34 Size size) const {
35 if (!start_offset.empty()) {
36 // Default to start if available.
37 s << "auto " << GetName() << "_it = to_bound + (" << start_offset << ") / 8;";
38 } else if (!end_offset.empty()) {
39 Size byte_offset = size + end_offset;
40 s << "auto " << GetName() << "_it = to_bound (+ to_bound.NumBytesRemaining() - (" << byte_offset
41 << ") / 8);";
42 } else {
43 ERROR(this) << "Ambiguous offset for field.";
44 }
45 return 0; // num_leading_bits
46 }
47
GenExtractor(std::ostream & s,int,bool) const48 void CustomFieldFixedSize::GenExtractor(std::ostream& s, int, bool) const {
49 s << "*" << GetName() << "_ptr = " << GetName() << "_it.extract<" << GetDataType() << ">();";
50 }
51
HasParameterValidator() const52 bool CustomFieldFixedSize::HasParameterValidator() const { return false; }
53
GenParameterValidator(std::ostream &) const54 void CustomFieldFixedSize::GenParameterValidator(std::ostream&) const {
55 // Do nothing.
56 }
57
GenInserter(std::ostream & s) const58 void CustomFieldFixedSize::GenInserter(std::ostream& s) const {
59 s << "insert(" << GetName() << "_, i);";
60 }
61
GenValidator(std::ostream &) const62 void CustomFieldFixedSize::GenValidator(std::ostream&) const {
63 // Do nothing.
64 }
65
GenStringRepresentation(std::ostream & s,std::string accessor) const66 void CustomFieldFixedSize::GenStringRepresentation(std::ostream& s, std::string accessor) const {
67 // We assume that custom fields will have a ToString() method
68 s << accessor << ".ToString()";
69 }
70