BWAPI
|
00001 // Copyright (c) 1997 Utrecht University (The Netherlands), 00002 // ETH Zurich (Switzerland), Freie Universitaet Berlin (Germany), 00003 // INRIA Sophia-Antipolis (France), Martin-Luther-University Halle-Wittenberg 00004 // (Germany), Max-Planck-Institute Saarbruecken (Germany), RISC Linz (Austria), 00005 // and Tel-Aviv University (Israel). All rights reserved. 00006 // 00007 // This file is part of CGAL (www.cgal.org); you can redistribute it and/or 00008 // modify it under the terms of the GNU Lesser General Public License as 00009 // published by the Free Software Foundation; version 2.1 of the License. 00010 // See the file LICENSE.LGPL distributed with CGAL. 00011 // 00012 // Licensees holding a valid commercial license may use this file in 00013 // accordance with the commercial license agreement provided with the software. 00014 // 00015 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 00016 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 00017 // 00018 // $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.5-branch/iostream/include/CGAL/IO/io.h $ 00019 // $Id: io.h 40468 2007-09-27 08:10:36Z slimbach $ 00020 // 00021 // 00022 // Author(s) : Andreas Fabri 00023 00024 00025 #ifndef CGAL_IO_H 00026 #define CGAL_IO_H 00027 00028 #include <iostream> 00029 #include <CGAL/tags.h> 00030 #include <CGAL/IO/io_tags.h> 00031 #include <CGAL/IO/Color.h> 00032 00033 00034 CGAL_BEGIN_NAMESPACE 00035 00036 class IO { 00037 public: 00038 static int mode; 00039 enum Mode {ASCII = 0, PRETTY, BINARY}; 00040 }; 00041 00042 template <class T, class F = ::CGAL::Null_tag > 00043 class Output_rep { 00044 const T& t; 00045 public: 00047 Output_rep( const T& tt) : t(tt) {} 00049 std::ostream& operator()( std::ostream& out) const { return (out << t); } 00050 }; 00051 00055 template <class T, class F> 00056 std::ostream& 00057 operator<<( std::ostream& out, Output_rep<T,F> rep) { 00058 return rep( out); 00059 } 00060 00062 template <class T> 00063 Output_rep<T> 00064 oformat( const T& t) { return Output_rep<T>(t); } 00065 00067 template <class T, class F> 00068 Output_rep<T,F> 00069 oformat( const T& t, F) { return Output_rep<T,F>(t); } 00070 00071 00072 00080 template <class T> 00081 class Input_rep { 00082 T& t; 00083 public: 00085 Input_rep( T& tt) : t(tt) {} 00087 std::istream& operator()( std::istream& in) const { return (in >> t); } 00088 }; 00089 00093 template <class T> 00094 std::istream& 00095 operator>>( std::istream& in, Input_rep<T> rep) { 00096 return rep( in); 00097 } 00098 00100 template <class T> 00101 Input_rep<T> 00102 iformat( T& t) { return Input_rep<T>(t); } 00103 00104 00105 template <class T, class F = Null_tag > 00106 class Benchmark_rep { 00107 const T& t; 00108 public: 00110 Benchmark_rep( const T& tt) : t(tt) {} 00112 std::ostream& operator()( std::ostream& out) const { 00113 return out << t; 00114 } 00115 00116 // static function to get the benchmark name 00117 static std::string get_benchmark_name() { 00118 return ""; 00119 } 00120 }; 00121 00122 template <class T, class F> 00123 std::ostream& operator<<( std::ostream& out, Benchmark_rep<T,F> rep) { 00124 return rep( out); 00125 } 00126 00127 template <class T> 00128 Benchmark_rep<T> bmformat( const T& t) { return Benchmark_rep<T>(t); } 00129 00130 template <class T, class F> 00131 Benchmark_rep<T,F> bmformat( const T& t, F) { return Benchmark_rep<T,F>(t); } 00132 00133 00134 00135 IO::Mode 00136 get_mode(std::ios& i); 00137 00138 IO::Mode 00139 set_ascii_mode(std::ios& i); 00140 00141 IO::Mode 00142 set_binary_mode(std::ios& i); 00143 00144 IO::Mode 00145 set_pretty_mode(std::ios& i); 00146 00147 IO::Mode 00148 set_mode(std::ios& i, IO::Mode m); 00149 00150 bool 00151 is_pretty(std::ios& i); 00152 00153 bool 00154 is_ascii(std::ios& i); 00155 00156 bool 00157 is_binary(std::ios& i); 00158 00159 template < class T > 00160 inline 00161 void 00162 write(std::ostream& os, const T& t, const io_Read_write&) 00163 { 00164 os.write(reinterpret_cast<const char*>(&t), sizeof(t)); 00165 } 00166 00167 00168 template < class T > 00169 inline 00170 void 00171 write(std::ostream& os, const T& t, const io_Operator&) 00172 { 00173 os << oformat(t); 00174 } 00175 00176 00177 template < class T > 00178 inline 00179 void 00180 write(std::ostream& os, const T& t, const io_Extract_insert&) 00181 { 00182 insert(os, t); 00183 } 00184 00185 00186 template < class T > 00187 inline 00188 void 00189 write(std::ostream& os, const T& t) 00190 { 00191 write(os, t, typename Io_traits<T>::Io_tag()); 00192 } 00193 00194 00195 template < class T > 00196 inline 00197 void 00198 read(std::istream& is, T& t, const io_Read_write&) 00199 { 00200 is.read(reinterpret_cast<char*>(&t), sizeof(t)); 00201 } 00202 00203 00204 template < class T > 00205 inline 00206 void 00207 read(std::istream& is, T& t, const io_Operator&) 00208 { 00209 is >> iformat(t); 00210 } 00211 00212 00213 template < class T > 00214 inline 00215 void 00216 read(std::istream& is, T& t, const io_Extract_insert&) 00217 { 00218 extract(is, t); 00219 } 00220 00221 00222 template < class T > 00223 inline 00224 void 00225 read(std::istream& is, T& t) 00226 { 00227 read(is, t, typename Io_traits<T>::Io_tag()); 00228 } 00229 00230 00231 inline 00232 std::ostream& operator<<( std::ostream& out, const Color& col) 00233 { 00234 switch(out.iword(IO::mode)) { 00235 case IO::ASCII : 00236 return out << static_cast<int>(col.red()) << ' ' 00237 << static_cast<int>(col.green()) << ' ' 00238 << static_cast<int>(col.blue()); 00239 case IO::BINARY : 00240 write(out, static_cast<int>(col.red())); 00241 write(out, static_cast<int>(col.green())); 00242 write(out, static_cast<int>(col.blue())); 00243 return out; 00244 default: 00245 return out << "Color(" << static_cast<int>(col.red()) << ", " 00246 << static_cast<int>(col.green()) << ", " 00247 << static_cast<int>(col.blue()) << ')'; 00248 } 00249 } 00250 00251 inline 00252 std::istream &operator>>(std::istream &is, Color& col) 00253 { 00254 int r, g, b; 00255 switch(is.iword(IO::mode)) { 00256 case IO::ASCII : 00257 is >> r >> g >> b; 00258 break; 00259 case IO::BINARY : 00260 read(is, r); 00261 read(is, g); 00262 read(is, b); 00263 break; 00264 default: 00265 std::cerr << "" << std::endl; 00266 std::cerr << "Stream must be in ascii or binary mode" << std::endl; 00267 break; 00268 } 00269 col = Color(r,g,b); 00270 return is; 00271 } 00272 00273 const char* mode_name( IO::Mode m ); 00274 00275 // From polynomial.h TODO: Where to put this? 00276 void swallow(std::istream &is, char d); 00277 void swallow(std::istream &is, const std::string& s ); 00278 00279 00280 00281 CGAL_END_NAMESPACE 00282 00283 #endif // CGAL_IO_H