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/enum_field.h"
18 
19 #include "util.h"
20 
21 const std::string EnumField::kFieldType = "EnumField";
22 
EnumField(std::string name,EnumDef enum_def,std::string value,ParseLocation loc)23 EnumField::EnumField(std::string name, EnumDef enum_def, std::string value, ParseLocation loc)
24     : ScalarField(name, enum_def.size_, loc), enum_def_(enum_def), value_(value) {}
25 
GetFieldType() const26 const std::string& EnumField::GetFieldType() const { return EnumField::kFieldType; }
27 
GetEnumDef()28 EnumDef EnumField::GetEnumDef() { return enum_def_; }
29 
GetDataType() const30 std::string EnumField::GetDataType() const { return enum_def_.name_; }
31 
HasParameterValidator() const32 bool EnumField::HasParameterValidator() const { return false; }
33 
GenParameterValidator(std::ostream &) const34 void EnumField::GenParameterValidator(std::ostream&) const {
35   // Validated at compile time.
36 }
37 
GenInserter(std::ostream & s) const38 void EnumField::GenInserter(std::ostream& s) const {
39   s << "insert(static_cast<" << util::GetTypeForSize(GetSize().bits()) << ">(";
40   s << GetName() << "_), i, " << GetSize().bits() << ");";
41 }
42 
GenValidator(std::ostream &) const43 void EnumField::GenValidator(std::ostream&) const {
44   // Do nothing
45 }
46 
GenStringRepresentation(std::ostream & s,std::string accessor) const47 void EnumField::GenStringRepresentation(std::ostream& s, std::string accessor) const {
48   s << GetDataType() << "Text(" << accessor << ")";
49 }
50