summaryrefslogtreecommitdiff
path: root/src/lib/eolian_cxx/grammar/eo_class_constructors_generator.hh
blob: 73847a8c39289112e2a31d1d46ee18dcbb44bf36 (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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603

#ifndef EOLIAN_CXX_STD_EO_CLASS_CONSTRUCTORS_GENERATOR_HH
#define EOLIAN_CXX_STD_EO_CLASS_CONSTRUCTORS_GENERATOR_HH

#include <iosfwd>
#include <string>

#include "eo_types.hh"
#include "tab.hh"
#include "comment.hh"
#include "parameters_generator.hh"
#include "namespace_generator.hh"

namespace efl { namespace eolian { namespace grammar {

struct class_name
{
   std::string _name;
   class_name(std::string name)
     : _name(name)
   {}
};

inline std::ostream&
operator<<(std::ostream& out, class_name const& x)
{
   out << x._name;
   return out;
}

struct class_inheritance
{
   eo_class const& _cls;
   class_inheritance(eo_class const& cls)
     : _cls(cls)
   {}
};

inline std::ostream&
operator<<(std::ostream& out, class_inheritance const& x)
{
   eo_class const& cls = x._cls;

   ancestors_container_type::const_iterator it,
     first = cls.ancestors.cbegin(),
     last = cls.ancestors.cend();
   for (it = first; it != last; ++it)
     {
        out << tab(2) << ", EO_CXX_INHERIT(" << *it << ")" << endl;
     }
   return out;
}

struct constructor_functor_type_name
{
   eo_constructor const& _ctor;
   constructor_functor_type_name(eo_constructor const& ctor)
     : _ctor(ctor)
   {}
};

inline std::ostream&
operator<<(std::ostream& out, constructor_functor_type_name const& x)
{
   out << "_c_" << x._ctor.name;
   return out;
}

struct constructor_functor_type_decl
{
   eo_constructor const& _ctor;
   constructor_functor_type_decl(eo_constructor const& ctor)
     : _ctor(ctor)
   {}
};

inline std::ostream&
operator<<(std::ostream& out, constructor_functor_type_decl const& x)
{
   out << constructor_functor_type_name(x._ctor);

   if (parameters_count_callbacks(x._ctor.params) == 0)
     return out;

   bool comma = false;
   out << "<";
   auto first = x._ctor.params.cbegin(), last = x._ctor.params.cend();
   for(auto it = first; it != last; ++it)
     {
        if (type_is_callback((*it).type) && it+1 != last)
          {
             if (comma)
               out << ", ";
             else
               comma = true;
             out << template_parameter_type((*it).type, (*it).name);
          }
     }
   return out << ">";
}

struct functors_constructor_methods
{
   eo_class const& _cls;
   functors_constructor_methods(eo_class const& cls) : _cls(cls) {}
};

inline std::ostream&
operator<<(std::ostream& out, functors_constructor_methods const& x)
{
   constructors_container_type::const_iterator it,
     first = x._cls.all_constructors.cbegin(),
     last = x._cls.all_constructors.cend();
   for (it = first; it != last; ++it)
     {
        eo_constructor const& c = *it;

        // Hide documentation condition
        out << comment("@cond LOCAL", 1);

        // Struct declaration
        out << template_parameters_declaration(c.params, 1)
            << tab(1) << "struct " << constructor_functor_type_name(c) << endl
            << tab(1) << "{" << endl;

        // Callbacks typedefs
        out << parameters_cxx_generic(c.params,
                 [](param_data d)
                 {
                    if (d.is_cb)
                      d.out << tab(2)
                            << parameter_remove_reference_typedef(d.type, d.name)
                            << endl;
                 }
               )
            << endl;

        // Struct constructor
        out << tab(2) << "explicit " << constructor_functor_type_name(c) << "("
            << parameters_declaration(c.params) << ")"
            << parameters_cxx_generic(c.params,
                  [](param_data d)
                  {
                     if(d.pos == 0u)
                        d.out << endl << tab(3) << ": ";
                     else
                        d.out << ", ";

                     if (d.is_cb)
                       d.out << callback_tmp(d.name) << "(new "
                             << template_parameter_type(d.type, d.name)
                             << "(" << parameter_forward(d.type, d.name) << "))";
                     else
                       d.out << d.name << "(" << parameter_forward(d.type, d.name) << ")";
                  }
               )
            << endl
            << tab(2) << "{}" << endl;

        // Struct operator()
        out << tab(2) << "void operator()()" << endl
            << tab(2) << "{" << endl
            << tab(3) << "::" << c.impl << "(" << parameters_forward_to_c(c.params) << ");" << endl
            << tab(2) << "}" << endl;

        // Register event to free allocated callbacks when the Eo* is deleted
        out << tab(2) << "void register_ev_del_free_callback(Eo* _eoptr)" << endl
            << tab(2) << "{" << endl
            << tab(3) << "(void) _eoptr;" << endl
            << parameters_cxx_generic(c.params,
                 [](param_data d)
                 {
                    if (d.is_cb)
                      d.out << tab(3)
                            << "eo_do(_eoptr," << endl
                            << tab(4) << "eo_event_callback_add(EO_BASE_EVENT_DEL, "
                            << "&::efl::eolian::free_callback_callback<"
                            << parameter_no_ref_type(d.type, d.name)
                            << ">, " << callback_tmp(d.name) << "));" << endl;
                 })
            << tab(2) << "}" << endl;

        // Struct member variables
        out << endl
            << parameters_cxx_generic(c.params,
                  [](param_data d)
                  {
                     d.out << tab(2);
                     if (d.is_cb)
                       d.out << parameter_no_ref_type(d.type, d.name) << "* "
                             << callback_tmp(d.name);
                     else
                       d.out << parameter_type(d.type, d.name) << " " << d.name;
                     d.out << ";" << endl;
                  }
               );

        // Close struct
        out << tab(1) << "};" << endl;

        // End documentation condition
        out << comment("@endcond", 1) << endl;
     }

   return out;
}

struct constructor_method_function_declarations
{
   eo_class const& _cls;
   constructor_method_function_declarations(eo_class const& cls) : _cls(cls) {}
};

inline std::ostream&
operator<<(std::ostream& out, constructor_method_function_declarations const& x)
{
   constructors_container_type::const_iterator it,
     first = x._cls.all_constructors.cbegin(),
     last = x._cls.all_constructors.cend();
   for (it = first; it != last; ++it)
     {
        eo_constructor const& c = *it;

        // "eo_constructor" is already called in the eo_add_ref macro (used in
        // _ctors_call).
        // Creating a function with this name yields an error in the eo_add_ref
        // macro expansion, because "eo_constructor" will refers to the class
        // function instead of the Eo.Base function which is intended.
        if (c.name == "eo_constructor")
          {
             continue;
          }

        out << comment(c.comment, 1)
            << template_parameters_declaration(c.params, 1)
            << tab(1) << "static " << constructor_functor_type_decl(c)
            << " " << c.name << "("
            << parameters_declaration(c.params) << ");" << endl << endl;
     }

   return out;
}

struct constructor_method_function_definitions
{
   eo_class const& _cls;
   constructor_method_function_definitions(eo_class const& cls) : _cls(cls) {}
};

inline std::ostream&
operator<<(std::ostream& out, constructor_method_function_definitions const& x)
{
   constructors_container_type::const_iterator it,
     first = x._cls.all_constructors.cbegin(),
     last = x._cls.all_constructors.cend();
   for (it = first; it != last; ++it)
     {
        eo_constructor const& c = *it;

        // Same explanation as the one in constructor_method_function_declarations
        if (c.name == "eo_constructor")
          {
             continue;
          }

        out << template_parameters_declaration(c.params, 0)
            << "inline " << full_name(x._cls)
            << "::" << constructor_functor_type_decl(c) << " "
            << full_name(x._cls, false) << "::" << c.name << "("
            << parameters_declaration(c.params) << ")" << endl
            << "{" << endl
            << tab(1) << "return " << constructor_functor_type_decl(c) << "("
            << parameters_forward(c.params) << ");" << endl
            << "}" << endl << endl;
     }

   return out;
}

struct comment_constructor_with_constructor_methods
{
   eo_class const& _cls;
   comment_constructor_with_constructor_methods(eo_class const& cls)
     : _cls(cls)
   {}
};

inline std::ostream&
operator<<(std::ostream& out, comment_constructor_with_constructor_methods const& x)
{
   out << tab(1) << "/**" << endl
       << tab(2) << "@brief Constructs a new " << full_name(x._cls, false) << " object." << endl
       << endl
       << tab(2) << "Constructs a new " << full_name(x._cls, false) << " object. If you want this object to be a child" << endl
       << tab(2) << "of another Eo object, use an @ref efl::eo::parent expression, like the example." << endl
       << endl;

   if (x._cls.constructors.size())
     {
        bool singular = (x._cls.constructors.size() == 1);
        out << tab(2) << "Since this class have " << (singular ? "a " : "")
            << "necessary constructor method" << (singular ? "" : "s")
            <<  ", you must call " << (singular ? "it" : "each one of them") << endl
            << tab(2) << "in the right place within this constructor parameters." << endl
            << endl;
     }

   if (!x._cls.optional_constructors.empty())
     {
        out << tab(2) << "Optional constructors may be called in any combination as the" << endl
            << tab(2) << "last parameters." << endl
            << endl;
     }

   out << tab(2) << "Example:" << endl
       << tab(2) << "@code" << endl
       << tab(2) << full_name(x._cls, false) << " my_" << x._cls.name << "(efl::eo::parent = parent_object";

   for (eo_constructor const& c : x._cls.all_constructors)
      out << "," << endl
          << tab(3) << "my_" << x._cls.name << "." << c.name << "(" << parameters_names(c.params) << ")";

   out << ");" << endl
       << tab(2) << "@endcode" << endl
       << endl;

   for (eo_constructor const& c : x._cls.all_constructors)
     out << tab(2) << "@see " << x._cls.name << "::" << c.name << endl;
   out << tab(2) << "@see " << x._cls.name << "(Eo* eo)" << endl;

   return out << tab(1) << "*/" << endl;
}

struct constructor_with_constructor_methods
{
   eo_class const& _cls;
   bool _with_parent;
   constructor_with_constructor_methods(eo_class const& cls, bool with_parent)
     : _cls(cls)
     , _with_parent(with_parent)
   {}
};

inline std::ostream&
operator<<(std::ostream& out, constructor_with_constructor_methods const& x)
{
   unsigned cb_count = 0;

   constructors_container_type::const_iterator it,
     first = x._cls.constructors.cbegin(),
     last = x._cls.constructors.cend();
   for (it = first; it != last; ++it)
     {
        cb_count += parameters_count_callbacks((*it).params);
     }

   if (cb_count != 0 || !x._cls.optional_constructors.empty())
     {
        out << tab(1) << "template <";
        for (unsigned i = 0; i != cb_count; ++i)
          {
             if (i != 0)
               out << ", ";
             out << "typename F" << i;
          }
        if (!x._cls.optional_constructors.empty())
          {
             if (cb_count != 0)
               out << ", ";
             out << "typename... FOpts";
          }
        out << ">" << endl;
     }

   out << tab(1) << "explicit " << x._cls.name << "(";

   if (x._with_parent)
     out << "::efl::eo::parent_type _p";

   {
      unsigned cb_idx = 0;
      for (it = first; it != last; ++it)
        {
           if (x._with_parent || it != first)
             out << ", ";
           out << constructor_functor_type_name(*it);

           if (cb_count != 0 && parameters_count_callbacks((*it).params) != 0)
             {
                out << "<"
                    << parameters_cxx_generic((*it).params,
                         [&cb_idx](param_data d)
                         {
                            if (d.is_cb)
                              d.out << (d.cb_idx ? ", " : "") << "F" << cb_idx++;
                         })
                    << ">";
             }
           out << " _c" << (it-first);
        }
        assert(cb_idx == cb_count);
   }

   if (!x._cls.optional_constructors.empty())
     {
        if (x._with_parent || first != last)
          out << ", ";
        out << "FOpts&&... _opts";
     }

   out << ")" << endl
       << tab(2) << ": " << x._cls.name << "(_ctors_call("
       << (x._with_parent ? "_p" : "::efl::eo::parent = nullptr");
   for (it = first; it != last; ++it)
     {
        out << ", _c" << (it-first);
     }
   if (!x._cls.optional_constructors.empty())
     {
        out << ", std::forward<FOpts>(_opts)...";
     }
   out << "))" << endl
       << tab(1) << "{}" << endl;

   return out;
}

struct constructors_with_constructor_methods
{
   eo_class const& _cls;
   constructors_with_constructor_methods(eo_class const& cls)
     : _cls(cls)
   {}
};

inline std::ostream&
operator<<(std::ostream& out, constructors_with_constructor_methods const& x)
{
   out << tab(1) << "//@{" << endl
       << comment_constructor_with_constructor_methods(x._cls)
       << constructor_with_constructor_methods(x._cls, true) << endl
       << constructor_with_constructor_methods(x._cls, false)
       << tab(1) << "//@}" << endl << endl;
   return out;
}

struct constructor_eo
{
   eo_class const& _cls;
   constructor_eo(eo_class const& cls)
     : _cls(cls)
   {}
};

inline std::ostream&
operator<<(std::ostream& out, constructor_eo const& x)
{
   std::string doc = "@brief Eo Constructor.\n\n"
     "Constructs the object from an Eo* pointer stealing its ownership.\n\n"
     "@param eo The Eo object pointer.\n\n";
   out << comment(doc, 1)
       << tab(1)
       << "explicit " << x._cls.name << "(Eo* eo)" << endl
       << tab(2) << ": ::efl::eo::concrete(eo)" << endl
       << tab(1) << "{}" << endl << endl;

   out << comment(
                  "@brief nullptr_t Constructor.\n\n"
                  "Constructs an empty (null) object.\n\n"
                  , 1
                 )
       << tab(1)
       << "explicit " << x._cls.name << "(std::nullptr_t)" << endl
       << tab(2) << ": ::efl::eo::concrete(nullptr)" << endl
       << tab(1) << "{}" << endl << endl;
   return out;
}

struct copy_constructor
{
   eo_class const& _cls;
   copy_constructor(eo_class const& cls)
     : _cls(cls)
   {}
};

inline std::ostream&
operator<<(std::ostream& out, copy_constructor const& x)
{
   std::string doc = "@brief Copy Constructor.\n\n";
   out << comment(doc, 1)
       << tab(1)
       << x._cls.name << "(" << x._cls.name << " const& other)" << endl
       << tab(2) << ": " << x._cls.name
       << "(eo_ref(other._eo_ptr()))" << endl
       << tab(1) << "{}" << endl << endl;
   return out;
}

struct destructor
{
   eo_class const& _cls;
   destructor(eo_class const& cls)
     : _cls(cls)
   {}
};

inline std::ostream&
operator<<(std::ostream& out, destructor const& x)
{
   out << tab(1)
       << '~' << x._cls.name << "() {}" << endl << endl;
   return out;
}

struct function_call_constructor_methods
{
   eo_class const& _cls;
   function_call_constructor_methods(eo_class const& cls) : _cls(cls) {}
};

inline std::ostream&
operator<<(std::ostream& out, function_call_constructor_methods const& x)
{
   out << comment("@internal", 1);

   unsigned cb_count = 0;

   constructors_container_type::const_iterator it,
     first = x._cls.constructors.cbegin(),
     last = x._cls.constructors.cend();
   for (it = first; it != last; ++it)
     {
        cb_count += parameters_count_callbacks((*it).params);
     }
   if (cb_count != 0 || !x._cls.optional_constructors.empty())
     {
        out << tab(1) << "template <";
        for (unsigned i = 0; i != cb_count; ++i)
          {
             if (i != 0)
               out << ", ";
             out << "typename F" << i;
          }
        if (!x._cls.optional_constructors.empty())
          {
             if (cb_count != 0)
               out << ", ";
             out << "typename... FOpts";
          }
        out << ">" << endl;
     }

   unsigned cb_idx = 0;
   out << tab(1) << "static Eo* _ctors_call(::efl::eo::parent_type _p";
   for (it = first; it != last; ++it)
     {
        out << ", " << constructor_functor_type_name(*it);

        if (cb_count != 0 && parameters_count_callbacks((*it).params) != 0)
          {
             out << "<"
                 << parameters_cxx_generic((*it).params,
                      [&cb_idx](param_data d)
                      {
                         if (d.is_cb)
                           d.out << (d.cb_idx ? ", " : "") << "F" << cb_idx++;
                      })
                 << ">";
          }
        out << " _c" << (it-first);
     }
   assert(cb_idx == cb_count);

   if (!x._cls.optional_constructors.empty())
     out << ", FOpts&&... _opts";

   out << ")" << endl
       << tab(1) << "{" << endl
       << tab(2) << "Eo* _ret_eo = eo_add_ref(" << x._cls.eo_name << ", _p._eo_raw";
   for (it = first; it != last; ++it)
     {
        out << ", _c" << (it-first) << "()";
     }
   if (!x._cls.optional_constructors.empty())
     out << ", ::efl::eolian::call_ctors(_opts...)";
   out << ");" << endl << endl;

   for (it = first; it != last; ++it)
     out << tab(2) << "_c" << (it-first) << ".register_ev_del_free_callback(_ret_eo);" << endl;

   if (!x._cls.optional_constructors.empty())
     out << tab(2) << "::efl::eolian::register_ev_del_free_callback(_ret_eo, _opts...);" << endl;

   out << tab(2) << "return _ret_eo;" << endl
       << tab(1) << "}" << endl << endl;

   return out;
}

} } } // namespace efl { namespace eolian { namespace grammar {

#endif // EOLIAN_CXX_STD_EO_CLASS_CONSTRUCTORS_GENERATOR_HH