summaryrefslogtreecommitdiff
path: root/system/gd/packet/parser/fields/custom_field.cc
blob: 295fbf5ca53ab9bde1ed4ffba9b7b30502feef2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
 * Copyright 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "fields/custom_field.h"
#include "util.h"

const std::string CustomField::kFieldType = "CustomField";

CustomField::CustomField(std::string name, std::string type_name, ParseLocation loc)
    : PacketField(name, loc), type_name_(type_name) {}

const std::string& CustomField::GetFieldType() const {
  return CustomField::kFieldType;
}

Size CustomField::GetSize() const {
  return Size();
}

Size CustomField::GetBuilderSize() const {
  std::string ret = "(" + GetName() + "_.size() * 8) ";
  return ret;
}

std::string CustomField::GetDataType() const {
  return type_name_;
}

void CustomField::GenExtractor(std::ostream& s, int, bool) const {
  s << "auto optional_it = ";
  s << GetDataType() << "::Parse( " << GetName() << "_ptr, " << GetName() << "_it);";
  s << "if (optional_it) {";
  s << GetName() << "_it = *optional_it;";
  s << "} else {";
  s << GetName() << "_it = " << GetName() << "_it + " << GetName() << "_it.NumBytesRemaining();";
  s << GetName() << "_ptr = nullptr;";
  s << "}";
}

std::string CustomField::GetGetterFunctionName() const {
  std::stringstream ss;
  ss << "Get" << util::UnderscoreToCamelCase(GetName());
  return ss.str();
}

void CustomField::GenGetter(std::ostream& s, Size start_offset, Size end_offset) const {
  s << "std::unique_ptr<" << GetDataType() << "> " << GetGetterFunctionName() << "() const {";
  s << "ASSERT(was_validated_);";
  s << "size_t end_index = size();";
  s << "auto to_bound = begin();";

  int num_leading_bits = GenBounds(s, start_offset, end_offset, GetSize());
  s << "std::unique_ptr<" << GetDataType() << "> " << GetName() << "_value";
  s << " = std::make_unique<" << GetDataType() << ">();";
  s << GetDataType() << "* " << GetName() << "_ptr = " << GetName() << "_value.get();";
  GenExtractor(s, num_leading_bits, false);
  s << "if (" << GetName() << "_ptr == nullptr) {" << GetName() << "_value.reset(); }";
  s << "return " << GetName() << "_value;";
  s << "}\n";
}

std::string CustomField::GetBuilderParameterType() const {
  return GetDataType();
}

bool CustomField::HasParameterValidator() const {
  return false;
}

void CustomField::GenParameterValidator(std::ostream&) const {
  // Do nothing.
}

void CustomField::GenInserter(std::ostream& s) const {
  s << GetName() << "_.Serialize(i);";
}

void CustomField::GenValidator(std::ostream&) const {
  // Do nothing.
}

void CustomField::GenStringRepresentation(std::ostream& s, std::string accessor) const {
  s << accessor << "->ToString()";
}

void CustomField::GenBuilderParameterFromView(std::ostream& s) const {
  s << "*view.Get" << util::UnderscoreToCamelCase(GetName()) << "()";
}

std::string CustomField::GetRustDataType() const {
  return type_name_;
}

void CustomField::GenRustGetter(std::ostream&, Size, Size, std::string) const {}

void CustomField::GenRustWriter(std::ostream&, Size, Size) const {}