1 /*
2  * Copyright 2019 by its authors. See AUTHORS.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef EOLIAN_MONO_ENUM_DEFINITION_HH
17 #define EOLIAN_MONO_ENUM_DEFINITION_HH
18 
19 #include "grammar/generator.hpp"
20 #include "grammar/klass_def.hpp"
21 #include "grammar/indentation.hpp"
22 #include "grammar/list.hpp"
23 #include "grammar/alternative.hpp"
24 #include "type.hh"
25 #include "name_helpers.hh"
26 #include "using_decl.hh"
27 
28 namespace eolian_mono {
29 
30 struct enum_definition_generator
31 {
32   template <typename OutputIterator, typename Context>
generateeolian_mono::enum_definition_generator33   bool generate(OutputIterator sink, attributes::enum_def const& enum_, Context const& context) const
34   {
35      auto options = efl::eolian::grammar::context_find_tag<options_context>(context);
36 
37      if(!options.want_beta && enum_.is_beta)
38        return true;
39 
40      if(!name_helpers::open_namespaces(sink, enum_.namespaces, context))
41        return false;
42 
43      std::string enum_name = name_helpers::typedecl_managed_name(enum_);
44      std::string flags_attribute;
45 
46      // CA1717
47      if (utils::ends_with(enum_name, "Flags") || utils::ends_with(enum_name, "InputHints")) // Special provision while Text api is revamped
48        flags_attribute = "[Flags]";
49 
50      if(!as_generator
51         (
52          documentation(1)
53          << scope_tab << flags_attribute << "\n"
54          << scope_tab << "[Efl.Eo.BindingEntity]\n"
55          << scope_tab << "public enum " << enum_name << "\n"
56          << scope_tab << "{\n"
57          )
58         .generate(sink, enum_, context))
59        return false;
60 
61      // iterate enum fiels
62      for(auto first = std::begin(enum_.fields)
63              , last = std::end(enum_.fields); first != last; ++first)
64        {
65           auto name = name_helpers::enum_field_managed_name((*first).name);
66           auto literal = (*first).value.literal;
67           if (!as_generator
68               (
69                documentation(2)
70                << scope_tab(2) << string << " = " << string << ",\n"
71               )
72               .generate(sink, std::make_tuple(*first, name, literal), context))
73             return false;
74        }
75 
76      if(!as_generator(scope_tab << "}\n").generate(sink, attributes::unused, context)) return false;
77 
78      if(!name_helpers::close_namespaces(sink, enum_.namespaces, context))
79        return false;
80 
81      return true;
82   }
83 };
84 
85 enum_definition_generator const enum_definition = {};
86 
87 }
88 
89 namespace efl { namespace eolian { namespace grammar {
90 
91 template <>
92 struct is_eager_generator< ::eolian_mono::enum_definition_generator> : std::true_type {};
93 template <>
94 struct is_generator< ::eolian_mono::enum_definition_generator> : std::true_type {};
95 
96 namespace type_traits {
97 template <>
98 struct attributes_needed< ::eolian_mono::enum_definition_generator> : std::integral_constant<int, 1> {};
99 }
100 
101 } } }
102 
103 #endif
104