view: add make_view, ref-qualifiers, comparators

This commit is contained in:
Danny Robson 2016-03-17 18:12:34 +11:00
parent 6fda597e07
commit 28d44593d8
5 changed files with 156 additions and 22 deletions

View File

@ -268,6 +268,7 @@ UTIL_FILES = \
vector.ipp \
version.cpp \
version.hpp \
view.cpp \
view.ipp \
view.hpp
@ -403,7 +404,8 @@ TEST_BIN = \
test/tuple \
test/uri \
test/vector \
test/version
test/version \
test/view
if PLATFORM_WIN32

18
test/view.cpp Normal file
View File

@ -0,0 +1,18 @@
#include <cruft/util/tap.hpp>
#include <cruft/util/view.hpp>
int
main (int, char**)
{
util::TAP::logger tap;
const std::string s = "this is a test string";
const std::string t = "not the same string";
tap.expect_eq (s, util::view<std::string::const_iterator> (s.cbegin (), s.cend ()), "string-const_iterator inequality comparison");
tap.expect_eq (s, util::view<const char*> (&*s.cbegin (), &*s.cend ()), "string-pointer equality comparison");
tap.expect_neq (t, util::make_view (s), "string-view inequality");
return tap.status ();
}

64
view.cpp Normal file
View File

@ -0,0 +1,64 @@
/*
* 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 2016 Danny Robson <danny@nerdcruft.net>
*/
#include "./view.hpp"
///////////////////////////////////////////////////////////////////////////////
#define EQUALITY(A,B) \
bool \
util::operator== (A a, B b) \
{ \
return a.size () == a.size () && \
std::equal (a.cbegin (), \
a.cend (), \
b.cbegin ()); \
}
EQUALITY(const std::string&, view<const char*>)
EQUALITY(const std::string&, view<char*>)
EQUALITY(const std::string&, view<std::string::const_iterator>)
EQUALITY(const std::string&, view<std::string::iterator>)
EQUALITY(view<const char*>, const std::string&)
EQUALITY(view<char*>, const std::string&)
EQUALITY(view<std::string::const_iterator>, const std::string&)
EQUALITY(view<std::string::iterator>, const std::string&)
#undef EQUALITY
///////////////////////////////////////////////////////////////////////////////
namespace util {
#define OSTREAM(A) \
template <> \
std::ostream& \
operator<< (std::ostream &os, view<A> a) { \
std::copy (a.cbegin (), \
a.cend (), \
std::ostream_iterator<decltype(a)::value_type> (os)); \
\
return os; \
}
OSTREAM(const char*)
OSTREAM(char*)
OSTREAM(std::string::const_iterator)
OSTREAM(std::string::iterator)
#undef OSTREAM
}

View File

@ -11,7 +11,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
* Copyright 2015-2016 Danny Robson <danny@nerdcruft.net>
*/
@ -30,14 +30,20 @@ namespace util {
using value_type = typename std::iterator_traits<remove_restrict_t<T>>::value_type;
template <size_t S>
constexpr view (const value_type (&arr)[S]) noexcept;
constexpr view (T first, T last) noexcept;
constexpr explicit
view (const value_type (&arr)[S]) noexcept;
constexpr T begin (void) const noexcept;
constexpr T end (void) const noexcept;
constexpr
view (T first, T last) noexcept;
constexpr T cbegin (void) const noexcept;
constexpr T cend (void) const noexcept;
constexpr T& begin (void) noexcept;
constexpr T& end (void) noexcept;
constexpr const T& begin (void) const noexcept;
constexpr const T& end (void) const noexcept;
constexpr const T& cbegin (void) const noexcept;
constexpr const T& cend (void) const noexcept;
constexpr T find (value_type) const noexcept;
@ -54,6 +60,28 @@ namespace util {
T m_end;
};
template <typename T>
view<typename T::iterator>
make_view (T&);
template <typename T>
view<typename T::const_iterator>
make_view (const T&);
template <typename T>
auto
make_view (T&&) = delete;
bool operator== (const std::string&, view<const char*>);
bool operator== (const std::string&, view<char*>);
bool operator== (const std::string&, view<std::string::const_iterator>);
bool operator== (const std::string&, view<std::string::iterator>);
bool operator== (view<const char*>, const std::string&);
bool operator== (view<char*>, const std::string&);
bool operator== (view<std::string::const_iterator>, const std::string&);
bool operator== (view<std::string::iterator>, const std::string&);
template <typename T>
std::ostream&
operator<< (std::ostream&, view<T>);

View File

@ -21,7 +21,8 @@
#define __UTIL_VIEW_IPP
#include "debug.hpp"
#include "./debug.hpp"
#include "./iterator.hpp"
///////////////////////////////////////////////////////////////////////////////
@ -45,7 +46,25 @@ util::view<T>::view (T _begin, T _end) noexcept:
//-----------------------------------------------------------------------------
template <typename T>
constexpr T
constexpr T&
util::view<T>::begin (void) noexcept
{
return m_begin;
}
//-----------------------------------------------------------------------------
template <typename T>
constexpr T&
util::view<T>::end (void) noexcept
{
return m_end;
}
//-----------------------------------------------------------------------------
template <typename T>
constexpr const T&
util::view<T>::begin (void) const noexcept
{
return cbegin ();
@ -54,7 +73,7 @@ util::view<T>::begin (void) const noexcept
//-----------------------------------------------------------------------------
template <typename T>
constexpr T
constexpr const T&
util::view<T>::end (void) const noexcept
{
return cend ();
@ -63,7 +82,7 @@ util::view<T>::end (void) const noexcept
//-----------------------------------------------------------------------------
template <typename T>
constexpr T
constexpr const T&
util::view<T>::cbegin (void) const noexcept
{
return m_begin;
@ -72,7 +91,7 @@ util::view<T>::cbegin (void) const noexcept
//-----------------------------------------------------------------------------
template <typename T>
constexpr T
constexpr const T&
util::view<T>::cend (void) const noexcept
{
return m_end;
@ -133,14 +152,17 @@ util::view<T>::operator== (const view<T> rhs) const noexcept
///////////////////////////////////////////////////////////////////////////////
template <typename T>
std::ostream&
util::operator<< (std::ostream &os, util::view<T> v)
util::view<typename T::iterator>
util::make_view (T &t)
{
os << '[';
for (auto &i: v)
os << v << ", ";
os << ']';
return os;
return util::view<typename T::iterator> { t.begin (), t.end () };
}
//-----------------------------------------------------------------------------
template <typename T>
util::view<typename T::const_iterator>
util::make_view (const T &t)
{
return util::view<typename T::const_iterator> { t.cbegin (), t.cend () };
}