libcruft-util/adapter.hpp

150 lines
4.4 KiB
C++
Raw Normal View History

/*
2018-08-04 15:14:06 +10:00
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
*/
#ifndef __UTIL_ADAPTER_HPP
#define __UTIL_ADAPTER_HPP
#include <array>
#include <cstddef>
#include <iterator>
2017-01-05 15:06:49 +11:00
namespace util::adapter {
namespace container {
// reverse a container for range-based-for
template <typename T>
struct reverse {
2017-05-23 12:50:51 +10:00
explicit reverse (T &_target):
m_target (_target)
{ ; }
auto begin (void) & { return m_target.rbegin (); }
auto end (void) & { return m_target.rend (); }
auto begin (void) const& { return m_target.rbegin (); }
auto end (void) const& { return m_target.rend (); }
auto cbegin (void) const& { return m_target.crbegin (); }
auto cend (void) const& { return m_target.crend (); }
private:
T &m_target;
};
template <typename Container>
auto
make_reverse (Container &c)
{
return reverse<Container> { c };
};
// adapt a container's range methods to return indices rather than iterators
template <typename T>
struct indices {
using typename T::size_type;
2017-05-23 12:50:51 +10:00
explicit indices (T &_target):
m_target (_target)
{ ; }
size_type begin (void) { return 0; }
size_type end (void) { return m_target.size (); }
private:
T m_target;
};
}
namespace iterator {
// adapt an iterator to return the n-th tuple element of the
// underlying iterator::value_type
template <size_t I, typename It>
struct scalar : public std::iterator<
typename std::iterator_traits<It>::iterator_category,
typename std::tuple_element<
I,
typename std::iterator_traits<
It
>::value_type
>::type,
typename std::iterator_traits<It>::difference_type
> {
public:
using reference = typename std::iterator_traits<scalar<I,It>>::reference;
using value_type = typename std::iterator_traits<scalar<I,It>>::value_type;
2017-05-23 12:50:51 +10:00
explicit scalar (It _inner):
m_inner (_inner)
{ ; }
const reference operator* (void) const
{ return std::get<I> (*m_inner); }
reference operator* (void)
{ return std::get<I> (*m_inner); }
bool operator== (scalar<I,It> rhs) { return m_inner == rhs.m_inner; }
bool operator!= (scalar<I,It> rhs) { return m_inner != rhs.m_inner; }
scalar<I,It>& operator++ (void)
{ ++m_inner; return *this; }
private:
It m_inner;
};
//template <
// typename It,
// size_t S,
// typename T
//>
//struct explode : public std::enable_if<
// std::is_same<
// typename std::iterator_traits<It>::value_type,
// std::array<T,S>
// >::value,
// typename std::iterator<
// typename std::iterator_traits<It>::iterator_category,
// typename std::tuple_element<
// typename std::iterator_traits<It>::value_type
// >::type,
// typename std::iterator_traits<It>::difference_type
// >::type
//> {
//public:
// explode (It _inner):
// m_inner (_inner),
// m_sequence (0)
// { ; }
// const reference operator* (void) const
// { return (*m_inner)[m_seqence]; }
// reference operator* (void)
// { return (*m_inner)[m_seqence]; }
// bool operator== (explode rhs) { return m_inner == rhs.m_inner && m_sequence == rhs.m_sequence; }
// bool operator!= (explode rhs) { return !(*this == rhs); }
// explode<It>& operator++ (void)
// {
// if (++m_sequence >= S)
// ++m_inner;
// }
//private:
// It m_inner;
// size_t m_sequence;
//};
}
2017-01-05 15:06:49 +11:00
}
#endif