THRIFT-632. java: Constants of enum types don't behave well
This patch causes constants of all types to be resolved differently by the compiler, and makes it so that constants of enum types contain a reference to the enum type so that code generators can produce the correct names.
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@892358 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/compiler/cpp/src/parse/t_enum.h b/compiler/cpp/src/parse/t_enum.h
index 740f95c..45d1606 100644
--- a/compiler/cpp/src/parse/t_enum.h
+++ b/compiler/cpp/src/parse/t_enum.h
@@ -44,6 +44,28 @@
return constants_;
}
+ t_enum_value* get_constant_by_name(const std::string name) {
+ const std::vector<t_enum_value*>& enum_values = get_constants();
+ std::vector<t_enum_value*>::const_iterator c_iter;
+ for (c_iter = enum_values.begin(); c_iter != enum_values.end(); ++c_iter) {
+ if ((*c_iter)->get_name() == name) {
+ return *c_iter;
+ }
+ }
+ return NULL;
+ }
+
+ t_enum_value* get_constant_by_value(int64_t value) {
+ const std::vector<t_enum_value*>& enum_values = get_constants();
+ std::vector<t_enum_value*>::const_iterator c_iter;
+ for (c_iter = enum_values.begin(); c_iter != enum_values.end(); ++c_iter) {
+ if ((*c_iter)->get_value() == value) {
+ return *c_iter;
+ }
+ }
+ return NULL;
+ }
+
bool is_enum() const {
return true;
}
@@ -52,6 +74,19 @@
return "enum";
}
+ void resolve_values() {
+ const std::vector<t_enum_value*>& enum_values = get_constants();
+ std::vector<t_enum_value*>::const_iterator c_iter;
+ int lastValue = -1;
+ for (c_iter = enum_values.begin(); c_iter != enum_values.end(); ++c_iter) {
+ if (! (*c_iter)->has_value()) {
+ (*c_iter)->set_value(++lastValue);
+ } else {
+ lastValue = (*c_iter)->get_value();
+ }
+ }
+ }
+
private:
std::vector<t_enum_value*> constants_;
};