bpp-seq3  3.0.0
StreamSequenceIterator.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #ifndef BPP_SEQ_IO_STREAMSEQUENCEITERATOR_H
6 #define BPP_SEQ_IO_STREAMSEQUENCEITERATOR_H
7 
8 
9 #include "../SequenceIterator.h"
10 #include "ISequenceStream.h"
11 
12 // From the STL:
13 #include <istream>
14 
15 namespace bpp
16 {
20 template<class SequenceType>
22  public virtual TemplateSequenceIteratorInterface<SequenceType>
23 {
24 private:
25  std::shared_ptr<const Alphabet> alphabet_;
26  std::shared_ptr< const ISequenceStream<SequenceType>> seqStream_;
27  str::shared_ptr<std::istream> stream_;
28  SequenceType* nextSeq_;
29 
30 public:
32  std::shared_ptr<ISequenceStream> seqStream,
33  std::shared_ptr<std::istream> stream,
34  std::shared_ptr<const Alphabet> alphabet) :
35  alphabet_(alphabet),
36  seqStream_(seqStream),
37  stream_(stream),
38  nextSeq_(new SequenceType(alphabet_))
39 
40  {
41  bool test = seqStream_->nextSequence(*stream_, *nextSeq_);
42  if (!test)
43  {
44  delete nextSeq_;
45  nextSeq_ = 0; // No more sequence available
46  }
47  }
48 
50 
51 private:
52  // Recopy is forbidden
54  alphabet_(ssi.alphabet_),
56  stream_(ssi.stream_),
57  nextSeq_(0) {}
58 
60  {
61  alphabet_ = ssi.alphabet_;
62  seqStream_ = ssi.seqStream_;
63  stream_ = ssi.stream_;
64  nextSeq_ = 0;
65  return *this;
66  }
67 
68 public:
69  std::unique_ptr<SequenceType> nextSequence() override
70  {
71  unique_ptr<SequenceType> seq(nextSeq_);
72  if (nextSeq_)
73  {
74  nextSeq_ = new SequenceType(alphabet_);
75  bool test = seqStream_->nextSequence(*stream_, *nextSeq_);
76  if (!test)
77  {
78  delete nextSeq_;
79  nextSeq_ = 0; // No more sequence available
80  }
81  }
82  return seq;
83  }
84 
85  bool hasMoreSequences() const override { return nextSeq_ != 0; }
86 };
87 
88 using StreamSequenceIterator = TemplateStreamSequenceIterator<Sequence>
89  using StreamSequenceWithQualityIterator = TemplateStreamSequenceIterator<SequenceWithQuality>
90  using StreamProbabilisticSequenceIterator = TemplateStreamSequenceIterator<ProbabilisticSequence>
91 } // end of namespace bpp.
92 #endif // BPP_SEQ_IO_STREAMSEQUENCEITERATOR_H
Generic sequence iterator interface, allowing to loop over sequences.
A sequence iterator based on a sequence stream.
StreamSequenceIterator(std::shared_ptr< ISequenceStream > seqStream, std::shared_ptr< std::istream > stream, std::shared_ptr< const Alphabet > alphabet)
StreamSequenceIterator & operator=(const StreamSequenceIterator &ssi)
StreamSequenceIterator(const StreamSequenceIterator &ssi)
str::shared_ptr< std::istream > stream_
std::shared_ptr< const Alphabet > alphabet_
std::unique_ptr< SequenceType > nextSequence() override
std::shared_ptr< const ISequenceStream< SequenceType > > seqStream_
This alphabet is used to deal NumericAlphabet.