compiler/cpp/src/generate/t_go_generator.cc
/**
 * Generates code for an enumerated type. Done using a class to scope
 * the values.
 *
 * @param tenum The enumeration
 */
Generates code for an enumerated type. Done using a class to scope
the values.

@param tenum The enumeration
Generates code for an enumerated type . Done using a class to scope the values . @param tenum The enumeration
void t_go_generator::generate_enum(t_enum* tenum) {
  std::ostringstream to_string_mapping, from_string_mapping;
  std::string tenum_name(publicize(tenum->get_name()));
  generate_go_docstring(f_types_, tenum);
  f_types_ <<
    "type " << tenum_name << " int" << endl <<
    "const (" << endl;
  to_string_mapping <<
    indent() << "func (p " << tenum_name << ") String() string {" << endl <<
    indent() << "  switch p {" << endl;
  from_string_mapping <<
    indent() << "func From" << tenum_name << "String(s string) " << tenum_name << " {" << endl <<
    indent() << "  switch s {" << endl;
  vector<t_enum_value*> constants = tenum->get_constants();
  vector<t_enum_value*>::iterator c_iter;
  int value = -1;
  for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
    if ((*c_iter)->has_value()) {
      value = (*c_iter)->get_value();
    } else {
      ++value;
    }
    string iter_std_name(escape_string((*c_iter)->get_name()));
    string iter_name((*c_iter)->get_name());
    f_types_ <<
      indent() << "  " << iter_name << ' ' << tenum_name << " = " << value << endl;
    to_string_mapping <<
      indent() << "  case " << iter_name << ": return \"" << iter_std_name << "\"" << endl;
    if(iter_std_name != escape_string(iter_name)) {
      from_string_mapping <<
        indent() << "  case \"" << iter_std_name << "\", \"" << escape_string(iter_name) << "\": return " << iter_name << endl;
    } else {
      from_string_mapping <<
        indent() << "  case \"" << iter_std_name << "\": return " << iter_name << endl;
    }
  }
  to_string_mapping <<
    indent() << "  }" << endl <<
    indent() << "  return \"\"" << endl <<
    indent() << "}" << endl;
  from_string_mapping <<
    indent() << "  }" << endl <<
    indent() << "  return " << tenum_name << "(-10000)" << endl <<
    indent() << "}" << endl;
  f_types_ <<
    indent() << ")" << endl <<
    to_string_mapping.str() << endl << from_string_mapping.str() << endl <<
    indent() << "func (p " << tenum_name << ") Value() int {" << endl <<
    indent() << "  return int(p)" << endl <<
    indent() << "}" << endl << endl <<
    indent() << "func (p " << tenum_name << ") IsEnum() bool {" << endl <<
    indent() << "  return true" << endl <<
    indent() << "}" << endl << endl;
}
void t_go_generator :: generate_enum ( t_enum * tenum ) { std :: ostringstream to_string_mapping , from_string_mapping ; std :: string tenum_name ( publicize ( tenum -> get_name ( ) ) ) ; generate_go_docstring ( f_types_ , tenum ) ; f_types_ << " " << tenum_name << " " << endl << " " << endl ; to_string_mapping << indent ( ) << " " << tenum_name << " " << endl << indent ( ) << " " << endl ; from_string_mapping << indent ( ) << " " << tenum_name << " " << tenum_name << " " << endl << indent ( ) << " " << endl ; vector < t_enum_value * > constants = tenum -> get_constants ( ) ; vector < t_enum_value * > :: iterator c_iter ; int value = -1 ; for ( c_iter = constants . begin ( ) ; c_iter != constants . end ( ) ; ++ c_iter ) { if ( ( * c_iter ) -> has_value ( ) ) { value = ( * c_iter ) -> get_value ( ) ; } else { ++ value ; } string iter_std_name ( escape_string ( ( * c_iter ) -> get_name ( ) ) ) ; string iter_name ( ( * c_iter ) -> get_name ( ) ) ; f_types_ << indent ( ) << " " << iter_name << ' ' << tenum_name << " " << value << endl ; to_string_mapping << indent ( ) << " " << iter_name << " \" " << iter_std_name << " \" " << endl ; if ( iter_std_name != escape_string ( iter_name ) ) { from_string_mapping << indent ( ) << " \" " << iter_std_name << " \" \" " << escape_string ( iter_name ) << " \" " << iter_name << endl ; } else { from_string_mapping << indent ( ) << " \" " << iter_std_name << " \" " << iter_name << endl ; } } to_string_mapping << indent ( ) << " " << endl << indent ( ) << " \" \" " << endl << indent ( ) << " " << endl ; from_string_mapping << indent ( ) << " " << endl << indent ( ) << " " << tenum_name << " " << endl << indent ( ) << " " << endl ; f_types_ << indent ( ) << " " << endl << to_string_mapping . str ( ) << endl << from_string_mapping . str ( ) << endl << indent ( ) << " " << tenum_name << " " << endl << indent ( ) << " " << endl << indent ( ) << " " << endl << endl << indent ( ) << " " << tenum_name << " " << endl << indent ( ) << " " << endl << indent ( ) << " " << endl << endl ; }
Generates code for an enumerated type.
Generates code for an enumerated type .
// Dictionaries to/from string names of enums