summaryrefslogtreecommitdiff
path: root/src/bin/eolian_mono/eolian/mono/struct_definition.hh
diff options
context:
space:
mode:
authorLauro Moura <lauromoura@expertisesolutions.com.br>2019-11-19 01:48:01 -0300
committerLauro Moura <lauromoura@expertisesolutions.com.br>2019-11-19 21:49:35 -0300
commitb1eb794a913e753929635aa80aeab8c2211db462 (patch)
treed36a71f0f1ba8a08de8d1e3078917088e1d07b59 /src/bin/eolian_mono/eolian/mono/struct_definition.hh
parente36b1930bf04694fb67dd7e741dbcb5231bf5e58 (diff)
csharp: Fix CA1815 for generated structs and aliases
Summary: Adds IEquatable<T> and friends. Reviewers: felipealmeida, YOhoho, brunobelo Reviewed By: brunobelo Subscribers: cedric, #reviewers, segfaultxavi, #committers Tags: #efl Maniphest Tasks: T8418 Differential Revision: https://phab.enlightenment.org/D10694
Diffstat (limited to 'src/bin/eolian_mono/eolian/mono/struct_definition.hh')
-rw-r--r--src/bin/eolian_mono/eolian/mono/struct_definition.hh114
1 files changed, 112 insertions, 2 deletions
diff --git a/src/bin/eolian_mono/eolian/mono/struct_definition.hh b/src/bin/eolian_mono/eolian/mono/struct_definition.hh
index b3b8d717f6..7c0bc9e4e1 100644
--- a/src/bin/eolian_mono/eolian/mono/struct_definition.hh
+++ b/src/bin/eolian_mono/eolian/mono/struct_definition.hh
@@ -21,6 +21,7 @@
21#include "grammar/indentation.hpp" 21#include "grammar/indentation.hpp"
22#include "grammar/list.hpp" 22#include "grammar/list.hpp"
23#include "grammar/alternative.hpp" 23#include "grammar/alternative.hpp"
24#include "grammar/attribute_reorder.hpp"
24#include "name_helpers.hh" 25#include "name_helpers.hh"
25#include "helpers.hh" 26#include "helpers.hh"
26#include "type.hh" 27#include "type.hh"
@@ -408,14 +409,15 @@ struct struct_definition_generator
408 auto const& indent = current_indentation(context); 409 auto const& indent = current_indentation(context);
409 if(!as_generator(documentation).generate(sink, struct_, context)) 410 if(!as_generator(documentation).generate(sink, struct_, context))
410 return false; 411 return false;
412 auto struct_managed_name = binding_struct_name(struct_);
411 if(!as_generator 413 if(!as_generator
412 ( 414 (
413 indent << "[StructLayout(LayoutKind.Sequential)]\n" 415 indent << "[StructLayout(LayoutKind.Sequential)]\n"
414 << indent << "[Efl.Eo.BindingEntity]\n" 416 << indent << "[Efl.Eo.BindingEntity]\n"
415 << indent << "public struct " << string << "\n" 417 << indent << "public struct " << struct_managed_name << " : IEquatable<" << struct_managed_name << ">\n"
416 << indent << "{\n" 418 << indent << "{\n"
417 ) 419 )
418 .generate(sink, binding_struct_name(struct_), context)) 420 .generate(sink, attributes::unused, context))
419 return false; 421 return false;
420 422
421 // iterate struct fields 423 // iterate struct fields
@@ -472,6 +474,114 @@ struct struct_definition_generator
472 return false; 474 return false;
473 } 475 }
474 476
477 std::string since_line;
478 if (!struct_.documentation.since.empty())
479 if (!as_generator(indent << scope_tab << "/// <para>Since EFL " + struct_.documentation.since + ".</para>\n"
480 ).generate(std::back_inserter(since_line), attributes::unused, context))
481 return false;
482
483 // GetHashCode (needed by the equality comparisons)
484 if (!as_generator(
485 indent << scope_tab << "/// <summary>Get a hash code for this item.\n"
486 << since_line
487 << indent << scope_tab << "/// </summary>\n"
488 << indent << scope_tab << "public override int GetHashCode()\n"
489 << indent << scope_tab << "{\n"
490 ).generate(sink, attributes::unused, context))
491 return false;
492
493 if (struct_.fields.size() != 0 )
494 {
495 // int hash = 17;
496 // hash = 23 * fieldA.GetHashCode();
497 // hash = 23 * fieldB.GetHashCode();
498 // hash = 23 * fieldC.GetHashCode();
499 // return hash
500 if (!as_generator(
501 indent << scope_tab << scope_tab << "int hash = 17;\n"
502 << *(indent << scope_tab << scope_tab << "hash = hash * 23 + " << name_helpers::struct_field_name << ".GetHashCode();\n")
503 << indent << scope_tab << scope_tab << "return hash;\n"
504 ).generate(sink, struct_.fields, context))
505 return false;
506 }
507 else
508 {
509 // Just compare the place holder pointers
510 if (!as_generator(
511 "return field.GetHashCode();\n"
512 ).generate(sink, attributes::unused, context))
513 return false;
514 }
515
516 if (!as_generator(
517 indent << scope_tab << "}\n"
518 ).generate(sink, attributes::unused, context))
519 return false;
520
521 // IEquatable<T> Equals
522 if (!as_generator(
523 indent << scope_tab << "/// <summary>Equality comparison.\n"
524 << since_line
525 << indent << scope_tab << "/// </summary>\n"
526 << indent << scope_tab << "public bool Equals(" << struct_managed_name << " other)\n"
527 << indent << scope_tab << "{\n"
528 << indent << scope_tab << scope_tab << "return "
529 ).generate(sink, attributes::unused, context))
530 return false;
531
532 if (struct_.fields.size() != 0 )
533 {
534 if (!as_generator(
535 grammar::attribute_reorder<-1, -1>((name_helpers::struct_field_name << " == other." << name_helpers::struct_field_name)) % " && "
536 ).generate(sink, struct_.fields, context))
537 return false;
538 }
539 else
540 {
541 // Just compare the place holder pointers
542 if (!as_generator(
543 "field.Equals(other.field)"
544 ).generate(sink, attributes::unused, context))
545 return false;
546 }
547
548
549 if (!as_generator(
550 indent << scope_tab << scope_tab << ";\n"
551 << indent << scope_tab << "}\n"
552 ).generate(sink, attributes::unused, context))
553 return false;
554
555 // ValueType.Equals
556 if (!as_generator(
557 indent << scope_tab << "/// <summary>Equality comparison.\n"
558 << since_line
559 << indent << scope_tab << "/// </summary>\n"
560 << indent << scope_tab << "public override bool Equals(object other)\n"
561 << indent << scope_tab << scope_tab << "=> ((other is " << struct_managed_name << ") ? Equals((" << struct_managed_name << ")other) : false);\n"
562 ).generate(sink, attributes::unused, context))
563 return false;
564
565 // Equality operators
566 if (!as_generator(
567 indent << scope_tab << "/// <summary>Equality comparison.\n"
568 << since_line
569 << indent << scope_tab << "/// </summary>\n"
570 << indent << scope_tab << "public static bool operator ==(" << struct_managed_name << " lhs, " << struct_managed_name << " rhs)\n"
571 << indent << scope_tab << scope_tab << "=> lhs.Equals(rhs);"
572 ).generate(sink, attributes::unused, context))
573 return false;
574
575 if (!as_generator(
576 indent << scope_tab << "/// <summary>Equality comparison.\n"
577 << since_line
578 << indent << scope_tab << "/// </summary>\n"
579 << indent << scope_tab << "public static bool operator !=(" << struct_managed_name << " lhs, " << struct_managed_name << " rhs)\n"
580 << indent << scope_tab << scope_tab << "=> !lhs.Equals(rhs);"
581 ).generate(sink, attributes::unused, context))
582 return false;
583
584 // Conversions from/to internal struct and IntPtrs
475 if(!as_generator( 585 if(!as_generator(
476 indent << scope_tab << "/// <summary>Implicit conversion to the managed representation from a native pointer.\n" 586 indent << scope_tab << "/// <summary>Implicit conversion to the managed representation from a native pointer.\n"
477 ).generate(sink, attributes::unused, context)) 587 ).generate(sink, attributes::unused, context))