Commit aa0427cb authored by neverlord's avatar neverlord

iterator interface

parent 5a913696
......@@ -135,7 +135,7 @@ class any_tuple
return *reinterpret_cast<T*>(mutable_at(p));
}
typedef type_value_pair const* const_iterator;
typedef detail::abstract_tuple::const_iterator const_iterator;
inline const_iterator begin() const { return m_vals->begin(); }
......
......@@ -152,12 +152,6 @@ class any_tuple_view
return typeid(detail::object_array);
}
typedef type_value_pair const* const_iterator;
inline const_iterator begin() const { return m_values.data(); }
inline const_iterator end() const { return begin() + m_values.size(); }
};
} // namespace cppa
......
......@@ -31,6 +31,9 @@
#ifndef ABSTRACT_TUPLE_HPP
#define ABSTRACT_TUPLE_HPP
#include <iterator>
#include "cppa/config.hpp"
#include "cppa/ref_counted.hpp"
#include "cppa/type_value_pair.hpp"
#include "cppa/uniform_type_info.hpp"
......@@ -42,14 +45,10 @@ namespace cppa { namespace detail {
struct abstract_tuple : ref_counted
{
typedef type_value_pair const* const_iterator;
// mutators
virtual void* mutable_at(size_t pos) = 0;
// accessors
virtual const_iterator begin() const = 0;
virtual const_iterator end() const = 0;
virtual size_t size() const = 0;
virtual abstract_tuple* copy() const = 0;
virtual void const* at(size_t pos) const = 0;
......@@ -59,6 +58,58 @@ struct abstract_tuple : ref_counted
bool equals(abstract_tuple const& other) const;
// iterator support
class const_iterator : public std::iterator<std::bidirectional_iterator_tag,
type_value_pair>
{
size_t m_pos;
abstract_tuple const& m_tuple;
public:
const_iterator(abstract_tuple const& tup, size_t pos = 0) : m_pos(pos), m_tuple(tup) { }
const_iterator(const_iterator const&) = default;
inline bool operator==(const_iterator const& other) const
{
CPPA_REQUIRE(&(other.m_tuple) == &(other.m_tuple));
return other.m_pos == m_pos;
}
inline bool operator!=(const_iterator const& other) const
{
return !(*this == other);
}
inline const_iterator& operator++()
{
++m_pos;
return *this;
}
inline const_iterator& operator--()
{
CPPA_REQUIRE(m_pos > 0);
--m_pos;
return *this;
}
inline size_t position() const { return m_pos; }
void const* value() const { return m_tuple.at(m_pos); }
uniform_type_info const* type() const { return m_tuple.type_at(m_pos); }
type_value_pair operator*() { return {type(), value()}; }
};
inline const_iterator begin() const { return {*this}; }
inline const_iterator end() const { return {*this, size()}; }
};
} } // namespace cppa::detail
......
......@@ -72,16 +72,6 @@ class decorated_tuple : public abstract_tuple
init(v);
}
virtual const_iterator begin() const
{
return m_data;
}
virtual const_iterator end() const
{
return static_cast<const_iterator>(m_data) + sizeof...(ElementTypes);
}
virtual void* mutable_at(size_t pos)
{
CPPA_REQUIRE(pos < size());
......
......@@ -40,8 +40,6 @@ struct empty_tuple : abstract_tuple
using abstract_tuple::const_iterator;
const_iterator begin() const;
const_iterator end() const;
size_t size() const;
void* mutable_at(size_t);
abstract_tuple* copy() const;
......
......@@ -38,7 +38,6 @@
#include "cppa/any_tuple.hpp"
#include "cppa/tuple_cast.hpp"
#include "cppa/util/duration.hpp"
#include "cppa/any_tuple_view.hpp"
#include "cppa/util/apply_tuple.hpp"
#include "cppa/util/fixed_vector.hpp"
#include "cppa/util/callable_trait.hpp"
......@@ -62,7 +61,6 @@ class invokable_base
invokable_base() = default;
virtual ~invokable_base();
virtual bool invoke(any_tuple const&) const = 0;
virtual bool invoke(any_tuple_view const&) const = 0;
};
......@@ -106,13 +104,6 @@ class timed_invokable_impl : public timed_invokable
return true;
}
bool invoke(any_tuple_view const&) const
{
m_target();
return true;
}
};
class invokable : public invokable_base
......@@ -163,11 +154,6 @@ class invokable_impl : public invokable
return invoke_impl(data);
}
bool invoke(any_tuple_view const& data) const
{
return invoke_impl(data);
}
intermediate* get_intermediate(any_tuple const& data)
{
auto tuple_option = tuple_cast(data, *m_pattern);
......@@ -218,11 +204,6 @@ class invokable_impl<0, Fun, Tuple, Pattern> : public invokable
return invoke_impl(data);
}
bool invoke(any_tuple_view const& data) const
{
return invoke_impl(data);
}
intermediate* get_intermediate(any_tuple const& data)
{
return matches(data, *m_pattern) ? &m_iimpl : nullptr;
......
......@@ -45,23 +45,10 @@
namespace cppa { namespace detail {
template<typename T>
inline auto ptr_type(T& ptr, util::enable_if<std::is_pointer<T>>* = 0) -> T
{
return ptr;
}
template<typename T>
inline auto ptr_type(T& iter, util::disable_if<std::is_pointer<T>>* = 0) -> decltype(iter.operator->())
{
return iter.operator->();
}
template<class MappingVector, typename Iterator>
class push_mapping_decorator
{
size_t pos;
Iterator iter;
MappingVector& mapping;
size_t commited_size;
......@@ -71,7 +58,7 @@ class push_mapping_decorator
typedef MappingVector mapping_vector;
push_mapping_decorator(Iterator const& i, MappingVector& mv)
: pos(0), iter(i), mapping(mv), commited_size(0)
: iter(i), mapping(mv), commited_size(0)
{
}
......@@ -79,29 +66,29 @@ class push_mapping_decorator
inline push_mapping_decorator& operator++()
{
++pos;
++iter;
return *this;
}
inline bool operator==(Iterator const& i)
inline push_mapping_decorator& operator--()
{
return iter == i;
--iter;
return *this;
}
inline decltype(*iter) operator*()
{
return *iter;
}
inline bool operator==(Iterator const& i) { return iter == i; }
inline decltype(ptr_type(iter)) operator->()
{
return ptr_type(iter);
}
inline bool operator!=(Iterator const& i) { return iter != i; }
inline decltype(iter.value()) value() const { return iter.value(); }
inline decltype(iter.type()) type() const { return iter.type(); }
inline size_t position() const { return iter.position(); }
inline void push_mapping()
{
mapping.push_back(pos);
mapping.push_back(position());
}
inline void commit_mapping()
......@@ -150,7 +137,7 @@ template<typename T>
inline typename util::enable_if<is_pm_decorated<T>, void>::type
push_mapping(T& iter)
{
iter->push_mapping();
iter.push_mapping();
}
template<typename T>
......@@ -161,7 +148,7 @@ template<typename T>
inline typename util::enable_if<is_pm_decorated<T>, void>::type
commit_mapping(T& iter)
{
iter->commit_mapping();
iter.commit_mapping();
}
template<typename T>
......@@ -172,20 +159,20 @@ template<typename T>
inline typename util::enable_if<is_pm_decorated<T>, void>::type
rollback_mapping(T& iter)
{
iter->rollback_mapping();
iter.rollback_mapping();
}
template<class TupleBeginIterator, class TupleEndIterator, class PatternIterator>
bool matches(TupleBeginIterator tbegin, TupleEndIterator tend,
PatternIterator pbegin, PatternIterator pend)
{
for ( ; !(pbegin == pend && tbegin == tend); ++pbegin, ++tbegin)
while (!(pbegin == pend && tbegin == tend))
{
if (pbegin == pend)
{
return false;
}
else if (pbegin->first == nullptr) // nullptr == wildcard (anything)
else if (pbegin.type() == nullptr) // nullptr == wildcard (anything)
{
// perform submatching
++pbegin;
......@@ -206,11 +193,11 @@ bool matches(TupleBeginIterator tbegin, TupleEndIterator tend,
return false; // no submatch found
}
// compare types
else if (tbegin != tend && pbegin->first == tbegin->first)
else if (tbegin != tend && pbegin.type() == tbegin.type())
{
// compare values if needed
if ( pbegin->second == nullptr
|| pbegin->first->equals(pbegin->second, tbegin->second))
if ( pbegin.value() == nullptr
|| pbegin.type()->equals(pbegin.value(), tbegin.value()))
{
push_mapping(tbegin);
}
......
......@@ -43,7 +43,6 @@ class object_array : public abstract_tuple
{
std::vector<object> m_elements;
std::vector<type_value_pair> m_data;
public:
......@@ -61,10 +60,6 @@ class object_array : public abstract_tuple
void* mutable_at(size_t pos);
const_iterator begin() const;
const_iterator end() const;
size_t size() const;
abstract_tuple* copy() const;
......
......@@ -61,26 +61,13 @@ class tuple_vals : public abstract_tuple
static types_array<ElementTypes...> m_types;
type_value_pair m_view[sizeof...(ElementTypes)];
void init_view()
{
for (size_t i = 0; i < sizeof...(ElementTypes); ++i)
{
m_view[i].first = m_types[i];
m_view[i].second = m_data.at(i);
}
}
public:
using abstract_tuple::const_iterator;
tuple_vals() : m_data() { init_view(); }
tuple_vals() : m_data() { }
tuple_vals(tuple_vals const& other) : super(), m_data(other.m_data) { init_view(); }
tuple_vals(tuple_vals const& other) : super(), m_data(other.m_data) { }
tuple_vals(ElementTypes const&... args) : m_data(args...) { init_view(); }
tuple_vals(ElementTypes const&... args) : m_data(args...) { }
inline data_type const& data() const
{
......@@ -92,16 +79,6 @@ class tuple_vals : public abstract_tuple
return m_data;
}
const_iterator begin() const
{
return m_view;
}
const_iterator end() const
{
return begin() + sizeof...(ElementTypes);
}
size_t size() const
{
return sizeof...(ElementTypes);
......@@ -115,14 +92,12 @@ class tuple_vals : public abstract_tuple
void const* at(size_t pos) const
{
CPPA_REQUIRE(pos < size());
return m_view[pos].second;
return m_data.at(pos);
}
void* mutable_at(size_t pos)
{
CPPA_REQUIRE(pos < size());
// safe, because tuple_cals is used in cow_ptrs only
return const_cast<void*>(m_view[pos].second);
return const_cast<void*>(at(pos));
}
uniform_type_info const* type_at(size_t pos) const
......
......@@ -50,7 +50,6 @@ namespace cppa {
class any_tuple;
class invoke_rules;
class any_tuple_view;
class timed_invoke_rules;
typedef std::list<detail::invokable_ptr> invokable_list;
......@@ -88,8 +87,6 @@ class invoke_rules_base
*/
bool operator()(any_tuple const& data) const;
bool operator()(any_tuple_view const& data) const;
/**
* @brief Tries to match @p data with one of the stored patterns.
* @param data Data tuple that should be matched.
......
......@@ -35,7 +35,6 @@
#include "cppa/tuple.hpp"
#include "cppa/invoke_rules.hpp"
#include "cppa/any_tuple_view.hpp"
#include "cppa/util/if_else.hpp"
#include "cppa/util/enable_if.hpp"
......@@ -49,14 +48,16 @@ namespace detail {
template<typename Tuple>
struct match_helper
{
static constexpr bool is_view = std::is_same<Tuple, any_tuple_view>::value;
//static constexpr bool is_view = std::is_same<Tuple, any_tuple_view>::value;
static constexpr bool is_view = false;
Tuple what;
template<typename T>
match_helper(T const& w, typename util::enable_if_c<std::is_same<T, T>::value && is_view>::type* =0)
: what(w) { }
template<typename T>
match_helper(T const& w, typename util::enable_if_c<std::is_same<T, T>::value && !is_view>::type* =0)
: what(make_tuple(w)) { }
//template<typename T>
//match_helper(T const& w, typename util::enable_if_c<std::is_same<T, T>::value && !is_view>::type* =0)
// : what(make_tuple(w)) { }
void operator()(invoke_rules& rules) { rules(what); }
void operator()(invoke_rules&& rules) { rules(what); }
template<typename Head, typename... Tail>
......@@ -76,6 +77,7 @@ struct match_helper
} // namespace detail
/*
template<typename T>
auto match(T const& x)
-> detail::match_helper<
......@@ -86,6 +88,7 @@ auto match(T const& x)
{
return {x};
}
*/
} // namespace cppa
......
......@@ -84,7 +84,50 @@ class pattern
typedef util::fixed_vector<size_t, filtered_types::size> mapping_vector;
typedef type_value_pair const* const_iterator;
class const_iterator
{
type_value_pair const* iter;
public:
const_iterator(type_value_pair const* i) : iter(i) { }
const_iterator(const_iterator const&) = default;
inline uniform_type_info const* type() const { return iter->first; }
inline void const* value() const { return iter->second; }
inline decltype(iter) operator->() { return iter; }
inline decltype(*iter) operator*() { return *iter; }
inline const_iterator& operator++()
{
++iter;
return *this;
}
inline const_iterator& operator--()
{
--iter;
return *this;
}
inline bool operator==(const_iterator const& other) const
{
return iter == other.iter;
}
inline bool operator!=(const_iterator const& other) const
{
return iter != other.iter;
}
};
//typedef type_value_pair const* const_iterator;
const_iterator begin() const { return m_ptrs; }
......
......@@ -36,7 +36,6 @@
#include "cppa/option.hpp"
#include "cppa/pattern.hpp"
#include "cppa/any_tuple.hpp"
#include "cppa/any_tuple_view.hpp"
#include "cppa/util/type_list.hpp"
#include "cppa/detail/matches.hpp"
#include "cppa/detail/types_array.hpp"
......@@ -125,6 +124,7 @@ auto tuple_cast(any_tuple const& tup)
return tuple_cast_impl<tuple_type, any_tuple, T...>(tup);
}
/*
template<typename... P>
auto tuple_cast(any_tuple_view const& tup, pattern<P...> const& p)
-> option<
......@@ -152,6 +152,7 @@ auto tuple_cast(any_tuple_view const& tup)
typedef typename result_type::value_type tuple_type;
return tuple_cast_impl<tuple_type, any_tuple_view, T...>(tup);
}
*/
} // namespace cppa
......
......@@ -33,16 +33,6 @@
namespace cppa { namespace detail {
abstract_tuple::const_iterator empty_tuple::begin() const
{
return nullptr;
}
abstract_tuple::const_iterator empty_tuple::end() const
{
return nullptr;
}
size_t empty_tuple::size() const
{
return 0;
......
......@@ -55,15 +55,6 @@ bool invoke_rules_base::operator()(const any_tuple& data) const
return false;
}
bool invoke_rules_base::operator()(any_tuple_view const& data) const
{
for (detail::invokable_ptr const& ptr : m_list)
{
if (ptr->invoke(data)) return true;
}
return false;
}
detail::intermediate*
invoke_rules_base::get_intermediate(const any_tuple& t) const
{
......
......@@ -49,15 +49,11 @@ object_array::object_array(object_array const& other)
void object_array::push_back(object const& what)
{
m_elements.push_back(what);
auto& back = m_elements.back();
m_data.push_back({back.type(), back.value()});
}
void object_array::push_back(object&& what)
{
m_elements.push_back(std::move(what));
auto& back = m_elements.back();
m_data.push_back({back.type(), back.value()});
}
void* object_array::mutable_at(size_t pos)
......@@ -80,16 +76,6 @@ void const* object_array::at(size_t pos) const
return m_elements[pos].value();
}
abstract_tuple::const_iterator object_array::begin() const
{
return m_data.data();
}
abstract_tuple::const_iterator object_array::end() const
{
return begin() + m_data.size();
}
bool object_array::equals(cppa::detail::abstract_tuple const& ut) const
{
if (size() == ut.size())
......
......@@ -42,6 +42,7 @@ size_t test__tuple()
// get a view of t0
any_tuple atup0(t0);
CPPA_CHECK(atup0.size() == 2 && atup0.at(0) == &get<0>(t0));
/*
auto v1opt = tuple_cast<std::string, anything>(any_tuple_view(atup0));
// the tuple_view forces atup0 to detach from t0
CPPA_CHECK(atup0.size() == 2 && atup0.at(0) != &get<0>(t0));
......@@ -55,6 +56,7 @@ size_t test__tuple()
CPPA_CHECK_EQUAL(v1_0, "1");
CPPA_CHECK_EQUAL(atup0.at(0), &(get<0>(v1))); // point to the same
}
*/
// use tuple cast to get a subtuple
any_tuple at0(t0);
auto v0opt = tuple_cast<std::string, anything>(at0);
......
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