summaryrefslogtreecommitdiff
path: root/src/bin/eolian_mono
diff options
context:
space:
mode:
authorLauro Moura <lauromoura@expertisesolutions.com.br>2018-03-08 15:43:14 -0800
committerCedric BAIL <cedric@osg.samsung.com>2018-03-08 15:46:45 -0800
commit067a8baffa692668acc0f19c91e2198195f480e2 (patch)
treea77e8608e2e3fce0f7ea3c0f745b3739844c3995 /src/bin/eolian_mono
parent34673ab983f2ebab038016b6a0aff4e160c43225 (diff)
csharp: generate helper constructors for structs.
Summary: C# does not have a literal form for structs (like C++'s {} aggregate initialization). Before this commit the user would need to explicitly instantiate a struct and assign the required values to it, like: eina.Size2D size; size.W = width; size.H = height; widget.SetSize(size); As a workaround, this commit generates helper constructor with parameters corresponding to the struct fields in the order they are declared. These parameters have default values if one does not want to explicitly initialize all fields directly. With these constructs, the above code could be translated to: widget.SetSize(new eina.Size2D(width, height)); It should be noted that the constructed struct will live on the managed memory (GC) instead of the stack. Test Plan: run "make check" Reviewers: felipealmeida Subscribers: cedric Differential Revision: https://phab.enlightenment.org/D5838 Signed-off-by: Cedric BAIL <cedric@osg.samsung.com>
Diffstat (limited to 'src/bin/eolian_mono')
-rw-r--r--src/bin/eolian_mono/eolian/mono/helpers.hh6
-rw-r--r--src/bin/eolian_mono/eolian/mono/struct_definition.hh23
-rw-r--r--src/bin/eolian_mono/eolian/mono/struct_fields.hh68
3 files changed, 90 insertions, 7 deletions
diff --git a/src/bin/eolian_mono/eolian/mono/helpers.hh b/src/bin/eolian_mono/eolian/mono/helpers.hh
index 7d013bd6f5..e02fd05b70 100644
--- a/src/bin/eolian_mono/eolian/mono/helpers.hh
+++ b/src/bin/eolian_mono/eolian/mono/helpers.hh
@@ -76,6 +76,12 @@ inline bool need_pointer_conversion(attributes::regular_type_def const* regular)
76 return false; 76 return false;
77} 77}
78 78
79inline std::string to_field_name(std::string const& in)
80{
81 std::string field_name = in;
82 field_name[0] = std::toupper(field_name[0]); // Hack to allow 'static' as a field name
83 return field_name;
84}
79 85
80} 86}
81 87
diff --git a/src/bin/eolian_mono/eolian/mono/struct_definition.hh b/src/bin/eolian_mono/eolian/mono/struct_definition.hh
index 324e787748..a0ff03315e 100644
--- a/src/bin/eolian_mono/eolian/mono/struct_definition.hh
+++ b/src/bin/eolian_mono/eolian/mono/struct_definition.hh
@@ -11,6 +11,7 @@
11#include "keyword.hh" 11#include "keyword.hh"
12#include "using_decl.hh" 12#include "using_decl.hh"
13#include "documentation.hh" 13#include "documentation.hh"
14#include "struct_fields.hh"
14 15
15namespace eolian_mono { 16namespace eolian_mono {
16 17
@@ -24,13 +25,6 @@ inline std::string binding_struct_internal_name(attributes::struct_def const& st
24 return struct_.cxx_name + "_StructInternal"; 25 return struct_.cxx_name + "_StructInternal";
25} 26}
26 27
27inline std::string to_field_name(std::string const& in)
28{
29 std::string field_name = in;
30 field_name[0] = std::toupper(field_name[0]); // Hack to allow 'static' as a field name
31 return field_name;
32}
33
34struct struct_definition_generator 28struct struct_definition_generator
35{ 29{
36 template <typename OutputIterator, typename Context> 30 template <typename OutputIterator, typename Context>
@@ -70,6 +64,21 @@ struct struct_definition_generator
70 if (!as_generator("///<summary>Placeholder field</summary>\npublic IntPtr field;\n").generate(sink, nullptr, context)) 64 if (!as_generator("///<summary>Placeholder field</summary>\npublic IntPtr field;\n").generate(sink, nullptr, context))
71 return false; 65 return false;
72 } 66 }
67 else
68 {
69 // Constructor with default parameters for easy struct initialization
70 auto struct_name = binding_struct_name(struct_);
71 if(!as_generator(
72 scope_tab << "///<summary>Constructor for " << string << ".</summary>\n"
73 << scope_tab << "public " << string << "(\n"
74 << ((scope_tab << scope_tab << field_argument_default) % ",\n")
75 << scope_tab << ")\n"
76 << scope_tab << "{\n"
77 << *(scope_tab << scope_tab << field_argument_assignment << ";\n")
78 << scope_tab << "}\n")
79 .generate(sink, std::make_tuple(struct_name, struct_name, struct_.fields, struct_.fields), context))
80 return false;
81 }
73 82
74 if(!as_generator("}\n").generate(sink, attributes::unused, context)) return false; 83 if(!as_generator("}\n").generate(sink, attributes::unused, context)) return false;
75 84
diff --git a/src/bin/eolian_mono/eolian/mono/struct_fields.hh b/src/bin/eolian_mono/eolian/mono/struct_fields.hh
new file mode 100644
index 0000000000..0cd018b06f
--- /dev/null
+++ b/src/bin/eolian_mono/eolian/mono/struct_fields.hh
@@ -0,0 +1,68 @@
1#ifndef EOLIAN_MONO_STRUCT_FIELDS_HH
2#define EOLIAN_MONO_STRUCT_FIELDS_HH
3
4#include "grammar/generator.hpp"
5#include "grammar/klass_def.hpp"
6#include "grammar/indentation.hpp"
7#include "grammar/list.hpp"
8#include "grammar/alternative.hpp"
9#include "helpers.hh"
10#include "type.hh"
11#include "keyword.hh"
12#include "using_decl.hh"
13#include "documentation.hh"
14
15namespace eolian_mono {
16
17struct field_argument_default_generator
18{
19 template<typename OutputIterator, typename Context>
20 bool generate(OutputIterator sink, attributes::struct_field_def const& field, Context const& context) const
21 {
22 if (!as_generator(type << " " << string << "=default(" << type << ")")
23 .generate(sink, std::make_tuple(field.type, to_field_name(field.name), field.type), context))
24 return false;
25 return true;
26 }
27} const field_argument_default {};
28
29struct field_argument_assignment_generator
30{
31 template<typename OutputIterator, typename Context>
32 bool generate(OutputIterator sink, attributes::struct_field_def const& field, Context const& context) const
33 {
34 if (!as_generator("this." << string << " = " << string)
35 .generate(sink, std::make_tuple(to_field_name(field.name), to_field_name(field.name)), context))
36 return false;
37 return true;
38 }
39} const field_argument_assignment {};
40
41}
42
43namespace efl { namespace eolian { namespace grammar {
44
45template<>
46struct is_eager_generator< ::eolian_mono::field_argument_default_generator> : std::true_type {};
47template<>
48struct is_generator< ::eolian_mono::field_argument_default_generator> : std::true_type {};
49
50template<>
51struct is_eager_generator< ::eolian_mono::field_argument_assignment_generator> : std::true_type {};
52template<>
53struct is_generator< ::eolian_mono::field_argument_assignment_generator> : std::true_type {};
54
55namespace type_traits {
56
57template <>
58struct attributes_needed< ::eolian_mono::field_argument_default_generator> : std::integral_constant<int, 1> {};
59
60template <>
61struct attributes_needed< ::eolian_mono::field_argument_assignment_generator> : std::integral_constant<int, 1> {};
62
63}
64
65} } }
66
67#endif
68