Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame^] | 1 | #ifndef T_GENERATOR_H |
| 2 | #define T_GENERATOR_H |
| 3 | |
| 4 | #include <string> |
| 5 | #include <iostream> |
| 6 | #include "parse/t_program.h" |
| 7 | |
| 8 | /** |
| 9 | * Base class for a thrift code generator. This class defines the basic |
| 10 | * routines for code generation and contains the top level method that |
| 11 | * dispatches code generation across various components. |
| 12 | * |
| 13 | * @author Mark Slee <mcslee@facebook.com> |
| 14 | */ |
| 15 | class t_generator { |
| 16 | public: |
| 17 | t_generator() {} |
| 18 | virtual ~t_generator() {} |
| 19 | |
| 20 | /** |
| 21 | * Framework generator method that iterates over all the parts of a program |
| 22 | * and performs general actions. This is implemented by the base class and |
| 23 | * should not be overwritten in the subclasses. |
| 24 | */ |
| 25 | void generate_program (t_program* tprogram); |
| 26 | |
| 27 | protected: |
| 28 | /** Optional methods that may be imlemented by subclasses. */ |
| 29 | virtual void init_generator (t_program* tprogram) {} |
| 30 | virtual void close_generator () {} |
| 31 | |
| 32 | /** Pure virtual methods implemented by the generator subclasses. */ |
| 33 | virtual void generate_typedef (t_typedef* ttypedef) = 0; |
| 34 | virtual void generate_enum (t_enum* tenum) = 0; |
| 35 | virtual void generate_struct (t_struct* tstruct) = 0; |
| 36 | virtual void generate_service (t_service* tservice) = 0; |
| 37 | |
| 38 | /** Indentation level modifiers */ |
| 39 | void indent_up() { ++indent_; } |
| 40 | void indent_down() { --indent_; } |
| 41 | |
| 42 | /** Indentation print function */ |
| 43 | std::string indent() { |
| 44 | std::string ind = ""; |
| 45 | int i; |
| 46 | for (i = 0; i < indent_; ++i) { |
| 47 | ind += " "; |
| 48 | } |
| 49 | return ind; |
| 50 | } |
| 51 | |
| 52 | /** Indentation wrapper */ |
| 53 | std::ostream& indent(std::ostream &os) { |
| 54 | return os << indent(); |
| 55 | } |
| 56 | |
| 57 | private: |
| 58 | /** Indentation level */ |
| 59 | int indent_; |
| 60 | |
| 61 | }; |
| 62 | |
| 63 | #endif |