bmv2
Designing your own switch target with bmv2
enums.h
1 /* Copyright 2013-present Barefoot Networks, Inc.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 /*
17  * Antonin Bas (antonin@barefootnetworks.com)
18  *
19  */
20 
21 #ifndef BM_BM_SIM_ENUMS_H_
22 #define BM_BM_SIM_ENUMS_H_
23 
24 #include <string>
25 #include <unordered_map>
26 
27 namespace bm {
28 
29 // for this class, "name" refers to an enum entry's full name (i.e. enum name +
30 // "." + entry name), "enum_name" refers to an enum's name, and "entry_name"
31 // refers to an entry's name within an enum declaration.
32 class EnumMap {
33  public:
34  using type_t = int;
35 
36  // returns an enum entry value from its full name; will thow a
37  // std::out_of_range exception if does not exist
38  type_t from_name(const std::string &name) const;
39  // returns an enum entry full name from the enum name and the entry value;
40  // will throw a std::out_of_range exception if does not exist
41  const std::string &to_name(const std::string &enum_name, type_t v) const;
42 
43  // returns true iff the enum does not already exist
44  bool add_enum(const std::string &enum_name);
45  // returns true iff the enum exists and the entry name / value have not been
46  // taken yet
47  bool add_entry(const std::string &enum_name, const std::string &entry_name,
48  type_t v);
49 
50  private:
51  using EntryMap = std::unordered_map<type_t, std::string>;
52 
53  std::unordered_map<std::string, type_t> map_name_to_v{};
54  std::unordered_map<std::string, EntryMap> map_enum_name_to_entries{};
55 };
56 
57 } // namespace bm
58 
59 #endif // BM_BM_SIM_ENUMS_H_