view: move ostream operator into header

This commit is contained in:
Danny Robson 2017-12-26 17:30:41 +11:00
parent dc51fa2ed0
commit a67069a333
2 changed files with 17 additions and 35 deletions

View File

@ -11,37 +11,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2016-2017 Danny Robson <danny@nerdcruft.net>
* Copyright 2017 Danny Robson <danny@nerdcruft.net>
*/
#include "view.hpp"
#include <algorithm>
#include <iterator>
///////////////////////////////////////////////////////////////////////////////
namespace util {
#define OSTREAM(A) \
template <> \
std::ostream& \
operator<< (std::ostream &os, view<A> a) { \
std::copy (a.cbegin (), \
std::find (a.cbegin (), a.cend (), '\0'), \
std::ostream_iterator<decltype(a)::value_type> (os)); \
\
return os; \
}
OSTREAM(const char*)
OSTREAM(char*)
OSTREAM( signed char*)
OSTREAM(unsigned char*)
OSTREAM(const signed char*)
OSTREAM(const unsigned char*)
OSTREAM(std::string::const_iterator)
OSTREAM(std::string::iterator)
#undef OSTREAM
}

View File

@ -27,6 +27,7 @@
#include <string>
#include <cstring>
#include <stdexcept>
#include <iterator>
namespace util {
template <typename IteratorA, typename IteratorB = IteratorA>
@ -356,6 +357,21 @@ namespace util {
}
///////////////////////////////////////////////////////////////////////////
template <typename IteratorT>
std::ostream&
operator<< (std::ostream &os, view<IteratorT> val)
{
std::copy (
std::cbegin (val),
std::cend (val),
std::ostream_iterator<typename decltype(val)::value_type> (os)
);
return os;
}
///////////////////////////////////////////////////////////////////////////
/// a basic stringlike comparison operator that behaves as
/// std::string::compare would.
@ -378,10 +394,6 @@ namespace util {
}
///////////////////////////////////////////////////////////////////////////
template <typename IteratorT>
std::ostream&
operator<< (std::ostream&, view<IteratorT>);
}