summaryrefslogtreecommitdiff
path: root/src/lib/eolian_cxx/grammar/type_function_declaration.hpp
blob: 71ca630f94802603996c7ab75bc63fd7d02012dc (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
#ifndef EOLIAN_CXX_TYPE_FUNCTION_DECLARATION_HH
#define EOLIAN_CXX_TYPE_FUNCTION_DECLARATION_HH

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

#include "grammar/string.hpp"
#include "grammar/indentation.hpp"
#include "grammar/list.hpp"
#include "grammar/alternative.hpp"
#include "grammar/type.hpp"
#include "grammar/parameter.hpp"
#include "grammar/keyword.hpp"
#include "grammar/converting_argument.hpp"
#include "grammar/eps.hpp"

namespace efl { namespace eolian { namespace grammar {

/** This generates the caller struct for function pointers. */
struct type_function_declaration_generator {
   type_function_declaration_generator() {}

   template <typename OutputIterator, typename Context>
   bool generate(OutputIterator sink, attributes::function_def const& f, Context const& ctx) const
   {
      std::string guard = f.c_name + "_defined";

      if (!as_generator("#ifndef " << string << "\n" <<
                        "#define " << string << "\n")
          .generate(sink, std::make_tuple(guard, guard), add_upper_case_context(ctx)))
        return false;

      // efl::eolian::function_wrapper<T, F>
      if (!as_generator("namespace efl { namespace eolian {\n")
          .generate(sink, attributes::unused, add_lower_case_context(ctx)))
      return false;

      if (!as_generator(
             "template <typename F>\n"
             "struct function_wrapper<" << string << ", F, struct " << string << "__function_tag> {\n"
             << scope_tab << "function_wrapper(F cxx_func) : _cxx_func(cxx_func) {}\n"
             ).generate(sink, std::make_tuple(f.c_name, f.c_name), ctx))
        return false;

      if (!as_generator(
             scope_tab << "void *data_to_c() { return static_cast<void *>(this); }\n"
             << scope_tab << string << " func_to_c() const { return &caller; }\n"
             << scope_tab << "Eina_Free_Cb free_to_c() const { return &deleter; }\n"
             << "private:\n"
             << scope_tab << "F _cxx_func;\n"
             << scope_tab << "static void deleter(void *data) {\n"
             << scope_tab << scope_tab << "delete static_cast<function_wrapper<" << string << ", F, ::efl::eolian::" << string << "__function_tag>*>(data);\n"
             << scope_tab << "}\n"
             ).generate(sink, std::make_tuple(f.c_name, f.c_name, f.c_name), ctx))
        return false;

      std::vector<std::string> c_args;
      for (auto itr : f.parameters)
        c_args.push_back(", " + itr.type.c_type + " " + itr.param_name);
      if (!as_generator(
             scope_tab << "static " << string << " caller(void *cxx_call_data"
             << *(string) << ") {\n"
             << scope_tab << scope_tab << "auto fw = static_cast<function_wrapper<"
             << string << ", F, ::efl::eolian::" << string << "__function_tag>*>(cxx_call_data);\n"
             ).generate(sink, std::make_tuple(f.return_type.c_type, c_args, f.c_name, f.c_name), ctx))
        return false;

      if (f.return_type != attributes::void_
          && !as_generator(scope_tab << scope_tab << "auto __return_value =\n")
          .generate(sink, attributes::unused, ctx))
        return false;

      if (!f.parameters.empty())
        {
           std::vector<attributes::parameter_def> params;
           for (auto itr = f.parameters.begin() + 1; itr != f.parameters.end(); itr++)
             params.push_back(*itr);
           if (!as_generator(
                  scope_tab << scope_tab << "fw->_cxx_func(" << parameter_as_argument << *(", " << parameter_as_argument) << ");\n"
                  ).generate(sink, std::make_tuple(*f.parameters.begin(), params), ctx))
               return false;
        }

      if (f.return_type != attributes::void_
          && !as_generator(scope_tab << scope_tab << "return ::efl::eolian::convert_to_c<"
                           << type << ">(__return_value);\n")
          .generate(sink, f.return_type, ctx))
        return false;

      if (!as_generator(scope_tab << "}\n").generate(sink, attributes::unused, ctx))
        return false;

      if (!as_generator("};\n"
                        "} }\n"
                        "#endif\n\n")
          .generate(sink, attributes::unused, ctx))
        return false;

      return true;
   }
};

template <>
struct is_eager_generator<type_function_declaration_generator> : std::true_type {};
template <>
struct is_generator<type_function_declaration_generator> : std::true_type {};
namespace type_traits {
template <>
struct attributes_needed<type_function_declaration_generator> : std::integral_constant<int, 1> {};
}

struct type_function_declaration_terminal
{
  type_function_declaration_generator operator()() const
  {
    return type_function_declaration_generator{};
  }
} const type_function_declaration = {};

} } }

#endif