Commit patch to not break on spaces.
[bowtie.git] / SeqAn-1.1 / seqan / basic / basic_holder.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_holder.h,v 1.1 2008/08/25 16:20:01 langmead Exp $
19  ==========================================================================*/
20
21 #ifndef SEQAN_HEADER_BASIC_HOLDER_H
22 #define SEQAN_HEADER_BASIC_HOLDER_H
23
24 namespace SEQAN_NAMESPACE_MAIN
25 {
26
27 //////////////////////////////////////////////////////////////////////////////
28 // addRef
29 //////////////////////////////////////////////////////////////////////////////
30 /**
31 .Function.addRef:
32 ..summary:Called when dependency is added. 
33 ..cat:Dependent Objects
34 ..signature:addRef(host)
35 ..param.host:The host object.
36 ..remarks.text:A call of this function denotes that a client object is about to become
37 dependent on $host$.
38 ..remarks.text:The default behavior is: Do nothing.
39 ..see:Class.Holder
40 */
41
42 template <typename T>
43 inline void 
44 addRef(T & /*me*/)
45 {// general: do nothing
46 SEQAN_CHECKPOINT
47 }
48 template <typename T>
49 inline void 
50 addRef(T const & /*me*/)
51 {// general: do nothing
52 SEQAN_CHECKPOINT
53 }
54
55 //////////////////////////////////////////////////////////////////////////////
56 // releaseRef
57 //////////////////////////////////////////////////////////////////////////////
58 /**
59 .Function.releaseRef:
60 ..summary:Called when dependency is released. 
61 ..cat:Dependent Objects
62 ..signature:releaseRef(host)
63 ..param.host:The host object.
64 ..remarks.text:A call of this function denotes that a former dependent client object 
65 ceases to be dependent on $host$.
66 ..remarks.text:The default behavior is: Do nothing.
67 ..see:Class.Holder
68 ..see:Function.addRef
69 */
70 template <typename T>
71 inline void 
72 releaseRef(T & /*me*/)
73 {// general: do nothing
74 SEQAN_CHECKPOINT
75 }
76 template <typename T>
77 inline void 
78 releaseRef(T const & /*me*/)
79 {// general: do nothing
80 SEQAN_CHECKPOINT
81 }
82
83 //////////////////////////////////////////////////////////////////////////////
84 // Tags
85
86 struct Simple;
87 struct Tristate;
88
89
90 //////////////////////////////////////////////////////////////////////////////
91 // Holder
92 //////////////////////////////////////////////////////////////////////////////
93
94 /**
95 .Class.Holder:
96 ..cat:Basic
97 ..summary:Manages relationship to another object.
98 ..signature:Holder<TValue, TSpec>
99 ..param.TValue:Type of the managed object.
100 ...metafunction:Metafunction.Value
101 ..param.TSpec:The specializing type.
102 ...metafunction:Metafunction.Spec
103 ...default:$Tristate$
104 ..remarks.text:The main purpose of this class is to facilitate the handling of
105 member objects. If we want class $A$ to be dependent on or the owner of another object of class $B$, 
106 then we add a data member of type $Holder<B>$ to $A$. 
107 $Holder$ offers some useful access functions, stores the kind of relationship between $A$ and $B$,
108 and executes all needed @Function.addRef@ and @Function.releaseRef@ calls.
109 */
110
111 template <typename TValue, typename TSpec = Tristate>
112 struct Holder;
113
114
115 //////////////////////////////////////////////////////////////////////////////
116 // METAFUNCTIONS
117 //////////////////////////////////////////////////////////////////////////////
118
119 ///.Metafunction.Value.param.T.type:Class.Holder
120
121 template <typename TValue, typename TSpec>
122 struct Value< Holder<TValue, TSpec> >
123 {
124         typedef typename _RemoveConst<TValue>::Type Type;
125 };
126 template <typename TValue, typename TSpec>
127 struct Value< Holder<TValue, TSpec> const>
128 {
129         typedef typename _RemoveConst<TValue>::Type Type;
130 };
131
132 //////////////////////////////////////////////////////////////////////////////
133
134 ///.Metafunction.Spec.param.T.type:Class.Holder
135
136 template <typename TValue, typename TSpec>
137 struct Spec< Holder<TValue, TSpec> >
138 {
139         typedef TSpec Type;
140 };
141 template <typename TValue, typename TSpec>
142 struct Spec< Holder<TValue, TSpec> const>
143 {
144         typedef TSpec Type;
145 };
146
147 //////////////////////////////////////////////////////////////////////////////
148
149 ///.Metafunction.Reference.param.T.type:Class.Holder
150
151 template <typename TValue, typename TSpec>
152 struct Reference< Holder<TValue, TSpec> >
153 {
154         typedef typename Value< Holder<TValue, TSpec> >::Type & Type;
155 };
156 template <typename TValue, typename TSpec>
157 struct Reference< Holder<TValue, TSpec> const>
158 {
159         typedef typename Value< Holder<TValue, TSpec> const>::Type & Type;
160 };
161
162
163 //////////////////////////////////////////////////////////////////////////////
164 //////////////////////////////////////////////////////////////////////////////
165 // Tristate Holder
166 //////////////////////////////////////////////////////////////////////////////
167 /**
168 .Spec.Tristate Holder
169 ..cat:Holders
170 ..summary:Holder that can be empty, dependent, or owner.
171 ..signature:Holder<TValue, Tristate>
172 ..param.TValue:Type of the managed object.
173 ..general:Class.Holder
174 ..remarks.text:A tristate holder $A$ that holds an object $B$ has one of the following states:
175 ..remarks.text:- owner: $A$ is the owner of $B$. If $A$ is destroyed, $B$ will be destroyed automatically.
176 ..remarks.text:- dependent: $A$ depends on $B$. $B$ should not be destroyed as long as $A$ is used.
177 ..remarks.text:- empty: there is currently no object reference stored in the holder $A$.
178 ..remarks.text:The state of the holder can be determined by @Function.empty@ and @Function.dependent@.
179 */
180
181 template <typename TValue>
182 struct Holder<TValue, Tristate>
183 {
184 public:
185         enum EHolderState
186         {
187                 EMPTY = 0,
188                 OWNER = 1,
189                 DEPENDENT = ~0
190         };
191
192 //      typedef typename _RemoveConst<TValue>::Type TValue_NotConst;
193         typedef typename Value<Holder>::Type THostValue;
194
195         typename _Pointer<THostValue>::Type data_value;
196         EHolderState data_state;
197
198 //____________________________________________________________________________
199
200 /**
201 .Memfunc.Holder:
202 ..class:Class.Holder
203 ..summary:Constructor
204 ..signature:Holder<TValue, TInfix> ()
205 ..signature:Holder<TValue, TInfix> (holder)
206 ..signature:Holder<TValue, TInfix> (value)
207 ..param.holder:Another holder object.
208 ..param.value:An object of type $TValue$.
209 ..remarks.text:
210 The default constructor creates a holder that is in state 'empty'.
211 If a $value$ is passed to the constructor, the holder will be in state 'dependent'.
212 */
213
214         Holder():
215                 data_state(EMPTY)
216         {
217 SEQAN_CHECKPOINT
218         }
219         Holder(Holder const & source_):
220                 data_state(EMPTY)
221         {
222 SEQAN_CHECKPOINT
223                 assign(*this, source_);
224         }
225         Holder(typename _Parameter<THostValue>::Type value_):
226                 data_state(EMPTY)
227         {
228 SEQAN_CHECKPOINT
229                 setValue(*this, value_);
230         }
231         Holder(typename _ConstParameter<THostValue>::Type value_):
232                 data_state(EMPTY)
233         {
234 SEQAN_CHECKPOINT
235                 assignValue(*this, value_);
236         }
237
238 /**
239 .Memfunc.~Holder:
240 ..class:Class.Holder
241 ..summary:Destructor
242 ..signature:~Holder()
243 ..remarks.text:
244 If the holder is in state 'owner', the holded object will be destoyed too.
245 */
246         ~Holder()
247         {
248 SEQAN_CHECKPOINT
249                 clear(*this);
250         }
251
252 //____________________________________________________________________________
253
254         Holder const &
255         operator = (Holder const & source_)
256         {
257 SEQAN_CHECKPOINT
258                 assign(*this, source_);
259                 return *this;
260         }
261
262         Holder const &
263         operator = (typename _ConstParameter<THostValue>::Type value_)
264         {
265 SEQAN_CHECKPOINT
266                 assignValue(*this, value_);
267                 return *this;
268         }
269
270         operator typename _Parameter<THostValue>::Type()
271         {
272 SEQAN_CHECKPOINT
273                 return *data_value;
274         }
275 //____________________________________________________________________________
276
277 };
278
279 //////////////////////////////////////////////////////////////////////////////
280 // FUNCTIONS
281 //////////////////////////////////////////////////////////////////////////////
282
283 ///.Function.empty.param.object.type:Class.Holder
284
285 template <typename TValue>
286 inline bool
287 empty(Holder<TValue, Tristate> const & me)
288 {
289 SEQAN_CHECKPOINT
290         return (me.data_state == Holder<TValue, Tristate>::EMPTY);
291 }
292
293 //////////////////////////////////////////////////////////////////////////////
294
295 ///.Function.dependent.param.object.type:Class.Holder
296
297 template <typename TValue>
298 inline bool
299 dependent(Holder<TValue, Tristate> const & me)
300 {
301 SEQAN_CHECKPOINT
302         return (me.data_state == Holder<TValue, Tristate>::DEPENDENT);
303 }
304
305 //////////////////////////////////////////////////////////////////////////////
306
307 ///.Function.clear.param.object.type:Class.Holder
308 ///.Function.clear.remarks.text:If $clear$ is applied on a @Class.Holder@ object,
309 ///the state of this object is set to 'empty'.
310
311 template <typename TValue>
312 inline void
313 clear(Holder<TValue, Tristate> & me)
314 {
315         switch (me.data_state)
316         {
317         case Holder<TValue, Tristate>::EMPTY:
318                 break;
319
320         case Holder<TValue, Tristate>::DEPENDENT:
321                 {
322 SEQAN_CHECKPOINT
323                 releaseRef(_toParameter<TValue>(me.data_value));
324                 me.data_state = Holder<TValue, Tristate>::EMPTY;
325                 }
326                 break;
327
328         default: /*Holder<TValue, TSpec>::OWNER*/
329                 {
330 SEQAN_CHECKPOINT
331                 valueDestruct(me.data_value);
332                 deallocate(me, me.data_value, 1);
333                 me.data_state = Holder<TValue, Tristate>::EMPTY;
334                 }
335                 break;
336         }
337 }
338
339 //////////////////////////////////////////////////////////////////////////////
340 /**
341 .Function.create:
342 ..summary:Makes an object to owner of its content.
343 ..cat:Dependent Objects
344 ..signature:create(holder [, object])
345 ..param.holder:A holder object.
346 ...type:Class.Holder
347 ..param.object:Object from which a copy is made and stored in $holder$. (optional)
348 ...type:Metafunction.Value.Value<Holder>::Type
349 ..remarks.text:After this operation, $holder$ will be in state 'owner'.
350 If $object$ is specified, $holder$ will hold a copy of $object$ at the end of this function.
351 If $object$ is not specified, the action depends on the former state of $holder$:
352 ..remarks.text:- If the state of $holder$ was 'empty', a new object is default constructed and stored into $holder$.
353 ..remarks.text:- If the state of $holder$ was 'dependent', a copy of the former object is made and stored into $holder$. 
354 ..remarks.text:- If the state of $holder$ was already 'owner', nothing happens.
355 ..see:Class.Holder
356 */
357
358 template <typename TValue>
359 inline void
360 create(Holder<TValue, Tristate> & me)
361 {
362         typedef Holder<TValue, Tristate> THolder;
363
364         switch (me.data_state)
365         {
366         case Holder<TValue, Tristate>::EMPTY:
367                 {
368 SEQAN_CHECKPOINT
369                 allocate(me, me.data_value, 1);
370                 valueConstruct(me.data_value);
371                 me.data_state = THolder::OWNER;
372                 }
373                 break;
374
375         case THolder::DEPENDENT:
376                 {
377 SEQAN_CHECKPOINT
378                 typename _Parameter<TValue>::Type old_value = value(me);
379                 allocate(me, me.data_value, 1);
380                 valueConstruct(me.data_value, old_value);
381                 me.data_state = THolder::OWNER;
382                 releaseRef(old_value);
383                 }
384                 break;
385         default:;
386         }
387 }
388
389 //____________________________________________________________________________
390
391 template <typename TValue>
392 inline void
393 create(Holder<TValue, Tristate> & me,
394            typename _Parameter<TValue const>::Type value_)
395 {
396 SEQAN_CHECKPOINT
397
398         if (me.data_state == Holder<TValue, Tristate>::OWNER)
399         {
400                 assign(_toParameter(me.data_value), value_);
401                 return;
402         }
403
404         clear(me);
405         allocate(me, me.data_value, 1);
406         valueConstruct(me.data_value, value_);
407         me.data_state = Holder<TValue, Tristate>::OWNER;
408 }
409
410 //////////////////////////////////////////////////////////////////////////////
411 /**
412 .Function.detach:
413 ..summary:Makes an object independent from other objects.
414 ..cat:Dependent Objects
415 ..signature:detach(object)
416 ..param.object:An object.
417 ...type:Class.Holder
418 ..remarks:
419 After this function, $object$ does not depends from any other entity outside of $object$,
420 like a @Function.source@ or a @Function.host@, and @Function.dependent.dependent(object)@ returns $false$ 
421 ..see:Function.source
422 ..see:Function.host
423 ..see:Function.createSource
424 ..see:Function.create
425 */
426
427 template <typename TValue>
428 inline void
429 detach(Holder<TValue, Tristate> & me)
430 {
431 SEQAN_CHECKPOINT
432         create(me);
433 }
434
435 //////////////////////////////////////////////////////////////////////////////
436 /**
437 .Function.setValue:
438 ..cat:Content Manipulation
439 ..summary:Makes holder dependent.
440 ..signature:setValue(holder, object)
441 ..param.holder:A holder object.
442 ...type:Class.Holder
443 ..param.object:Object from which $holder$ will be dependent.
444 ...type:Metafunction.Value.Value<Holder>::Type
445 ..remarks.text:After this operation, $holder$ will be dependent in state 'dependent'.
446 ..see:Class.Holder
447 */
448
449 template <typename TValue>
450 inline void
451 setValue(Holder<TValue, Tristate> & me,
452                  typename _Parameter<TValue>::Type value_)
453 {
454 SEQAN_CHECKPOINT
455         typedef typename Value<Holder<TValue, Tristate> >::Type THolderType;
456
457         clear(me);
458         me.data_value = _toPointer(value_);
459         me.data_state = Holder<TValue, Tristate>::DEPENDENT;
460         addRef(_toParameter<THolderType>(me.data_value));
461 }
462
463 template <typename TValue, typename TValue2>
464 inline void
465 setValue(Holder<TValue, Tristate> & me,
466                  TValue2 const & value_)
467 {
468 SEQAN_CHECKPOINT
469         set(value(me), value_);
470 }
471
472 //////////////////////////////////////////////////////////////////////////////
473
474 ///.Function.value.param.object.type:Class.Holder
475
476 template <typename TValue>
477 inline typename Reference<Holder<TValue, Tristate> >::Type
478 value(Holder<TValue, Tristate> & me)
479 {
480 SEQAN_CHECKPOINT
481         typedef Holder<TValue, Tristate> THolder;
482
483         if (empty(me))
484         {
485                 allocate(me, me.data_value, 1);
486                 valueConstruct(me.data_value);
487                 me.data_state = THolder::OWNER;
488         }
489
490         typedef typename Value<Holder<TValue, Tristate> >::Type THolderType;
491         return _toParameter<THolderType>(me.data_value);
492 }
493 template <typename TValue>
494 inline typename Reference<Holder<TValue, Tristate> const>::Type
495 value(Holder<TValue, Tristate> const & me)
496 {
497 SEQAN_CHECKPOINT
498         SEQAN_ASSERT(!empty(me));
499
500         typedef typename Value<Holder<TValue, Tristate> >::Type THolderType;
501         return _toParameter<THolderType>(me.data_value);
502 }
503
504 //////////////////////////////////////////////////////////////////////////////
505
506 ///.Function.assignValue.param.object.type:Class.Holder
507
508 template <typename TValue, typename TSource>
509 inline void
510 assignValue(Holder<TValue, Tristate> & me,
511                         TSource const & value_)
512 {
513 SEQAN_CHECKPOINT
514         typedef typename Value<Holder<TValue, Tristate> >::Type THostValue;
515         if (empty(me))
516         {
517                 create(me, value_);
518         }
519         else
520         {
521                 assign(_toParameter<THostValue>(me.data_value), value_);
522         }
523 }
524
525 //////////////////////////////////////////////////////////////////////////////
526
527 ///.Function.moveValue.param.object.type:Class.Holder
528
529 template <typename TValue, typename TSource>
530 inline void
531 moveValue(Holder<TValue, Tristate> & me,
532                   TSource const & value_)
533 {
534 SEQAN_CHECKPOINT
535         if (empty(me))
536         {
537                 create(me, value_);
538         }
539         else
540         {
541                 move(value(me), value_);
542         }
543 }
544
545 //////////////////////////////////////////////////////////////////////////////
546
547 ///.Function.assign.param.target.type:Class.Holder
548 ///.Function.assign.param.source.type:Class.Holder
549
550 template <typename TValue>
551 inline void
552 assign(Holder<TValue, Tristate> & target_,
553            Holder<TValue, Tristate> const & source_)
554 {
555 SEQAN_CHECKPOINT
556         switch(source_.data_state)
557         {
558         case Holder<TValue, Tristate>::EMPTY:
559                 {
560                 clear(target_);
561                 }
562                 break;
563
564         case Holder<TValue, Tristate>::OWNER:
565                 {
566                 assignValue(target_, value(source_));
567                 }
568                 break;
569
570         default: /*case Holder<TValue, Tristate>::DEPENDENT*/
571                 {
572                 setValue(target_, value(source_));
573                 }
574                 break;
575         }
576 }
577
578 //////////////////////////////////////////////////////////////////////////////
579 //////////////////////////////////////////////////////////////////////////////
580 // Simple Holder
581 //////////////////////////////////////////////////////////////////////////////
582 //??? TODO: Documentation of Simple Holder
583
584 template <typename TValue>
585 struct Holder<TValue, Simple>
586 {
587         typedef typename Value<Holder>::Type THolderValue;
588         typedef typename _Parameter<THolderValue>::Type THolderParameter;
589
590         mutable THolderValue data_value;
591 //____________________________________________________________________________
592
593         Holder()
594         {
595 SEQAN_CHECKPOINT
596         }
597         Holder(Holder & source_):
598                 data_value(source_.data_value)
599         {
600 SEQAN_CHECKPOINT
601         }
602         Holder(Holder const & source_):
603                 data_value(source_.data_value)
604         {
605 SEQAN_CHECKPOINT
606         }
607         template <typename TSource>
608         Holder(TSource & value_):
609                 data_value(value_)
610         {
611 SEQAN_CHECKPOINT
612         }
613         template <typename TSource>
614         Holder(TSource const & value_):
615                 data_value(value_)
616         {
617 SEQAN_CHECKPOINT
618         }
619 /*
620         Holder(TValue const & value_):
621                 data_value(value_)
622         {
623 SEQAN_CHECKPOINT
624         }
625 */
626         ~Holder()
627         {
628 SEQAN_CHECKPOINT
629         }
630
631 //____________________________________________________________________________
632
633         Holder const &
634         operator = (Holder const & source_)
635         {
636 SEQAN_CHECKPOINT
637                 data_value = source_.data_value;
638                 return *this;
639         }
640
641         Holder const &
642         operator = (THolderValue const & value_)
643         {
644 SEQAN_CHECKPOINT
645                 data_value = value_;
646                 return *this;
647         }
648
649         operator THolderParameter()
650         {
651 SEQAN_CHECKPOINT
652                 return *data_value;
653         }
654 //____________________________________________________________________________
655 };
656
657
658 //////////////////////////////////////////////////////////////////////////////
659 // FUNCTIONS
660 //////////////////////////////////////////////////////////////////////////////
661
662 template <typename TValue>
663 inline bool
664 empty(Holder<TValue, Simple> const & me)
665 {
666 SEQAN_CHECKPOINT
667         return false;
668 }
669
670 //////////////////////////////////////////////////////////////////////////////
671
672 template <typename TValue>
673 inline bool
674 dependent(Holder<TValue, Simple> const & me)
675 {
676 SEQAN_CHECKPOINT
677         return false;
678 }
679
680 //////////////////////////////////////////////////////////////////////////////
681
682 template <typename TValue>
683 inline void
684 clear(Holder<TValue, Simple> & me)
685 {
686 SEQAN_CHECKPOINT
687 }
688
689 //////////////////////////////////////////////////////////////////////////////
690
691 template <typename TValue>
692 inline void
693 create(Holder<TValue, Simple> & me)
694 {
695 SEQAN_CHECKPOINT
696 }
697
698 //____________________________________________________________________________
699
700 template <typename TValue>
701 inline void
702 create(Holder<TValue, Simple> & me,
703            TValue const & value_)
704 {
705 SEQAN_CHECKPOINT
706         me.data_value = value_;
707 }
708
709 //////////////////////////////////////////////////////////////////////////////
710
711 template <typename TValue>
712 inline void
713 detach(Holder<TValue, Simple> & me)
714 {
715 SEQAN_CHECKPOINT
716 }
717
718 //////////////////////////////////////////////////////////////////////////////
719
720 template <typename TValue>
721 inline void
722 setValue(Holder<TValue, Simple> & me,
723                  TValue const & value_)
724 {
725 SEQAN_CHECKPOINT
726         me.data_value = value_;
727 }
728
729 //////////////////////////////////////////////////////////////////////////////
730
731 template <typename TValue>
732 inline typename Reference<Holder<TValue, Simple> >::Type
733 value(Holder<TValue, Simple> & me)
734 {
735 SEQAN_CHECKPOINT
736         return me.data_value;
737 }
738 template <typename TValue>
739 inline typename Reference<Holder<TValue, Simple> const>::Type
740 value(Holder<TValue, Simple> const & me)
741 {
742 SEQAN_CHECKPOINT
743         return me.data_value;
744 }
745
746 //////////////////////////////////////////////////////////////////////////////
747
748 template <typename TValue, typename TSource>
749 inline void
750 assignValue(Holder<TValue, Simple> & me,
751                         TSource const & value_)
752 {
753 SEQAN_CHECKPOINT
754         assignValue(me.data_value, value_);
755 }
756
757 //////////////////////////////////////////////////////////////////////////////
758
759 template <typename TValue, typename TSource>
760 inline void
761 moveValue(Holder<TValue, Simple> & me,
762                   TSource const & value_)
763 {
764 SEQAN_CHECKPOINT
765         move(me.data_value, value_);
766 }
767
768 //////////////////////////////////////////////////////////////////////////////
769
770 template <typename TValue>
771 inline void
772 assign(Holder<TValue, Simple> & target_,
773            Holder<TValue, Simple> const & source_)
774 {
775 SEQAN_CHECKPOINT
776         assignValue(target_, source_);
777 }
778
779
780 //////////////////////////////////////////////////////////////////////////////
781 //////////////////////////////////////////////////////////////////////////////
782
783
784
785
786 //////////////////////////////////////////////////////////////////////////////
787 // New Tristate Holder that works also on pointers
788 //////////////////////////////////////////////////////////////////////////////
789
790 struct Tristate2;
791
792 template <typename TValue>
793 struct Holder<TValue, Tristate2>
794 {
795 public:
796         enum EHolderState
797         {
798                 EMPTY = 0,
799                 OWNER = 1,
800                 DEPENDENT = ~0
801         };
802
803         typedef typename Value<Holder>::Type THostValue;
804
805         TValue * data_value;
806         EHolderState data_state;
807
808 //____________________________________________________________________________
809
810         Holder():
811                 data_state(EMPTY)
812         {
813 SEQAN_CHECKPOINT
814         }
815         Holder(Holder const & source_):
816                 data_state(EMPTY)
817         {
818 SEQAN_CHECKPOINT
819                 assign(*this, source_);
820         }
821         Holder(typename _Parameter<THostValue>::Type value_):
822                 data_state(EMPTY)
823         {
824 SEQAN_CHECKPOINT
825                 setValue(*this, value_);
826         }
827         Holder(typename _ConstParameter<THostValue>::Type value_):
828                 data_state(EMPTY)
829         {
830 SEQAN_CHECKPOINT
831                 assignValue(*this, value_);
832         }
833         ~Holder()
834         {
835 SEQAN_CHECKPOINT
836                 clear(*this);
837         }
838
839 //____________________________________________________________________________
840
841         Holder const &
842         operator = (Holder const & source_)
843         {
844 SEQAN_CHECKPOINT
845                 assign(*this, source_);
846                 return *this;
847         }
848
849         Holder const &
850         operator = (TValue const & value_)
851         {
852 SEQAN_CHECKPOINT
853                 assignValue(*this, value_);
854                 return *this;
855         }
856
857         operator TValue &()
858         {
859 SEQAN_CHECKPOINT
860                 return *data_value;
861         }
862 //____________________________________________________________________________
863
864 };
865
866 //////////////////////////////////////////////////////////////////////////////
867 // FUNCTIONS
868 //////////////////////////////////////////////////////////////////////////////
869
870 ///.Function.empty.param.object.type:Class.Holder
871
872 template <typename TValue>
873 inline bool
874 empty(Holder<TValue, Tristate2> const & me)
875 {
876 SEQAN_CHECKPOINT
877         return (me.data_state == Holder<TValue, Tristate2>::EMPTY);
878 }
879
880 //////////////////////////////////////////////////////////////////////////////
881
882 ///.Function.dependent.param.object.type:Class.Holder
883
884 template <typename TValue>
885 inline bool
886 dependent(Holder<TValue, Tristate2> const & me)
887 {
888 SEQAN_CHECKPOINT
889         return (me.data_state == Holder<TValue, Tristate2>::DEPENDENT);
890 }
891
892 //////////////////////////////////////////////////////////////////////////////
893
894 ///.Function.clear.param.object.type:Class.Holder
895 ///.Function.clear.remarks.text:If $clear$ is applied on a @Class.Holder@ object,
896 ///the state of this object is set to 'empty'.
897
898 template <typename TValue>
899 inline void
900 clear(Holder<TValue, Tristate2> & me)
901 {
902         switch (me.data_state)
903         {
904         case Holder<TValue, Tristate2>::EMPTY:
905                 break;
906
907         case Holder<TValue, Tristate2>::DEPENDENT:
908                 {
909 SEQAN_CHECKPOINT
910                 releaseRef(*(me.data_value));
911                 me.data_state = Holder<TValue, Tristate2>::EMPTY;
912                 }
913                 break;
914
915         default: /*Holder<TValue, TSpec>::OWNER*/
916                 {
917 SEQAN_CHECKPOINT
918                 valueDestruct(me.data_value);
919                 deallocate(me, me.data_value, 1);
920                 me.data_state = Holder<TValue, Tristate2>::EMPTY;
921                 }
922                 break;
923         }
924 }
925
926 //////////////////////////////////////////////////////////////////////////////
927
928
929 template <typename TValue>
930 inline void
931 create(Holder<TValue, Tristate2> & me)
932 {
933         typedef Holder<TValue, Tristate2> THolder;
934
935         switch (me.data_state)
936         {
937         case Holder<TValue, Tristate2>::EMPTY:
938                 {
939 SEQAN_CHECKPOINT
940                 allocate(me, me.data_value, 1);
941                 valueConstruct(me.data_value);
942                 me.data_state = THolder::OWNER;
943                 }
944                 break;
945
946         case THolder::DEPENDENT:
947                 {
948 SEQAN_CHECKPOINT
949                 TValue & old_value = value(me);
950                 allocate(me, me.data_value, 1);
951                 valueConstruct(me.data_value, old_value);
952                 me.data_state = THolder::OWNER;
953                 releaseRef(old_value);
954                 }
955                 break;
956         default:;
957         }
958 }
959
960 //____________________________________________________________________________
961
962 template <typename TValue>
963 inline void
964 create(Holder<TValue, Tristate2> & me,
965            TValue const & value_)
966 {
967 SEQAN_CHECKPOINT
968
969         if (me.data_state == Holder<TValue, Tristate2>::OWNER)
970         {
971                 assign(*(me.data_value), value_);
972                 return;
973         }
974
975         clear(me);
976         allocate(me, me.data_value, 1);
977         valueConstruct(me.data_value, value_);
978         me.data_state = Holder<TValue, Tristate2>::OWNER;
979 }
980
981 //////////////////////////////////////////////////////////////////////////////
982
983
984 template <typename TValue>
985 inline void
986 detach(Holder<TValue, Tristate2> & me)
987 {
988 SEQAN_CHECKPOINT
989         create(me);
990 }
991
992 //////////////////////////////////////////////////////////////////////////////
993
994
995 template <typename TValue>
996 inline void
997 setValue(Holder<TValue, Tristate2> & me,
998                  TValue & value_)
999 {
1000 SEQAN_CHECKPOINT
1001         typedef typename Value<Holder<TValue, Tristate2> >::Type THolderType;
1002
1003         clear(me);
1004         me.data_value = & value_;
1005         me.data_state = Holder<TValue, Tristate2>::DEPENDENT;
1006         addRef(value_);
1007 }
1008
1009 //////////////////////////////////////////////////////////////////////////////
1010
1011 ///.Function.value.param.object.type:Class.Holder
1012
1013 template <typename TValue>
1014 inline typename Reference<Holder<TValue, Tristate2> >::Type
1015 value(Holder<TValue, Tristate2> & me)
1016 {
1017 SEQAN_CHECKPOINT
1018         typedef Holder<TValue, Tristate2> THolder;
1019
1020         if (empty(me))
1021         {
1022                 allocate(me, me.data_value, 1);
1023                 valueConstruct(me.data_value);
1024                 me.data_state = THolder::OWNER;
1025         }
1026
1027         typedef typename Value<Holder<TValue, Tristate2> >::Type THolderType;
1028         return *(me.data_value);
1029 }
1030 template <typename TValue>
1031 inline typename Reference<Holder<TValue, Tristate2> const>::Type
1032 value(Holder<TValue, Tristate2> const & me)
1033 {
1034 SEQAN_CHECKPOINT
1035         SEQAN_ASSERT(!empty(me));
1036
1037         return *(me.data_value);
1038 }
1039
1040
1041 //////////////////////////////////////////////////////////////////////////////
1042
1043 ///.Function.assignValue.param.object.type:Class.Holder
1044
1045 template <typename TValue, typename TSource>
1046 inline void
1047 assignValue(Holder<TValue, Tristate2> & me,
1048                         TSource const & value_)
1049 {
1050 SEQAN_CHECKPOINT
1051         typedef typename Value<Holder<TValue, Tristate2> >::Type THostValue;
1052         if (empty(me))
1053         {
1054                 create(me, value_);
1055         }
1056         else
1057         {
1058                 assign(*(me.data_value), value_);
1059         }
1060 }
1061
1062 //////////////////////////////////////////////////////////////////////////////
1063
1064 ///.Function.moveValue.param.object.type:Class.Holder
1065
1066 template <typename TValue, typename TSource>
1067 inline void
1068 moveValue(Holder<TValue, Tristate2> & me,
1069                   TSource const & value_)
1070 {
1071 SEQAN_CHECKPOINT
1072         if (empty(me))
1073         {
1074                 create(me, value_);
1075         }
1076         else
1077         {
1078                 move(value(me), value_);
1079         }
1080 }
1081
1082 //////////////////////////////////////////////////////////////////////////////
1083
1084 ///.Function.assign.param.target.type:Class.Holder
1085 ///.Function.assign.param.source.type:Class.Holder
1086
1087 template <typename TValue>
1088 inline void
1089 assign(Holder<TValue, Tristate2> & target_,
1090            Holder<TValue, Tristate2> const & source_)
1091 {
1092 SEQAN_CHECKPOINT
1093         switch(source_.data_state)
1094         {
1095         case Holder<TValue, Tristate2>::EMPTY:
1096                 {
1097                 clear(target_);
1098                 }
1099                 break;
1100
1101         case Holder<TValue, Tristate2>::OWNER:
1102                 {
1103                 assignValue(target_, value(source_));
1104                 }
1105                 break;
1106
1107         default: /*case Holder<TValue, Tristate2>::DEPENDENT*/
1108                 {
1109                 setValue(target_, value(source_));
1110                 }
1111                 break;
1112         }
1113 }
1114 //////////////////////////////////////////////////////////////////////////////
1115
1116 } //namespace SEQAN_NAMESPACE_MAIN
1117
1118 #endif //#ifndef SEQAN_HEADER_...
1119
1120