view: move implementation inline

this allows support for arbitrary iterator types
This commit is contained in:
Danny Robson 2015-09-22 18:31:47 +10:00
parent 86834281b8
commit 56b5c34b5d
3 changed files with 21 additions and 22 deletions

View File

@ -263,7 +263,7 @@ UTIL_FILES = \
vector.ipp \ vector.ipp \
version.cpp \ version.cpp \
version.hpp \ version.hpp \
view.cpp \ view.ipp \
view.hpp view.hpp
if PLATFORM_LINUX if PLATFORM_LINUX

View File

@ -48,4 +48,7 @@ namespace util {
template <typename T> template <typename T>
std::ostream& operator<< (std::ostream&, view<T>); std::ostream& operator<< (std::ostream&, view<T>);
} }
#include "./view.ipp"
#endif #endif

View File

@ -15,19 +15,19 @@
*/ */
#include "view.hpp" #ifdef __UTIL_VIEW_IPP
#error
#endif
#define __UTIL_VIEW_IPP
#include "debug.hpp" #include "debug.hpp"
#include <cstring>
#include <iterator> #include <iterator>
using util::view;
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename T> template <typename T>
view<T>::view (T _begin, T _end): util::view<T>::view (T _begin, T _end):
m_begin (_begin), m_begin (_begin),
m_end (_end) m_end (_end)
{ ; } { ; }
@ -36,7 +36,7 @@ view<T>::view (T _begin, T _end):
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename T> template <typename T>
T T
view<T>::begin (void) util::view<T>::begin (void)
{ {
return m_begin; return m_begin;
} }
@ -45,16 +45,16 @@ view<T>::begin (void)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename T> template <typename T>
T T
view<T>::end (void) util::view<T>::end (void)
{ {
return m_end; return m_end;
} }
//----------------------------------------------------------------------------- ///////////////////////////////////////////////////////////////////////////////
template <typename T> template <typename T>
bool bool
view<T>::empty (void) const util::view<T>::empty (void) const
{ {
return m_begin == m_end; return m_begin == m_end;
} }
@ -63,33 +63,33 @@ view<T>::empty (void) const
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename T> template <typename T>
size_t size_t
view<T>::size (void) const util::view<T>::size (void) const
{ {
return std::distance (m_begin, m_end); return std::distance (m_begin, m_end);
} }
//----------------------------------------------------------------------------- ///////////////////////////////////////////////////////////////////////////////
template <typename T> template <typename T>
const typename view<T>::value_type& const typename util::view<T>::value_type&
view<T>::operator[] (size_t idx) const util::view<T>::operator[] (size_t idx) const
{ {
CHECK_LT (m_begin + idx, m_end); CHECK_LT (m_begin + idx, m_end);
return m_begin[idx]; return m_begin[idx];
} }
//----------------------------------------------------------------------------- ///////////////////////////////////////////////////////////////////////////////
template <typename T> template <typename T>
bool bool
view<T>::operator== (const view<T> rhs) const util::view<T>::operator== (const view<T> rhs) const
{ {
return rhs.m_begin == m_begin && return rhs.m_begin == m_begin &&
rhs.m_end == m_end; rhs.m_end == m_end;
} }
//----------------------------------------------------------------------------- ///////////////////////////////////////////////////////////////////////////////
template <typename T> template <typename T>
std::ostream& std::ostream&
util::operator<< (std::ostream &os, util::view<T> v) util::operator<< (std::ostream &os, util::view<T> v)
@ -97,7 +97,3 @@ util::operator<< (std::ostream &os, util::view<T> v)
std::copy (v.begin (), v.end (), std::ostream_iterator<char> (os)); std::copy (v.begin (), v.end (), std::ostream_iterator<char> (os));
return os; return os;
} }
///////////////////////////////////////////////////////////////////////////////
template struct util::view<const char*>;