summaryrefslogtreecommitdiff
path: root/src/lib/eolian_cxx/grammar/class_definition.hpp
blob: 2cea90872ac2d04785a7feff9cc6a34f3b2662ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
 * Copyright 2019 by its authors. See AUTHORS.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef EOLIAN_CXX_CLASS_DEFINITION_HH
#define EOLIAN_CXX_CLASS_DEFINITION_HH

#include "grammar/generator.hpp"
#include "grammar/klass_def.hpp"

#include "grammar/indentation.hpp"
#include "grammar/list.hpp"
#include "grammar/alternative.hpp"
#include "grammar/type.hpp"
#include "grammar/parameter.hpp"
#include "grammar/function_declaration.hpp"
#include "grammar/case.hpp"
#include "grammar/address_of.hpp"
#include "grammar/attribute_reorder.hpp"
#include "grammar/attribute_conditional.hpp"
#include "grammar/attribute_replace.hpp"
#include "grammar/part_declaration.hpp"

namespace efl { namespace eolian { namespace grammar {

struct class_definition_generator
{
   template <typename OutputIterator, typename Context>
   bool generate(OutputIterator sink, attributes::klass_def const& cls, Context const& context) const
   {
     std::vector<std::string> cpp_namespaces = attributes::cpp_namespaces(cls.namespaces);
     auto open_namespace = *("namespace " << string << " { ") << "\n";
     if(!as_generator(open_namespace).generate(sink, cpp_namespaces, add_lower_case_context(context))) return false;

#ifdef USE_EOCXX_INHERIT_ONLY
     if(!as_generator("struct " << string << " : private ::efl::eo::concrete\n"
                      << scope_tab << ", ::eo_cxx"
                      << *("::" << lower_case[string]) << "::" << string)
           .generate(sink, std::make_tuple(cls.cxx_name, attributes::cpp_namespaces(cls.namespaces), cls.cxx_name), context))
       return false;
#else
     if(!as_generator("struct " << string << " : private ::efl::eo::concrete")
        .generate(sink, cls.cxx_name, context))
       return false;
#endif

     for(auto&& i : cls.inherits)
       {
         if(!as_generator("\n" << scope_tab << ", EO_CXX_INHERIT(" << *("::" << lower_case[string]) << "::" << string << ")")
            .generate(sink, std::make_tuple(attributes::cpp_namespaces(i.namespaces), i.eolian_name), context))
           return false;
       }
     if(!as_generator("\n{\n").generate(sink, attributes::unused, context)) return false;

     // constructors
     if(!as_generator(
            scope_tab << "explicit " << string << "( ::Eo* eo)\n"
         << scope_tab << scope_tab << ": ::efl::eo::concrete(eo) {}\n"
         << scope_tab << string << "(std::nullptr_t)\n"
         << scope_tab << scope_tab << ": ::efl::eo::concrete(nullptr) {}\n"
         << scope_tab << "explicit " << string << "() = default;\n"
         << scope_tab << string << "(" << string << " const&) = default;\n"
         << scope_tab << string << "(" << string << "&&) = default;\n"
         << scope_tab << string << "& operator=(" << string << " const&) = default;\n"
         << scope_tab << string << "& operator=(" << string << "&&) = default;\n"
         << scope_tab << "template <typename Derived>\n"
         << scope_tab << string << "(Derived&& derived\n"
         << scope_tab << scope_tab << ", typename std::enable_if<\n"
         << scope_tab << scope_tab << scope_tab << "::efl::eo::is_eolian_object<Derived>::value\n"
         << scope_tab << scope_tab << scope_tab << " && std::is_base_of< " << string << ", Derived>::value>::type* = 0)\n"
         << scope_tab << scope_tab << scope_tab << ": ::efl::eo::concrete(derived._eo_ptr()) {}\n\n"
              ).generate(sink, attributes::make_infinite_tuple(cls.cxx_name), context))
       return false;

     if((cls.type == attributes::class_type::regular) && !as_generator(
            scope_tab << string << "( ::efl::eo::instantiate_t)\n"
         << scope_tab << "{\n"
         << scope_tab << scope_tab << "::efl::eolian::do_eo_add( ::efl::eo::concrete::_eo_raw, ::efl::eo::concrete{nullptr}, _eo_class());\n"
         << scope_tab << "}\n"
         << scope_tab << "template <typename T>\n"
         << scope_tab << "explicit " << string << "( ::efl::eo::instantiate_t, T&& parent, typename std::enable_if< ::efl::eo::is_eolian_object<T>::value>::type* = 0)\n"
         << scope_tab << "{\n"
         << scope_tab << scope_tab << "::efl::eolian::do_eo_add( ::efl::eo::concrete::_eo_raw, parent, _eo_class());\n"
         << scope_tab << "}\n"
         << scope_tab << "template <typename F> " << string << "( ::efl::eo::instantiate_t, F&& f, typename ::std::enable_if< ::efl::eolian::is_constructor_lambda<F, " << string << " >::value>::type* = 0)\n"
         << scope_tab << "{\n"
         << scope_tab << scope_tab << "::efl::eolian::do_eo_add( ::efl::eo::concrete::_eo_raw, ::efl::eo::concrete{nullptr}, _eo_class(), *this, std::forward<F>(f));\n"
         << scope_tab << "}\n"
         << scope_tab << "template <typename T, typename F> " << string << "(  ::efl::eo::instantiate_t, T&& parent, F&& f, typename ::std::enable_if< ::efl::eolian::is_constructor_lambda<F, " << string << " >::value && ::efl::eo::is_eolian_object<T>::value>::type* = 0)\n"
         << scope_tab << "{\n"
         << scope_tab << scope_tab << "::efl::eolian::do_eo_add( ::efl::eo::concrete::_eo_raw, parent, _eo_class(), *this, std::forward<F>(f));\n"
         << scope_tab << "}\n\n"
         // << scope_tab << "explicit " << string << "( ::efl::eo::concrete const& parent)\n"
         // << scope_tab << scope_tab << ": ::efl::eo::concrete( ::efl::eo::do_eo_add(parent)) {}\n"
         // << scope_tab << "template <typename F>\n"
         // << scope_tab << "explicit " << string << "( ::efl::eo::concrete const& parent, F f)\n"
         // << scope_tab << scope_tab << ": ::efl::eo::concrete( ::efl::eo::do_eo_add(parent, f)) {}\n"
         // << scope_tab << "template <typename F>\n"
         // << scope_tab << "explicit " << string << "(F f)\n"
         // << scope_tab << scope_tab << ": ::efl::eo::concrete( ::efl::eo::do_eo_add( ::efl::eo::concrete{nullptr}, f)) {}\n"
        ).generate(sink, attributes::make_infinite_tuple(cls.cxx_name), context)) return false;
     
#ifndef USE_EOCXX_INHERIT_ONLY
     if(!as_generator(*(function_declaration(get_klass_name(cls))))
        .generate(sink, cls.functions, context)) return false;
#endif
                                             
     // static Efl_Class const* _eo_class();
     std::string suffix;
     switch(cls.type)
       {
       case attributes::class_type::regular:
       case attributes::class_type::abstract_:
         suffix = "CLASS";
         break;
       case attributes::class_type::mixin:
         suffix = "MIXIN";
         break;
       case attributes::class_type::interface_:
         suffix = "INTERFACE";
         break;
       }

     if(!as_generator
        (
            scope_tab << "static Efl_Class const* _eo_class()\n"
            << scope_tab << "{\n"
            << scope_tab << scope_tab << "return "
        ).generate(sink,  attributes::unused, context)) return false;
     if(!as_generator
        (*(string << "_") << string << "_" << string)
        .generate(sink, std::make_tuple(cls.namespaces, cls.eolian_name, suffix), add_upper_case_context(context)))
       return false;
     if(!as_generator(";\n" << scope_tab << "}\n").generate(sink, attributes::unused, context)) return false;

     if(!as_generator(
         scope_tab << "Eo* _eo_ptr() const { return *(reinterpret_cast<Eo **>"
              << "(const_cast<" << string << " *>(this))); }\n"
        ).generate(sink, cls.cxx_name, context))
       return false;

     for (auto&& e : cls.events)
       {
          if (e.is_beta)
            {
               suffix = "BETA";
               if(!as_generator
                     ("#ifdef " << *(string << "_") << string << "_" << string << "\n")
                     .generate(sink, std::make_tuple(cls.namespaces, cls.eolian_name, suffix), add_upper_case_context(context)))
                 return false;
            }
          if (e.is_protected)
            {
               suffix = "PROTECTED";
               if(!as_generator
                     ("#ifdef " << *(string << "_") << string << "_" << string << "\n")
                     .generate(sink, std::make_tuple(cls.namespaces, cls.eolian_name, suffix), add_upper_case_context(context)))
                 return false;
            }
          if(!as_generator
             (
              *attribute_reorder<1, 2, 0, 1>
              ((scope_tab << "static struct " << string_replace(',', '_') << "_event\n"
                << scope_tab << "{\n"
                << scope_tab << scope_tab << "static Efl_Event_Description const* description()\n"
                << scope_tab << scope_tab << "{ return " << string << "; }\n"
                << scope_tab << scope_tab << "typedef "
                << (attribute_conditional([] (eina::optional<attributes::type_def> const& t) { return !!t; })
                    [attribute_replace([] (eina::optional<attributes::type_def> const& t) { return *t; }) [type]]
                    | "void")
                << " parameter_type;\n"
                << scope_tab << "} const " << string_replace(',', '_') << "_event;\n"
             ))).generate(sink, std::vector<attributes::event_def>{e}, context))
            return false;

          /* EXPERIMENTAL: event_cb_add */
          if (!as_generator("#ifdef EFL_CXXPERIMENTAL\n").generate(sink, attributes::unused, context))
            return false;
          if (!as_generator(
                 scope_tab << "template <typename F>\n"
                 << scope_tab << "typename std::enable_if<std::is_bind_expression<F>::value, ::efl::eolian::signal_connection>::type\n"
                 << scope_tab << string_replace(',', '_') << "_event_cb_add(F function)\n"
                 << scope_tab << "{\n"
                 << scope_tab << scope_tab << "return ::efl::eolian::event_add(" << string_replace(',', '_') << "_event, *this, function);\n"
                 << scope_tab << "}\n")
              .generate(sink, std::make_tuple(e.name, e.name), context))
            return false;
          if (!as_generator(
                 scope_tab << "template <typename F>\n"
                 << scope_tab << "typename std::enable_if<!std::is_bind_expression<F>::value, ::efl::eolian::signal_connection>::type\n"
                 << scope_tab << string_replace(',', '_') << "_event_cb_add(F function)\n"
                 << scope_tab << "{\n"
                 << scope_tab << scope_tab << "return ::efl::eolian::event_add(" << string_replace(',', '_') << "_event, *this, std::bind(function));\n"
                 << scope_tab << "}\n")
              .generate(sink, std::make_tuple(e.name, e.name), context))
            return false;
          if (!as_generator("#endif\n").generate(sink, attributes::unused, context))
            return false;

          if (e.is_beta && !as_generator("#endif\n").generate(sink, attributes::unused, context))
            return false;
          if (e.is_protected && !as_generator("#endif\n").generate(sink, attributes::unused, context))
            return false;
       }
     
     // /// @cond LOCAL
     if(!as_generator(scope_tab << "/// @cond LOCAL\n").generate(sink, attributes::unused, context)) return false;
     
     if(!as_generator(address_of).generate(sink, cls, context)) return false;

     // /// @endcond
     if(!as_generator(scope_tab << "/// @endcond\n").generate(sink, attributes::unused, context)) return false;

     // EXPERIMENTAL: Parts
     if (!cls.parts.empty())
       {
          if(!as_generator("#ifdef EFL_CXXPERIMENTAL\n").generate(sink, attributes::unused, context)) return false;
          if(!as_generator(*(scope_tab << part_declaration << ";\n"))
                .generate(sink, cls.parts, context)) return false;
          if(!as_generator("#endif \n").generate(sink, attributes::unused, context)) return false;
       }

     if(!as_generator(   scope_tab << "::efl::eo::wref<" << string << "> _get_wref() const { "
                         "return ::efl::eo::wref<" << string << ">(*this); }\n"
                         ).generate(sink, std::make_tuple(cls.cxx_name, cls.cxx_name), context)) return false;

     // eo_concrete
     if(!as_generator(   scope_tab << "::efl::eo::concrete const& _get_concrete() const { return *this; }\n"
                      << scope_tab << "::efl::eo::concrete& _get_concrete() { return *this; }\n"
                     ).generate(sink, attributes::unused, context)) return false;
     
     if(!as_generator(   scope_tab << "using ::efl::eo::concrete::_eo_ptr;\n"
                      << scope_tab << "using ::efl::eo::concrete::_release;\n"
                      << scope_tab << "using ::efl::eo::concrete::_reset;\n"
                      << scope_tab << "using ::efl::eo::concrete::_delete;\n"
                      << scope_tab << "using ::efl::eo::concrete::operator bool;\n"
                      ).generate(sink, attributes::unused, context)) return false;

     // EXPERIMENTAL: wref and implicit conversion to Eo*
     if(!as_generator("#ifdef EFL_CXXPERIMENTAL\n").generate(sink, attributes::unused, context)) return false;
     // For easy wref, operator-> in wref needs to also return a pointer type
     if(!as_generator(   scope_tab << "const " << string << "* operator->() const { return this; }\n"
                     ).generate(sink, std::make_tuple(cls.cxx_name, cls.cxx_name), context)) return false;
     if(!as_generator(   scope_tab << string << "* operator->() { return this; }\n"
                     ).generate(sink, std::make_tuple(cls.cxx_name, cls.cxx_name), context)) return false;
     // For easy interfacing with C: no need to use _eo_ptr()
     if(!as_generator(   scope_tab << "operator Eo*() const { return _eo_ptr(); }\n"
                     ).generate(sink, attributes::unused, context)) return false;
     if(!as_generator("#endif \n").generate(sink, attributes::unused, context)) return false;

     if(!as_generator(   scope_tab << "friend bool operator==(" << string << " const& lhs, " << string << " const& rhs)\n"
                      << scope_tab << "{ return lhs._get_concrete() == rhs._get_concrete(); }\n"
                      << scope_tab << "friend bool operator!=(" << string << " const& lhs, " << string << " const& rhs)\n"
                      << scope_tab << "{ return !(lhs == rhs); }\n"
                     << "};\n").generate(sink, attributes::make_infinite_tuple(cls.cxx_name), context)) return false;
     
     // static asserts
     if(!as_generator("static_assert(sizeof(" << string << ") == sizeof(Eo*), \"\");\n")
        .generate(sink, cls.cxx_name, context)) return false;
     if(!as_generator("static_assert(std::is_standard_layout<" << string << ">::value, \"\");\n")
        .generate(sink, cls.cxx_name, context)) return false;

     auto close_namespace = *(lit("} ")) << "\n";
     if(!as_generator(close_namespace).generate(sink, cpp_namespaces, context)) return false;
     
     return true;
   }
};

template <>
struct is_eager_generator<class_definition_generator> : std::true_type {};
template <>
struct is_generator<class_definition_generator> : std::true_type {};

namespace type_traits {
template <>
struct attributes_needed<class_definition_generator> : std::integral_constant<int, 1> {};
}
      
class_definition_generator const class_definition = {};
      
} } }

#endif