Main Page
Related Pages
Modules
Namespaces
Classes
Files
Examples
File List
File Members
wire-cell-build
gen
test
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
;
16
std::string
s
;
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
{
58
IMySource
&
m_src
;
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
}
IMySource::eof
virtual bool eof()=0
MyData
Definition:
test_stream.cxx:12
genie::units::m
static const double m
Definition:
Units.h:79
MyData::s
std::string s
Definition:
test_stream.cxx:16
string
std::string string
Definition:
nybbler.cc:12
MyFilter::m_mod
int m_mod
Definition:
test_stream.cxx:59
operator<<
std::ostream & operator<<(std::ostream &o, const MyData &md)
Definition:
test_stream.cxx:24
mf
Definition:
MessageLogger.cc:50
MyFilter::MyFilter
MyFilter(IMySource &src, int m=2)
Definition:
test_stream.cxx:62
main
int main()
Definition:
test_stream.cxx:83
MyFilter::operator()
virtual MyDataPtr operator()()
Definition:
test_stream.cxx:65
std
STL namespace.
MyData::d
double d
Definition:
test_stream.cxx:15
MyFilter::~MyFilter
virtual ~MyFilter()
Definition:
test_stream.cxx:63
MyDataPtr
std::shared_ptr< const MyData > MyDataPtr
Definition:
test_stream.cxx:29
MyData::MyData
MyData(int i=0, float f=0.0, double d=0.0, const std::string &s="")
Definition:
test_stream.cxx:17
MyGen::m_max
int m_max
Definition:
test_stream.cxx:41
MyGen::operator()
virtual MyDataPtr operator()()
Definition:
test_stream.cxx:45
MyFilter::eof
virtual bool eof()
Definition:
test_stream.cxx:74
IMySource
Definition:
test_stream.cxx:32
MyFilter
Definition:
test_stream.cxx:57
MyDataStore
std::set< MyDataPtr > MyDataStore
Definition:
test_stream.cxx:30
Testing.h
MyData::i
int i
Definition:
test_stream.cxx:13
MyGen::m_count
int m_count
Definition:
test_stream.cxx:40
max
static int max(int a, int b)
Definition:
fortranscanner.cpp:60366
genie::units::mg
static const double mg
Definition:
Units.h:146
MyFilter::m_src
IMySource & m_src
Definition:
test_stream.cxx:58
python.root_metadata.md
md
Definition:
root_metadata.py:178
test.p
p
Definition:
test.py:223
MyGen::MyGen
MyGen(int max=-1)
Definition:
test_stream.cxx:43
MyGen::eof
virtual bool eof()
Definition:
test_stream.cxx:50
IMySource::~IMySource
virtual ~IMySource()
Definition:
test_stream.cxx:34
MyGen
Definition:
test_stream.cxx:39
MyData::f
float f
Definition:
test_stream.cxx:14
endl
QTextStream & endl(QTextStream &s)
Definition:
qtextstream.cpp:2030
Generated by
1.8.11