3902 |
05 May 20 |
peter |
1 |
#ifndef _theplu_yat_utility_binary_istream_iterator_ |
3902 |
05 May 20 |
peter |
2 |
#define _theplu_yat_utility_binary_istream_iterator_ |
3902 |
05 May 20 |
peter |
3 |
|
3902 |
05 May 20 |
peter |
// $Id$ |
3902 |
05 May 20 |
peter |
5 |
|
3902 |
05 May 20 |
peter |
6 |
/* |
3902 |
05 May 20 |
peter |
Copyright (C) 2020 Peter Johansson |
3902 |
05 May 20 |
peter |
8 |
|
3902 |
05 May 20 |
peter |
This file is part of the yat library, http://dev.thep.lu.se/yat |
3902 |
05 May 20 |
peter |
10 |
|
3902 |
05 May 20 |
peter |
The yat library is free software; you can redistribute it and/or |
3902 |
05 May 20 |
peter |
modify it under the terms of the GNU General Public License as |
3902 |
05 May 20 |
peter |
published by the Free Software Foundation; either version 3 of the |
3902 |
05 May 20 |
peter |
License, or (at your option) any later version. |
3902 |
05 May 20 |
peter |
15 |
|
3902 |
05 May 20 |
peter |
The yat library is distributed in the hope that it will be useful, |
3902 |
05 May 20 |
peter |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
3902 |
05 May 20 |
peter |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3902 |
05 May 20 |
peter |
General Public License for more details. |
3902 |
05 May 20 |
peter |
20 |
|
3902 |
05 May 20 |
peter |
You should have received a copy of the GNU General Public License |
3902 |
05 May 20 |
peter |
along with yat. If not, see <http://www.gnu.org/licenses/>. |
3902 |
05 May 20 |
peter |
23 |
*/ |
3902 |
05 May 20 |
peter |
24 |
|
3902 |
05 May 20 |
peter |
25 |
#include "utility.h" |
3902 |
05 May 20 |
peter |
26 |
|
3902 |
05 May 20 |
peter |
27 |
#include <boost/iterator/iterator_facade.hpp> |
3902 |
05 May 20 |
peter |
28 |
|
3902 |
05 May 20 |
peter |
29 |
#include <iterator> |
3902 |
05 May 20 |
peter |
30 |
|
3902 |
05 May 20 |
peter |
31 |
namespace theplu { |
3902 |
05 May 20 |
peter |
32 |
namespace yat { |
3902 |
05 May 20 |
peter |
33 |
namespace utility { |
3902 |
05 May 20 |
peter |
34 |
|
3902 |
05 May 20 |
peter |
35 |
/** |
3902 |
05 May 20 |
peter |
An Iterator that can be used to extract data in binary format |
3902 |
05 May 20 |
peter |
from a stream into a range of \c T. BinaryIstreamIterator models |
3902 |
05 May 20 |
peter |
an \input_iterator and an \lvalue_iterator (\c reference_type is |
3902 |
05 May 20 |
peter |
\c const \c T&). |
3902 |
05 May 20 |
peter |
40 |
|
3902 |
05 May 20 |
peter |
\c T is expected to be a native numerical type. |
3902 |
05 May 20 |
peter |
42 |
|
3902 |
05 May 20 |
peter |
\since New in yat 0.18 |
3902 |
05 May 20 |
peter |
44 |
*/ |
3902 |
05 May 20 |
peter |
45 |
template<typename T> |
3902 |
05 May 20 |
peter |
46 |
class BinaryIstreamIterator |
3902 |
05 May 20 |
peter |
47 |
: public boost::iterator_facade< |
3902 |
05 May 20 |
peter |
48 |
BinaryIstreamIterator<T>, const T, std::input_iterator_tag |
3902 |
05 May 20 |
peter |
49 |
> |
3902 |
05 May 20 |
peter |
50 |
{ |
3902 |
05 May 20 |
peter |
51 |
public: |
3902 |
05 May 20 |
peter |
52 |
/** |
3902 |
05 May 20 |
peter |
Create an end-of-stream iterator. Note, iterator is not |
3902 |
05 May 20 |
peter |
dereferencable. |
3902 |
05 May 20 |
peter |
55 |
*/ |
3902 |
05 May 20 |
peter |
56 |
BinaryIstreamIterator(void) |
3902 |
05 May 20 |
peter |
57 |
: istream_(NULL) {} |
3902 |
05 May 20 |
peter |
58 |
|
3902 |
05 May 20 |
peter |
59 |
/** |
3902 |
05 May 20 |
peter |
\brief Constructor |
3902 |
05 May 20 |
peter |
61 |
|
3902 |
05 May 20 |
peter |
\param is istream to read data from. |
3902 |
05 May 20 |
peter |
63 |
*/ |
3902 |
05 May 20 |
peter |
64 |
explicit BinaryIstreamIterator(std::istream& is) |
3902 |
05 May 20 |
peter |
65 |
: istream_(&is) |
3902 |
05 May 20 |
peter |
66 |
{ |
3902 |
05 May 20 |
peter |
67 |
increment(); |
3902 |
05 May 20 |
peter |
68 |
} |
3902 |
05 May 20 |
peter |
69 |
|
3902 |
05 May 20 |
peter |
70 |
private: |
3902 |
05 May 20 |
peter |
71 |
friend class boost::iterator_core_access; |
3902 |
05 May 20 |
peter |
72 |
|
3902 |
05 May 20 |
peter |
73 |
std::istream* istream_; |
3902 |
05 May 20 |
peter |
74 |
T value_; |
3902 |
05 May 20 |
peter |
75 |
|
3902 |
05 May 20 |
peter |
76 |
typename BinaryIstreamIterator<T>::reference dereference(void) const |
3902 |
05 May 20 |
peter |
77 |
{ return value_; } |
3902 |
05 May 20 |
peter |
78 |
|
3902 |
05 May 20 |
peter |
79 |
bool equal(const BinaryIstreamIterator<T>& other) const |
3902 |
05 May 20 |
peter |
80 |
{ return istream_ == other.istream_; } |
3902 |
05 May 20 |
peter |
81 |
|
3902 |
05 May 20 |
peter |
82 |
void increment(void) |
3902 |
05 May 20 |
peter |
83 |
{ |
3902 |
05 May 20 |
peter |
84 |
if (!istream_) |
3902 |
05 May 20 |
peter |
85 |
return; |
3902 |
05 May 20 |
peter |
86 |
if (!binary_read(*istream_, value_)) { |
3902 |
05 May 20 |
peter |
87 |
istream_ = NULL; |
3902 |
05 May 20 |
peter |
88 |
} |
3902 |
05 May 20 |
peter |
89 |
} |
3902 |
05 May 20 |
peter |
// Using compiler generated copy |
3902 |
05 May 20 |
peter |
//BinaryIstreamIterator(const BinaryIstreamIterator&); |
3902 |
05 May 20 |
peter |
//BinaryIstreamIterator& operator=(const BinaryIstreamIterator&); |
3902 |
05 May 20 |
peter |
93 |
}; |
3902 |
05 May 20 |
peter |
94 |
|
3902 |
05 May 20 |
peter |
95 |
}}} // of namespace utility, yat, and theplu |
3902 |
05 May 20 |
peter |
96 |
|
3902 |
05 May 20 |
peter |
97 |
#endif |