Commit patch to not break on spaces.
[bowtie.git] / SeqAn-1.1 / seqan / basic / basic_proxy.h
1  /*==========================================================================
2                 SeqAn - The Library for Sequence Analysis
3                           http://www.seqan.de 
4  ============================================================================
5   Copyright (C) 2007
6
7   This library is free software; you can redistribute it and/or
8   modify it under the terms of the GNU Lesser General Public
9   License as published by the Free Software Foundation; either
10   version 3 of the License, or (at your option) any later version.
11
12   This library is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   Lesser General Public License for more details.
16
17  ============================================================================
18   $Id: basic_proxy.h,v 1.1 2008/08/25 16:20:02 langmead Exp $
19  ==========================================================================*/
20
21 #ifndef SEQAN_HEADER_BASIC_PROXY_H
22 #define SEQAN_HEADER_BASIC_PROXY_H
23
24
25 namespace SEQAN_NAMESPACE_MAIN
26 {
27
28
29 //////////////////////////////////////////////////////////////////////////////
30 // Proxy
31 //////////////////////////////////////////////////////////////////////////////
32 /**
33 .Class.Proxy:
34 ..cat:Basic
35 ..summary:Emulates object of another class.
36 ..signature:Proxy<TSpec>
37 ..param.TSpec:The specializing type.
38 ...metafunction:Metafunction.Spec
39 ..remarks.text:Use @Metafunction.Value@ to get the emulated type.
40 An instance of $Proxy$ behaves like an object of its value type.
41 $Proxy$ can be used as reference type (see @Metafunction.Reference@).
42 ..remarks.text:Note that functions that are both general and specialized for 
43 the value type should be specialized for $Proxy<TSpec>$ too, 
44 since otherwise the general version will be called.
45 */
46
47 template <typename TSpec>
48 struct Proxy;
49
50 //////////////////////////////////////////////////////////////////////////////
51
52 ///.Metafunction.Spec.param.T.type:Class.Proxy
53
54 template <typename TSpec>
55 struct Spec< Proxy<TSpec> >
56 {
57         typedef TSpec Type;
58 };
59 template <typename TSpec>
60 struct Spec< Proxy<TSpec> const>
61 {
62         typedef TSpec Type;
63 };
64
65 //////////////////////////////////////////////////////////////////////////////
66 // Iterator Proxy
67 //////////////////////////////////////////////////////////////////////////////
68 /**
69 .Spec.Iterator Proxy:
70 ..cat:Proxies
71 ..general:Class.Proxy
72 ..summary:Proxy that is implemented by an iterator.
73 ..signature:Proxy<IteratorProxy<TIterator> >
74 ..param.TIterator:Iterator type.
75 ..remarks.text:The value type of an iterator proxy is the value type of the
76 iterator $TIterator$.
77 */
78
79 template <typename TIterator>
80 struct IteratorProxy;
81
82 //____________________________________________________________________________
83
84 template <typename TIterator>
85 struct Proxy<IteratorProxy<TIterator> >
86 {
87 public:
88         typedef typename Value<Proxy>::Type TValue;
89         typedef typename GetValue<Proxy>::Type TAccessor;
90
91         typedef typename _RemoveConst<TAccessor>::Type TAccessor_NotConst;
92
93 private:
94         TIterator data_iterator;
95
96 public:
97         Proxy(TIterator const _it):
98                 data_iterator(_it)
99         {
100 SEQAN_CHECKPOINT
101         }
102         Proxy(Proxy const & _other):
103                 data_iterator(_other.data_iterator)
104         {
105 SEQAN_CHECKPOINT
106         }
107
108         ~Proxy()
109         {
110 SEQAN_CHECKPOINT
111         }
112
113         Proxy const &
114         operator = (Proxy const & _other)
115         {
116 SEQAN_CHECKPOINT
117                 assignValue(data_iterator, getValue(_other.data_iterator));
118                 return *this;
119         }
120
121         Proxy const &
122         operator = (TValue const & _value)
123         {
124 SEQAN_CHECKPOINT
125                 assignValue(data_iterator, _value);
126                 return *this;
127         }
128
129         operator TAccessor_NotConst()
130         {
131 SEQAN_CHECKPOINT
132                 return getValue(data_iterator);
133         }
134
135 //____________________________________________________________________________
136
137         //not documented
138         friend inline TIterator &
139         iter(Proxy & me)
140         {
141                 return me.data_iterator;
142         }
143         friend inline TIterator const &
144         iter(Proxy const & me)
145         {
146                 return me.data_iterator;
147         }
148
149 //____________________________________________________________________________
150 };
151
152 //////////////////////////////////////////////////////////////////////////////
153 // Metafunctions
154 //////////////////////////////////////////////////////////////////////////////
155
156 ///.Metafunction.Value.param.T.type:Class.Proxy
157
158 template <typename TIterator>
159 struct Value< Proxy<IteratorProxy<TIterator> > >:
160         Value<TIterator>
161 {
162 };
163 template <typename TIterator>
164 struct Value< Proxy<IteratorProxy<TIterator> > const>
165 {
166         typedef typename Value<TIterator>::Type const Type;
167 };
168
169 //////////////////////////////////////////////////////////////////////////////
170
171 ///.Metafunction.GetValue.param.T.type:Class.Proxy
172
173 template <typename TIterator>
174 struct GetValue< Proxy<IteratorProxy<TIterator> > >:
175         GetValue<TIterator>
176 {
177 };
178 template <typename TIterator>
179 struct GetValue< Proxy<IteratorProxy<TIterator> > const>
180 {
181         typedef typename GetValue<TIterator const>::Type Type;
182 };
183
184 //////////////////////////////////////////////////////////////////////////////
185
186 ///.Metafunction.Reference.param.T.type:Class.Proxy
187
188 template <typename TIterator>
189 struct Reference< Proxy<IteratorProxy<TIterator> > >
190 {
191         typedef Proxy<IteratorProxy<TIterator> > Type;
192 };
193 template <typename TIterator>
194 struct Reference< Proxy<IteratorProxy<TIterator> > const >
195 {
196         typedef Proxy<IteratorProxy<TIterator> > const Type;
197 };
198
199 //////////////////////////////////////////////////////////////////////////////
200
201 ///.Metafunction.Size.param.T.type:Class.Proxy
202
203 template <typename TIterator>
204 struct Size< Proxy<IteratorProxy<TIterator> > >:
205         Size<TIterator>
206 {
207 };
208 template <typename TIterator>
209 struct Size< Proxy<IteratorProxy<TIterator> > const>:
210         Size<TIterator>
211 {
212 };
213
214 //////////////////////////////////////////////////////////////////////////////
215
216 ///.Metafunction.Difference.param.T.type:Class.Proxy
217
218 template <typename TIterator>
219 struct Difference< Proxy<IteratorProxy<TIterator> > >:
220         Difference<TIterator>
221 {
222 };
223 template <typename TIterator>
224 struct Difference< Proxy<IteratorProxy<TIterator> > const>:
225         Difference<TIterator>
226 {
227 };
228
229 //////////////////////////////////////////////////////////////////////////////
230 //////////////////////////////////////////////////////////////////////////////
231
232 template <typename TSpec>
233 typename GetValue<Proxy<TSpec> >::Type
234 getValue(Proxy<TSpec> & me)
235 {
236         return getValue(iter(me));
237 }
238 template <typename TSpec>
239 typename GetValue<Proxy<TSpec> const>::Type
240 getValue(Proxy<TSpec> const & me)
241 {
242         return getValue(iter(me));
243 }
244
245 //////////////////////////////////////////////////////////////////////////////
246 // Comparison
247 //////////////////////////////////////////////////////////////////////////////
248
249 template <typename TSpec, typename T>
250 struct CompareType <Proxy<TSpec>, T>
251 {
252         typedef typename Value<Proxy<TSpec> >::Type TValue;
253         typedef typename _RemoveConst<TValue>::Type TValue_NoConst;
254         typedef typename CompareType<TValue_NoConst, T>::Type Type;
255 };
256
257 //???TODO: Symmetrie von CompareType herstellen
258 //____________________________________________________________________________
259
260 template <typename TTarget, typename T, typename TSpec>
261 inline typename Convert<TTarget, Proxy<TSpec> >::Type
262 convertImpl(Convert<TTarget, T> const,
263                         Proxy<TSpec> & source)
264 {
265         return convert<TTarget>(getValue(source));
266 }
267 template <typename TTarget, typename T, typename TSpec>
268 inline typename Convert<TTarget, Proxy<TSpec> const>::Type
269 convertImpl(Convert<TTarget, T> const,
270                         Proxy<TSpec> const & source)
271 {
272         return convert<TTarget>(getValue(source));
273 }
274 //////////////////////////////////////////////////////////////////////////////
275 // operator ==
276
277 template <typename TSpec, typename TRight>
278 inline bool
279 operator == (Proxy<TSpec> const & left_, 
280                          TRight const & right_)
281 {
282 SEQAN_CHECKPOINT
283         typedef Proxy<TSpec> TLeft;
284         typedef typename CompareType<TLeft, TRight>::Type TCompareType;
285         return convert<TCompareType>(left_) == convert<TCompareType>(right_);
286 }
287
288 template <typename TLeft, typename TSpec>
289 inline bool
290 operator == (TLeft const & left_, 
291                          Proxy<TSpec> const & right_)
292 {
293 SEQAN_CHECKPOINT
294         typedef Proxy<TSpec> TRight;
295         typedef typename CompareType<TRight, TLeft>::Type TCompareType;
296         return convert<TCompareType>(left_) == convert<TCompareType>(right_);
297 }
298
299 template <typename TLeftSpec, typename TRightSpec>
300 inline bool
301 operator == (Proxy<TLeftSpec> const & left_, 
302                          Proxy<TRightSpec> const & right_)
303 {
304 SEQAN_CHECKPOINT
305         typedef Proxy<TLeftSpec> TLeft;
306         typedef Proxy<TRightSpec> TRight;
307         typedef typename CompareType<TLeft, TRight>::Type TCompareType;
308         return convert<TCompareType>(left_) == convert<TCompareType>(right_);
309 }
310
311 template <typename TSpec>
312 inline bool
313 operator == (Proxy<TSpec> const & left_, 
314                          Proxy<TSpec> const & right_)
315 {
316 SEQAN_CHECKPOINT
317         typedef typename GetValue<Proxy<TSpec> >::Type TAccessor;
318         return convert<TAccessor>(left_) == convert<TAccessor>(right_);
319 }
320
321 //____________________________________________________________________________
322 // operator !=
323
324 template <typename TSpec, typename TRight>
325 inline bool
326 operator != (Proxy<TSpec> const & left_, 
327                          TRight const & right_)
328 {
329 SEQAN_CHECKPOINT
330         typedef Proxy<TSpec> TLeft;
331         typedef typename CompareType<TLeft, TRight>::Type TCompareType;
332         return convert<TCompareType>(left_) != convert<TCompareType>(right_);
333 }
334
335 template <typename TLeft, typename TSpec>
336 inline bool
337 operator != (TLeft const & left_, 
338                          Proxy<TSpec> const & right_)
339 {
340 SEQAN_CHECKPOINT
341         typedef Proxy<TSpec> TRight;
342         typedef typename CompareType<TRight, TLeft>::Type TCompareType;
343         return convert<TCompareType>(left_) != convert<TCompareType>(right_);
344 }
345
346 template <typename TLeftSpec, typename TRightSpec>
347 inline bool
348 operator != (Proxy<TLeftSpec> const & left_, 
349                          Proxy<TRightSpec> const & right_)
350 {
351 SEQAN_CHECKPOINT
352         typedef Proxy<TLeftSpec> TLeft;
353         typedef Proxy<TRightSpec> TRight;
354         typedef typename CompareType<TLeft, TRight>::Type TCompareType;
355         return convert<TCompareType>(left_) != convert<TCompareType>(right_);
356 }
357
358 template <typename TSpec>
359 inline bool
360 operator != (Proxy<TSpec> const & left_, 
361                          Proxy<TSpec> const & right_)
362 {
363 SEQAN_CHECKPOINT
364         typedef typename GetValue<Proxy<TSpec> >::Type TAccessor;
365         return convert<TAccessor>(left_) != convert<TAccessor>(right_);
366 }
367
368
369 //____________________________________________________________________________
370 // operator <
371
372 template <typename TSpec, typename TRight>
373 inline bool
374 operator < (Proxy<TSpec> const & left_, 
375                         TRight const & right_)
376 {
377 SEQAN_CHECKPOINT
378         typedef Proxy<TSpec> TLeft;
379         typedef typename CompareType<TLeft, TRight>::Type TCompareType;
380         return convert<TCompareType>(left_) < convert<TCompareType>(right_);
381 }
382
383 template <typename TLeft, typename TSpec>
384 inline bool
385 operator < (TLeft const & left_, 
386                         Proxy<TSpec> const & right_)
387 {
388 SEQAN_CHECKPOINT
389         typedef Proxy<TSpec> TRight;
390         typedef typename CompareType<TRight, TLeft>::Type TCompareType;
391         return convert<TCompareType>(left_) < convert<TCompareType>(right_);
392 }
393
394 template <typename TLeftSpec, typename TRightSpec>
395 inline bool
396 operator < (Proxy<TLeftSpec> const & left_, 
397                         Proxy<TRightSpec> const & right_)
398 {
399 SEQAN_CHECKPOINT
400         typedef Proxy<TLeftSpec> TLeft;
401         typedef Proxy<TRightSpec> TRight;
402         typedef typename CompareType<TLeft, TRight>::Type TCompareType;
403         return convert<TCompareType>(left_) < convert<TCompareType>(right_);
404 }
405
406 template <typename TSpec>
407 inline bool
408 operator < (Proxy<TSpec> const & left_, 
409                         Proxy<TSpec> const & right_)
410 {
411 SEQAN_CHECKPOINT
412         typedef typename GetValue<Proxy<TSpec> >::Type TAccessor;
413         return convert<TAccessor>(left_) < convert<TAccessor>(right_);
414 }
415
416 //____________________________________________________________________________
417 // operator <=
418
419 template <typename TSpec, typename TRight>
420 inline bool
421 operator <= (Proxy<TSpec> const & left_, 
422                          TRight const & right_)
423 {
424 SEQAN_CHECKPOINT
425         typedef Proxy<TSpec> TLeft;
426         typedef typename CompareType<TLeft, TRight>::Type TCompareType;
427         return convert<TCompareType>(left_) <= convert<TCompareType>(right_);
428 }
429
430 template <typename TLeft, typename TSpec>
431 inline bool
432 operator <= (TLeft const & left_, 
433                          Proxy<TSpec> const & right_)
434 {
435 SEQAN_CHECKPOINT
436         typedef Proxy<TSpec> TRight;
437         typedef typename CompareType<TRight, TLeft>::Type TCompareType;
438         return convert<TCompareType>(left_) <= convert<TCompareType>(right_);
439 }
440
441 template <typename TLeftSpec, typename TRightSpec>
442 inline bool
443 operator <= (Proxy<TLeftSpec> const & left_, 
444                          Proxy<TRightSpec> const & right_)
445 {
446 SEQAN_CHECKPOINT
447         typedef Proxy<TLeftSpec> TLeft;
448         typedef Proxy<TRightSpec> TRight;
449         typedef typename CompareType<TLeft, TRight>::Type TCompareType;
450         return convert<TCompareType>(left_) <= convert<TCompareType>(right_);
451 }
452
453 template <typename TSpec>
454 inline bool
455 operator <= (Proxy<TSpec> const & left_, 
456                          Proxy<TSpec> const & right_)
457 {
458 SEQAN_CHECKPOINT
459         typedef typename GetValue<Proxy<TSpec> >::Type TAccessor;
460         return convert<TAccessor>(left_) <= convert<TAccessor>(right_);
461 }
462
463
464 //____________________________________________________________________________
465 // operator >
466
467 template <typename TSpec, typename TRight>
468 inline bool
469 operator > (Proxy<TSpec> const & left_, 
470                         TRight const & right_)
471 {
472 SEQAN_CHECKPOINT
473         typedef Proxy<TSpec> TLeft;
474         typedef typename CompareType<TLeft, TRight>::Type TCompareType;
475         return convert<TCompareType>(left_) > convert<TCompareType>(right_);
476 }
477
478 template <typename TLeft, typename TSpec>
479 inline bool
480 operator > (TLeft const & left_, 
481                         Proxy<TSpec> const & right_)
482 {
483 SEQAN_CHECKPOINT
484         typedef Proxy<TSpec> TRight;
485         typedef typename CompareType<TRight, TLeft>::Type TCompareType;
486         return convert<TCompareType>(left_) > convert<TCompareType>(right_);
487 }
488
489 template <typename TLeftSpec, typename TRightSpec>
490 inline bool
491 operator > (Proxy<TLeftSpec> const & left_, 
492                         Proxy<TRightSpec> const & right_)
493 {
494 SEQAN_CHECKPOINT
495         typedef Proxy<TLeftSpec> TLeft;
496         typedef Proxy<TRightSpec> TRight;
497         typedef typename CompareType<TLeft, TRight>::Type TCompareType;
498         return convert<TCompareType>(left_) > convert<TCompareType>(right_);
499 }
500
501 template <typename TSpec>
502 inline bool
503 operator > (Proxy<TSpec> const & left_, 
504                         Proxy<TSpec> const & right_)
505 {
506 SEQAN_CHECKPOINT
507         typedef typename GetValue<Proxy<TSpec> >::Type TAccessor;
508         return convert<TAccessor>(left_) > convert<TAccessor>(right_);
509 }
510
511
512 //____________________________________________________________________________
513 // operator >=
514
515 template <typename TSpec, typename TRight>
516 inline bool
517 operator >= (Proxy<TSpec> const & left_, 
518                          TRight const & right_)
519 {
520 SEQAN_CHECKPOINT
521         typedef Proxy<TSpec> TLeft;
522         typedef typename CompareType<TLeft, TRight>::Type TCompareType;
523         return convert<TCompareType>(left_) >= convert<TCompareType>(right_);
524 }
525
526 template <typename TLeft, typename TSpec>
527 inline bool
528 operator >= (TLeft const & left_, 
529                          Proxy<TSpec> const & right_)
530 {
531 SEQAN_CHECKPOINT
532         typedef Proxy<TSpec> TRight;
533         typedef typename CompareType<TRight, TLeft>::Type TCompareType;
534         return convert<TCompareType>(left_) >= convert<TCompareType>(right_);
535 }
536
537 template <typename TLeftSpec, typename TRightSpec>
538 inline bool
539 operator >= (Proxy<TLeftSpec> const & left_, 
540                          Proxy<TRightSpec> const & right_)
541 {
542 SEQAN_CHECKPOINT
543         typedef Proxy<TLeftSpec> TLeft;
544         typedef Proxy<TRightSpec> TRight;
545         typedef typename CompareType<TLeft, TRight>::Type TCompareType;
546         return convert<TCompareType>(left_) >= convert<TCompareType>(right_);
547 }
548
549 template <typename TSpec>
550 inline bool
551 operator >= (Proxy<TSpec> const & left_, 
552                          Proxy<TSpec> const & right_)
553 {
554 SEQAN_CHECKPOINT
555         typedef typename GetValue<Proxy<TSpec> >::Type TAccessor;
556         return convert<TAccessor>(left_) >= convert<TAccessor>(right_);
557 }
558
559 //////////////////////////////////////////////////////////////////////////////
560
561 template <typename TStream, typename TSpec>
562 inline TStream &
563 operator >> (TStream & strm,
564                          Proxy<TSpec> & proxy)
565 {
566         typedef Proxy<TSpec> TProxy;
567         typedef typename Value<TProxy>::Type TValue;
568         TValue temp;
569         strm >> temp;
570         assignValue(iter(proxy), temp);
571         return strm;
572 }
573 template <typename TStream, typename TSpec>
574 inline TStream &
575 operator >> (TStream & strm,
576                          Proxy<TSpec> const& proxy)
577 {
578         typedef Proxy<TSpec> TProxy;
579         typedef typename Value<TProxy>::Type TValue;
580         TValue temp;
581         strm >> temp;
582         assignValue(iter(proxy), temp);
583         return strm;
584 }
585
586
587 template <typename TStream, typename TSpec>
588 inline TStream &
589 operator << (TStream & strm,
590                          Proxy<TSpec> & proxy)
591 {
592         return strm << getValue(proxy);
593 }
594 template <typename TStream, typename TSpec>
595 inline TStream &
596 operator << (TStream & strm,
597                          Proxy<TSpec> const & proxy)
598 {
599         return strm << getValue(proxy);
600 }
601
602
603 //////////////////////////////////////////////////////////////////////////////
604 } //namespace SEQAN_NAMESPACE_MAIN
605
606 #endif //#ifndef SEQAN_HEADER_...