CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_3/src/MagneticField/Interpolation/src/bstream_iterator.h

Go to the documentation of this file.
00001 #ifndef bstream_iterator_H
00002 #define bstream_iterator_H
00003 
00004 #include "binary_ifstream.h"
00005 #include "binary_ofstream.h"
00006 #include <iterator>
00007 #include "FWCore/Utilities/interface/Visibility.h"
00008 
00009 template <typename T>
00010 class bistream_iterator : 
00011     public std::iterator<std::input_iterator_tag, T, ptrdiff_t, const T*, const T&>
00012 {
00013 public:
00014 
00015     bistream_iterator() : stream_(0) {}
00016 
00017     bistream_iterator( binary_ifstream& s) : stream_(&s) {
00018         read();
00019     }
00020 
00021     const T& operator*() const {return value_;}
00022 
00023     const T* operator->() const {return &value_;}
00024 
00025     bistream_iterator& operator++() {read(); return *this;}
00026 
00027     bistream_iterator& operator++(int) {
00028         bistream_iterator tmp;
00029         read();
00030         return tmp;
00031     }
00032 
00033     bool operator==(const bistream_iterator& rhs) {
00034         return stream_ == rhs.stream_;
00035     }
00036 
00037     bool operator!=(const bistream_iterator& rhs) {
00038         return !operator==(rhs);
00039     }
00040 
00041 private:
00042 
00043     binary_ifstream* stream_;
00044     T               value_;
00045 
00046     void read() {
00047         if (stream_ != 0) {
00048             // if (!(*stream_ >> value_)) stream_ = 0;
00049             if (!(*stream_ >> value_)) {
00050                 stream_ = 0;
00051                 // std::cout << "istream_iterator: stream turned bad, set stream_ to zero" << std::endl;
00052             }
00053         }
00054     }
00055 
00056 };
00057 
00058 template <typename T> 
00059 class dso_internal bostream_iterator :
00060     public std::iterator<std::output_iterator_tag,void,void,void,void> {
00061 public:
00062 
00063     bostream_iterator( binary_ofstream& s) : stream_(&s) {}
00064 
00065     bostream_iterator& operator=( const T& t) {
00066         *stream_ << t;
00067         return *this;
00068     }
00069 
00070     bostream_iterator& operator*() {return *this;}
00071     bostream_iterator& operator++() {return *this;}
00072     bostream_iterator& operator++(int) {return *this;}
00073 
00074 private:
00075 
00076     binary_ofstream* stream_;
00077 
00078 };
00079 
00080 #endif