From 77a94e227a083f0e12a0d2a66bcf48167579208d Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Mon, 19 Oct 2015 12:06:23 +1100 Subject: [PATCH] adapter: wrappers for iterators and containers --- Makefile.am | 2 + adapter.cpp | 48 +++++++++++++++++ adapter.hpp | 150 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 200 insertions(+) create mode 100644 adapter.cpp create mode 100644 adapter.hpp diff --git a/Makefile.am b/Makefile.am index 05ef13e1..2aac2dcf 100644 --- a/Makefile.am +++ b/Makefile.am @@ -9,6 +9,8 @@ AM_CXXFLAGS = $(BOOST_CPPFLAGS) $(ZLIB_CFLAGS) ## Source definitions UTIL_FILES = \ + adapter.hpp \ + adapter.cpp \ backtrace.hpp \ bezier.cpp \ bezier.hpp \ diff --git a/adapter.cpp b/adapter.cpp new file mode 100644 index 00000000..15893435 --- /dev/null +++ b/adapter.cpp @@ -0,0 +1,48 @@ + + +#include "adapter.hpp" + +//#include +//#include +//#include +// +// +//using util::adapter::scalar; +// +// +//void +//foo (void) { +// std::vector> 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 +// >::value +// ); +// +// static_assert ( +// std::is_same< +// typename std::tuple_element<0, std::tuple>::type, +// int +// >::value +// ); +// +// iterator_t end (vals.end ()); +// +// //static_assert ( +// // std::is_same< +// // typename iterator_t::reference, +// // //decltype(*std::declval >> ()), +// // //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; +//} diff --git a/adapter.hpp b/adapter.hpp new file mode 100644 index 00000000..c5156667 --- /dev/null +++ b/adapter.hpp @@ -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 + */ + +#ifndef __UTIL_ADAPTER_HPP +#define __UTIL_ADAPTER_HPP + +#include +#include +#include +#include + +namespace util { namespace adapter { + namespace container { + // reverse a container for range-based-for + template + 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 + 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 + struct scalar : public std::iterator< + typename std::iterator_traits::iterator_category, + typename std::tuple_element< + I, + typename std::iterator_traits< + It + >::value_type + >::type, + typename std::iterator_traits::difference_type + > { + public: + using reference = typename std::iterator_traits>::reference; + using value_type = typename std::iterator_traits>::value_type; + + scalar (It _inner): + m_inner (_inner) + { ; } + + const reference operator* (void) const + { return std::get (*m_inner); } + + reference operator* (void) + { return std::get (*m_inner); } + + bool operator== (scalar rhs) { return m_inner == rhs.m_inner; } + bool operator!= (scalar rhs) { return m_inner != rhs.m_inner; } + + scalar& 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::value_type, + // std::array + // >::value, + // typename std::iterator< + // typename std::iterator_traits::iterator_category, + // typename std::tuple_element< + // typename std::iterator_traits::value_type + // >::type, + // typename std::iterator_traits::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& operator++ (void) + // { + // if (++m_sequence >= S) + // ++m_inner; + // } + + //private: + // It m_inner; + // size_t m_sequence; + //}; + } +} } + +#endif