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 #pragma once
18 
19 #include <deque>
20 #include <map>
21 #include <optional>
22 
23 #include "checksum_def.h"
24 #include "custom_field_def.h"
25 #include "enum_def.h"
26 #include "enum_gen.h"
27 #include "packet_def.h"
28 #include "struct_def.h"
29 
30 class Declarations {
31  public:
AddTypeDef(std::string name,TypeDef * def)32   void AddTypeDef(std::string name, TypeDef* def) {
33     auto it = type_defs_.find(name);
34     if (it != type_defs_.end()) {
35       ERROR() << "Redefinition of Type " << name;
36     }
37     type_defs_.insert(std::pair(name, def));
38     type_defs_queue_.push_back(std::pair(name, def));
39   }
40 
GetTypeDef(const std::string & name)41   TypeDef* GetTypeDef(const std::string& name) {
42     auto it = type_defs_.find(name);
43     if (it == type_defs_.end()) {
44       return nullptr;
45     }
46 
47     return it->second;
48   }
49 
AddPacketDef(std::string name,PacketDef def)50   void AddPacketDef(std::string name, PacketDef def) {
51     auto it = packet_defs_.find(name);
52     if (it != packet_defs_.end()) {
53       ERROR() << "Redefinition of Packet " << name;
54     }
55     packet_defs_.insert(std::pair(name, def));
56     packet_defs_queue_.push_back(std::pair(name, def));
57   }
58 
GetPacketDef(const std::string & name)59   PacketDef* GetPacketDef(const std::string& name) {
60     auto it = packet_defs_.find(name);
61     if (it == packet_defs_.end()) {
62       return nullptr;
63     }
64 
65     return &(it->second);
66   }
67 
AddGroupDef(std::string name,FieldList * group_def)68   void AddGroupDef(std::string name, FieldList* group_def) {
69     auto it = group_defs_.find(name);
70     if (it != group_defs_.end()) {
71       ERROR() << "Redefinition of group " << name;
72     }
73     group_defs_.insert(std::pair(name, group_def));
74   }
75 
GetGroupDef(std::string name)76   FieldList* GetGroupDef(std::string name) {
77     if (group_defs_.find(name) == group_defs_.end()) {
78       return nullptr;
79     }
80 
81     return group_defs_.at(name);
82   }
83 
84   std::map<std::string, FieldList*> group_defs_;
85 
86   std::map<std::string, TypeDef*> type_defs_;
87   std::deque<std::pair<std::string, TypeDef*>> type_defs_queue_;
88   std::map<std::string, PacketDef> packet_defs_;
89   std::deque<std::pair<std::string, PacketDef>> packet_defs_queue_;
90   bool is_little_endian;
91 };
92