Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add variant_associative_view::iterator #241

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/rttr/detail/impl/associative_mapper_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ struct associative_container_mapper_wrapper : iterator_wrapper_base<Tp>
return variant(std::ref(base_class::get_key(it)));
}

template<typename..., typename V = value_t, enable_if_t<!std::is_void<V>::value, int> = 0>
static variant
get_value(iterator_data& itr)
{
auto& it = itr_wrapper::get_iterator(itr);
return variant(std::ref(base_class::get_value(it)));
}

template<typename..., typename V = value_t, enable_if_t<!std::is_void<V>::value, int> = 0>
static variant
get_value(const iterator_data& itr)
Expand All @@ -80,6 +88,13 @@ struct associative_container_mapper_wrapper : iterator_wrapper_base<Tp>
return variant(std::ref(base_class::get_value(it)));
}

template<typename..., typename V = value_t, enable_if_t<std::is_void<V>::value, int> = 0>
static variant
get_value(iterator_data& itr)
{
return variant();
}

template<typename..., typename V = value_t, enable_if_t<std::is_void<V>::value, int> = 0>
static variant
get_value(const iterator_data& itr)
Expand Down Expand Up @@ -478,6 +493,11 @@ struct associative_container_empty
return variant();
}

static variant get_value(iterator_data& itr)
{
return variant();
}

static variant get_value(const iterator_data& itr)
{
return variant();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class RTTR_LOCAL variant_associative_view_private
m_delete_func(associative_container_empty::destroy),
m_get_key_func(associative_container_empty::get_key),
m_get_value_func(associative_container_empty::get_value),
m_get_const_value_func(associative_container_empty::get_value),
m_advance_func(associative_container_empty::advance),
m_find_func(associative_container_empty::find),
m_erase_func(associative_container_empty::erase),
Expand Down Expand Up @@ -85,6 +86,7 @@ class RTTR_LOCAL variant_associative_view_private
m_delete_func(associative_container_mapper_wrapper<RawType, ConstType>::destroy),
m_get_key_func(associative_container_mapper_wrapper<RawType, ConstType>::get_key),
m_get_value_func(associative_container_mapper_wrapper<RawType, ConstType>::get_value),
m_get_const_value_func(associative_container_mapper_wrapper<RawType, ConstType>::get_value),
m_advance_func(associative_container_mapper_wrapper<RawType, ConstType>::advance),
m_find_func(associative_container_mapper_wrapper<RawType, ConstType>::find),
m_erase_func(associative_container_mapper_wrapper<RawType, ConstType>::erase),
Expand Down Expand Up @@ -157,22 +159,37 @@ class RTTR_LOCAL variant_associative_view_private
return m_get_key_func(itr);
}

RTTR_INLINE const variant get_value(const iterator_data& itr) const
RTTR_INLINE variant get_value(iterator_data& itr) const
{
return m_get_value_func(itr);
}

RTTR_INLINE const std::pair<variant, variant> get_key_value(const iterator_data& itr) const
RTTR_INLINE const variant get_value(const iterator_data& itr) const
{
return m_get_const_value_func(itr);
}

RTTR_INLINE std::pair<variant, variant> get_key_value(iterator_data& itr) const
{
return {m_get_key_func(itr), m_get_value_func(itr)};
}

RTTR_INLINE const std::pair<variant, variant> get_key_value(const iterator_data& itr) const
{
return {m_get_key_func(itr), m_get_const_value_func(itr)};
}


RTTR_INLINE void advance(iterator_data& itr, std::ptrdiff_t index) const
{
m_advance_func(itr, index);
}

RTTR_INLINE void find(iterator_data& itr, argument& key) const
{
m_find_func(m_container, itr, key);
}

