Om
iterator_source.cpp
Go to the documentation of this file.
1 
15 #ifndef Om_Source_IteratorSource_
16 
18 
19  #ifdef Om_Macro_Test_
20 
22 
23  #ifndef Om_Macro_Precompilation_
24 
25  #include "boost/test/unit_test.hpp"
26 
27  #endif
28 
29 namespace Om {
30 
31  BOOST_AUTO_TEST_SUITE(IteratorSourceTest)
32 
33  namespace {
34 
35  static void CopyWithIterators(
36  char const theCodeUnitIterator[],
37  std::string & theSinkString
38  ) {
40  char const,
41  char const *
42  > IteratorSource;
43  IteratorSource theSource(theCodeUnitIterator);
44  IteratorSource const theSourceEnd("");
45 
46  Sink::ContainerBackSink<
47  char const,
48  std::string
49  > theSink(theSinkString);
50 
51  std::copy(
52  theSource,
53  theSourceEnd,
54  theSink
55  );
56  }
57 
58  class Item {
59 
60  public:
61 
62  // Note: this is intentionally non-explicit.
63  Item(char const theCodeUnit):
64  thisCodeUnit(theCodeUnit) {}
65 
66  bool operator !() const {
67  return !this->thisCodeUnit;
68  }
69 
70  char thisCodeUnit;
71 
72  };
73 
74  }
75 
76  BOOST_AUTO_TEST_CASE(GeneralTest) {
77  typedef Item * Iterator;
79  Item,
80  Iterator
81  > IteratorSource;
82 
83  Item theSourceArray[] = {
84  '0',
85  '1',
86  '2',
87  0
88  };
89  IteratorSource theSource(theSourceArray);
90  Item theSourceArrayEnd[] = {0};
91  IteratorSource const theSourceEnd(theSourceArrayEnd);
92 
93  BOOST_CHECK(theSourceEnd != theSource);
94  BOOST_CHECK_EQUAL(
95  '0',
96  theSource->thisCodeUnit
97  );
98 
99  ++theSource;
100  BOOST_CHECK(theSourceEnd != theSource);
101  BOOST_CHECK_EQUAL(
102  '1',
103  (*theSource).thisCodeUnit
104  );
105 
106  IteratorSource theIterator = ++theSource;
107  BOOST_CHECK(theSourceEnd != theSource);
108  BOOST_CHECK_EQUAL(
109  '2',
110  theIterator->thisCodeUnit
111  );
112  BOOST_CHECK_EQUAL(
113  '2',
114  theSource->thisCodeUnit
115  );
116 
117  theIterator->thisCodeUnit = '3';
118  (*theSource).thisCodeUnit = '4';
119  BOOST_CHECK_EQUAL(
120  '4',
121  (*theIterator).thisCodeUnit
122  );
123  BOOST_CHECK_EQUAL(
124  '4',
125  theSource->thisCodeUnit
126  );
127  BOOST_CHECK_EQUAL(
128  '0',
129  theSourceArray[0].thisCodeUnit
130  );
131  BOOST_CHECK_EQUAL(
132  '1',
133  theSourceArray[1].thisCodeUnit
134  );
135  BOOST_CHECK_EQUAL(
136  '4',
137  theSourceArray[2].thisCodeUnit
138  );
139  BOOST_CHECK_EQUAL(
140  0,
141  theSourceArray[3].thisCodeUnit
142  );
143 
144  ++theSource;
145  BOOST_CHECK(theSourceEnd == theSource);
146  }
147 
148  BOOST_AUTO_TEST_CASE(CopyMultipleItemsTest) {
149  char const theSourceNullTerminatedString[] = "01";
150  std::string theSinkString;
151  CopyWithIterators(
152  theSourceNullTerminatedString,
153  theSinkString
154  );
155 
156  BOOST_CHECK_EQUAL(
157  theSourceNullTerminatedString,
158  theSinkString
159  );
160  }
161 
162  BOOST_AUTO_TEST_CASE(CopySingleItemTest) {
163  char const theSourceNullTerminatedString[] = "0";
164  std::string theSinkString;
165  CopyWithIterators(
166  theSourceNullTerminatedString,
167  theSinkString
168  );
169 
170  BOOST_CHECK_EQUAL(
171  theSourceNullTerminatedString,
172  theSinkString
173  );
174  }
175 
176  BOOST_AUTO_TEST_CASE(CopyNoItemsTest) {
177  char const theSourceNullTerminatedString[] = "";
178  std::string theSinkString;
179  CopyWithIterators(
180  theSourceNullTerminatedString,
181  theSinkString
182  );
183 
184  BOOST_CHECK_EQUAL(
185  theSourceNullTerminatedString,
186  theSinkString
187  );
188  }
189 
190  BOOST_AUTO_TEST_SUITE_END()
191 
192 }
193 
194  #endif
195 
196 #else
197 
198 // MARK: - Om::Source::IteratorSource
199 
200  #define Template_ \
201  template < \
202  typename ThisItem, \
203  typename ThisIterator \
204  >
205 
206  #define Type_ \
207  Om::Source::IteratorSource< \
208  ThisItem, \
209  ThisIterator \
210  >
211 
212 // MARK: public (non-static)
213 
214 Template_
215 inline Type_::IteratorSource(ThisIterator const theIterator):
216 thisIterator(theIterator) {}
217 
218 Template_
219 inline Type_ & Type_::operator =(IteratorSource theIteratorSource) {
220  this->Swap(theIteratorSource);
221  return *this;
222 }
223 
224 Template_
225 inline bool Type_::operator !() const {
226  return !*this->thisIterator;
227 }
228 
229 Template_
230 inline ThisItem & Type_::operator *() const {
231  assert(!!*this->thisIterator);
232  return *this->thisIterator;
233 }
234 
235 Template_
236 inline bool Type_::Equals(IteratorSource const & theIteratorSource) const {
237  return (
238  this->thisIterator == theIteratorSource.thisIterator ||
239  (
240  !*this->thisIterator &&
241  !*theIteratorSource.thisIterator
242  )
243  );
244 }
245 
246 Template_
247 inline void Type_::Pop() {
248  assert(!!*this->thisIterator);
249  ++this->thisIterator;
250 }
251 
252 Template_
253 inline void Type_::Swap(IteratorSource & theIteratorSource) {
254  boost::swap(
255  this->thisIterator,
256  theIteratorSource.thisIterator
257  );
258 }
259 
260  #undef Type_
261  #undef Template_
262 
263 // MARK: - Om::Source::
264 
265  #define Template_ \
266  template < \
267  typename TheItem, \
268  typename TheIterator \
269  >
270 
271  #define Type_ \
272  Om::Source::IteratorSource< \
273  TheItem, \
274  TheIterator \
275  >
276 
277 Template_
278 inline bool Om::Source::operator ==(
279  Type_ const & theFirst,
280  Type_ const & theSecond
281 ) {
282  return theFirst.Equals(theSecond);
283 }
284 
285 Template_
286 inline bool Om::Source::operator !=(
287  Type_ const & theFirst,
288  Type_ const & theSecond
289 ) {
290  return !theFirst.Equals(theSecond);
291 }
292 
293 // MARK: - boost::
294 
295 Template_
296 inline void boost::swap(
297  Type_ & theFirst,
298  Type_ & theSecond
299 ) {
300  theFirst.Swap(theSecond);
301 }
302 
303  #undef Type_
304  #undef Template_
305 
306 #endif
A Source adapter for a sentinal-terminated input iterator.
Om header file.
bool operator==(CodePointSource< TheCodeUnitIterator > const &, CodePointSource< TheCodeUnitIterator > const &)
bool operator!=(CodePointSource< TheCodeUnitIterator > const &, CodePointSource< TheCodeUnitIterator > const &)
The Om library.
Definition: code_point.hpp:26
void swap(Om::Language::Expression &, Om::Language::Expression &)