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, ParseLocation loc)
24     : ScalarField(name, size, loc), type_name_(type_name) {}
25 
GetFieldType() const26 const std::string& CustomFieldFixedSize::GetFieldType() const {
27   return CustomFieldFixedSize::kFieldType;
28 }
29 
GetDataType() const30 std::string CustomFieldFixedSize::GetDataType() const {
31   return type_name_;
32 }
33 
GenBounds(std::ostream & s,Size start_offset,Size end_offset,Size size) const34 int CustomFieldFixedSize::GenBounds(std::ostream& s, Size start_offset, Size end_offset, 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 << ") / 8);";
41   } else {
42     ERROR(this) << "Ambiguous offset for field.";
43   }
44   return 0;  // num_leading_bits
45 }
46 
GenExtractor(std::ostream & s,int,bool) const47 void CustomFieldFixedSize::GenExtractor(std::ostream& s, int, bool) const {
48   s << "*" << GetName() << "_ptr = " << GetName() << "_it.extract<" << GetDataType() << ">();";
49 }
50 
HasParameterValidator() const51 bool CustomFieldFixedSize::HasParameterValidator() const {
52   return false;
53 }
54 
GenParameterValidator(std::ostream &) const55 void CustomFieldFixedSize::GenParameterValidator(std::ostream&) const {
56   // Do nothing.
57 }
58 
GenInserter(std::ostream & s) const59 void CustomFieldFixedSize::GenInserter(std::ostream& s) const {
60   s << "insert(" << GetName() << "_, i);";
61 }
62 
GenValidator(std::ostream &) const63 void CustomFieldFixedSize::GenValidator(std::ostream&) const {
64   // Do nothing.
65 }
66 
GenStringRepresentation(std::ostream & s,std::string accessor) const67 void CustomFieldFixedSize::GenStringRepresentation(std::ostream& s, std::string accessor) const {
68   // We assume that custom fields will have a ToString() method
69   s << accessor << ".ToString()";
70 }
71