1 // Copyright (c) 2006- Facebook
2 // Distributed under the Thrift Software License
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
15 #include "t_oop_generator.h"
18 * C++ code generator. This is legitimacy incarnate.
20 * @author Mark Slee <mcslee@facebook.com>
22 class t_cpp_generator
: public t_oop_generator
{
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";
33 * Init and close methods
36 void init_generator();
37 void close_generator();
39 void generate_consts(std::vector
<t_const
*> consts
);
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);
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
);
85 * Serialization constructs
88 void generate_deserialize_field (std::ofstream
& out
,
90 std::string prefix
="",
91 std::string suffix
="");
93 void generate_deserialize_struct (std::ofstream
& out
,
95 std::string prefix
="");
97 void generate_deserialize_container (std::ofstream
& out
,
99 std::string prefix
="");
101 void generate_deserialize_set_element (std::ofstream
& out
,
103 std::string prefix
="");
105 void generate_deserialize_map_element (std::ofstream
& out
,
107 std::string prefix
="");
109 void generate_deserialize_list_element (std::ofstream
& out
,
114 void generate_serialize_field (std::ofstream
& out
,
116 std::string prefix
="",
117 std::string suffix
="");
119 void generate_serialize_struct (std::ofstream
& out
,
121 std::string prefix
="");
123 void generate_serialize_container (std::ofstream
& out
,
125 std::string prefix
="");
127 void generate_serialize_map_element (std::ofstream
& out
,
131 void generate_serialize_set_element (std::ofstream
& out
,
135 void generate_serialize_list_element (std::ofstream
& out
,
140 * Helper rendering functions
143 std::string
namespace_prefix(std::string ns
);
144 std::string
namespace_open(std::string ns
);
145 std::string
namespace_close(std::string ns
);
146 std::string
type_name(t_type
* ttype
, bool in_typedef
=false, bool arg
=false);
147 std::string
base_type_name(t_base_type::t_base tbase
);
148 std::string
declare_field(t_field
* tfield
, bool init
=false, bool pointer
=false, bool constant
=false, bool reference
=false);
149 std::string
function_signature(t_function
* tfunction
, std::string prefix
="");
150 std::string
argument_list(t_struct
* tstruct
);
151 std::string
type_to_enum(t_type
* ttype
);
152 std::string
local_reflection_name(const char*, t_type
* ttype
);
154 // These handles checking gen_dense_ and checking for duplicates.
155 void generate_local_reflection(std::ofstream
& out
, t_type
* ttype
, bool is_definition
);
156 void generate_local_reflection_pointer(std::ofstream
& out
, t_type
* ttype
);
158 bool is_complex_type(t_type
* ttype
) {
159 ttype
= get_true_type(ttype
);
162 ttype
->is_container() ||
163 ttype
->is_struct() ||
164 ttype
->is_xception() ||
165 (ttype
->is_base_type() && (((t_base_type
*)ttype
)->get_base() == t_base_type::TYPE_STRING
));
168 void set_use_include_prefix(bool use_include_prefix
) {
169 use_include_prefix_
= use_include_prefix
;
174 * Returns the include prefix to use for a file generated by program, or the
175 * empty string if no include prefix should be used.
177 std::string
get_include_prefix(const t_program
& program
) const;
180 * True iff we should generate local reflection metadata for TDenseProtocol.
185 * True iff we should use a path prefix in our #include statements for other
186 * thrift-generated header files.
188 bool use_include_prefix_
;
191 * Strings for namespace, computed once up front then used directly
194 std::string ns_open_
;
195 std::string ns_close_
;
198 * File streams, stored here to avoid passing them as parameters to every
202 std::ofstream f_types_
;
203 std::ofstream f_types_impl_
;
204 std::ofstream f_header_
;
205 std::ofstream f_service_
;
208 * When generating local reflections, make sure we don't generate duplicates.
210 std::set
<std::string
> reflected_fingerprints_
;