1 /*==========================================================================
2 SeqAn - The Library for Sequence Analysis
4 ============================================================================
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.
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.
17 ============================================================================
18 $Id: basic_volatile_ptr.h,v 1.1 2008/08/25 16:20:01 langmead Exp $
19 ==========================================================================*/
21 #ifndef SEQAN_HEADER_BASIC_VOLATILE_PTR_H
22 #define SEQAN_HEADER_BASIC_VOLATILE_PTR_H
24 //////////////////////////////////////////////////////////////////////////////
26 namespace SEQAN_NAMESPACE_MAIN
29 //////////////////////////////////////////////////////////////////////////////
31 // allows you to handle volatile data (used by ext. string during swapping)
33 // imagine volatile pointers as nodes in an undirected graph
34 // when you assign one to another then they are connected
35 // all pointers in a connection component points to the same value
36 // by calling nukeCopies you can destroy the component and set all pointers to NULL
38 template < typename Type >
41 typedef VolatilePtr _Self;
42 typedef VolatilePtr* _SelfPtr;
43 typedef VolatilePtr& _SelfRef;
45 typedef Type& reference;
46 typedef const Type& const_reference;
47 typedef Type* pointer;
50 _SelfPtr next; // prev == NULL means this is the master node
51 _SelfPtr prev; // prev == NULL means this is the master node
53 VolatilePtr() { // volatile pinters behave like normal pointers
54 prev = this; // and are not initialized (ptr) per default
58 VolatilePtr(const pointer _p) {
64 VolatilePtr(const _Self& _vp) {
70 VolatilePtr(_SelfRef _vp) {
81 template <typename size_type>
82 inline reference operator[] (size_type offset) {
86 template <typename size_type>
87 inline const_reference operator[] (size_type offset) const {
91 inline _Self& operator=(_Self const &_Right) {
94 if (ptr) hangOn(const_cast<_Self&>(_Right));
98 inline _Self& operator=(pointer const _Right) {
104 inline bool isLonely() {
108 inline void nukeCopies() {
111 _SelfPtr tmp = p->next;
121 inline bool operator== (const _Self &I) const {
125 inline bool operator!= (const _Self &I) const {
129 inline operator pointer () const {
135 inline void hangOn(_SelfRef _prev) {
136 // hang on between _prev and _prev.next
143 inline void hangOff() {
151 template <typename TValue>
152 inline void nukeCopies(TValue* &) {}
154 template <typename TValue>
155 inline void nukeCopies(VolatilePtr<TValue> &ptr) { ptr.nukeCopies(); }