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"
17 // TODO(mcslee): Paramaterize the output dir
18 #define T_CPP_DIR "gen-cpp"
21 * C++ code generator. This is legitimacy incarnate.
23 * @author Mark Slee <mcslee@facebook.com>
25 class t_cpp_generator
: public t_oop_generator
{
27 t_cpp_generator(t_program
* program
, bool gen_dense
) :
28 t_oop_generator(program
),
29 gen_dense_(gen_dense
) {}
32 * Init and close methods
35 void init_generator();
36 void close_generator();
38 void generate_consts(std::vector
<t_const
*> consts
);
41 * Program-level generation functions
44 void generate_typedef(t_typedef
* ttypedef
);
45 void generate_enum(t_enum
* tenum
);
46 void generate_struct(t_struct
* tstruct
) {
47 generate_cpp_struct(tstruct
, false);
49 void generate_xception(t_struct
* txception
) {
50 generate_cpp_struct(txception
, true);
52 void generate_cpp_struct(t_struct
* tstruct
, bool is_exception
);
54 void generate_service(t_service
* tservice
);
56 void print_const_value(std::ofstream
& out
, std::string name
, t_type
* type
, t_const_value
* value
);
57 std::string
render_const_value(std::ofstream
& out
, std::string name
, t_type
* type
, t_const_value
* value
);
59 void generate_struct_definition (std::ofstream
& out
, t_struct
* tstruct
, bool is_exception
=false, bool pointers
=false, bool read
=true, bool write
=true);
60 void generate_struct_fingerprint (std::ofstream
& out
, t_struct
* tstruct
, bool is_definition
);
61 void generate_struct_reader (std::ofstream
& out
, t_struct
* tstruct
, bool pointers
=false);
62 void generate_struct_writer (std::ofstream
& out
, t_struct
* tstruct
, bool pointers
=false);
63 void generate_struct_result_writer (std::ofstream
& out
, t_struct
* tstruct
, bool pointers
=false);
66 * Service-level generation functions
69 void generate_service_interface (t_service
* tservice
);
70 void generate_service_null (t_service
* tservice
);
71 void generate_service_multiface (t_service
* tservice
);
72 void generate_service_helpers (t_service
* tservice
);
73 void generate_service_client (t_service
* tservice
);
74 void generate_service_processor (t_service
* tservice
);
75 void generate_service_skeleton (t_service
* tservice
);
76 void generate_process_function (t_service
* tservice
, t_function
* tfunction
);
77 void generate_function_helpers (t_service
* tservice
, t_function
* tfunction
);
79 void generate_service_limited_reflector(t_service
* tservice
);
80 bool generate_type_limited_reflection(t_type
* ttype
, std::string target
);
81 bool generate_simple_type_limited_reflection(std::ostream
& out
, t_type
* ttype
, std::string target
);
84 * Serialization constructs
87 void generate_deserialize_field (std::ofstream
& out
,
89 std::string prefix
="",
90 std::string suffix
="");
92 void generate_deserialize_struct (std::ofstream
& out
,
94 std::string prefix
="");
96 void generate_deserialize_container (std::ofstream
& out
,
98 std::string prefix
="");
100 void generate_deserialize_set_element (std::ofstream
& out
,
102 std::string prefix
="");
104 void generate_deserialize_map_element (std::ofstream
& out
,
106 std::string prefix
="");
108 void generate_deserialize_list_element (std::ofstream
& out
,
110 std::string prefix
="");
112 void generate_serialize_field (std::ofstream
& out
,
114 std::string prefix
="",
115 std::string suffix
="");
117 void generate_serialize_struct (std::ofstream
& out
,
119 std::string prefix
="");
121 void generate_serialize_container (std::ofstream
& out
,
123 std::string prefix
="");
125 void generate_serialize_map_element (std::ofstream
& out
,
129 void generate_serialize_set_element (std::ofstream
& out
,
133 void generate_serialize_list_element (std::ofstream
& out
,
138 * Helper rendering functions
141 std::string
namespace_prefix(std::string ns
);
142 std::string
namespace_open(std::string ns
);
143 std::string
namespace_close(std::string ns
);
144 std::string
type_name(t_type
* ttype
, bool in_typedef
=false, bool arg
=false);
145 std::string
base_type_name(t_base_type::t_base tbase
);
146 std::string
declare_field(t_field
* tfield
, bool init
=false, bool pointer
=false, bool constant
=false);
147 std::string
function_signature(t_function
* tfunction
, std::string prefix
="");
148 std::string
argument_list(t_struct
* tstruct
);
149 std::string
type_to_enum(t_type
* ttype
);
150 std::string
local_reflection_name(const char*, t_type
* ttype
);
152 // This handles checking gen_dense_ and checking for duplicates.
153 void generate_local_reflection(std::ofstream
& out
, t_type
* ttype
, bool is_definition
);
155 bool is_complex_type(t_type
* ttype
) {
156 ttype
= get_true_type(ttype
);
159 ttype
->is_container() ||
160 ttype
->is_struct() ||
161 ttype
->is_xception() ||
162 (ttype
->is_base_type() && (((t_base_type
*)ttype
)->get_base() == t_base_type::TYPE_STRING
));
168 * True iff we should generate local reflection metadata for TDenseProtocol.
173 * Strings for namespace, computed once up front then used directly
176 std::string ns_open_
;
177 std::string ns_close_
;
180 * File streams, stored here to avoid passing them as parameters to every
184 std::ofstream f_types_
;
185 std::ofstream f_types_impl_
;
186 std::ofstream f_header_
;
187 std::ofstream f_service_
;
190 * When generating local reflections, make sure we don't generate duplicates.
192 std::set
<std::string
> reflected_fingerprints_
;