test_stream.cxx
Go to the documentation of this file.
1 // test boost iostream with objects
2 // http://stackoverflow.com/questions/14086417/how-to-write-custom-input-stream-in-c
3 
4 
5 #include "WireCellUtil/Testing.h"
6 
7 #include <string>
8 #include <memory>
9 #include <set>
10 #include <iostream>
11 
12 struct MyData {
13  int i;
14  float f;
15  double d;
17  MyData(int i=0, float f=0.0, double d=0.0, const std::string& s="")
18  : i(i), f(f), d(d), s(s) {}
19 
20 
21 };
22 
23 
24 std::ostream& operator<<(std::ostream& o, const MyData& md) {
25  o << "<MyData i:" << md.i << " f:" << md.f << " d:" << md.d << " s:\"" << md.s << "\">";
26  return o;
27 }
28 
29 typedef std::shared_ptr<const MyData> MyDataPtr;
30 typedef std::set<MyDataPtr> MyDataStore;
31 
32 class IMySource {
33 public:
34  virtual ~IMySource() {}
35  virtual MyDataPtr operator()() = 0;
36  virtual bool eof() = 0;
37 };
38 
39 class MyGen : virtual public IMySource {
40  int m_count;
41  int m_max;
42 public:
43  MyGen(int max=-1) : m_count(0), m_max(max) {}
44 
45  virtual MyDataPtr operator()() {
46  MyData* rawp = new MyData(m_count, m_count, m_count);
47  ++m_count;
48  return MyDataPtr(rawp);
49  }
50  virtual bool eof() {
51  if (m_max < 0) return false;
52  return m_count >= m_max;
53  }
54 };
55 
56 
57 class MyFilter : virtual public IMySource {
59  int m_mod;
60 
61 public:
62  MyFilter(IMySource& src, int m=2) : m_src(src), m_mod(m) {}
63  virtual ~MyFilter() {};
64 
65  virtual MyDataPtr operator()() {
66  while (! m_src.eof()) {
67  MyDataPtr p = m_src();
68  if (0 == p->i % m_mod) {
69  return p;
70  }
71  }
72  return 0;
73  }
74  virtual bool eof() {
75  return m_src.eof();
76  }
77 };
78 
79 
80 
81 using namespace std;
82 
83 int main()
84 {
85  MyGen mg(10);
86  MyFilter mf(mg);
87  MyDataPtr p;
88  while ((p = mf())) {
89  cerr << *p << endl;
90  }
91 }
virtual bool eof()=0
static const double m
Definition: Units.h:79
std::string s
Definition: test_stream.cxx:16
std::string string
Definition: nybbler.cc:12
std::ostream & operator<<(std::ostream &o, const MyData &md)
Definition: test_stream.cxx:24
MyFilter(IMySource &src, int m=2)
Definition: test_stream.cxx:62
int main()
Definition: test_stream.cxx:83
virtual MyDataPtr operator()()
Definition: test_stream.cxx:65
STL namespace.
double d
Definition: test_stream.cxx:15
virtual ~MyFilter()
Definition: test_stream.cxx:63
std::shared_ptr< const MyData > MyDataPtr
Definition: test_stream.cxx:29
MyData(int i=0, float f=0.0, double d=0.0, const std::string &s="")
Definition: test_stream.cxx:17
int m_max
Definition: test_stream.cxx:41
virtual MyDataPtr operator()()
Definition: test_stream.cxx:45
virtual bool eof()
Definition: test_stream.cxx:74
std::set< MyDataPtr > MyDataStore
Definition: test_stream.cxx:30
int m_count
Definition: test_stream.cxx:40
static int max(int a, int b)
static const double mg
Definition: Units.h:146
IMySource & m_src
Definition: test_stream.cxx:58
p
Definition: test.py:223
MyGen(int max=-1)
Definition: test_stream.cxx:43
virtual bool eof()
Definition: test_stream.cxx:50
virtual ~IMySource()
Definition: test_stream.cxx:34
float f
Definition: test_stream.cxx:14
QTextStream & endl(QTextStream &s)