adapter: wrappers for iterators and containers

This commit is contained in:
Danny Robson 2015-10-19 12:06:23 +11:00
parent 634f2af9cc
commit 77a94e227a
3 changed files with 200 additions and 0 deletions

View File

@ -9,6 +9,8 @@ AM_CXXFLAGS = $(BOOST_CPPFLAGS) $(ZLIB_CFLAGS)
## Source definitions
UTIL_FILES = \
adapter.hpp \
adapter.cpp \
backtrace.hpp \
bezier.cpp \
bezier.hpp \

48
adapter.cpp Normal file
View File

@ -0,0 +1,48 @@
#include "adapter.hpp"
//#include <vector>
//#include <tuple>
//#include <iostream>
//
//
//using util::adapter::scalar;
//
//
//void
//foo (void) {
// std::vector<std::tuple<int,int,int>> vals;
//
// using iterator_t = util::adapter::scalar<0, decltype(vals.begin ())>;
//
// static_assert (
// std::is_same<
// typename std::iterator_traits<
// decltype(vals.begin ())
// >::value_type,
// std::tuple<int,int,int>
// >::value
// );
//
// static_assert (
// std::is_same<
// typename std::tuple_element<0, std::tuple<int,int,int>>::type,
// int
// >::value
// );
//
// iterator_t end (vals.end ());
//
// //static_assert (
// // std::is_same<
// // typename iterator_t::reference,
// // //decltype(*std::declval <scalar<0,std::tuple<int,int,int>>> ()),
// // //typename scalar<0,decltype(vals.begin ())>::value_type,
// // int&
// // >::value
// //);
//
// for (auto p = iterator_t (vals.begin ()), last = iterator_t (vals.end ()); p != last; ++p)
// int b = *p;
//}

150
adapter.hpp Normal file
View File

@ -0,0 +1,150 @@
/*
* 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.
*
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
*/
#ifndef __UTIL_ADAPTER_HPP
#define __UTIL_ADAPTER_HPP
#include <cstdlib>
#include <iterator>
#include <utility>
#include <tuple>
namespace util { namespace adapter {
namespace container {
// reverse a container for range-based-for
template <typename T>
struct reverse {
reverse (T &_target):
m_target (_target)
{ ; }
auto begin (void) { return m_target.begin (); }
auto end (void) { return m_target.end (); }
auto begin (void) const { return m_target.begin (); }
auto end (void) const { return m_target.end (); }
auto cbegin (void) { return m_target.cbegin (); }
auto cend (void) { return m_target.cend (); }
private:
T &m_target;
};
// adapt a container's range methods to return indices rather than iterators
template <typename T>
struct indices {
using typename T::size_type;
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;
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;
//};
}
} }
#endif