// Copyright {Jagger Software Limited} 2003 #if !defined CONTAINER_STRAND_VIEW_INCLUDED || \ defined CONTAINER_STRAND_VIEW_TEMPLATE_INCLUDED #error "container/strand_view_view-template.hpp" #included directly #endif #define CONTAINER_STRAND_VIEW_TEMPLATE_INCLUDED #include "container/range_view.hpp" #include "contract/pre_condition.hpp" #include #include #include #include namespace container // strand_view - 'tors { template strand_view::strand_view(const range_view & source) : from(source.begin) , until(source.end) { } template strand_view::strand_view(iterator start, iterator finish) : from(start) , until(finish) { } } namespace container // strand_view - capacity { template bool strand_view::empty() const { return size() == 0; } template size_t strand_view::size() const { return std::distance(begin(), end()); } } namespace container // strand_view - conversion { using std::string; template string strand_view::as_string() const { return string(begin(), end()); } } namespace container // strand_view - iteration { template iterator strand_view::begin() const { return from; } template iterator strand_view::end() const { return until; } } namespace container // strand_view - end element access { template char strand_view::front() const { PRE_CONDITION(!empty()); return begin()[0]; } template char strand_view::back() const { PRE_CONDITION(!empty()); return (*this)[size() - 1]; } } namespace container // strand_view - subscripting { template char strand_view::operator[](size_t at) const { range_check(at); return begin()[at]; } } namespace container // strand_view - validation { template void strand_view::range_check(size_t at) const { if (at >= size()) { throw ::std::out_of_range("container::strand_view<>::operator[at]"); } } } namespace container // strand_view - convenience { template strand_view make_strand_view(iterator lhs, iterator rhs) { return strand_view(lhs, rhs); } template strand_view make_strand_view(const range_view & source) { return strand_view(source.begin, source.end); } } namespace container // strand_view - streaming { template ::std::ostream & operator<<(::std::ostream & out, const strand_view & written) { for (size_t at = 0; at != written.size(); ++at) { out << written[at]; } return out; } } namespace // implementation { template bool equal(const ::container::strand_view & lhs, const ::container::strand_view & rhs) { return lhs.size() == rhs.size() && ::std::equal(lhs.begin(), lhs.end(), rhs.begin()); } template bool equal(const ::container::strand_view & lhs, const ::std::string & rhs) { return lhs.size() == rhs.size() && ::std::equal(lhs.begin(), lhs.end(), rhs.begin()); } template bool equal(const ::container::strand_view & lhs, const char * rhs) { PRE_CONDITION(rhs != 0); return lhs.size() == std::strlen(rhs) && ::std::equal(lhs.begin(), lhs.end(), rhs); } } namespace container // strand_view - in/equality { template bool operator==(const strand_view & lhs, const strand_view & rhs) { return ::equal(lhs, rhs); } template bool operator!=(const strand_view & lhs, const strand_view & rhs) { return !::equal(lhs, rhs); } template bool operator==(const strand_view & lhs, const ::std::string & rhs) { return ::equal(lhs, rhs); } template bool operator!=(const strand_view & lhs, const ::std::string & rhs) { return !::equal(lhs, rhs); } template bool operator==(const ::std::string & lhs, const strand_view & rhs) { return ::equal(rhs, lhs); } template bool operator!=(const ::std::string & lhs, const strand_view & rhs) { return !::equal(rhs, lhs); } template bool operator==(const strand_view & lhs, const char * rhs) { return ::equal(lhs, rhs); } template bool operator!=(const strand_view & lhs, const char * rhs) { return !::equal(lhs, rhs); } template bool operator==(const char * lhs, const strand_view & rhs) { return ::equal(rhs, lhs); } template bool operator!=(const char * lhs, const strand_view & rhs) { return !::equal(rhs, lhs); } }