00001 #ifndef ___animal_io_h____
00002 #define ___animal_io_h____
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include <iostream>
00013 #include <utility>
00014
00015 #include <animal/container_traits.h>
00016 using std::cout;
00017 using std::cerr;
00018 using std::cin;
00019 using std::endl;
00020
00021
00022 namespace animal
00023 {
00024
00026 template<class T>
00027 struct v_Output
00028 {
00029 const T& t;
00030 char* sep;
00031
00033 v_Output( const T& t, char* s ):t(t),sep(s){}
00034 };
00035
00037 template<class T>
00038 struct v_Input
00039 {
00040 T& t;
00041
00043 v_Input( T& t):t(t){}
00044 };
00045
00046
00048 template<class T>
00049 struct m_Output
00050 {
00051 const T& t;
00052
00054 m_Output( const T& t):t(t){}
00055 };
00056
00058 template<class T>
00059 struct m_Input
00060 {
00061 T& t;
00062
00064 m_Input( T& t):t(t){}
00065 };
00066
00067
00068
00069
00071
00072
00073
00075 template<class Container>
00076 void print( const Container& container)
00077 {
00078 typename Container::const_iterator c=container.begin(),
00079 end=container.end();
00080 for( ; c!=end; ++c )
00081 std::cout << *c << " ";
00082 std::cout << endl;
00083 }
00084
00086 template<class Container>
00087 void print( const Container& container, std::ostream& out)
00088 {
00089 typename Container::const_iterator c=container.begin(),
00090 end=container.end();
00091 for( ; c!=end; ++c )
00092 out << *c << " ";
00093 out << endl;
00094 }
00095
00096
00097
00099 template<class T>
00100 v_Output<T>
00101 v_output( const T& t, char* s=" " ) { return v_Output<T>(t,s); }
00102
00104 template<class T>
00105 std::ostream& operator << (std::ostream& out, v_Output<T> ot )
00106 {
00107 for( typename container_traits<T const>::iterator i=begin(ot.t), aend=end(ot.t); i!=aend; ++i )
00108 out << *i << ot.sep;
00109 return out;
00110 }
00111
00113 template<class T>
00114 v_Input<T>
00115 v_input( T& t ) { return v_Input<T>(t); }
00116
00118 template<class T>
00119 std::istream& operator >> (std::istream& in, v_Input<T> it )
00120 {
00121 for( typename container_traits<T>::iterator i=begin(it.t), iend=end(it.t); i!=iend; ++i )
00122 in >> *i;
00123 return in;
00124 }
00125
00126
00127
00129 template<class T>
00130 m_Output<T>
00131 m_output( const T& t ) { return m_Output<T>(t); }
00132
00134 template<class T>
00135 std::ostream& operator << (std::ostream& out, m_Output<T> ot )
00136 {
00137 for( typename container_traits<T const>::iterator i=begin(ot.t), aend=end(ot.t); i!=aend; ++i )
00138 out << "\n" << v_output(*i) ;
00139 return out;
00140 }
00141
00143 template<class T>
00144 m_Input<T>
00145 m_input( T& t ) { return m_Input<T>(t); }
00146
00148 template<class T>
00149 std::istream& operator >> (std::istream& in, m_Input<T> it )
00150 {
00151 for( typename container_traits<T>::iterator i=begin(it.t), iend=end(it.t); i!=iend; ++i )
00152 in >> v_input(*i);
00153 return in;
00154 }
00155
00157
00158
00159
00161 template<class C1,class C2>
00162 std::ostream& operator << (std::ostream& out, const std::pair<C1,C2>& p )
00163 {
00164 out << "( " <<p.first<<","<<p.second<<" )";
00165 return out;
00166 }
00167
00169 template<class C1,class C2>
00170 std::istream& operator >> (std::istream& in, std::pair<C1,C2>& p )
00171 {
00172 in >> p.first >> p.second;
00173 return in;
00174 }
00175
00176 }
00177
00180 #endif