r1317@opsdev009 (orig r70384): mcslee | 2007-11-16 16:32:36 -0800
[amiethrift.git] / compiler / cpp / src / generate / t_generator.h
blob8452f1f4b2046a13fcf77ecfe9696c0ba24d5bc4
1 // Copyright (c) 2006- Facebook
2 // Distributed under the Thrift Software License
3 //
4 // See accompanying file LICENSE or visit the Thrift site at:
5 // http://developers.facebook.com/thrift/
7 #ifndef T_GENERATOR_H
8 #define T_GENERATOR_H
10 #include <string>
11 #include <iostream>
12 #include <sstream>
13 #include "parse/t_program.h"
14 #include "globals.h"
16 /**
17 * Base class for a thrift code generator. This class defines the basic
18 * routines for code generation and contains the top level method that
19 * dispatches code generation across various components.
21 * @author Mark Slee <mcslee@facebook.com>
23 class t_generator {
24 public:
25 t_generator(t_program* program) {
26 tmp_ = 0;
27 indent_ = 0;
28 program_ = program;
29 program_name_ = get_program_name(program);
32 virtual ~t_generator() {}
34 /**
35 * Framework generator method that iterates over all the parts of a program
36 * and performs general actions. This is implemented by the base class and
37 * should not be overwritten in the subclasses.
39 void generate_program();
41 protected:
43 /**
44 * Optional methods that may be imlemented by subclasses to take necessary
45 * steps at the beginning or end of code generation.
48 virtual void init_generator() {}
49 virtual void close_generator() {}
51 virtual void generate_consts(std::vector<t_const*> consts);
53 /**
54 * Pure virtual methods implemented by the generator subclasses.
57 virtual void generate_typedef (t_typedef* ttypedef) = 0;
58 virtual void generate_enum (t_enum* tenum) = 0;
59 virtual void generate_const (t_const* tconst) {}
60 virtual void generate_struct (t_struct* tstruct) = 0;
61 virtual void generate_service (t_service* tservice) = 0;
62 virtual void generate_xception (t_struct* txception) {
63 // By default exceptions are the same as structs
64 generate_struct(txception);
67 /**
68 * Method to get the program name, may be overridden
70 virtual std::string get_program_name(t_program* tprogram) {
71 return tprogram->get_name();
74 /**
75 * Method to get the service name, may be overridden
77 virtual std::string get_service_name(t_service* tservice) {
78 return tservice->get_name();
81 /**
82 * Get the current output directory
84 virtual std::string get_out_dir() const {
85 return program_->get_out_path() + out_dir_base_ + "/";
88 /**
89 * Creates a unique temporary variable name, which is just "name" with a
90 * number appended to it (i.e. name35)
92 std::string tmp(std::string name) {
93 std::ostringstream out;
94 out << name << tmp_++;
95 return out.str();
98 /**
99 * Indentation level modifiers
102 void indent_up(){
103 ++indent_;
106 void indent_down() {
107 --indent_;
111 * Indentation print function
113 std::string indent() {
114 std::string ind = "";
115 int i;
116 for (i = 0; i < indent_; ++i) {
117 ind += " ";
119 return ind;
123 * Indentation utility wrapper
125 std::ostream& indent(std::ostream &os) {
126 return os << indent();
130 * Capitalization helpers
132 std::string capitalize(std::string in) {
133 in[0] = toupper(in[0]);
134 return in;
136 std::string decapitalize(std::string in) {
137 in[0] = tolower(in[0]);
138 return in;
142 * Get the true type behind a series of typedefs.
144 static t_type* get_true_type(t_type* type) {
145 while (type->is_typedef()) {
146 type = ((t_typedef*)type)->get_type();
148 return type;
151 protected:
153 * The program being generated
155 t_program* program_;
158 * Quick accessor for formatted program name that is currently being
159 * generated.
161 std::string program_name_;
164 * Quick accessor for formatted service name that is currently being
165 * generated.
167 std::string service_name_;
170 * Output type-specifc directory name ("gen-*")
172 std::string out_dir_base_;
174 private:
176 * Current code indentation level
178 int indent_;
181 * Temporary variable counter, for making unique variable names
183 int tmp_;
186 #endif