diff --git a/view.hpp b/view.hpp index 8384b869..6b4fedae 100644 --- a/view.hpp +++ b/view.hpp @@ -48,7 +48,12 @@ namespace util { //--------------------------------------------------------------------- - template ,void>> + // cosntruction from pointer/size represenations for ease of use with + // legacy C code. + template < + typename CountT, + typename = std::enable_if_t,void> + > view ( const BeginT &_begin, CountT _size @@ -58,11 +63,12 @@ namespace util { //--------------------------------------------------------------------- + // implicit conversion from pointer views to const pointer views template < typename ValueT, typename = std::enable_if_t< std::is_same_v && - std::is_same_v + std::is_same_v > > view (const view &rhs): @@ -76,21 +82,51 @@ namespace util { // accidentally include the trailing null in the data. template view (const char (&value)[N]): - view (std::begin (value), std::begin (value) + N - 1) + view {std::begin (value), std::begin (value) + N - 1} { static_assert (N > 0); } + + //--------------------------------------------------------------------- + view (const char *str): + view { str, str + strlen (str) } + { ; } + + + //--------------------------------------------------------------------- + view (char *str): + view (str, str + strlen (str)) + { ; } + + + //--------------------------------------------------------------------- + template + view (char (&value)[N]): + view {std::begin (value), std::begin (value) + N - 1} + { + static_assert (N > 0); + } + + //--------------------------------------------------------------------- template view (const ValueT(&value)[N]): - view (std::begin (value), std::end (value)) + view {std::begin (value), std::end (value)} { ; } + + //--------------------------------------------------------------------- + template + view (ValueT(&value)[N]): + view {std::begin (value), std::end (value)} + { ; } + + //--------------------------------------------------------------------- constexpr view (const view &rhs) noexcept: - view (rhs.m_begin, rhs.m_end) + view {rhs.m_begin, rhs.m_end} { ; } @@ -100,7 +136,7 @@ namespace util { // class as a base for unique owning pointers without exposing the // begin/end data members to them directly. constexpr view (view &&rhs) noexcept: - view (std::move (rhs.m_begin), std::move (rhs.m_end)) + view {std::move (rhs.m_begin), std::move (rhs.m_end)} { ; } @@ -124,33 +160,23 @@ namespace util { //--------------------------------------------------------------------- - // non-contigous containers should use their begin/end iterators - // directly - template < - typename ContainerT, - typename std::enable_if_t,ContainerT*> = nullptr - > - constexpr explicit - view ( - ContainerT &klass - ): - view (std::begin (klass), std::end (klass)) + template + view (const std::basic_string &val): + view (std::data (val), std::data (val) + std::size (val)) { ; } //--------------------------------------------------------------------- - // contiguous containers are often used with pointers to their contents - // for other operations so we directly construct them using pointers to - // their data for user convenience. - template < - typename ContainerT, - typename std::enable_if_t,ContainerT*> = nullptr - > - constexpr explicit - view ( - ContainerT &klass - ): - view (std::data (klass), std::data (klass) + std::size (klass)) + template + view (const std::vector &rhs): + view (std::data (rhs), std::data (rhs) + std::size (rhs)) + { ; } + + + //--------------------------------------------------------------------- + template + view (std::vector &rhs): + view (std::data (rhs), std::data (rhs) + std::size (rhs)) { ; } @@ -280,6 +306,12 @@ namespace util { view (const ValueT(&)[N]) -> view; + //------------------------------------------------------------------------- + view (const char*) -> view; + + view (char*) -> view; + + //------------------------------------------------------------------------- template < typename IteratorT, @@ -291,28 +323,19 @@ namespace util { view (IteratorT, SizeT) -> view; - //------------------------------------------------------------------------- - template < - typename ContainerT, - typename = std::enable_if_t, void> - > - view (ContainerT&) -> view< - typename ContainerT::value_type*, - typename ContainerT::value_type* - >; + template + view (std::basic_string &) -> view; - //------------------------------------------------------------------------- - template < - typename ContainerT, - typename = std::enable_if_t, void> - > - view (ContainerT&) -> view< - decltype (std::begin (std::declval ())), - decltype (std::end (std::declval ())) - >; + template + view (const std::basic_string &) -> view; + + template + view (std::vector&) -> view; + template + view (const std::vector&) -> view; /////////////////////////////////////////////////////////////////////////// @@ -478,13 +501,13 @@ namespace util { /////////////////////////////////////////////////////////////////////////// - template + template std::ostream& - operator<< (std::ostream &os, view val) + operator<< (std::ostream &os, view val) { std::copy ( std::cbegin (val), - std::cend (val), + std::cend (val), std::ostream_iterator (os) );