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.h"
18 
19 #include "util.h"
20 
21 const std::string CustomField::kFieldType = "CustomField";
22 
CustomField(std::string name,std::string type_name,ParseLocation loc)23 CustomField::CustomField(std::string name, std::string type_name, ParseLocation loc)
24     : PacketField(name, loc), type_name_(type_name) {}
25 
GetFieldType() const26 const std::string& CustomField::GetFieldType() const { return CustomField::kFieldType; }
27 
GetSize() const28 Size CustomField::GetSize() const { return Size(); }
29 
GetBuilderSize() const30 Size CustomField::GetBuilderSize() const {
31   std::string ret = "(" + GetName() + "_.size() * 8) ";
32   return ret;
33 }
34 
GetDataType() const35 std::string CustomField::GetDataType() const { return type_name_; }
36 
GenExtractor(std::ostream & s,int,bool) const37 void CustomField::GenExtractor(std::ostream& s, int, bool) const {
38   s << "auto optional_it = ";
39   s << GetDataType() << "::Parse( " << GetName() << "_ptr, " << GetName() << "_it);";
40   s << "if (optional_it) {";
41   s << GetName() << "_it = *optional_it;";
42   s << "} else {";
43   s << GetName() << "_it = " << GetName() << "_it + " << GetName() << "_it.NumBytesRemaining();";
44   s << GetName() << "_ptr = nullptr;";
45   s << "}";
46 }
47 
GetGetterFunctionName() const48 std::string CustomField::GetGetterFunctionName() const {
49   std::stringstream ss;
50   ss << "Get" << util::UnderscoreToCamelCase(GetName());
51   return ss.str();
52 }
53 
GenGetter(std::ostream & s,Size start_offset,Size end_offset) const54 void CustomField::GenGetter(std::ostream& s, Size start_offset, Size end_offset) const {
55   s << "std::unique_ptr<" << GetDataType() << "> " << GetGetterFunctionName() << "() const {";
56   s << "ASSERT(was_validated_);";
57   s << "size_t end_index = size();";
58   s << "auto to_bound = begin();";
59 
60   int num_leading_bits = GenBounds(s, start_offset, end_offset, GetSize());
61   s << "std::unique_ptr<" << GetDataType() << "> " << GetName() << "_value";
62   s << " = std::make_unique<" << GetDataType() << ">();";
63   s << GetDataType() << "* " << GetName() << "_ptr = " << GetName() << "_value.get();";
64   GenExtractor(s, num_leading_bits, false);
65   s << "if (" << GetName() << "_ptr == nullptr) {" << GetName() << "_value.reset(); }";
66   s << "return " << GetName() << "_value;";
67   s << "}\n";
68 }
69 
GetBuilderParameterType() const70 std::string CustomField::GetBuilderParameterType() const { return GetDataType(); }
71 
HasParameterValidator() const72 bool CustomField::HasParameterValidator() const { return false; }
73 
GenParameterValidator(std::ostream &) const74 void CustomField::GenParameterValidator(std::ostream&) const {
75   // Do nothing.
76 }
77 
GenInserter(std::ostream & s) const78 void CustomField::GenInserter(std::ostream& s) const { s << GetName() << "_.Serialize(i);"; }
79 
GenValidator(std::ostream &) const80 void CustomField::GenValidator(std::ostream&) const {
81   // Do nothing.
82 }
83 
GenStringRepresentation(std::ostream & s,std::string accessor) const84 void CustomField::GenStringRepresentation(std::ostream& s, std::string accessor) const {
85   s << accessor << "->ToString()";
86 }
87 
GenBuilderParameterFromView(std::ostream & s) const88 void CustomField::GenBuilderParameterFromView(std::ostream& s) const {
89   s << "*view.Get" << util::UnderscoreToCamelCase(GetName()) << "()";
90 }
91