19
19
#include < stdexcept>
20
20
21
21
#include < fmt/core.h>
22
+ #include < thrift/compiler/ast/t_function.h>
22
23
#include < thrift/compiler/ast/t_list.h>
23
24
#include < thrift/compiler/ast/t_map.h>
24
25
#include < thrift/compiler/ast/t_node.h>
26
+ #include < thrift/compiler/ast/t_program.h>
25
27
#include < thrift/compiler/ast/t_set.h>
26
28
#include < thrift/compiler/ast/t_struct.h>
27
29
#include < thrift/compiler/ast/t_typedef.h>
@@ -44,6 +46,23 @@ const t_type* find_first_type(const t_type& node) {
44
46
45
47
} // namespace
46
48
49
+ namespace detail {
50
+
51
+ std::string gen_template_type (
52
+ std::string template_name, std::initializer_list<std::string> args) {
53
+ template_name += " <" ;
54
+ auto delim = " " ;
55
+ for (const auto & arg : args) {
56
+ template_name += delim;
57
+ delim = " , " ;
58
+ template_name += arg;
59
+ }
60
+ template_name += " >" ;
61
+ return template_name;
62
+ }
63
+
64
+ } // namespace detail
65
+
47
66
const std::string& cpp_name_resolver::get_native_type (
48
67
const t_field& field, const t_structured& parent) {
49
68
const t_type& type = *field.type ();
@@ -65,7 +84,7 @@ const std::string& cpp_name_resolver::get_native_type(
65
84
// If @cpp.Adapter is used on typedef of the field, use the typedef name.
66
85
if (const auto * typedf = dynamic_cast <const t_typedef*>(&type)) {
67
86
if (find_structured_adapter_annotation (*typedf)) {
68
- return namespaces_. get_namespaced_name (*typedf);
87
+ return get_namespaced_name (*typedf);
69
88
}
70
89
}
71
90
@@ -181,7 +200,7 @@ const std::string& cpp_name_resolver::get_underlying_namespaced_name(
181
200
auto extra = get_extra_namespace (node);
182
201
return fmt::format (
183
202
" {}::{}{}" ,
184
- namespaces_. get_namespace (*program),
203
+ get_namespace (*program),
185
204
extra ? *extra + " ::" : " " ,
186
205
get_underlying_name (node));
187
206
}
@@ -198,7 +217,7 @@ const std::string& cpp_name_resolver::get_underlying_name(const t_type& node) {
198
217
return value->get_string ();
199
218
}
200
219
}
201
- return gen::cpp::namespace_resolver ::get_cpp_name (node);
220
+ return cpp_name_resolver ::get_cpp_name (node);
202
221
}
203
222
204
223
const std::string* cpp_name_resolver::get_extra_namespace (const t_type& node) {
@@ -212,8 +231,8 @@ const std::string* cpp_name_resolver::get_extra_namespace(const t_type& node) {
212
231
const auto * name = annotation->get_value_from_structured_annotation_or_null (
213
232
" underlyingName" );
214
233
if (name == nullptr || name->get_string ().empty ()) {
215
- static const std::string kDefault = " detail" ;
216
- return &kDefault ;
234
+ static const std::string detail = " detail" ;
235
+ return &detail ;
217
236
}
218
237
}
219
238
return nullptr ;
@@ -265,6 +284,28 @@ const std::string* cpp_name_resolver::find_first_adapter(const t_field& field) {
265
284
return nullptr ;
266
285
}
267
286
287
+ std::vector<std::string> cpp_name_resolver::gen_namespace_components (
288
+ const t_program& program) {
289
+ t_program::namespace_config conf;
290
+ conf.no_top_level_domain = true ;
291
+ auto components = program.gen_namespace_or_default (" cpp2" , conf);
292
+ if (components.empty ()) {
293
+ components = program.gen_namespace_or_default (" cpp" , conf);
294
+ components.push_back (" cpp2" );
295
+ }
296
+ return components;
297
+ }
298
+
299
+ std::string cpp_name_resolver::gen_namespace (const t_program& program) {
300
+ return " ::" + gen_unprefixed_namespace (program);
301
+ }
302
+
303
+ std::string cpp_name_resolver::gen_unprefixed_namespace (
304
+ const t_program& program) {
305
+ const auto components = gen_namespace_components (program);
306
+ return fmt::format (" {}" , fmt::join (components, " ::" ));
307
+ }
308
+
268
309
bool cpp_name_resolver::can_resolve_to_scalar (const t_type& node) {
269
310
return node.get_true_type ()->is_scalar () || find_first_adapter (node) ||
270
311
find_first_type (node);
@@ -274,16 +315,16 @@ const std::string& cpp_name_resolver::default_template(
274
315
t_container::type ctype) {
275
316
switch (ctype) {
276
317
case t_container::type::t_list: {
277
- static const auto & kValue = *new std::string (" ::std::vector" );
278
- return kValue ;
318
+ static const auto & value = *new std::string (" ::std::vector" );
319
+ return value ;
279
320
}
280
321
case t_container::type::t_set: {
281
- static const auto & kValue = *new std::string (" ::std::set" );
282
- return kValue ;
322
+ static const auto & value = *new std::string (" ::std::set" );
323
+ return value ;
283
324
}
284
325
case t_container::type::t_map: {
285
- static const auto & kValue = *new std::string (" ::std::map" );
286
- return kValue ;
326
+ static const auto & value = *new std::string (" ::std::map" );
327
+ return value ;
287
328
}
288
329
}
289
330
throw std::runtime_error (
@@ -294,41 +335,41 @@ const std::string& cpp_name_resolver::default_type(
294
335
t_primitive_type::type btype) {
295
336
switch (btype) {
296
337
case t_primitive_type::type::t_void: {
297
- static const auto & kValue = *new std::string (" void" );
298
- return kValue ;
338
+ static const auto & value = *new std::string (" void" );
339
+ return value ;
299
340
}
300
341
case t_primitive_type::type::t_bool: {
301
- static const auto & kValue = *new std::string (" bool" );
302
- return kValue ;
342
+ static const auto & value = *new std::string (" bool" );
343
+ return value ;
303
344
}
304
345
case t_primitive_type::type::t_byte: {
305
- static const auto & kValue = *new std::string (" ::std::int8_t" );
306
- return kValue ;
346
+ static const auto & value = *new std::string (" ::std::int8_t" );
347
+ return value ;
307
348
}
308
349
case t_primitive_type::type::t_i16: {
309
- static const auto & kValue = *new std::string (" ::std::int16_t" );
310
- return kValue ;
350
+ static const auto & value = *new std::string (" ::std::int16_t" );
351
+ return value ;
311
352
}
312
353
case t_primitive_type::type::t_i32: {
313
- static const auto & kValue = *new std::string (" ::std::int32_t" );
314
- return kValue ;
354
+ static const auto & value = *new std::string (" ::std::int32_t" );
355
+ return value ;
315
356
}
316
357
case t_primitive_type::type::t_i64: {
317
- static const auto & kValue = *new std::string (" ::std::int64_t" );
318
- return kValue ;
358
+ static const auto & value = *new std::string (" ::std::int64_t" );
359
+ return value ;
319
360
}
320
361
case t_primitive_type::type::t_float: {
321
- static const auto & kValue = *new std::string (" float" );
322
- return kValue ;
362
+ static const auto & value = *new std::string (" float" );
363
+ return value ;
323
364
}
324
365
case t_primitive_type::type::t_double: {
325
- static const auto & kValue = *new std::string (" double" );
326
- return kValue ;
366
+ static const auto & value = *new std::string (" double" );
367
+ return value ;
327
368
}
328
369
case t_primitive_type::type::t_string:
329
370
case t_primitive_type::type::t_binary: {
330
- static const auto & kValue = *new std::string (" ::std::string" );
331
- return kValue ;
371
+ static const auto & value = *new std::string (" ::std::string" );
372
+ return value ;
332
373
}
333
374
}
334
375
throw std::runtime_error (
@@ -421,7 +462,7 @@ std::string cpp_name_resolver::gen_standard_type(
421
462
}
422
463
423
464
// For everything else, just use namespaced name.
424
- return namespaces_. get_namespaced_name (node);
465
+ return get_namespaced_name (node);
425
466
}
426
467
427
468
std::string cpp_name_resolver::gen_container_type (
0 commit comments