Commit patch to not break on spaces.
[bowtie.git] / SeqAn-1.1 / seqan / basic / basic_allocator_to_std.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_allocator_to_std.h,v 1.1 2008/08/25 16:20:01 langmead Exp $
19  ==========================================================================*/
20
21 #ifndef SEQAN_HEADER_BASIC_ALLOCATOR_TO_STD_H
22 #define SEQAN_HEADER_BASIC_ALLOCATOR_TO_STD_H
23
24
25 namespace SEQAN_NAMESPACE_MAIN
26 {
27
28 //////////////////////////////////////////////////////////////////////////////
29 //helper caller for calling functions that have same name as member functions
30
31 template <typename TMe, typename TValue, typename TSize>
32 inline void call_allocate(TMe & me, TValue * & data, TSize const count)
33 {
34         allocate(me, data, count);
35 }
36 template <typename TMe, typename TValue, typename TSize>
37 inline void call_deallocate(TMe & me, TValue * data, TSize const count)
38 {
39         deallocate(me, data, count);
40 }
41
42 //////////////////////////////////////////////////////////////////////////////
43 //Filter that adapts seqan allocator zu std allocator
44 /**
45 .Class.ToStdAllocator:
46 ..summary:Emulates standard conform allocator.
47 ..signature:ToStdAllocator<THost, TValue>
48 ..param.THost:Type of the host allocator object.
49 ...text:This object is used to call @Function.allocate@ and @Function.deallocate@.
50 ..param.TValue:Type of allocated items.
51 ..remarks:The member functions $allocate$ and $deallocate$ of $ToStdAllocator$ call
52 the (globale) functions @Function.allocate@ and @Function.deallocate@, respectively. The globale functions
53 get an allocator object as their first arguments. This allocator object is not the $ToStdAllocator$ object itself,
54 but the host object that was given to the constructor. 
55 ..remarks:
56 ..see:Function.allocate
57 ..see:Function.deallocate
58 */
59 template <typename THost, typename TValue>
60 struct ToStdAllocator
61 {
62         typedef TValue value_type;
63         typedef value_type * pointer;
64         typedef value_type & reference;
65         typedef value_type const * const_pointer;
66         typedef value_type const & const_reference;
67
68 //      typedef typename THost::Size size_type;
69 //      typedef typename THost::Difference difference_type;
70         typedef size_t size_type;
71         typedef ptrdiff_t difference_type;
72
73 /**
74 .Memfunc.ToStdAllocator:
75 ..summary:Constructor
76 ..signature:ToStdAllocator(host)
77 ..class:Class.ToStdAllocator
78 ..param.host:The host object that is used as allocator for @Function.allocate@ and @Function.deallocate@.
79 */
80         ToStdAllocator(THost & host): m_host(& host)
81         {
82         }
83         ToStdAllocator(ToStdAllocator const & alloc): m_host(alloc.m_host)
84         {
85         }
86         ToStdAllocator & operator= (ToStdAllocator const & alloc)
87         {
88                 m_host = alloc.m_host;
89         }
90         ~ToStdAllocator()
91         {
92         }
93
94 /**
95 .Function.host:
96 ..summary:The object a given object depends on.
97 ..cat:Dependent Objects
98 ..signature:host(object)
99 ..param.object:An object.
100 ...type:Class.ToStdAllocator
101 ..returns:The host object.
102 */
103     friend THost & host(ToStdAllocator & me)
104     {
105         return *me.m_host;
106     }
107
108         pointer allocate(size_type count)
109         {
110                 value_type * ptr;
111                 call_allocate(*m_host, ptr, count);
112                 return pointer(ptr);
113         }
114         pointer allocate(size_type count, const void *)
115         {
116                 value_type * ptr;
117                 call_allocate(*m_host, ptr, count);
118                 return pointer(ptr);
119         }
120
121         void deallocate(pointer data, size_type count)
122         {
123                 call_deallocate(*m_host, data, count);
124         }
125
126         void construct(pointer ptr, const_reference data)
127         {
128                 new(ptr) TValue(data);
129         }
130
131         void destroy(pointer ptr)
132         {
133                 ptr->~TValue();
134         }
135
136         pointer address(reference value) const
137         {
138                 return (&value);
139         }
140         const_pointer address(const_reference value) const
141         {
142                 return (&value);
143         }
144
145         size_type max_size() const
146         {
147                 return ~0UL / sizeof(value_type);
148         }
149
150         template<class TValue2>
151         struct rebind
152         {
153                 typedef ToStdAllocator<THost, TValue2> other;
154         };
155
156         private:
157                 THost * m_host;
158 };
159 //////////////////////////////////////////////////////////////////////////////
160
161
162
163 //returns std-allocator type (for allocators)
164 template <typename T, typename TData>
165 struct StdAllocator
166 {
167         typedef ToStdAllocator<T, TData> Type;
168 };
169
170
171 //////////////////////////////////////////////////////////////////////////////
172
173 } //namespace SEQAN_NAMESPACE_MAIN
174
175
176 //////////////////////////////////////////////////////////////////////////////
177
178 #endif //#ifndef SEQAN_HEADER_...