compiler/cpp/src/generate/t_hs_generator.cc
/**
 * Generates code for an enumerated type.
 * the values.
 *
 * @param tenum The enumeration
 */
Generates code for an enumerated type.
the values.

@param tenum The enumeration
Generates code for an enumerated type . the values . @param tenum The enumeration
void t_hs_generator::generate_enum(t_enum* tenum) {
  indent(f_types_) << "data " << capitalize(tenum->get_name()) << " = ";
  indent_up();
  vector<t_enum_value*> constants = tenum->get_constants();
  vector<t_enum_value*>::iterator c_iter;
  bool first = true;
  for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
    string name = capitalize((*c_iter)->get_name());
    f_types_ << (first ? "" : "|");
    f_types_ << name;
    first = false;
  }
  indent(f_types_) << "deriving (Show,Eq, Typeable, Ord)" << endl;
  indent_down();
  string ename = capitalize(tenum->get_name());
  indent(f_types_) << "instance Enum " << ename << " where" << endl;
  indent_up();
  indent(f_types_) << "fromEnum t = case t of" << endl;
  indent_up();
  for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
    int value = (*c_iter)->get_value();
    string name = capitalize((*c_iter)->get_name());
    indent(f_types_) << name << " -> " << value << endl;
  }
  indent_down();
  indent(f_types_) << "toEnum t = case t of" << endl;
  indent_up();
  for(c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
    int value = (*c_iter)->get_value();
    string name = capitalize((*c_iter)->get_name());
    indent(f_types_) << value << " -> " << name << endl;
  }
  indent(f_types_) << "_ -> throw ThriftException" << endl;
  indent_down();
  indent_down();
  indent(f_types_) << "instance Hashable " << ename << " where" << endl;
  indent_up();
  indent(f_types_) << "hashWithSalt salt = hashWithSalt salt . fromEnum" << endl;
  indent_down();
}
void t_hs_generator :: generate_enum ( t_enum * tenum ) { indent ( f_types_ ) << " " << capitalize ( tenum -> get_name ( ) ) << " " ; indent_up ( ) ; vector < t_enum_value * > constants = tenum -> get_constants ( ) ; vector < t_enum_value * > :: iterator c_iter ; bool first = true ; for ( c_iter = constants . begin ( ) ; c_iter != constants . end ( ) ; ++ c_iter ) { string name = capitalize ( ( * c_iter ) -> get_name ( ) ) ; f_types_ << ( first ? " " : " " ) ; f_types_ << name ; first = false ; } indent ( f_types_ ) << " " << endl ; indent_down ( ) ; string ename = capitalize ( tenum -> get_name ( ) ) ; indent ( f_types_ ) << " " << ename << " " << endl ; indent_up ( ) ; indent ( f_types_ ) << " " << endl ; indent_up ( ) ; for ( c_iter = constants . begin ( ) ; c_iter != constants . end ( ) ; ++ c_iter ) { int value = ( * c_iter ) -> get_value ( ) ; string name = capitalize ( ( * c_iter ) -> get_name ( ) ) ; indent ( f_types_ ) << name << " " << value << endl ; } indent_down ( ) ; indent ( f_types_ ) << " " << endl ; indent_up ( ) ; for ( c_iter = constants . begin ( ) ; c_iter != constants . end ( ) ; ++ c_iter ) { int value = ( * c_iter ) -> get_value ( ) ; string name = capitalize ( ( * c_iter ) -> get_name ( ) ) ; indent ( f_types_ ) << value << " " << name << endl ; } indent ( f_types_ ) << " " << endl ; indent_down ( ) ; indent_down ( ) ; indent ( f_types_ ) << " " << ename << " " << endl ; indent_up ( ) ; indent ( f_types_ ) << " " << endl ; indent_down ( ) ; }
Generates code for an enumerated type.
Generates code for an enumerated type .