// // Copyright (c) 2002 by Stephen C. Dewhurst // Permission to use, copy, modify, distribute and sell this software for any purpose is // hereby granted without fee, provided that the above copyright notice appears in all copies // and that both that copyright notice and this permission notice appear in supporting documentation. // The author or makes no representations about the suitability of this software for any purpose. // It is provided "as is" without express or implied warranty. // // MultiOut // // Described in CUJ 20(2), February 2002: "Output Iterator Adapters" // MultiOut is an output iterator adapter that allows an output sequence to be sent to an // unbounded number of output streams simultaneously. #ifndef MULTIOUT_H #define MULTIOUT_H #include template class MultiOut : public std::iterator::value_type,void> { public: typedef typename std::iterator_traits::value_type value_type; MultiOut( Out1 a, Out2 b ) : _a(a), _b(b) {} MultiOut &operator ++() { ++_a; ++_b; return *this; } MultiOut operator ++(int) { MultiOut tmp( *this ); ++*this; return tmp; } MultiOut &operator =( const value_type &v ) { *_a = v; *_b = v; return *this; } bool operator ==( const MultiOut &that ) const { return _a == that._a && _b == that._b; } bool operator !=( const MultiOut &that ) const { return !(*this == that); } MultiOut &operator *() { return *this; } private: Out1 _a; Out2 _b; }; // Small change in helper functions from presentation in article: // This version "propagates" the value_type of the first iterator type // so that subsequent iterators do not have to define a value type. // (Note that inserters, ostream_iterators, etc. often do not define // a value type. This is legal but unfortunate.) struct X { template struct M2 { typedef MultiOut T; }; template struct M3 { typedef MultiOut::T,Out3> T; }; template struct M4 { typedef MultiOut::T,Out4> T; }; }; template MultiOut multiOut( Out1 a, Out2 b ) { return MultiOut( a, b ); } template typename X::M3::T multiOut( Out1 a, Out2 b, Out3 c ) { return X::M3::T( multiOut(a,b), c ); } template typename X::M4::T multiOut( Out1 a, Out2 b, Out3 c, Out4 d ) { return X::M4::T( multiOut(a,b,c), d ); } #endif