r1459@opsdev009 (orig r77477): dreiss | 2008-01-11 12:59:03 -0800
[amiethrift.git] / compiler / cpp / src / generate / t_py_generator.h
blob4732561cc370ea4093ce7c2f582dbf3f5606d28f
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_PY_GENERATOR_H
8 #define T_PY_GENERATOR_H
10 #include <string>
11 #include <fstream>
12 #include <iostream>
13 #include <vector>
15 #include "t_generator.h"
17 /**
18 * Python code generator.
20 * @author Mark Slee <mcslee@facebook.com>
22 class t_py_generator : public t_generator {
23 public:
24 t_py_generator(t_program* program, bool gen_newstyle) :
25 t_generator(program),
26 gen_newstyle_(gen_newstyle) {
28 out_dir_base_ = "gen-py";
31 /**
32 * Init and close methods
35 void init_generator();
36 void close_generator();
38 /**
39 * Program-level generation functions
42 void generate_typedef (t_typedef* ttypedef);
43 void generate_enum (t_enum* tenum);
44 void generate_const (t_const* tconst);
45 void generate_struct (t_struct* tstruct);
46 void generate_xception (t_struct* txception);
47 void generate_service (t_service* tservice);
49 std::string render_const_value(t_type* type, t_const_value* value);
51 /**
52 * Struct generation code
55 void generate_py_struct(t_struct* tstruct, bool is_exception);
56 void generate_py_struct_definition(std::ofstream& out, t_struct* tstruct, bool is_xception=false, bool is_result=false);
57 void generate_py_struct_reader(std::ofstream& out, t_struct* tstruct);
58 void generate_py_struct_writer(std::ofstream& out, t_struct* tstruct);
59 void generate_py_function_helpers(t_function* tfunction);
61 /**
62 * Service-level generation functions
65 void generate_service_helpers (t_service* tservice);
66 void generate_service_interface (t_service* tservice);
67 void generate_service_client (t_service* tservice);
68 void generate_service_remote (t_service* tservice);
69 void generate_service_server (t_service* tservice);
70 void generate_process_function (t_service* tservice, t_function* tfunction);
72 /**
73 * Serialization constructs
76 void generate_deserialize_field (std::ofstream &out,
77 t_field* tfield,
78 std::string prefix="",
79 bool inclass=false);
81 void generate_deserialize_struct (std::ofstream &out,
82 t_struct* tstruct,
83 std::string prefix="");
85 void generate_deserialize_container (std::ofstream &out,
86 t_type* ttype,
87 std::string prefix="");
89 void generate_deserialize_set_element (std::ofstream &out,
90 t_set* tset,
91 std::string prefix="");
93 void generate_deserialize_map_element (std::ofstream &out,
94 t_map* tmap,
95 std::string prefix="");
97 void generate_deserialize_list_element (std::ofstream &out,
98 t_list* tlist,
99 std::string prefix="");
101 void generate_serialize_field (std::ofstream &out,
102 t_field* tfield,
103 std::string prefix="");
105 void generate_serialize_struct (std::ofstream &out,
106 t_struct* tstruct,
107 std::string prefix="");
109 void generate_serialize_container (std::ofstream &out,
110 t_type* ttype,
111 std::string prefix="");
113 void generate_serialize_map_element (std::ofstream &out,
114 t_map* tmap,
115 std::string kiter,
116 std::string viter);
118 void generate_serialize_set_element (std::ofstream &out,
119 t_set* tmap,
120 std::string iter);
122 void generate_serialize_list_element (std::ofstream &out,
123 t_list* tlist,
124 std::string iter);
127 * Helper rendering functions
130 std::string py_autogen_comment();
131 std::string py_imports();
132 std::string render_includes();
133 std::string render_fastbinary_includes();
134 std::string declare_field(t_field* tfield);
135 std::string type_name(t_type* ttype);
136 std::string function_signature(t_function* tfunction, std::string prefix="");
137 std::string argument_list(t_struct* tstruct);
138 std::string type_to_enum(t_type* ttype);
139 std::string type_to_spec_args(t_type* ttype);
141 static std::string get_real_py_module(const t_program* program) {
142 std::string real_module = program->get_py_module();
143 if (real_module.empty()) {
144 return program->get_name();
146 return real_module;
149 private:
152 * True iff we should generate new-style classes.
154 bool gen_newstyle_;
157 * File streams
160 std::ofstream f_types_;
161 std::ofstream f_consts_;
162 std::ofstream f_service_;
164 std::string package_dir_;
168 #endif