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 
20 #ifndef T_ENUM_H
21 #define T_ENUM_H
22 
23 #include <vector>
24 
25 #include "thrift/parse/t_enum_value.h"
26 #include "thrift/parse/t_type.h"
27 
28 /**
29  * An enumerated type. A list of constant objects with a name for the type.
30  *
31  */
32 class t_enum : public t_type {
33 public:
t_enum(t_program * program)34   t_enum(t_program* program) : t_type(program) {}
35 
set_name(const std::string & name)36   void set_name(const std::string& name) override { name_ = name; }
37 
append(t_enum_value * constant)38   void append(t_enum_value* constant) { constants_.push_back(constant); }
39 
get_constants()40   const std::vector<t_enum_value*>& get_constants() const { return constants_; }
41 
get_constant_by_name(const std::string & name)42   t_enum_value* get_constant_by_name(const std::string& name) const {
43     const std::vector<t_enum_value*>& enum_values = get_constants();
44     std::vector<t_enum_value*>::const_iterator c_iter;
45     for (c_iter = enum_values.begin(); c_iter != enum_values.end(); ++c_iter) {
46       if ((*c_iter)->get_name() == name) {
47         return *c_iter;
48       }
49     }
50     return nullptr;
51   }
52 
get_constant_by_value(int64_t value)53   t_enum_value* get_constant_by_value(int64_t value) const {
54     const std::vector<t_enum_value*>& enum_values = get_constants();
55     std::vector<t_enum_value*>::const_iterator c_iter;
56     for (c_iter = enum_values.begin(); c_iter != enum_values.end(); ++c_iter) {
57       if ((*c_iter)->get_value() == value) {
58         return *c_iter;
59       }
60     }
61     return nullptr;
62   }
63 
get_min_value()64   t_enum_value* get_min_value() const {
65     const std::vector<t_enum_value*>& enum_values = get_constants();
66     std::vector<t_enum_value*>::const_iterator c_iter;
67     t_enum_value* min_value;
68     if (enum_values.size() == 0) {
69       min_value = nullptr;
70     } else {
71       int min_value_value;
72       min_value = enum_values.front();
73       min_value_value = min_value->get_value();
74       for (c_iter = enum_values.begin(); c_iter != enum_values.end(); ++c_iter) {
75         if ((*c_iter)->get_value() < min_value_value) {
76           min_value = (*c_iter);
77           min_value_value = min_value->get_value();
78         }
79       }
80     }
81     return min_value;
82   }
83 
get_max_value()84   t_enum_value* get_max_value() const {
85     const std::vector<t_enum_value*>& enum_values = get_constants();
86     std::vector<t_enum_value*>::const_iterator c_iter;
87     t_enum_value* max_value;
88     if (enum_values.size() == 0) {
89       max_value = nullptr;
90     } else {
91       int max_value_value;
92       max_value = enum_values.back();
93       max_value_value = max_value->get_value();
94       for (c_iter = enum_values.begin(); c_iter != enum_values.end(); ++c_iter) {
95         if ((*c_iter)->get_value() > max_value_value) {
96           max_value = (*c_iter);
97           max_value_value = max_value->get_value();
98         }
99       }
100     }
101     return max_value;
102   }
103 
is_enum()104   bool is_enum() const override { return true; }
105 
106 private:
107   std::vector<t_enum_value*> constants_;
108 };
109 
110 #endif
111