blob: 59941e37f210665aeb6aed5562fd0f57f9a8cea9 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
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 */
Mark Sleee9ce01c2007-05-16 02:29:53 +000019
Mark Slee31985722006-05-24 21:45:31 +000020#ifndef T_ENUM_H
21#define T_ENUM_H
22
Mark Slee30152872006-11-28 01:24:07 +000023#include "t_enum_value.h"
Mark Slee31985722006-05-24 21:45:31 +000024#include <vector>
25
Mark Sleef5377b32006-10-10 01:42:59 +000026/**
Mark Slee30152872006-11-28 01:24:07 +000027 * An enumerated type. A list of constant objects with a name for the type.
Mark Sleef5377b32006-10-10 01:42:59 +000028 *
Mark Sleef5377b32006-10-10 01:42:59 +000029 */
Mark Slee31985722006-05-24 21:45:31 +000030class t_enum : public t_type {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010031public:
32 t_enum(t_program* program) : t_type(program) {}
Mark Slee31985722006-05-24 21:45:31 +000033
Konrad Grochowski16a23a62014-11-13 15:33:38 +010034 void set_name(const std::string& name) { name_ = name; }
Mark Slee32007a52008-01-18 00:57:59 +000035
Konrad Grochowski16a23a62014-11-13 15:33:38 +010036 void append(t_enum_value* constant) { constants_.push_back(constant); }
Mark Slee31985722006-05-24 21:45:31 +000037
Konrad Grochowski16a23a62014-11-13 15:33:38 +010038 const std::vector<t_enum_value*>& get_constants() { return constants_; }
Mark Sleef5377b32006-10-10 01:42:59 +000039
Bryan Duxbury2d804702009-12-18 19:41:11 +000040 t_enum_value* get_constant_by_name(const std::string name) {
41 const std::vector<t_enum_value*>& enum_values = get_constants();
42 std::vector<t_enum_value*>::const_iterator c_iter;
43 for (c_iter = enum_values.begin(); c_iter != enum_values.end(); ++c_iter) {
44 if ((*c_iter)->get_name() == name) {
45 return *c_iter;
46 }
47 }
48 return NULL;
49 }
50
51 t_enum_value* get_constant_by_value(int64_t value) {
52 const std::vector<t_enum_value*>& enum_values = get_constants();
53 std::vector<t_enum_value*>::const_iterator c_iter;
54 for (c_iter = enum_values.begin(); c_iter != enum_values.end(); ++c_iter) {
55 if ((*c_iter)->get_value() == value) {
56 return *c_iter;
57 }
58 }
59 return NULL;
60 }
61
Roger Meier60b7ad62014-07-29 23:23:36 +020062 t_enum_value* get_min_value() {
63 const std::vector<t_enum_value*>& enum_values = get_constants();
64 std::vector<t_enum_value*>::const_iterator c_iter;
65 t_enum_value* min_value;
66 if (enum_values.size() == 0) {
67 min_value = NULL;
Konrad Grochowski16a23a62014-11-13 15:33:38 +010068 } else {
Roger Meier60b7ad62014-07-29 23:23:36 +020069 int min_value_value;
70 min_value = enum_values.front();
Jens Geyerae0b22c2014-09-04 23:04:21 +020071 min_value_value = min_value->get_value();
Roger Meier60b7ad62014-07-29 23:23:36 +020072 for (c_iter = enum_values.begin(); c_iter != enum_values.end(); ++c_iter) {
Jens Geyerae0b22c2014-09-04 23:04:21 +020073 if ((*c_iter)->get_value() < min_value_value) {
Roger Meier60b7ad62014-07-29 23:23:36 +020074 min_value = (*c_iter);
75 min_value_value = min_value->get_value();
76 }
77 }
78 }
79 return min_value;
80 }
81
82 t_enum_value* get_max_value() {
83 const std::vector<t_enum_value*>& enum_values = get_constants();
84 std::vector<t_enum_value*>::const_iterator c_iter;
85 t_enum_value* max_value;
86 if (enum_values.size() == 0) {
87 max_value = NULL;
Konrad Grochowski16a23a62014-11-13 15:33:38 +010088 } else {
Roger Meier60b7ad62014-07-29 23:23:36 +020089 int max_value_value;
90 max_value = enum_values.back();
Jens Geyerae0b22c2014-09-04 23:04:21 +020091 max_value_value = max_value->get_value();
Roger Meier60b7ad62014-07-29 23:23:36 +020092 for (c_iter = enum_values.begin(); c_iter != enum_values.end(); ++c_iter) {
Jens Geyerae0b22c2014-09-04 23:04:21 +020093 if ((*c_iter)->get_value() > max_value_value) {
Roger Meier60b7ad62014-07-29 23:23:36 +020094 max_value = (*c_iter);
95 max_value_value = max_value->get_value();
96 }
97 }
98 }
99 return max_value;
100 }
101
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100102 bool is_enum() const { return true; }
Mark Slee31985722006-05-24 21:45:31 +0000103
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100104 virtual std::string get_fingerprint_material() const { return "enum"; }
David Reiss18bf22d2007-08-28 20:49:17 +0000105
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100106private:
Mark Slee30152872006-11-28 01:24:07 +0000107 std::vector<t_enum_value*> constants_;
Mark Slee31985722006-05-24 21:45:31 +0000108};
109
110#endif