Imported Upstream version 0.12.7
[bowtie.git] / SeqAn-1.1 / seqan / basic / basic_counted_ptr.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_counted_ptr.h,v 1.1 2008/08/25 16:20:01 langmead Exp $
19  ==========================================================================*/
20
21 // THIS FILE IS CURRENTLY NOT USED IN SEQAN
22
23 #ifndef SEQAN_HEADER_BASIC_COUNTED_PTR_H
24 #define SEQAN_HEADER_BASIC_COUNTED_PTR_H
25
26 namespace SEQAN_NAMESPACE_MAIN
27 {
28
29         //////////////////////////////////////////////////////////////////////////////
30         // counted pointer
31
32         template < typename Type >
33         struct CountedPtr
34         {
35                 typedef CountedPtr              _Self;
36                 typedef CountedPtr*         _SelfPtr;
37                 typedef CountedPtr&         _SelfRef;
38
39                 typedef Type&                   reference;
40                 typedef const Type&             const_reference;
41                 typedef Type*                   pointer;
42
43         explicit CountedPtr(pointer p = 0): // allocate a new counter
44             itsCounter(0)
45         {
46             if (p) itsCounter = new counter(p);
47         }
48
49         CountedPtr(const _Self& r) throw() {
50             acquire(r.itsCounter);
51         }
52
53         ~CountedPtr() {
54             release();
55         }
56
57         CountedPtr& operator=(const _Self& r)
58         {
59             if (this != &r) {
60                 release();
61                 acquire(r.itsCounter);
62             }
63             return *this;
64         }
65
66         reference operator*() const throw() {
67             return *itsCounter->ptr;
68         }
69
70         pointer operator->() const throw() {
71             return itsCounter->ptr;
72         }
73
74         pointer get() const throw() {
75             return itsCounter ? itsCounter->ptr : 0;
76         }
77
78         bool unique() const throw() {
79             return (itsCounter ? itsCounter->count == 1 : true);
80         }
81
82                 inline operator pointer () const {
83             return get();
84                 }
85
86     private:
87
88         struct counter {
89             pointer     ptr;
90             unsigned    count;
91             counter(pointer p = 0, unsigned c = 1):
92                 ptr(p),
93                 count(c) { }
94         }* itsCounter;
95
96         void acquire(counter* c) throw()
97         { // increment the count
98             itsCounter = c;
99             if (c) ++c->count;
100         }
101
102         void release()
103         { // decrement the count, delete if it is 0
104             if (itsCounter) {
105                 if (--itsCounter->count == 0) {
106                     delete itsCounter->ptr;
107                     delete itsCounter;
108                 }
109                 itsCounter = 0;
110             }
111         }
112     };
113
114 }
115
116 #endif