Classes | Typedefs | Functions | Variables
test_iterators.cxx File Reference
#include "WireCellUtil/Iterator.h"
#include "WireCellUtil/IteratorBase.h"
#include "WireCellUtil/Testing.h"
#include <boost/range.hpp>
#include <boost/function.hpp>
#include <vector>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <memory>
#include <cmath>

Go to the source code of this file.

Classes

struct  IMyClass
 
struct  MyClass
 
struct  SelectInt
 
struct  SelectInRange
 

Typedefs

typedef Iterator< const IMyClass * > my_iterator
 
typedef std::pair< my_iterator, my_iteratormy_range
 
typedef IteratorBase< const IMyClass * > my_base_iterator
 
typedef vector< MyClass * > MyStoreType
 
typedef IteratorAdapter< MyStoreType::iterator, my_base_iteratormy_adapted_iterator
 
typedef boost::function< bool(const IMyClass *)> my_selector
 

Functions

my_range get_data (MyStoreType &store)
 
int main ()
 

Variables

SelectInt get1 = SelectInt(1)
 
my_selector get10 = SelectInt(10)
 
SelectInRange just_right = SelectInRange(5,10)
 

Typedef Documentation

Definition at line 63 of file test_iterators.cxx.

Definition at line 50 of file test_iterators.cxx.

typedef Iterator<const IMyClass*> my_iterator

Definition at line 41 of file test_iterators.cxx.

typedef std::pair<my_iterator, my_iterator> my_range

Definition at line 44 of file test_iterators.cxx.

Definition at line 67 of file test_iterators.cxx.

typedef vector< MyClass* > MyStoreType

Definition at line 57 of file test_iterators.cxx.

Function Documentation

my_range get_data ( MyStoreType store)

Definition at line 90 of file test_iterators.cxx.

91 {
92  return my_range(my_adapted_iterator(store.begin()),
93  my_adapted_iterator(store.end()));
94 }
IteratorAdapter< MyStoreType::iterator, my_base_iterator > my_adapted_iterator
std::pair< my_iterator, my_iterator > my_range
int main ( void  )

Definition at line 97 of file test_iterators.cxx.

98 {
99 
100  // This would be deep in side some custom class.
101  MyStoreType store;
102  store.push_back(new MyClass(0,0.0));
103  store.push_back(new MyClass(1,42.));
104  store.push_back(new MyClass(2,6.9));
105 
106  MyClass* first = *(store.begin());
107  AssertMsg(first, "No first");
108  AssertMsg(first->get_i() == 0 && first->get_f() == 0.0, "first ain't first");
109 
110  // This would be an element of the base class's interface which
111  // the custom class implements. It returns generic iterators on
112  // the base/interface data type. These iterators may be copied
113  // without slicing.
114  my_range r = get_data(store);
115 
116  // Finally, here is some client of the interface using the data
117  // born deep inside the custom class and accessing it only via
118  // interfaces.
119  for (my_iterator it = boost::begin(r); it != boost::end(r); ++it) {
120  // make a temp for syntactic convenience/clarity
121  const IMyClass* myptr = *it;
122  const IMyClass& myobj = *myptr;
123  cout << myobj.get_i() << " "
124  << myobj.get_f() << endl;
125  }
126 
127 
128  vector< const IMyClass* > res(r.first, r.second);
129  AssertMsg(res.size(), "range constructor failed.");
130  res.clear();
131 
132  copy(r.first, r.second, back_inserter(res));
133  AssertMsg(res.size(), "copy failed.");
134  res.clear();
135 
136 
137  copy_if(boost::begin(r), boost::end(r), back_inserter(res), get1);
138  cerr << "Got: " << res.size() << endl;
139  AssertMsg(1 == res.size(), "Failed to get1");
140  AssertMsg(res[0]->get_f() == 42., "Got wrong1");
141  res.clear();
142 
143  copy_if(boost::begin(r), boost::end(r), back_inserter(res), get10);
144  AssertMsg(0 == res.size(), "Got get10 but should not.");
145  res.clear();
146 
147  copy_if(boost::begin(r), boost::end(r), back_inserter(res), just_right);
148  AssertMsg(1 == res.size(), "Failed to get just_right");
149  cerr << "Got: " << res[0]->get_i() << " " << res[0]->get_f() << endl;
150  AssertMsg(res[0]->get_i() == 2 && fabs(res[0]->get_f() - 6.9) < 1e-6, "Got just_wrong value");
151  res.clear();
152 
153  return 0;
154 }
my_selector get10
SelectInRange just_right
SelectInt get1
virtual float get_f() const
decltype(auto) constexpr end(T &&obj)
ADL-aware version of std::end.
Definition: StdUtils.h:72
const double e
virtual int get_i() const
vector< MyClass * > MyStoreType
virtual float get_f() const =0
#define AssertMsg
Definition: Testing.h:8
virtual int get_i() const =0
T copy(T const &v)
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:67
std::pair< my_iterator, my_iterator > my_range
my_range get_data(MyStoreType &store)
QTextStream & endl(QTextStream &s)

Variable Documentation

SelectInt get1 = SelectInt(1)

Definition at line 85 of file test_iterators.cxx.

my_selector get10 = SelectInt(10)

Definition at line 86 of file test_iterators.cxx.

SelectInRange just_right = SelectInRange(5,10)

Definition at line 87 of file test_iterators.cxx.