30 #ifndef binarystream_h__
31 #define binarystream_h__
33 #define HAVE_BOOST_IOSTREAMS
34 #ifdef HAVE_BOOST_IOSTREAMS
42 #include <boost/type_traits.hpp>
48 template<
typename _CharT,
typename _Traits = std::
char_traits<_CharT> >
52 template<
typename _CharT,
typename _Traits = std::
char_traits<_CharT> >
56 template<
typename _CharT,
typename _Traits = std::
char_traits<_CharT> >
69 template<
typename T,
typename A>
class vector;
70 template<
typename T,
typename A>
class deque;
71 template<
typename T,
typename A>
class list;
74 template<
typename T,
typename C,
typename A>
class set;
75 template<
typename T,
typename C,
typename A>
class multiset;
76 template<
typename K,
typename V,
typename C,
typename A>
class map;
77 template<
typename K,
typename V,
typename C,
typename A>
class multimap;
80 template<
typename T>
class valarray;
89 template <
typename First,
typename Second>
90 class is_pod< ::std::pair<First, Second> > :
91 public ::boost::integral_constant<bool,
92 is_pod<First>::value && is_pod<Second>::value
112 datatype_info(
const std::string &_name,
int _type_size,
bool _ispod)
113 : name(_name), type_size(_type_size), ispod(_ispod)
116 : name(di.name), type_size(di.type_size), ispod(di.ispod)
124 inline std::ostream &
operator<<(std::ostream &out,
const datatype_info &di);
126 typedef std::vector<datatype_info> manifest_t;
143 static bool passed =
false;
146 std::string tname = peyton::util::type_name<T>();
147 typedef typename ::boost::is_pod<T> pod;
159 std::cerr <<
"WARNING: Binary I/O as POD for type '" << peyton::util::type_name<T>() <<
"'\n";
169 #define BLESS_POD(T) \
172 class is_pod<T> : public true_type \
180 template<
typename _CharT,
typename _Traits>
189 binary::add_to_manifest<X>();
190 this->write(reinterpret_cast<const char *>(v), n*
sizeof(X));
194 explicit basic_obstream(std::basic_streambuf<_CharT, _Traits> *sb)
195 : std::basic_ostream<_CharT, _Traits>(sb)
198 : std::basic_ostream<_CharT, _Traits>(out.rdbuf())
203 template<
typename _CharT,
typename _Traits>
212 binary::add_to_manifest<X>();
213 this->read((
char *)(v), n*
sizeof(X));
217 explicit basic_ibstream(std::basic_streambuf<_CharT, _Traits> *sb)
218 :
std::basic_istream<_CharT, _Traits>(sb)
221 :
std::basic_istream<_CharT, _Traits>(in.rdbuf())
226 template<
typename _CharT,
typename _Traits>
232 explicit basic_bstream(std::basic_streambuf<_CharT, _Traits> *sb)
238 explicit basic_bstream(std::basic_iostream<_CharT, _Traits> &io)
254 #define BOSTREAM2(...) peyton::io::obstream& operator<<(peyton::io::obstream &out, __VA_ARGS__)
255 #define BISTREAM2(...) peyton::io::ibstream& operator>>(peyton::io::ibstream &in, __VA_ARGS__)
257 #define BOSTREAM2(T...) peyton::io::obstream& operator<<(peyton::io::obstream &out, T)
258 #define BISTREAM2(T...) peyton::io::ibstream& operator>>(peyton::io::ibstream &in, T)
268 return out.write_pod(&v, 1);
272 inline BISTREAM2(T &v)
274 return in.read_pod(&v, 1);
282 #define RETFAIL(x) if(!(x)) return in;
287 size_t len = strlen(v);
289 return out.write_pod(v, len);
297 return out.write_pod(v.c_str(), v.size());
302 inline BISTREAM2(std::string &v)
308 char *buf = (
char*)alloca(len+1);
310 RETFAIL(in.read_pod(buf, len));
319 template <
typename IT>
320 inline obstream& itwrite(
obstream &out,
unsigned int size, IT start, const ::boost::false_type&)
322 for(IT i = start; size != 0; --size, ++i) { out << *i; }
326 template <
typename IT>
327 inline obstream& itwrite(
obstream &out,
unsigned int size, IT start, const ::boost::true_type&)
329 return out.write_pod(start, size);
339 template <
typename IT>
348 typedef ::boost::is_pointer<IT> is_simple;
349 typedef ::boost::is_pod<typename std::iterator_traits<IT>::value_type> is_podd;
350 typedef ::boost::integral_constant<bool,
351 is_simple::value && is_podd::value
354 return details::itwrite(out, size, start, is_optimizable());
360 template <
typename C>
368 typename C::value_type tmp;
372 a.insert(a.end(), tmp);
380 template <
typename C>
387 typedef ::boost::is_pod<typename C::value_type> is_podd;
390 in.read_pod(&a[0], size);
394 for(
int i = 0; i != size; ++i) { in >> a[i]; }
402 template <
typename C>
410 typename C::key_type key;
425 template<
typename First,
typename Second>
426 inline BOSTREAM2(
const std::pair<First, Second> &v) {
return out << v.first << v.second; }
428 template <
typename T,
typename A>
429 inline BOSTREAM2(
const std::vector<T, A> &a) {
return itwrite(out, a.size(), &a[0]); }
430 template <
typename T,
typename A>
431 inline BOSTREAM2(
const std::deque<T, A> &a) {
return itwrite(out, a.size(), a.begin()); }
432 template <
typename T,
typename A>
433 inline BOSTREAM2(
const std::list<T, A> &a) {
return itwrite(out, a.size(), a.begin()); }
435 template <
typename T,
typename C,
typename A>
436 inline BOSTREAM2(
const std::set<T, C, A> &a) {
return itwrite(out, a.size(), a.begin()); }
437 template <
typename T,
typename C,
typename A>
438 inline BOSTREAM2(
const std::multiset<T, C, A> &a) {
return itwrite(out, a.size(), a.begin()); }
440 template <
typename K,
typename V,
typename C,
typename A>
441 inline BOSTREAM2(
const std::map<K, V, C, A> &a) {
return itwrite(out, a.size(), a.begin()); }
442 template <
typename K,
typename V,
typename C,
typename A>
443 inline BOSTREAM2(
const std::multimap<K, V, C, A> &a) {
return itwrite(out, a.size(), a.begin()); }
445 template <
typename T>
446 inline BOSTREAM2(
const std::valarray<T> &a) {
return itwrite(out, a.size(), &a[0]); }
451 template<
typename First,
typename Second>
452 inline BISTREAM2(std::pair<First, Second> &v) {
return in >> v.first >> v.second; }
454 template <
typename T,
typename A>
455 inline BISTREAM2(std::vector<T, A> &a) {
return itreadvec(in, a); }
456 template <
typename T,
typename A>
457 inline BISTREAM2(std::deque<T, A> &a) {
return itread(in, a); }
458 template <
typename T,
typename A>
459 inline BISTREAM2(std::list<T, A> &a) {
return itread(in, a); }
461 template <
typename T,
typename C,
typename A>
462 inline BISTREAM2(std::set<T, C, A> &a) {
return itread(in, a); }
463 template <
typename T,
typename C,
typename A>
464 inline BISTREAM2(std::multiset<T, C, A> &a) {
return itread(in, a); }
466 template <
typename K,
typename V,
typename C,
typename A>
467 inline BISTREAM2(std::map<K, V, C, A> &a) {
return itreadmap(in, a); }
468 template <
typename K,
typename V,
typename C,
typename A>
469 inline BISTREAM2(std::multimap<K, V, C, A> &a) {
return itreadmap(in, a); }
471 template <
typename T>
472 inline BISTREAM2(std::valarray<T> &a) {
return itreadvec(in, a); }
477 #endif // HAVE_BOOST_IOSTREAMS
479 using namespace peyton::io;
481 #endif // binarystream_h__