RTTR_INLINE void find(iterator_data& itr, argument& key)
{
m_find_func(m_container, itr, key);
Expand Down Expand Up @@ -216,7 +233,8 @@ class RTTR_LOCAL variant_associative_view_private
using create_func = void(*)(iterator_data& itr_tgt, const iterator_data& itr_src);
using delete_func = void(*)(iterator_data& itr);
using get_key_func = variant (*)(const iterator_data& itr);
using get_value_func = variant (*)(const iterator_data& itr);
using get_value_func = variant(*)(iterator_data& itr);
using get_const_value_func = variant(*)(const iterator_data& itr);
using clear_func = void(*)(void* container);
using erase_func = std::size_t(*)(void* container, argument& key);
using find_func = void(*)(void* container, detail::iterator_data& itr, argument& key);
Expand All @@ -238,6 +256,7 @@ class RTTR_LOCAL variant_associative_view_private
delete_func m_delete_func;
get_key_func m_get_key_func;
get_value_func m_get_value_func;
get_const_value_func m_get_const_value_func;
advance_func m_advance_func;
find_func m_find_func;
erase_func m_erase_func;
Expand Down
192 changes: 191 additions & 1 deletion src/rttr/variant_associative_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,19 @@ std::pair<variant_associative_view::const_iterator, bool> variant_associative_vi

/////////////////////////////////////////////////////////////////////////////////////////

variant_associative_view::const_iterator variant_associative_view::find(argument arg)
variant_associative_view::iterator variant_associative_view::find(argument arg)
{
iterator itr(&m_view);

m_view.find(itr.m_itr, arg);

return itr;
}


/////////////////////////////////////////////////////////////////////////////////////////

variant_associative_view::const_iterator variant_associative_view::find(argument arg) const
{
const_iterator itr(&m_view);

Expand Down Expand Up @@ -190,6 +202,17 @@ variant_associative_view::equal_range(argument key)

/////////////////////////////////////////////////////////////////////////////////////////

variant_associative_view::iterator variant_associative_view::begin()
{
iterator itr(&m_view);

m_view.begin(itr.m_itr);

return itr;
}

/////////////////////////////////////////////////////////////////////////////////////////

variant_associative_view::const_iterator variant_associative_view::begin() const
{
const_iterator itr(&m_view);
Expand All @@ -201,6 +224,17 @@ variant_associative_view::const_iterator variant_associative_view::begin() const

/////////////////////////////////////////////////////////////////////////////////////////

variant_associative_view::iterator variant_associative_view::end()
{
iterator itr(&m_view);

m_view.end(itr.m_itr);

return itr;
}

/////////////////////////////////////////////////////////////////////////////////////////

variant_associative_view::const_iterator variant_associative_view::end() const
{
const_iterator itr(&m_view);
Expand All @@ -214,6 +248,162 @@ variant_associative_view::const_iterator variant_associative_view::end() const
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////

variant_associative_view::iterator::iterator(detail::variant_associative_view_private* view) RTTR_NOEXCEPT
: m_view(view)
{
}

/////////////////////////////////////////////////////////////////////////////////////////

variant_associative_view::iterator::~iterator()
{
m_view->destroy(m_itr);
}

/////////////////////////////////////////////////////////////////////////////////////////

variant_associative_view::iterator::iterator(const iterator &other)
: m_view(other.m_view),
m_itr(other.m_itr)
{
m_view->copy(m_itr, other.m_itr);
}

/////////////////////////////////////////////////////////////////////////////////////////

variant_associative_view::iterator& variant_associative_view::iterator::operator=(iterator other)
{
swap(other);
return *this;
}

/////////////////////////////////////////////////////////////////////////////////////////

void variant_associative_view::iterator::swap(iterator& other)
{
std::swap(m_itr, other.m_itr);
std::swap(m_view, other.m_view);
}

/////////////////////////////////////////////////////////////////////////////////////////

std::pair<variant, variant> variant_associative_view::iterator::operator*()
{
return m_view->get_key_value(m_itr);
}

/////////////////////////////////////////////////////////////////////////////////////////

const variant variant_associative_view::iterator::get_key() const
{
return m_view->get_key(m_itr);
}

/////////////////////////////////////////////////////////////////////////////////////////

variant variant_associative_view::iterator::get_value()
{
return m_view->get_value(m_itr);
}

/////////////////////////////////////////////////////////////////////////////////////////

variant_associative_view::iterator& variant_associative_view::iterator::operator++()
{
m_view->advance(m_itr, 1);
return *this;
}

/////////////////////////////////////////////////////////////////////////////////////////

variant_associative_view::iterator variant_associative_view::iterator::operator++(int)
{
iterator result(m_view);

m_view->copy(result.m_itr, m_itr);
m_view->advance(m_itr, 1);

return result;
}

/////////////////////////////////////////////////////////////////////////////////////////

variant_associative_view::iterator& variant_associative_view::iterator::operator--()
{
m_view->advance(m_itr, -1);
return *this;
}

/////////////////////////////////////////////////////////////////////////////////////////

variant_associative_view::iterator variant_associative_view::iterator::operator--(int)
{
iterator result(m_view);

m_view->copy(result.m_itr, m_itr);
m_view->advance(m_itr, -1);

return result;
}

/////////////////////////////////////////////////////////////////////////////////////////

variant_associative_view::iterator& variant_associative_view::iterator::operator+=(int i)
{
m_view->advance(m_itr, i);
return *this;
}

/////////////////////////////////////////////////////////////////////////////////////////

variant_associative_view::iterator& variant_associative_view::iterator::operator-=(int i)
{
m_view->advance(m_itr, -i);
return *this;
}

/////////////////////////////////////////////////////////////////////////////////////////

variant_associative_view::iterator variant_associative_view::iterator::operator+(int i) const
{
iterator result(m_view);

m_view->copy(result.m_itr, m_itr);
result.m_view->advance(result.m_itr, i);

return result;
}

/////////////////////////////////////////////////////////////////////////////////////////

variant_associative_view::iterator variant_associative_view::iterator::operator-(int i) const
{
iterator result(m_view);

m_view->copy(result.m_itr, m_itr);
result.m_view->advance(result.m_itr, -i);

return result;
}

/////////////////////////////////////////////////////////////////////////////////////////

bool variant_associative_view::iterator::operator==(const iterator& other) const
{
return m_view->equal(m_itr, other.m_itr);
}

/////////////////////////////////////////////////////////////////////////////////////////

bool variant_associative_view::iterator::operator!=(const iterator& other) const
{
return !m_view->equal(m_itr, other.m_itr);
}

/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////

variant_associative_view::const_iterator::const_iterator(const detail::variant_associative_view_private* view) RTTR_NOEXCEPT
: m_view(view)
{
Expand Down
Loading