View on GitHub

Vistle

Distributed Data-parallel Scientific Visualization in VR

vecstreambuf.h
Go to the documentation of this file.
1#ifndef VECSTREAMBUF_H
2#define VECSTREAMBUF_H
3
4#include <streambuf>
5#include <vector>
6#include <cstring>
8
9namespace vistle {
10
11template<typename Vector, typename TraitsT = std::char_traits<typename Vector::value_type>>
12class vecostreambuf: public std::basic_streambuf<typename Vector::value_type, TraitsT> {
13public:
15
16 int overflow(int ch)
17 {
18 if (ch != EOF) {
19 m_vector.push_back(ch);
20 return 0;
21 } else {
22 return EOF;
23 }
24 }
25
26 std::streamsize xsputn(const typename Vector::value_type *s, std::streamsize num)
27 {
28 size_t oldsize = m_vector.size();
29 if (oldsize + num > m_vector.capacity()) {
30 m_vector.reserve(std::max(2 * (oldsize + num), m_vector.capacity() * 2));
31 }
32 m_vector.resize(oldsize + num);
33 memcpy(m_vector.data() + oldsize, s, num);
34 return num;
35 }
36
37 std::size_t write(const void *ptr, std::size_t size)
38 {
39 return xsputn(static_cast<const typename Vector::value_type *>(ptr), size);
40 }
41
42 const Vector &get_vector() const { return m_vector; }
43
44 Vector &get_vector() { return m_vector; }
45
46private:
47 Vector m_vector;
48};
49
50
51template<typename Vector, typename TraitsT = std::char_traits<typename Vector::value_type>>
52class vecistreambuf: public std::basic_streambuf<typename Vector::value_type, TraitsT> {
53public:
54 vecistreambuf(const Vector &ve): vec(ve)
55 {
56 auto &v = const_cast<Vector &>(vec);
57 this->setg(v.data(), v.data(), v.data() + v.size());
58 }
59
60 std::size_t read(void *ptr, std::size_t size)
61 {
62 if (cur + size > vec.size())
63 size = vec.size() - cur;
64 memcpy(ptr, vec.data() + cur, size);
65 cur += size;
66 return size;
67 }
68
69 bool empty() const { return cur == 0; }
70 typename Vector::value_type peekch() const { return vec[cur]; }
71 typename Vector::value_type getch() { return vec[cur++]; }
72 void ungetch(char)
73 {
74 if (cur > 0)
75 --cur;
76 }
77
78private:
79 const Vector &vec;
80 size_t cur = 0;
81};
82
83
84} // namespace vistle
85#endif
Definition: vecstreambuf.h:52
Vector::value_type getch()
Definition: vecstreambuf.h:71
std::size_t read(void *ptr, std::size_t size)
Definition: vecstreambuf.h:60
vecistreambuf(const Vector &ve)
Definition: vecstreambuf.h:54
void ungetch(char)
Definition: vecstreambuf.h:72
Vector::value_type peekch() const
Definition: vecstreambuf.h:70
bool empty() const
Definition: vecstreambuf.h:69
Definition: vecstreambuf.h:12
const Vector & get_vector() const
Definition: vecstreambuf.h:42
vecostreambuf()
Definition: vecstreambuf.h:14
Vector & get_vector()
Definition: vecstreambuf.h:44
std::size_t write(const void *ptr, std::size_t size)
Definition: vecstreambuf.h:37
int overflow(int ch)
Definition: vecstreambuf.h:16
std::streamsize xsputn(const typename Vector::value_type *s, std::streamsize num)
Definition: vecstreambuf.h:26
Definition: allobjects.cpp:30
Vector3 Vector
Definition: vector.h:36