Commit 476c3825 authored by Martin Marinov's avatar Martin Marinov

Add a general method to stream containers with IOutputStream using iterators

parent e1610d0f
......@@ -53,18 +53,24 @@ public:
virtual IOutputStream& print(const Command&) = 0;
/*! Print an array of ElementT */
template <typename ElementT>
inline IOutputStream& print(const size_t _nmbr, const ElementT* const _elems)
template <typename IteratorT>
inline IOutputStream& print(const IteratorT _bgn_it, const IteratorT _end_it)
{
print("[ ");
for (size_t i = 0; i < _nmbr; ++i)
for (auto it = _bgn_it; it != _end_it; ++it)
{
*this << _elems[i];
*this << *it;
print(' ');
}
print(']');
return *this;
}
template <typename ElementT>
inline IOutputStream& print(const size_t _nmbr, const ElementT* const _elems)
{
return print(_elems, _elems + _nmbr);
}
};
BASEDLLEXPORT IOutputStream& operator<<(IOutputStream& _os, const int _i);
......@@ -90,12 +96,15 @@ BASEDLLEXPORT IOutputStream& operator<<(IOutputStream& _os, const Command& _co);
BASEDLLEXPORT IOutputStream& operator<<(IOutputStream& _os, const Command::Type _cmd_type);
//! IStream operator for std::vector<>
template <typename ElementT>
IOutputStream& operator<<(IOutputStream& _os, const std::vector<ElementT>& _vec)
{
return _vec.empty() ? _os.print(0, (ElementT*)nullptr) :
_os.print(_vec.size(), &_vec[0]);
}
#define BASE_STREAM_CONTAINER(CONTAINER) \
template <typename ElementT> \
Base::IOutputStream& operator<<( \
Base::IOutputStream& _os, const CONTAINER<ElementT>& _cntr) \
{ \
return _os.print(_cntr.begin(), _cntr.end()); \
}
BASE_STREAM_CONTAINER(std::vector);
#ifdef STD_ARRAY_AVAILABLE
//! IStream operator for std::array<>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment