Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-06-06 07:38:45

0001 #ifndef bstream_iterator_H
0002 #define bstream_iterator_H
0003 
0004 #include "MagneticField/Interpolation/interface/binary_ifstream.h"
0005 #include "binary_ofstream.h"
0006 #include <iterator>
0007 #include "FWCore/Utilities/interface/Visibility.h"
0008 
0009 template <typename T>
0010 class bistream_iterator : public std::iterator<std::input_iterator_tag, T, ptrdiff_t, const T*, const T&> {
0011 public:
0012   bistream_iterator() : stream_(0) {}
0013 
0014   bistream_iterator(binary_ifstream& s) : stream_(&s) { read(); }
0015 
0016   const T& operator*() const { return value_; }
0017 
0018   const T* operator->() const { return &value_; }
0019 
0020   bistream_iterator& operator++() {
0021     read();
0022     return *this;
0023   }
0024 
0025   bistream_iterator& operator++(int) {
0026     bistream_iterator tmp;
0027     read();
0028     return tmp;
0029   }
0030 
0031   bool operator==(const bistream_iterator& rhs) { return stream_ == rhs.stream_; }
0032 
0033   bool operator!=(const bistream_iterator& rhs) { return !operator==(rhs); }
0034 
0035 private:
0036   binary_ifstream* stream_;
0037   T value_;
0038 
0039   void read() {
0040     if (stream_ != 0) {
0041       // if (!(*stream_ >> value_)) stream_ = 0;
0042       if (!(*stream_ >> value_)) {
0043         stream_ = 0;
0044         // std::cout << "istream_iterator: stream turned bad, set stream_ to zero" << std::endl;
0045       }
0046     }
0047   }
0048 };
0049 
0050 template <typename T>
0051 class dso_internal bostream_iterator : public std::iterator<std::output_iterator_tag, void, void, void, void> {
0052 public:
0053   bostream_iterator(binary_ofstream& s) : stream_(&s) {}
0054 
0055   bostream_iterator& operator=(const T& t) {
0056     *stream_ << t;
0057     return *this;
0058   }
0059 
0060   bostream_iterator& operator*() { return *this; }
0061   bostream_iterator& operator++() { return *this; }
0062   bostream_iterator& operator++(int) { return *this; }
0063 
0064 private:
0065   binary_ofstream* stream_;
0066 };
0067 
0068 #endif