Simulant  21.12-1292
A portable game engine for Windows, OSX, Linux, Dreamcast, and PSP
stream_view.h
1 #pragma once
2 
3 #include <memory>
4 #include <istream>
5 
6 namespace smlt {
7 
8 /* StreamView is like a proxy to an underlying input stream. You can
9  * have multiple StreamViews sharing the same input stream, and each maintains
10  * its position in the stream for read operations.
11  *
12  * This is mainly for dealing with multiple audio streams using the same
13  * underlying audio file.
14  *
15  * This is a pseudo-istream. Perhaps at some point we can convert this to an
16  * actual istream/streambuf.
17  */
18 
19 class StreamView {
20 public:
21  StreamView(std::shared_ptr<std::istream> stream):
22  stream_(stream) {
23 
24  auto g = stream_->tellg();
25  stream_->seekg(0, std::ios_base::end);
26  stream_size_ = stream_->tellg();
27  stream_->seekg(g, std::ios_base::beg);
28  }
29 
30  StreamView& seekg(std::streamoff pos, std::ios_base::seekdir way) {
31  if(way == std::ios_base::beg) {
32  cursor_ = pos;
33  } else if(way == std::ios_base::cur) {
34  cursor_ += pos;
35  } else {
36  cursor_ = stream_size_ - pos;
37  }
38 
39  return *this;
40  }
41 
42  StreamView& read(char* s, std::streamsize n) {
43  auto g = stream_->tellg(); // push
44 
45  stream_->seekg(cursor_);
46  stream_->read(s, n);
47  cursor_ += n;
48 
49  stream_->seekg(g); // pop
50  return *this;
51  }
52 
53 private:
54  std::shared_ptr<std::istream> stream_;
55  std::streampos cursor_;
56  std::size_t stream_size_;
57 };
58 
59 
60 }
smlt::StreamView
Definition: stream_view.h:19
smlt
Definition: animation.cpp:25