r1515@opsdev009 (orig r80457): mcslee | 2008-01-30 15:23:15 -0800
[amiethrift.git] / compiler / cpp / src / generate / t_cpp_generator.h
blobba19c1cf749d445376cba8486e0b5fc6ae996fcc
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_CPP_GENERATOR_H
8 #define T_CPP_GENERATOR_H
10 #include <string>
11 #include <fstream>
12 #include <iostream>
13 #include <vector>
15 #include "t_oop_generator.h"
17 /**
18 * C++ code generator. This is legitimacy incarnate.
20 * @author Mark Slee <mcslee@facebook.com>
22 class t_cpp_generator : public t_oop_generator {
23 public:
24 t_cpp_generator(t_program* program, bool gen_dense) :
25 t_oop_generator(program),
26 gen_dense_(gen_dense),
27 use_include_prefix_(false) {
29 out_dir_base_ = "gen-cpp";
32 /**
33 * Init and close methods
36 void init_generator();
37 void close_generator();
39 void generate_consts(std::vector<t_const*> consts);
41 /**
42 * Program-level generation functions
45 void generate_typedef(t_typedef* ttypedef);
46 void generate_enum(t_enum* tenum);
47 void generate_struct(t_struct* tstruct) {
48 generate_cpp_struct(tstruct, false);
50 void generate_xception(t_struct* txception) {
51 generate_cpp_struct(txception, true);
53 void generate_cpp_struct(t_struct* tstruct, bool is_exception);
55 void generate_service(t_service* tservice);
57 void print_const_value(std::ofstream& out, std::string name, t_type* type, t_const_value* value);
58 std::string render_const_value(std::ofstream& out, std::string name, t_type* type, t_const_value* value);
60 void generate_struct_definition (std::ofstream& out, t_struct* tstruct, bool is_exception=false, bool pointers=false, bool read=true, bool write=true);
61 void generate_struct_fingerprint (std::ofstream& out, t_struct* tstruct, bool is_definition);
62 void generate_struct_reader (std::ofstream& out, t_struct* tstruct, bool pointers=false);
63 void generate_struct_writer (std::ofstream& out, t_struct* tstruct, bool pointers=false);
64 void generate_struct_result_writer (std::ofstream& out, t_struct* tstruct, bool pointers=false);
66 /**
67 * Service-level generation functions
70 void generate_service_interface (t_service* tservice);
71 void generate_service_null (t_service* tservice);
72 void generate_service_multiface (t_service* tservice);
73 void generate_service_helpers (t_service* tservice);
74 void generate_service_client (t_service* tservice);
75 void generate_service_processor (t_service* tservice);
76 void generate_service_skeleton (t_service* tservice);
77 void generate_process_function (t_service* tservice, t_function* tfunction);
78 void generate_function_helpers (t_service* tservice, t_function* tfunction);
80 void generate_service_limited_reflector(t_service* tservice);
81 bool generate_type_limited_reflection(t_type* ttype, std::string target);
82 bool generate_simple_type_limited_reflection(std::ostream& out, t_type* ttype, std::string target);
84 /**
85 * Serialization constructs
88 void generate_deserialize_field (std::ofstream& out,
89 t_field* tfield,
90 std::string prefix="",
91 std::string suffix="");
93 void generate_deserialize_struct (std::ofstream& out,
94 t_struct* tstruct,
95 std::string prefix="");
97 void generate_deserialize_container (std::ofstream& out,
98 t_type* ttype,
99 std::string prefix="");
101 void generate_deserialize_set_element (std::ofstream& out,
102 t_set* tset,
103 std::string prefix="");
105 void generate_deserialize_map_element (std::ofstream& out,
106 t_map* tmap,
107 std::string prefix="");
109 void generate_deserialize_list_element (std::ofstream& out,
110 t_list* tlist,
111 std::string prefix="");
113 void generate_serialize_field (std::ofstream& out,
114 t_field* tfield,
115 std::string prefix="",
116 std::string suffix="");
118 void generate_serialize_struct (std::ofstream& out,
119 t_struct* tstruct,
120 std::string prefix="");
122 void generate_serialize_container (std::ofstream& out,
123 t_type* ttype,
124 std::string prefix="");
126 void generate_serialize_map_element (std::ofstream& out,
127 t_map* tmap,
128 std::string iter);
130 void generate_serialize_set_element (std::ofstream& out,
131 t_set* tmap,
132 std::string iter);
134 void generate_serialize_list_element (std::ofstream& out,
135 t_list* tlist,
136 std::string iter);
139 * Helper rendering functions
142 std::string namespace_prefix(std::string ns);
143 std::string namespace_open(std::string ns);
144 std::string namespace_close(std::string ns);
145 std::string type_name(t_type* ttype, bool in_typedef=false, bool arg=false);
146 std::string base_type_name(t_base_type::t_base tbase);
147 std::string declare_field(t_field* tfield, bool init=false, bool pointer=false, bool constant=false, bool reference=false);
148 std::string function_signature(t_function* tfunction, std::string prefix="");
149 std::string argument_list(t_struct* tstruct);
150 std::string type_to_enum(t_type* ttype);
151 std::string local_reflection_name(const char*, t_type* ttype);
153 // These handles checking gen_dense_ and checking for duplicates.
154 void generate_local_reflection(std::ofstream& out, t_type* ttype, bool is_definition);
155 void generate_local_reflection_pointer(std::ofstream& out, t_type* ttype);
157 bool is_complex_type(t_type* ttype) {
158 ttype = get_true_type(ttype);
160 return
161 ttype->is_container() ||
162 ttype->is_struct() ||
163 ttype->is_xception() ||
164 (ttype->is_base_type() && (((t_base_type*)ttype)->get_base() == t_base_type::TYPE_STRING));
167 void set_use_include_prefix(bool use_include_prefix) {
168 use_include_prefix_ = use_include_prefix;
171 private:
173 * Returns the include prefix to use for a file generated by program, or the
174 * empty string if no include prefix should be used.
176 std::string get_include_prefix(const t_program& program) const;
179 * True iff we should generate local reflection metadata for TDenseProtocol.
181 bool gen_dense_;
184 * True iff we should use a path prefix in our #include statements for other
185 * thrift-generated header files.
187 bool use_include_prefix_;
190 * Strings for namespace, computed once up front then used directly
193 std::string ns_open_;
194 std::string ns_close_;
197 * File streams, stored here to avoid passing them as parameters to every
198 * function.
201 std::ofstream f_types_;
202 std::ofstream f_types_impl_;
203 std::ofstream f_header_;
204 std::ofstream f_service_;
207 * When generating local reflections, make sure we don't generate duplicates.
209 std::set<std::string> reflected_fingerprints_;
212 #endif