Commit 013df02d authored by Dominik Charousset's avatar Dominik Charousset

got rid of class `pattern`

the class `pattern` is no longer used by libcppa, since we use
match_expr and guards instead
parent 394c4f74
...@@ -129,7 +129,6 @@ set(LIBCPPA_SRC ...@@ -129,7 +129,6 @@ set(LIBCPPA_SRC
src/object_array.cpp src/object_array.cpp
src/opt.cpp src/opt.cpp
src/partial_function.cpp src/partial_function.cpp
src/pattern.cpp
src/primitive_variant.cpp src/primitive_variant.cpp
src/process_information.cpp src/process_information.cpp
src/protocol.cpp src/protocol.cpp
......
...@@ -100,7 +100,6 @@ cppa/object.hpp ...@@ -100,7 +100,6 @@ cppa/object.hpp
cppa/on.hpp cppa/on.hpp
cppa/option.hpp cppa/option.hpp
cppa/partial_function.hpp cppa/partial_function.hpp
cppa/pattern.hpp
cppa/primitive_type.hpp cppa/primitive_type.hpp
cppa/primitive_variant.hpp cppa/primitive_variant.hpp
cppa/process_information.hpp cppa/process_information.hpp
...@@ -195,7 +194,6 @@ src/local_actor.cpp ...@@ -195,7 +194,6 @@ src/local_actor.cpp
src/object.cpp src/object.cpp
src/object_array.cpp src/object_array.cpp
src/partial_function.cpp src/partial_function.cpp
src/pattern.cpp
src/primitive_variant.cpp src/primitive_variant.cpp
src/process_information.cpp src/process_information.cpp
src/receive.cpp src/receive.cpp
...@@ -297,3 +295,4 @@ cppa/actor_companion_mixin.hpp ...@@ -297,3 +295,4 @@ cppa/actor_companion_mixin.hpp
cppa/detail/singleton_mixin.hpp cppa/detail/singleton_mixin.hpp
cppa/response_handle.hpp cppa/response_handle.hpp
src/response_handle.cpp src/response_handle.cpp
cppa/wildcard_position.hpp
...@@ -35,7 +35,6 @@ ...@@ -35,7 +35,6 @@
#include "cppa/config.hpp" #include "cppa/config.hpp"
#include "cppa/either.hpp" #include "cppa/either.hpp"
#include "cppa/pattern.hpp"
#include "cppa/detail/receive_policy.hpp" #include "cppa/detail/receive_policy.hpp"
#include "cppa/detail/behavior_stack.hpp" #include "cppa/detail/behavior_stack.hpp"
......
...@@ -32,8 +32,9 @@ ...@@ -32,8 +32,9 @@
#define CPPA_MATCHES_HPP #define CPPA_MATCHES_HPP
#include <numeric> #include <numeric>
#include "cppa/pattern.hpp"
#include "cppa/any_tuple.hpp" #include "cppa/any_tuple.hpp"
#include "cppa/wildcard_position.hpp"
#include "cppa/util/type_list.hpp" #include "cppa/util/type_list.hpp"
...@@ -68,11 +69,6 @@ struct matcher<wildcard_position::nil, Tuple, T...> { ...@@ -68,11 +69,6 @@ struct matcher<wildcard_position::nil, Tuple, T...> {
} }
return false; return false;
} }
static inline bool vmatch(const Tuple& tup, const pattern<T...>& ptrn) {
CPPA_REQUIRE(tup.size() == sizeof...(T));
return ptrn._matches_values(tup);
}
}; };
template<class Tuple, typename... T> template<class Tuple, typename... T>
...@@ -98,10 +94,6 @@ struct matcher<wildcard_position::trailing, Tuple, T...> { ...@@ -98,10 +94,6 @@ struct matcher<wildcard_position::trailing, Tuple, T...> {
} }
return false; return false;
} }
static inline bool vmatch(const Tuple& tup, const pattern<T...>& ptrn) {
return ptrn._matches_values(tup);
}
}; };
template<class Tuple> template<class Tuple>
...@@ -112,9 +104,6 @@ struct matcher<wildcard_position::leading, Tuple, anything> { ...@@ -112,9 +104,6 @@ struct matcher<wildcard_position::leading, Tuple, anything> {
static inline bool tmatch(const Tuple&, util::fixed_vector<size_t, 0>&) { static inline bool tmatch(const Tuple&, util::fixed_vector<size_t, 0>&) {
return true; return true;
} }
static inline bool vmatch(const Tuple&, const pattern<anything>&) {
return true;
}
}; };
template<class Tuple, typename... T> template<class Tuple, typename... T>
...@@ -142,10 +131,6 @@ struct matcher<wildcard_position::leading, Tuple, T...> { ...@@ -142,10 +131,6 @@ struct matcher<wildcard_position::leading, Tuple, T...> {
} }
return false; return false;
} }
static inline bool vmatch(const Tuple& tup, const pattern<T...>& ptrn) {
return ptrn._matches_values(tup);
}
}; };
template<class Tuple, typename... T> template<class Tuple, typename... T>
...@@ -192,10 +177,6 @@ struct matcher<wildcard_position::in_between, Tuple, T...> { ...@@ -192,10 +177,6 @@ struct matcher<wildcard_position::in_between, Tuple, T...> {
} }
return false; return false;
} }
static inline bool vmatch(const Tuple& tup, const pattern<T...>& ptrn) {
return ptrn._matches_values(tup);
}
}; };
template<class Tuple, typename... T> template<class Tuple, typename... T>
...@@ -271,12 +252,6 @@ struct matcher<wildcard_position::multiple, Tuple, T...> { ...@@ -271,12 +252,6 @@ struct matcher<wildcard_position::multiple, Tuple, T...> {
} }
return false; return false;
} }
static inline bool vmatch(const Tuple& tup,
const pattern<T...>& ptrn,
const typename pattern<T...>::mapping_vector& mv) {
return ptrn._matches_values(tup, &mv);
}
}; };
// implementation for zero or one wildcards // implementation for zero or one wildcards
...@@ -291,21 +266,6 @@ struct match_impl { ...@@ -291,21 +266,6 @@ struct match_impl {
util::fixed_vector<size_t, Size>& mv) { util::fixed_vector<size_t, Size>& mv) {
return matcher<PC, Tuple, Ts...>::tmatch(tup, mv); return matcher<PC, Tuple, Ts...>::tmatch(tup, mv);
} }
static inline bool _(const Tuple& tup,
const pattern<Ts...>& p) {
return matcher<PC, Tuple, Ts...>::tmatch(tup)
&& ( p.has_values() == false
|| matcher<PC, Tuple, Ts...>::vmatch(tup, p));
}
static inline bool _(const Tuple& tup,
const pattern<Ts...>& p,
typename pattern<Ts...>::mapping_vector& mv) {
return matcher<PC, Tuple, Ts...>::tmatch(tup, mv)
&& ( p.has_values() == false
|| matcher<PC, Tuple, Ts...>::vmatch(tup, p));
}
}; };
// implementation for multiple wildcards // implementation for multiple wildcards
...@@ -323,24 +283,6 @@ struct match_impl<wildcard_position::multiple, Tuple, Ts...> { ...@@ -323,24 +283,6 @@ struct match_impl<wildcard_position::multiple, Tuple, Ts...> {
return matcher<PC, Tuple, Ts...>::tmatch(tup, mv); return matcher<PC, Tuple, Ts...>::tmatch(tup, mv);
} }
static inline bool _(const Tuple& tup, const pattern<Ts...>& p) {
if (p.has_values()) {
typename pattern<Ts...>::mapping_vector mv;
return matcher<PC, Tuple, Ts...>::tmatch(tup, mv)
&& matcher<PC, Tuple, Ts...>::vmatch(tup, p, mv);
}
return matcher<PC, Tuple, Ts...>::tmatch(tup);
}
static inline bool _(const Tuple& tup, const pattern<Ts...>& p,
typename pattern<Ts...>::mapping_vector& mv) {
if (p.has_values()) {
typename pattern<Ts...>::mapping_vector mv;
return matcher<PC, Tuple, Ts...>::tmatch(tup, mv)
&& matcher<PC, Tuple, Ts...>::vmatch(tup, p, mv);
}
return matcher<PC, Tuple, Ts...>::tmatch(tup, mv);
}
}; };
template<class Tuple, class List> template<class Tuple, class List>
...@@ -379,27 +321,6 @@ bool matches(const any_tuple& tup, ...@@ -379,27 +321,6 @@ bool matches(const any_tuple& tup,
::_(tup, mv); ::_(tup, mv);
} }
/*
* @brief Returns true if this tuple matches @p pn.
*/
template<typename... Ts>
bool matches(const any_tuple& tup, const pattern<Ts...>& pn) {
typedef util::type_list<Ts...> tl;
return match_impl<get_wildcard_position<tl>(), any_tuple, Ts...>
::_(tup, pn);
}
/*
* @brief Returns true if this tuple matches @p pn.
*/
template<typename... Ts>
bool matches(const any_tuple& tup, const pattern<Ts...>& pn,
typename pattern<Ts...>::mapping_vector& mv) {
typedef util::type_list<Ts...> tl;
return match_impl<get_wildcard_position<tl>(), any_tuple, Ts...>
::_(tup, pn, mv);
}
// support for type_list based matching // support for type_list based matching
template<typename... Ts> template<typename... Ts>
inline bool matches(const any_tuple& tup, const util::type_list<Ts...>&) { inline bool matches(const any_tuple& tup, const util::type_list<Ts...>&) {
...@@ -416,15 +337,6 @@ inline bool matches(const any_tuple& tup, const util::type_list<Ts...>&, ...@@ -416,15 +337,6 @@ inline bool matches(const any_tuple& tup, const util::type_list<Ts...>&,
return matches<Ts...>(tup, mv); return matches<Ts...>(tup, mv);
} }
/*
* @brief Returns true if this tuple matches the pattern <tt>{Ts...}</tt>
* (does not match for values).
*/
template<typename... Ts>
inline bool matches_types(const any_tuple& tup, const pattern<Ts...>&) {
return matches<Ts...>(tup);
}
template<typename... Ts> template<typename... Ts>
inline bool matches_types(const any_tuple& tup, const util::type_list<Ts...>&) { inline bool matches_types(const any_tuple& tup, const util::type_list<Ts...>&) {
return matches<Ts...>(tup); return matches<Ts...>(tup);
......
...@@ -31,7 +31,6 @@ ...@@ -31,7 +31,6 @@
#ifndef CPPA_TUPLE_CAST_IMPL_HPP #ifndef CPPA_TUPLE_CAST_IMPL_HPP
#define CPPA_TUPLE_CAST_IMPL_HPP #define CPPA_TUPLE_CAST_IMPL_HPP
#include "cppa/pattern.hpp"
#include "cppa/any_tuple.hpp" #include "cppa/any_tuple.hpp"
#include "cppa/detail/matches.hpp" #include "cppa/detail/matches.hpp"
...@@ -65,49 +64,6 @@ struct tuple_cast_impl { ...@@ -65,49 +64,6 @@ struct tuple_cast_impl {
mv)}; mv)};
return {}; return {};
} }
static inline option<Result> safe(any_tuple& tup, const pattern<T...>& p) {
mapping_vector mv;
if (matches(tup, p, mv)) return {Result::from(std::move(tup.vals()),
mv)};
return {};
}
static inline option<Result> unsafe(any_tuple& tup, const pattern<T...>& p) {
mapping_vector mv;
if (WP == wildcard_position::in_between) {
if (!p.has_values() || matcher<WP, any_tuple, T...>::vmatch(tup, p)) {
// first range
mv.resize(size);
auto begin = mv.begin();
std::iota(begin, begin + first_wc, 0);
// second range
begin = mv.begin() + first_wc;
std::iota(begin, mv.end(), tup.size() - (size - first_wc));
return {Result::from(std::move(tup.vals()), mv)};
}
}
else {
if (matches(tup, p, mv)) return {Result::from(std::move(tup.vals()),
mv)};
}
return {};
}
static inline Result force(any_tuple& tup, const pattern<T...>& p) {
mapping_vector mv;
if (WP == wildcard_position::in_between) {
// first range
mv.resize(size);
auto begin = mv.begin();
std::iota(begin, begin + first_wc, 0);
// second range
begin = mv.begin() + first_wc;
std::iota(begin, mv.end(), tup.size() - (size - first_wc));
return {Result::from(std::move(tup.vals()), mv)};
}
else {
matches(tup, p, mv);
return {Result::from(std::move(tup.vals()), mv)};
}
}
}; };
template<class Result, typename... T> template<class Result, typename... T>
...@@ -116,38 +72,11 @@ struct tuple_cast_impl<wildcard_position::nil, Result, T...> { ...@@ -116,38 +72,11 @@ struct tuple_cast_impl<wildcard_position::nil, Result, T...> {
if (matches<T...>(tup)) return {Result::from(std::move(tup.vals()))}; if (matches<T...>(tup)) return {Result::from(std::move(tup.vals()))};
return {}; return {};
} }
static inline option<Result> safe(any_tuple& tup, const pattern<T...>& p) {
if (matches(tup, p)) {
return {Result::from(std::move(tup.vals()))};
}
return {};
}
static inline option<Result> unsafe(any_tuple& tup, const pattern<T...>& p) {
if ( p.has_values() == false
|| matcher<wildcard_position::nil, any_tuple, T...>::vmatch(tup, p)) {
return {Result::from(std::move(tup.vals()))};
}
return {};
}
static inline Result force(any_tuple& tup, const pattern<T...>&) {
return {Result::from(std::move(tup.vals()))};
}
}; };
template<class Result, typename... T> template<class Result, typename... T>
struct tuple_cast_impl<wildcard_position::trailing, Result, T...> struct tuple_cast_impl<wildcard_position::trailing, Result, T...>
: tuple_cast_impl<wildcard_position::nil, Result, T...> { : tuple_cast_impl<wildcard_position::nil, Result, T...> {
static inline option<Result> unsafe(any_tuple& tup, const pattern<T...>& p) {
if ( p.has_values() == false
|| matcher<wildcard_position::trailing, any_tuple, T...>
::vmatch(tup, p)) {
return {Result::from(std::move(tup.vals()))};
}
return {};
}
static inline Result force(any_tuple& tup, const pattern<T...>&) {
return {Result::from(std::move(tup.vals()))};
}
}; };
template<class Result, typename... T> template<class Result, typename... T>
...@@ -157,24 +86,6 @@ struct tuple_cast_impl<wildcard_position::leading, Result, T...> { ...@@ -157,24 +86,6 @@ struct tuple_cast_impl<wildcard_position::leading, Result, T...> {
if (matches<T...>(tup)) return {Result::offset_subtuple(tup.vals(), o)}; if (matches<T...>(tup)) return {Result::offset_subtuple(tup.vals(), o)};
return {}; return {};
} }
static inline option<Result> safe(any_tuple& tup, const pattern<T...>& p) {
size_t o = tup.size() - (sizeof...(T) - 1);
if (matches(tup, p)) return {Result::offset_subtuple(tup.vals(), o)};
return {};
}
static inline option<Result> unsafe(any_tuple& tup, const pattern<T...>& p) {
if ( p.has_values() == false
|| matcher<wildcard_position::leading, any_tuple, T...>
::vmatch(tup, p)) {
size_t o = tup.size() - (sizeof...(T) - 1);
return Result::offset_subtuple(tup.vals(), o);
}
return {};
}
static inline Result force(any_tuple& tup, const pattern<T...>&) {
size_t o = tup.size() - (sizeof...(T) - 1);
return Result::offset_subtuple(tup.vals(), o);
}
}; };
} } } }
......
...@@ -38,7 +38,6 @@ ...@@ -38,7 +38,6 @@
#include "cppa/config.hpp" #include "cppa/config.hpp"
#include "cppa/either.hpp" #include "cppa/either.hpp"
#include "cppa/pattern.hpp"
#include "cppa/behavior.hpp" #include "cppa/behavior.hpp"
#include "cppa/detail/receive_policy.hpp" #include "cppa/detail/receive_policy.hpp"
#include "cppa/detail/behavior_stack.hpp" #include "cppa/detail/behavior_stack.hpp"
......
...@@ -32,7 +32,6 @@ ...@@ -32,7 +32,6 @@
#define CPPA_MATCH_EXPR_HPP #define CPPA_MATCH_EXPR_HPP
#include "cppa/option.hpp" #include "cppa/option.hpp"
#include "cppa/pattern.hpp"
#include "cppa/guard_expr.hpp" #include "cppa/guard_expr.hpp"
#include "cppa/partial_function.hpp" #include "cppa/partial_function.hpp"
#include "cppa/tpartial_function.hpp" #include "cppa/tpartial_function.hpp"
......
...@@ -36,7 +36,6 @@ ...@@ -36,7 +36,6 @@
#include <type_traits> #include <type_traits>
#include "cppa/atom.hpp" #include "cppa/atom.hpp"
#include "cppa/pattern.hpp"
#include "cppa/anything.hpp" #include "cppa/anything.hpp"
#include "cppa/behavior.hpp" #include "cppa/behavior.hpp"
#include "cppa/any_tuple.hpp" #include "cppa/any_tuple.hpp"
......
/******************************************************************************\
* ___ __ *
* /\_ \ __/\ \ *
* \//\ \ /\_\ \ \____ ___ _____ _____ __ *
* \ \ \ \/\ \ \ '__`\ /'___\/\ '__`\/\ '__`\ /'__`\ *
* \_\ \_\ \ \ \ \L\ \/\ \__/\ \ \L\ \ \ \L\ \/\ \L\.\_ *
* /\____\\ \_\ \_,__/\ \____\\ \ ,__/\ \ ,__/\ \__/.\_\ *
* \/____/ \/_/\/___/ \/____/ \ \ \/ \ \ \/ \/__/\/_/ *
* \ \_\ \ \_\ *
* \/_/ \/_/ *
* *
* Copyright (C) 2011, 2012 *
* Dominik Charousset <dominik.charousset@haw-hamburg.de> *
* *
* This file is part of libcppa. *
* libcppa is free software: you can redistribute it and/or modify it under *
* the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation, either version 3 of the License *
* or (at your option) any later version. *
* *
* libcppa is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with libcppa. If not, see <http://www.gnu.org/licenses/>. *
\******************************************************************************/
#ifndef CPPA_PATTERN_HPP
#define CPPA_PATTERN_HPP
#include <iostream>
#include <list>
#include <memory>
#include <vector>
#include <cstddef>
#include <type_traits>
#include "cppa/option.hpp"
#include "cppa/anything.hpp"
#include "cppa/any_tuple.hpp"
#include "cppa/uniform_type_info.hpp"
#include "cppa/util/guard.hpp"
#include "cppa/util/tbind.hpp"
#include "cppa/util/type_list.hpp"
#include "cppa/util/arg_match_t.hpp"
#include "cppa/util/fixed_vector.hpp"
#include "cppa/util/is_primitive.hpp"
#include "cppa/util/static_foreach.hpp"
#include "cppa/detail/boxed.hpp"
#include "cppa/detail/tdata.hpp"
#include "cppa/detail/types_array.hpp"
namespace cppa {
/**
* @brief Denotes the position of {@link cppa::anything anything} in a
* template parameter pack.
*/
enum class wildcard_position {
nil,
trailing,
leading,
in_between,
multiple
};
/**
* @brief Gets the position of {@link cppa::anything anything} from the
* type list @p Types.
* @tparam A template parameter pack as {@link cppa::util::type_list type_list}.
*/
template<typename Types>
constexpr wildcard_position get_wildcard_position() {
return util::tl_exists<Types, is_anything>::value
? ((util::tl_count<Types, is_anything>::value == 1)
? (std::is_same<typename Types::head, anything>::value
? wildcard_position::leading
: (std::is_same<typename Types::back, anything>::value
? wildcard_position::trailing
: wildcard_position::in_between))
: wildcard_position::multiple)
: wildcard_position::nil;
}
struct value_matcher {
const bool is_dummy;
inline value_matcher(bool dummy_impl = false) : is_dummy(dummy_impl) { }
virtual ~value_matcher();
virtual bool operator()(const any_tuple&) const = 0;
};
struct dummy_matcher : value_matcher {
inline dummy_matcher() : value_matcher(true) { }
bool operator()(const any_tuple&) const;
};
struct cmp_helper {
size_t i;
const any_tuple& tup;
cmp_helper(const any_tuple& tp, size_t pos = 0) : i(pos), tup(tp) { }
template<typename T>
inline bool operator()(const T& what) {
return what == tup.get_as<T>(i++);
}
template<typename T>
inline bool operator()(std::unique_ptr<util::guard<T> const >& g) {
return (*g)(tup.get_as<T>(i++));
}
template<typename T>
inline bool operator()(const util::wrapped<T>&) {
++i;
return true;
}
};
template<wildcard_position WP, class PatternTypes, class ValueTypes>
class value_matcher_impl;
template<typename... Ts, typename... Vs>
class value_matcher_impl<wildcard_position::nil,
util::type_list<Ts...>,
util::type_list<Vs...> > : public value_matcher {
detail::tdata<Vs...> m_values;
public:
template<typename... Args>
value_matcher_impl(Args&&... args) : m_values(std::forward<Args>(args)...) { }
bool operator()(const any_tuple& tup) const {
cmp_helper h{tup};
return util::static_foreach<0, sizeof...(Vs)>::eval(m_values, h);
}
};
template<typename... Ts, typename... Vs>
class value_matcher_impl<wildcard_position::trailing,
util::type_list<Ts...>,
util::type_list<Vs...> >
: public value_matcher_impl<wildcard_position::nil,
util::type_list<Ts...>,
util::type_list<Vs...> > {
typedef value_matcher_impl<wildcard_position::nil,
util::type_list<Ts...>,
util::type_list<Vs...> >
super;
public:
template<typename... Args>
value_matcher_impl(Args&&... args) : super(std::forward<Args>(args)...) { }
};
template<typename... Ts, typename... Vs>
class value_matcher_impl<wildcard_position::leading,
util::type_list<Ts...>,
util::type_list<Vs...> > : public value_matcher {
detail::tdata<Vs...> m_values;
public:
template<typename... Args>
value_matcher_impl(Args&&... args) : m_values(std::forward<Args>(args)...) { }
bool operator()(const any_tuple& tup) const {
cmp_helper h{tup, tup.size() - sizeof...(Ts)};
return util::static_foreach<0, sizeof...(Vs)>::eval(m_values, h);
}
};
template<typename... Ts, typename... Vs>
class value_matcher_impl<wildcard_position::in_between,
util::type_list<Ts...>,
util::type_list<Vs...> > : public value_matcher {
detail::tdata<Vs...> m_values;
public:
template<typename... Args>
value_matcher_impl(Args&&... args) : m_values(std::forward<Args>(args)...) { }
bool operator()(const any_tuple& tup) const {
static constexpr size_t wcpos =
static_cast<size_t>(util::tl_find<util::type_list<Ts...>, anything>::value);
static_assert(wcpos < sizeof...(Ts), "illegal wildcard position");
cmp_helper h0{tup, 0};
cmp_helper h1{tup, tup.size() - (sizeof...(Ts) - (wcpos + 1))};
static constexpr size_t range_end = (sizeof...(Vs) > wcpos)
? wcpos
: sizeof...(Vs);
return util::static_foreach<0, range_end>::eval(m_values, h0)
&& util::static_foreach<range_end + 1, sizeof...(Vs)>::eval(m_values, h1);
}
};
template<typename... Ts, typename... Vs>
class value_matcher_impl<wildcard_position::multiple,
util::type_list<Ts...>,
util::type_list<Vs...> > : public value_matcher {
public:
template<typename... Args>
value_matcher_impl(Args&&...) { }
bool operator()(const any_tuple&) const {
throw std::runtime_error("not implemented yet, sorry");
}
};
/**
* @brief A pattern matching for type and optionally value of tuple elements.
*/
template<typename... Types>
class pattern {
static_assert(sizeof...(Types) > 0, "empty pattern");
pattern(const pattern&) = delete;
pattern& operator=(const pattern&) = delete;
public:
static constexpr size_t size = sizeof...(Types);
static constexpr wildcard_position wildcard_pos =
get_wildcard_position<util::type_list<Types...> >();
/**
* @brief Parameter pack as {@link cppa::util::type_list type_list}.
*/
typedef util::type_list<Types...> types;
typedef typename types::head head_type;
typedef typename util::tl_filter_not<types, is_anything>::type
filtered_types;
static constexpr size_t filtered_size = filtered_types::size;
typedef util::fixed_vector<size_t, filtered_types::size> mapping_vector;
typedef const uniform_type_info* const_iterator;
typedef std::reverse_iterator<const_iterator> reverse_const_iterator;
inline const_iterator begin() const {
return detail::static_types_array<Types...>::arr.begin();
}
inline const_iterator end() const {
return detail::static_types_array<Types...>::arr.end();
}
inline reverse_const_iterator rbegin() const {
return reverse_const_iterator{end()};
}
inline reverse_const_iterator rend() const {
return reverse_const_iterator{begin()};
}
inline bool has_values() const { return m_vm->is_dummy == false; }
// @warning does NOT check types
bool _matches_values(const any_tuple& tup) const {
return (*m_vm)(tup);
}
pattern() : m_vm(new dummy_matcher) {
}
template<typename... Args>
pattern(head_type arg0, Args&&... args)
: m_vm(get_value_matcher(std::move(arg0), std::forward<Args>(args)...)) {
}
template<typename... Args>
pattern(const util::wrapped<head_type>& arg0, Args&&... args)
: m_vm(get_value_matcher(arg0, std::forward<Args>(args)...)) {
}
template<typename... Args>
pattern(const detail::tdata<Args...>& data) {
m_vm.reset(new value_matcher_impl<wildcard_pos, types, util::type_list<Args...> >{data});
}
pattern(std::unique_ptr<value_matcher>&& vm) : m_vm(std::move(vm)) {
if (!m_vm) m_vm.reset(new dummy_matcher);
}
static inline value_matcher* get_value_matcher() {
return nullptr;
}
template<typename Arg0, typename... Args>
static value_matcher* get_value_matcher(Arg0&& arg0, Args&&... args) {
using namespace util;
typedef typename tl_filter_not<
type_list<typename detail::strip_and_convert<Arg0>::type,
typename detail::strip_and_convert<Args>::type...>,
tbind<std::is_same, wrapped<arg_match_t> >::type
>::type
arg_types;
static_assert(arg_types::size <= size, "too many arguments");
if (tl_forall<arg_types, detail::is_boxed>::value) {
return new dummy_matcher;
}
return new value_matcher_impl<wildcard_pos, types, arg_types>{std::forward<Arg0>(arg0), std::forward<Args>(args)...};
}
private:
std::unique_ptr<value_matcher> m_vm;
};
template<typename TypeList>
struct pattern_from_type_list;
template<typename... Types>
struct pattern_from_type_list<util::type_list<Types...>> {
typedef pattern<Types...> type;
};
} // namespace cppa
#endif // CPPA_PATTERN_HPP
...@@ -44,7 +44,6 @@ ...@@ -44,7 +44,6 @@
#include "cppa/atom.hpp" #include "cppa/atom.hpp"
#include "cppa/either.hpp" #include "cppa/either.hpp"
#include "cppa/pattern.hpp"
#include "cppa/local_actor.hpp" #include "cppa/local_actor.hpp"
#include "cppa/exit_reason.hpp" #include "cppa/exit_reason.hpp"
#include "cppa/intrusive_ptr.hpp" #include "cppa/intrusive_ptr.hpp"
......
...@@ -34,7 +34,8 @@ ...@@ -34,7 +34,8 @@
#include <type_traits> #include <type_traits>
#include "cppa/option.hpp" #include "cppa/option.hpp"
#include "cppa/pattern.hpp"
#include "cppa/util/type_list.hpp"
#include "cppa/detail/matches.hpp" #include "cppa/detail/matches.hpp"
#include "cppa/detail/tuple_cast_impl.hpp" #include "cppa/detail/tuple_cast_impl.hpp"
...@@ -43,17 +44,6 @@ namespace cppa { ...@@ -43,17 +44,6 @@ namespace cppa {
#ifdef CPPA_DOCUMENTATION #ifdef CPPA_DOCUMENTATION
/**
* @brief Tries to cast @p tup to {@link cow_tuple cow_tuple<T...>} and moves
* the content of @p tup to the returned tuple on success.
* @param tup Dynamically typed tuple.
* @param pttrn Requested types with optional guard values.
* @returns An {@link option} for a {@link cow_tuple} with the types
* deduced from @p pttrn.
* @relates any_tuple
*/
auto moving_tuple_cast(any_tuple& tup, const pattern<T...>& pttrn);
/** /**
* @brief Tries to cast @p tup to {@link cow_tuple cow_tuple<T...>} and moves * @brief Tries to cast @p tup to {@link cow_tuple cow_tuple<T...>} and moves
* the content of @p tup to the returned tuple on success. * the content of @p tup to the returned tuple on success.
...@@ -76,17 +66,6 @@ auto moving_tuple_cast(any_tuple& tup); ...@@ -76,17 +66,6 @@ auto moving_tuple_cast(any_tuple& tup);
template<typename... T> template<typename... T>
auto moving_tuple_cast(any_tuple& tup, const util::type_list<T...>&); auto moving_tuple_cast(any_tuple& tup, const util::type_list<T...>&);
/**
* @brief Tries to cast @p tup to {@link cow_tuple cow_tuple<T...>}.
* @param tup Dynamically typed tuple.
* @param pttrn Requested types with optional guard values.
* @returns An {@link option} for a {@link cow_tuple} with the types
* deduced from @p pttrn.
* @relates any_tuple
*/
template<typename... T>
auto tuple_cast(any_tuple tup, const pattern<T...>& pttrn);
/** /**
* @brief Tries to cast @p tup to {@link cow_tuple cow_tuple<T...>}. * @brief Tries to cast @p tup to {@link cow_tuple cow_tuple<T...>}.
* @param tup Dynamically typed tuple. * @param tup Dynamically typed tuple.
...@@ -109,18 +88,6 @@ auto tuple_cast(any_tuple tup, const util::type_list<T...>&); ...@@ -109,18 +88,6 @@ auto tuple_cast(any_tuple tup, const util::type_list<T...>&);
#else #else
template<typename... T>
auto moving_tuple_cast(any_tuple& tup, const pattern<T...>& pttrn)
-> option<
typename cow_tuple_from_type_list<
typename pattern<T...>::filtered_types
>::type> {
typedef typename pattern<T...>::filtered_types filtered_types;
typedef typename cow_tuple_from_type_list<filtered_types>::type tuple_type;
static constexpr auto impl = get_wildcard_position<util::type_list<T...>>();
return detail::tuple_cast_impl<impl, tuple_type, T...>::safe(tup, pttrn);
}
template<typename... T> template<typename... T>
auto moving_tuple_cast(any_tuple& tup) auto moving_tuple_cast(any_tuple& tup)
-> option< -> option<
...@@ -141,15 +108,6 @@ auto moving_tuple_cast(any_tuple& tup, const util::type_list<T...>&) ...@@ -141,15 +108,6 @@ auto moving_tuple_cast(any_tuple& tup, const util::type_list<T...>&)
return moving_tuple_cast<T...>(tup); return moving_tuple_cast<T...>(tup);
} }
template<typename... T>
auto tuple_cast(any_tuple tup, const pattern<T...>& pttrn)
-> option<
typename cow_tuple_from_type_list<
typename pattern<T...>::filtered_types
>::type> {
return moving_tuple_cast(tup, pttrn);
}
template<typename... T> template<typename... T>
auto tuple_cast(any_tuple tup) auto tuple_cast(any_tuple tup)
-> option< -> option<
...@@ -168,39 +126,12 @@ auto tuple_cast(any_tuple tup, const util::type_list<T...>&) ...@@ -168,39 +126,12 @@ auto tuple_cast(any_tuple tup, const util::type_list<T...>&)
// ************************ for in-library use only! ************************ // // ************************ for in-library use only! ************************ //
// (moving) cast using a pattern; does not perform type checking
template<typename... T>
auto unsafe_tuple_cast(any_tuple& tup, const pattern<T...>& p)
-> option<
typename cow_tuple_from_type_list<
typename pattern<T...>::filtered_types
>::type> {
typedef typename pattern<T...>::filtered_types filtered_types;
typedef typename cow_tuple_from_type_list<filtered_types>::type tuple_type;
static constexpr auto impl =
get_wildcard_position<util::type_list<T...>>();
return detail::tuple_cast_impl<impl, tuple_type, T...>::unsafe(tup, p);
}
template<typename... T> template<typename... T>
auto unsafe_tuple_cast(any_tuple& tup, const util::type_list<T...>&) auto unsafe_tuple_cast(any_tuple& tup, const util::type_list<T...>&)
-> decltype(tuple_cast<T...>(tup)) { -> decltype(tuple_cast<T...>(tup)) {
return tuple_cast<T...>(tup); return tuple_cast<T...>(tup);
} }
// cast using a pattern; does neither perform type checking nor checks values
template<typename... T>
auto forced_tuple_cast(any_tuple& tup, const pattern<T...>& p)
-> typename cow_tuple_from_type_list<
typename pattern<T...>::filtered_types
>::type {
typedef typename pattern<T...>::filtered_types filtered_types;
typedef typename cow_tuple_from_type_list<filtered_types>::type tuple_type;
static constexpr auto impl =
get_wildcard_position<util::type_list<T...>>();
return detail::tuple_cast_impl<impl, tuple_type, T...>::force(tup, p);
}
#endif // CPPA_DOCUMENTATION #endif // CPPA_DOCUMENTATION
} // namespace cppa } // namespace cppa
......
...@@ -28,14 +28,46 @@ ...@@ -28,14 +28,46 @@
\******************************************************************************/ \******************************************************************************/
#include "cppa/pattern.hpp" #ifndef CPPA_WILDCARD_POSITION_HPP
#define CPPA_WILDCARD_POSITION_HPP
#include <type_traits>
#include "cppa/anything.hpp"
#include "cppa/util/type_list.hpp"
namespace cppa { namespace cppa {
value_matcher::~value_matcher() { } /**
* @brief Denotes the position of {@link cppa::anything anything} in a
* template parameter pack.
*/
enum class wildcard_position {
nil,
trailing,
leading,
in_between,
multiple
};
bool dummy_matcher::operator()(const any_tuple&) const { /**
return true; * @brief Gets the position of {@link cppa::anything anything} from the
* type list @p Types.
* @tparam A template parameter pack as {@link cppa::util::type_list type_list}.
*/
template<typename Types>
constexpr wildcard_position get_wildcard_position() {
return util::tl_exists<Types, is_anything>::value
? ((util::tl_count<Types, is_anything>::value == 1)
? (std::is_same<typename Types::head, anything>::value
? wildcard_position::leading
: (std::is_same<typename Types::back, anything>::value
? wildcard_position::trailing
: wildcard_position::in_between))
: wildcard_position::multiple)
: wildcard_position::nil;
} }
} // namespace cppa } // namespace cppa
#endif // CPPA_WILDCARD_POSITION_HPP
...@@ -7,10 +7,9 @@ ...@@ -7,10 +7,9 @@
#include "cppa/on.hpp" #include "cppa/on.hpp"
#include "cppa/atom.hpp" #include "cppa/atom.hpp"
#include "cppa/match.hpp" #include "cppa/match.hpp"
#include "cppa/cow_tuple.hpp"
#include "cppa/option.hpp" #include "cppa/option.hpp"
#include "cppa/pattern.hpp"
#include "cppa/announce.hpp" #include "cppa/announce.hpp"
#include "cppa/cow_tuple.hpp"
#include "cppa/tuple_cast.hpp" #include "cppa/tuple_cast.hpp"
#include "cppa/partial_function.hpp" #include "cppa/partial_function.hpp"
...@@ -65,6 +64,7 @@ void invoke_test(std::vector<any_tuple>& test_tuples, Testee& x) { ...@@ -65,6 +64,7 @@ void invoke_test(std::vector<any_tuple>& test_tuples, Testee& x) {
int main() { int main() {
CPPA_TEST(test__pattern); CPPA_TEST(test__pattern);
/*
pattern<int, anything, int> i3; pattern<int, anything, int> i3;
any_tuple i3_any_tup = make_cow_tuple(1, 2, 3); any_tuple i3_any_tup = make_cow_tuple(1, 2, 3);
...@@ -170,6 +170,7 @@ int main() { ...@@ -170,6 +170,7 @@ int main() {
} }
); );
CPPA_CHECK_EQUAL(true, invoked); CPPA_CHECK_EQUAL(true, invoked);
*/
/* /*
CPPA_CHECK(p0(x)); CPPA_CHECK(p0(x));
......
...@@ -14,7 +14,6 @@ ...@@ -14,7 +14,6 @@
#include "cppa/on.hpp" #include "cppa/on.hpp"
#include "cppa/cppa.hpp" #include "cppa/cppa.hpp"
#include "cppa/cow_tuple.hpp" #include "cppa/cow_tuple.hpp"
#include "cppa/pattern.hpp"
#include "cppa/any_tuple.hpp" #include "cppa/any_tuple.hpp"
#include "cppa/to_string.hpp" #include "cppa/to_string.hpp"
#include "cppa/tuple_cast.hpp" #include "cppa/tuple_cast.hpp"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment