r1144@dev030 (orig r57821): dreiss | 2007-08-30 18:42:55 -0700
[amiethrift.git] / compiler / cpp / src / generate / t_cpp_generator.h
blob70549904e84dc980e562b1a150e05c54467630c9
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 // TODO(mcslee): Paramaterize the output dir
18 #define T_CPP_DIR "gen-cpp"
20 /**
21 * C++ code generator. This is legitimacy incarnate.
23 * @author Mark Slee <mcslee@facebook.com>
25 class t_cpp_generator : public t_oop_generator {
26 public:
27 t_cpp_generator(t_program* program, bool gen_dense) :
28 t_oop_generator(program),
29 gen_dense_(gen_dense) {}
31 /**
32 * Init and close methods
35 void init_generator();
36 void close_generator();
38 void generate_consts(std::vector<t_const*> consts);
40 /**
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);
65 /**
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);
83 /**
84 * Serialization constructs
87 void generate_deserialize_field (std::ofstream& out,
88 t_field* tfield,
89 std::string prefix="",
90 std::string suffix="");
92 void generate_deserialize_struct (std::ofstream& out,
93 t_struct* tstruct,
94 std::string prefix="");
96 void generate_deserialize_container (std::ofstream& out,
97 t_type* ttype,
98 std::string prefix="");
100 void generate_deserialize_set_element (std::ofstream& out,
101 t_set* tset,
102 std::string prefix="");
104 void generate_deserialize_map_element (std::ofstream& out,
105 t_map* tmap,
106 std::string prefix="");
108 void generate_deserialize_list_element (std::ofstream& out,
109 t_list* tlist,
110 std::string prefix="");
112 void generate_serialize_field (std::ofstream& out,
113 t_field* tfield,
114 std::string prefix="",
115 std::string suffix="");
117 void generate_serialize_struct (std::ofstream& out,
118 t_struct* tstruct,
119 std::string prefix="");
121 void generate_serialize_container (std::ofstream& out,
122 t_type* ttype,
123 std::string prefix="");
125 void generate_serialize_map_element (std::ofstream& out,
126 t_map* tmap,
127 std::string iter);
129 void generate_serialize_set_element (std::ofstream& out,
130 t_set* tmap,
131 std::string iter);
133 void generate_serialize_list_element (std::ofstream& out,
134 t_list* tlist,
135 std::string iter);
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);
158 return
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));
165 private:
168 * True iff we should generate local reflection metadata for TDenseProtocol.
170 bool gen_dense_;
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
181 * function.
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_;
195 #endif