Line data Source code
1 : /* 2 : * Licensed to the Apache Software Foundation (ASF) under one 3 : * or more contributor license agreements. See the NOTICE file 4 : * distributed with this work for additional information 5 : * regarding copyright ownership. The ASF licenses this file 6 : * to you under the Apache License, Version 2.0 (the 7 : * "License"); you may not use this file except in compliance 8 : * with the License. You may obtain a copy of the License at 9 : * 10 : * http://www.apache.org/licenses/LICENSE-2.0 11 : * 12 : * Unless required by applicable law or agreed to in writing, 13 : * software distributed under the License is distributed on an 14 : * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 : * KIND, either express or implied. See the License for the 16 : * specific language governing permissions and limitations 17 : * under the License. 18 : * 19 : * Modifications Copyright (c) Juniper Networks, Inc. 20 : * Copyright Contributors to the OpenSDN project 21 : */ 22 : 23 : #ifndef T_ENUM_H 24 : #define T_ENUM_H 25 : 26 : #include "t_enum_value.h" 27 : #include <vector> 28 : 29 : /** 30 : * An enumerated type. A list of constant objects with a name for the type. 31 : * 32 : */ 33 : class t_enum : public t_type { 34 : public: 35 1010 : t_enum(t_program* program) : 36 1010 : t_type(program) {} 37 : 38 1010 : void set_name(const std::string& name) { 39 1010 : name_ = name; 40 1010 : } 41 : 42 8042 : void append(t_enum_value* constant) { 43 8042 : constants_.push_back(constant); 44 8042 : } 45 : 46 9748 : const std::vector<t_enum_value*>& get_constants() { 47 9748 : return constants_; 48 : } 49 : 50 3029 : t_enum_value* get_constant_by_name(const std::string name) { 51 3029 : const std::vector<t_enum_value*>& enum_values = get_constants(); 52 3029 : std::vector<t_enum_value*>::const_iterator c_iter; 53 27812 : for (c_iter = enum_values.begin(); c_iter != enum_values.end(); ++c_iter) { 54 27812 : if ((*c_iter)->get_name() == name) { 55 3029 : return *c_iter; 56 : } 57 : } 58 0 : return NULL; 59 : } 60 : 61 0 : t_enum_value* get_constant_by_value(int64_t value) { 62 0 : const std::vector<t_enum_value*>& enum_values = get_constants(); 63 0 : std::vector<t_enum_value*>::const_iterator c_iter; 64 0 : for (c_iter = enum_values.begin(); c_iter != enum_values.end(); ++c_iter) { 65 0 : if ((*c_iter)->get_value() == value) { 66 0 : return *c_iter; 67 : } 68 : } 69 0 : return NULL; 70 : } 71 : 72 16854 : bool is_enum() const { 73 16854 : return true; 74 : } 75 : 76 752 : virtual std::string get_fingerprint_material() const { 77 752 : return "enum"; 78 : } 79 : 80 1010 : void resolve_values() { 81 1010 : const std::vector<t_enum_value*>& enum_values = get_constants(); 82 1010 : std::vector<t_enum_value*>::const_iterator c_iter; 83 1010 : int lastValue = -1; 84 9052 : for (c_iter = enum_values.begin(); c_iter != enum_values.end(); ++c_iter) { 85 8042 : if (! (*c_iter)->has_value()) { 86 6518 : (*c_iter)->set_value(++lastValue); 87 : } else { 88 1524 : lastValue = (*c_iter)->get_value(); 89 : } 90 : } 91 1010 : } 92 : 93 : private: 94 : std::vector<t_enum_value*> constants_; 95 : }; 96 : 97 : #endif