csharp: Removed duplicated call_match implementations

Summary:
Small refactor. Keep it in a single header that other files could use.

Ref T8515

Reviewers: brunobelo, felipealmeida, jptiz

Reviewed By: jptiz

Subscribers: cedric, #reviewers, #committers

Tags: #efl

Maniphest Tasks: T8515

Differential Revision: https://phab.enlightenment.org/D10852
This commit is contained in:
Lauro Moura 2019-12-10 14:01:26 -03:00
parent 4318fe1254
commit 271b56889f
5 changed files with 63 additions and 36 deletions

View File

@ -18,14 +18,18 @@
#include <iterator>
#include <Eina.hh>
#include "grammar/generator.hpp"
#include "grammar/klass_def.hpp"
#include "type_impl.hh" // For call_match
#include "type_match.hh"
#include "name_helpers.hh"
#include "using_decl.hh"
namespace eolian_mono {
namespace eina = efl::eina;
template<typename OutputIterator, typename Context>
struct unpack_event_args_visitor
{
@ -114,7 +118,7 @@ struct unpack_event_args_visitor
return as_generator(conversion).generate(sink, attributes::unused, *context);
};
if (eina::optional<bool> b = call_match(match_table, filter_func, accept_func))
if (eina::optional<bool> b = type_match::get_match(match_table, filter_func, accept_func))
return *b;
else
{
@ -188,7 +192,7 @@ struct pack_event_info_and_call_visitor
).generate(sink, attributes::unused, *context);
};
if (eina::optional<bool> b = call_match(str_table, filter_func, str_accept_func))
if (eina::optional<bool> b = type_match::get_match(str_table, filter_func, str_accept_func))
return *b;
match const value_table [] =
@ -206,7 +210,7 @@ struct pack_event_info_and_call_visitor
).generate(sink, attributes::unused, *context);
};
if (eina::optional<bool> b = call_match(value_table, filter_func, value_accept_func))
if (eina::optional<bool> b = type_match::get_match(value_table, filter_func, value_accept_func))
return *b;
return value_accept_func("e.args");

View File

@ -21,25 +21,13 @@
#include "grammar/case.hpp"
#include "name_helpers.hh"
#include "type_impl.hh"
#include "type_match.hh"
namespace eolian_mono {
namespace eina = efl::eina;
namespace detail {
template <typename Array, typename SelectionPredicate, int N, typename AcceptFunc>
eina::optional<bool> call_annotation_match(Array const (&array)[N], SelectionPredicate predicate, AcceptFunc acceptFunc)
{
typedef Array const* iterator_type;
iterator_type match_iterator = &array[0], match_last = match_iterator + N;
match_iterator = std::find_if(match_iterator, match_last, predicate);
if(match_iterator != match_last)
{
return acceptFunc(match_iterator->function());
}
return {nullptr};
}
template <typename OutputIterator, typename Context>
struct marshall_annotation_visitor_generate
@ -157,7 +145,7 @@ struct marshall_annotation_visitor_generate
const auto& match_table = is_return ? return_match_table : parameter_match_table;
if(eina::optional<bool> b = call_annotation_match(match_table, predicate, acceptCb))
if(eina::optional<bool> b = type_match::get_match(match_table, predicate, acceptCb))
{
return *b;
}

View File

@ -21,6 +21,7 @@
#include "grammar/case.hpp"
#include "helpers.hh"
#include "name_helpers.hh"
#include "type_match.hh"
#include "type_impl.hh"
#include "generation_contexts.hh"
#include "blacklist.hh"
@ -192,7 +193,7 @@ struct marshall_type_visitor_generate
return as_generator(string << ".NativeStruct")
.generate(sink, name_helpers::type_full_managed_name(regular), *context);
}
else if (eina::optional<bool> b = call_match
else if (eina::optional<bool> b = type_match::get_match
(match_table
, [&] (match const& m)
{
@ -296,7 +297,7 @@ struct marshall_type_visitor_generate
&& as_generator("<" << (type(false, false, true) % ", ") << ">").generate(sink, complex.subtypes, *context);
};
if(eina::optional<bool> b = call_match
if(eina::optional<bool> b = type_match::get_match
(matches
, [&] (match const& m)
{

View File

@ -20,6 +20,7 @@
#include "grammar/klass_def.hpp"
#include "grammar/case.hpp"
#include "name_helpers.hh"
#include "type_match.hh"
namespace eolian_mono {
@ -69,19 +70,6 @@ attributes::complex_type_def replace_outer(attributes::complex_type_def v, attri
v.outer = regular;
return v;
}
template <typename Array, typename F, int N, typename A>
eina::optional<bool> call_match(Array const (&array)[N], F f, A a)
{
typedef Array const* iterator_type;
iterator_type match_iterator = &array[0], match_last = match_iterator + N;
match_iterator = std::find_if(match_iterator, match_last, f);
if(match_iterator != match_last)
{
return a(match_iterator->function());
}
return {nullptr};
}
template <typename OutputIterator, typename Context>
struct visitor_regular_type_def_printer
@ -248,7 +236,7 @@ struct visitor_generate
}} // FIXME add proper support for any_value_ref
};
std::string full_type_name = name_helpers::type_full_eolian_name(regular);
if(eina::optional<bool> b = call_match
if(eina::optional<bool> b = type_match::get_match
(optional_match_table
, [&] (match const& m)
{
@ -271,7 +259,7 @@ struct visitor_generate
is_optional = false;
return (*this)(r);
}
else if(eina::optional<bool> b = call_match
else if(eina::optional<bool> b = type_match::get_match
(match_table
, [&] (match const& m)
{
@ -441,7 +429,7 @@ struct visitor_generate
// && detail::generate_pointers(sink, pointers, *context, false);
};
if(eina::optional<bool> b = call_match
if(eina::optional<bool> b = type_match::get_match
(matches
, [&] (match const& m)
{

View File

@ -0,0 +1,46 @@
/*
* 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_MONO_TYPE_MATCH_HH
#define EOLIAN_MONO_TYPE_MATCH_HH
#include <Eina.hh>
namespace eolian_mono {
namespace type_match {
namespace eina = efl::eina;
/**
* Apply the given AcceptFunction to the first element that matches the SelectionPredicate.
*/
template <typename Array, typename SelectionPredicate, int N, typename AcceptFunc>
eina::optional<bool> get_match(Array const (&array)[N], SelectionPredicate predicate, AcceptFunc acceptFunc)
{
typedef Array const* iterator_type;
iterator_type match_iterator = &array[0], match_last = match_iterator + N;
match_iterator = std::find_if(match_iterator, match_last, predicate);
if(match_iterator != match_last)
{
return acceptFunc(match_iterator->function());
}
return {nullptr};
}
} // type_match
} // eolian_mono
#endif