summaryrefslogtreecommitdiff
path: root/src/bin/eolian_mono/eolian/mono/struct_definition.hh
diff options
context:
space:
mode:
authorJoão Paulo Taylor Ienczak Zanette <joao.tiz@expertisesolutions.com.br>2019-12-12 13:16:22 -0300
committerLauro Moura <lauromoura@expertisesolutions.com.br>2019-12-12 13:16:22 -0300
commite617ff441aa9cc9286846f3a49eca5ae26e85d0a (patch)
tree3f9aba198f038050d3f59d627441aaa3afa53717 /src/bin/eolian_mono/eolian/mono/struct_definition.hh
parent8a2ccfefe1b27f99e2848a7cf1f25ee7aee74107 (diff)
csharp: Add implicit conversion from tuples to structs.
Summary: Also generates implicit conversion for 1-field structs. Usage examples: ``` // With tuples Point2D p = (1, 2); obj.SetPoint((1, 2)); // With 1-field structs StringWrapper sw = "Must be handled by wrapper."; ``` Due to limitations of MSC compiler, implicit conversions are generated only if struct has 4 fields or less. Ref T8489. Reviewers: brunobelo, cedric, lauromoura, segfaultxavi Reviewed By: lauromoura Subscribers: #reviewers, #committers Tags: #efl Maniphest Tasks: T8489 Differential Revision: https://phab.enlightenment.org/D10801
Diffstat (limited to 'src/bin/eolian_mono/eolian/mono/struct_definition.hh')
-rw-r--r--src/bin/eolian_mono/eolian/mono/struct_definition.hh71
1 files changed, 67 insertions, 4 deletions
diff --git a/src/bin/eolian_mono/eolian/mono/struct_definition.hh b/src/bin/eolian_mono/eolian/mono/struct_definition.hh
index d83e91b591..03085335d1 100644
--- a/src/bin/eolian_mono/eolian/mono/struct_definition.hh
+++ b/src/bin/eolian_mono/eolian/mono/struct_definition.hh
@@ -403,6 +403,66 @@ struct struct_internal_definition_generator
403 403
404struct struct_definition_generator 404struct struct_definition_generator
405{ 405{
406 /**
407 * Generates an implicit operator for packing only if the struct has more
408 * than one attribute. Then operator will receive a tuple with the same of
409 * each attribute's type in the same order they were declared.
410 *
411 * Remarks: due to the MCS compiler's limitations, no operator is generated
412 * for structs with more than 4 fields.
413 */
414 template <typename OutputIterator, typename Context>
415 bool generate_implicit_operator(attributes::struct_def const& struct_
416 , OutputIterator sink
417 , Context const& context) const
418 {
419 if (struct_.fields.size() <= 1 || struct_.fields.size() > 4)
420 return true;
421
422 auto struct_name = binding_struct_name(struct_);
423 auto const& indent = current_indentation(context);
424
425 if (!as_generator(
426 indent << scope_tab << "/// <summary>Packs tuple into " << struct_name << " object.\n"
427 << indent << scope_tab << "///<para>Since EFL 1.24.</para>\n"
428 << indent << scope_tab << "///</summary>\n"
429 ).generate(sink, attributes::unused, context))
430 return false;
431
432 if (!as_generator(
433 indent << scope_tab << "public static implicit operator " << struct_name << "(\n"
434 << indent << scope_tab << scope_tab << "(\n"
435 << ((indent << scope_tab << scope_tab << " " << field_argument_decl) % ",\n") << "\n"
436 << indent << scope_tab << scope_tab << ") tuple)\n"
437 << indent << scope_tab << "{\n"
438 ).generate(sink, struct_.fields, context))
439 return false;
440
441 // object constructor
442 if (!as_generator(
443 indent << scope_tab << scope_tab << "return new " << struct_name << "{\n"
444 ).generate(sink, attributes::unused, context))
445 return false;
446
447 for (const auto& field: struct_.fields)
448 {
449 auto field_name = name_helpers::to_field_name(field.name);
450
451 if (!as_generator(
452 indent << scope_tab << scope_tab << scope_tab << field_name << " = tuple." << field_name << ",\n"
453 ).generate(sink, attributes::unused, context))
454 return false;
455 }
456
457 if (!as_generator(
458 indent << scope_tab << scope_tab << "};\n"
459 << indent << scope_tab << "}\n"
460 ).generate(sink, attributes::unused, context))
461 return false;
462
463 return true;
464 }
465
406 template <typename OutputIterator, typename Context> 466 template <typename OutputIterator, typename Context>
407 bool generate(OutputIterator sink, attributes::struct_def const& struct_, Context const& context) const 467 bool generate(OutputIterator sink, attributes::struct_def const& struct_, Context const& context) const
408 { 468 {
@@ -473,7 +533,10 @@ struct struct_definition_generator
473 << *(indent << scope_tab << scope_tab << field_argument_assignment << ";\n") 533 << *(indent << scope_tab << scope_tab << field_argument_assignment << ";\n")
474 << indent << scope_tab << "}\n\n") 534 << indent << scope_tab << "}\n\n")
475 .generate(sink, std::make_tuple(struct_.fields, struct_name, struct_.fields, struct_.fields), context)) 535 .generate(sink, std::make_tuple(struct_.fields, struct_name, struct_.fields, struct_.fields), context))
476 return false; 536 return false;
537
538 if (!generate_implicit_operator(struct_, sink, context))
539 return false;
477 } 540 }
478 541
479 std::string since_line; 542 std::string since_line;
@@ -546,9 +609,9 @@ struct struct_definition_generator
546 ).generate(sink, attributes::unused, context)) 609 ).generate(sink, attributes::unused, context))
547 return false; 610 return false;
548 } 611 }
549 612
550 613
551 if (!as_generator( 614 if (!as_generator(
552 indent << scope_tab << scope_tab << ";\n" 615 indent << scope_tab << scope_tab << ";\n"
553 << indent << scope_tab << "}\n" 616 << indent << scope_tab << "}\n"
554 ).generate(sink, attributes::unused, context)) 617 ).generate(sink, attributes::unused, context))