aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/list.h214
-rw-r--r--src/old/sptr.cpp1
-rw-r--r--src/old/sptr.h99
-rw-r--r--src/serversocket.h10
-rw-r--r--src/sptr.cpp1
-rw-r--r--src/sptr.h147
-rw-r--r--src/tests/list.cpp31
7 files changed, 382 insertions, 121 deletions
diff --git a/src/list.h b/src/list.h
index ec63496..4d16872 100644
--- a/src/list.h
+++ b/src/list.h
@@ -13,6 +13,15 @@ namespace Bu
13 ListLink *pNext; 13 ListLink *pNext;
14 ListLink *pPrev; 14 ListLink *pPrev;
15 }; 15 };
16
17 /**
18 * Linked list template container. This class is similar to the stl list
19 * class except for a few minor changes. First, it doesn't mimic a stack or
20 * queue, use the Stack or Queue clasess for that. Second, when const, all
21 * members are only accessable const. Third, erasing a location does not
22 * invalidate the iterator, it simply points to the next valid location, or
23 * end() if there are no more.
24 */
16 template<typename value, typename valuealloc=std::allocator<value>, typename linkalloc=std::allocator<struct ListLink<value> > > 25 template<typename value, typename valuealloc=std::allocator<value>, typename linkalloc=std::allocator<struct ListLink<value> > >
17 class List 26 class List
18 { 27 {
@@ -23,15 +32,59 @@ namespace Bu
23 public: 32 public:
24 List() : 33 List() :
25 pFirst( NULL ), 34 pFirst( NULL ),
26 pLast( NULL ) 35 pLast( NULL ),
36 nSize( 0 )
37 {
38 }
39
40 List( const MyType &src ) :
41 pFirst( NULL ),
42 pLast( NULL ),
43 nSize( 0 )
44 {
45 for( Link *pCur = src.pFirst; pCur; pCur = pCur->pNext )
46 {
47 append( *pCur->pValue );
48 }
49 }
50
51 ~List()
52 {
53 clear();
54 }
55
56 MyType &operator=( const MyType &src )
27 { 57 {
58 clear();
59 for( Link *pCur = src.pFirst; pCur; pCur = pCur->pNext )
60 {
61 append( *pCur->pValue );
62 }
63 }
64
65 void clear()
66 {
67 Link *pCur = pFirst;
68 for(;;)
69 {
70 if( pCur == NULL ) break;
71 va.destroy( pCur->pValue );
72 va.deallocate( pCur->pValue, sizeof( value ) );
73 Link *pTmp = pCur->pNext;
74 la.destroy( pCur );
75 la.deallocate( pCur, sizeof( Link ) );
76 pCur = pTmp;
77 }
78 pFirst = pLast = NULL;
79 nSize = 0;
28 } 80 }
29 81
30 void append( value v ) 82 void append( const value &v )
31 { 83 {
32 Link *pNew = la.allocate( sizeof( Link ) ); 84 Link *pNew = la.allocate( sizeof( Link ) );
33 pNew->pValue = va.allocate( sizeof( value ) ); 85 pNew->pValue = va.allocate( sizeof( value ) );
34 va.construct( pNew->pValue, v ); 86 va.construct( pNew->pValue, v );
87 nSize++;
35 if( pFirst == NULL ) 88 if( pFirst == NULL )
36 { 89 {
37 // Empty list 90 // Empty list
@@ -47,11 +100,12 @@ namespace Bu
47 } 100 }
48 } 101 }
49 102
50 void prepend( value v ) 103 void prepend( const value &v )
51 { 104 {
52 Link *pNew = la.allocate( sizeof( Link ) ); 105 Link *pNew = la.allocate( sizeof( Link ) );
53 pNew->pValue = va.allocate( sizeof( value ) ); 106 pNew->pValue = va.allocate( sizeof( value ) );
54 va.construct( pNew->pValue, v ); 107 va.construct( pNew->pValue, v );
108 nSize++;
55 if( pFirst == NULL ) 109 if( pFirst == NULL )
56 { 110 {
57 // Empty list 111 // Empty list
@@ -83,22 +137,22 @@ namespace Bu
83 } 137 }
84 138
85 public: 139 public:
86 bool operator==( const iterator &oth ) 140 bool operator==( const iterator &oth ) const
87 { 141 {
88 return ( pLink == oth.pLink ); 142 return ( pLink == oth.pLink );
89 } 143 }
90 144
91 bool operator==( const Link *pOth ) 145 bool operator==( const Link *pOth ) const
92 { 146 {
93 return ( pLink == pOth ); 147 return ( pLink == pOth );
94 } 148 }
95 149
96 bool operator!=( const iterator &oth ) 150 bool operator!=( const iterator &oth ) const
97 { 151 {
98 return ( pLink != oth.pLink ); 152 return ( pLink != oth.pLink );
99 } 153 }
100 154
101 bool operator!=( const Link *pOth ) 155 bool operator!=( const Link *pOth ) const
102 { 156 {
103 return ( pLink != pOth ); 157 return ( pLink != pOth );
104 } 158 }
@@ -110,40 +164,128 @@ namespace Bu
110 164
111 value *operator->() 165 value *operator->()
112 { 166 {
113 return pLink->pValue(); 167 return pLink->pValue;
114 } 168 }
115 169
116 iterator operator++() 170 iterator &operator++()
117 { 171 {
118 if( pLink != NULL ) 172 if( pLink != NULL )
119 pLink = pLink->pNext; 173 pLink = pLink->pNext;
120 return *this; 174 return *this;
121 } 175 }
122 176
123 iterator operator--() 177 iterator &operator--()
124 { 178 {
125 if( pLink != NULL ) 179 if( pLink != NULL )
126 pLink = pLink->pPrev; 180 pLink = pLink->pPrev;
127 return *this; 181 return *this;
128 } 182 }
129 183
130 iterator operator++( int ) 184 iterator &operator++( int )
131 { 185 {
132 if( pLink != NULL ) 186 if( pLink != NULL )
133 pLink = pLink->pNext; 187 pLink = pLink->pNext;
134 return *this; 188 return *this;
135 } 189 }
136 190
137 iterator operator--( int ) 191 iterator &operator--( int )
138 { 192 {
139 if( pLink != NULL ) 193 if( pLink != NULL )
140 pLink = pLink->pPrev; 194 pLink = pLink->pPrev;
141 return *this; 195 return *this;
142 } 196 }
143 197
144 iterator operator=( const iterator &oth ) 198 iterator &operator=( const iterator &oth )
145 { 199 {
146 pLink = oth.pLink; 200 pLink = oth.pLink;
201 return *this;
202 }
203 };
204
205 typedef struct const_iterator
206 {
207 friend class List<value, valuealloc, linkalloc>;
208 private:
209 Link *pLink;
210 const_iterator() :
211 pLink( NULL )
212 {
213 }
214
215 const_iterator( Link *pLink ) :
216 pLink( pLink )
217 {
218 }
219
220 public:
221 bool operator==( const const_iterator &oth ) const
222 {
223 return ( pLink == oth.pLink );
224 }
225
226 bool operator==( const Link *pOth ) const
227 {
228 return ( pLink == pOth );
229 }
230
231 bool operator!=( const const_iterator &oth ) const
232 {
233 return ( pLink != oth.pLink );
234 }
235
236 bool operator!=( const Link *pOth ) const
237 {
238 return ( pLink != pOth );
239 }
240
241 const value &operator*()
242 {
243 return *(pLink->pValue);
244 }
245
246 const value *operator->()
247 {
248 return pLink->pValue;
249 }
250
251 const_iterator &operator++()
252 {
253 if( pLink != NULL )
254 pLink = pLink->pNext;
255 return *this;
256 }
257
258 const_iterator &operator--()
259 {
260 if( pLink != NULL )
261 pLink = pLink->pPrev;
262 return *this;
263 }
264
265 const_iterator &operator++( int )
266 {
267 if( pLink != NULL )
268 pLink = pLink->pNext;
269 return *this;
270 }
271
272 const_iterator &operator--( int )
273 {
274 if( pLink != NULL )
275 pLink = pLink->pPrev;
276 return *this;
277 }
278
279 const_iterator &operator=( const iterator &oth )
280 {
281 pLink = oth.pLink;
282 return *this;
283 }
284
285 const_iterator &operator=( const const_iterator &oth )
286 {
287 pLink = oth.pLink;
288 return *this;
147 } 289 }
148 }; 290 };
149 291
@@ -152,17 +294,50 @@ namespace Bu
152 return iterator( pFirst ); 294 return iterator( pFirst );
153 } 295 }
154 296
155 const Link *end() 297 const const_iterator begin() const
298 {
299 return const_iterator( pFirst );
300 }
301
302 const Link *end() const
156 { 303 {
157 return NULL; 304 return NULL;
158 } 305 }
159 306
160 int getSize() 307 void erase( iterator &i )
308 {
309 Link *pCur = i.pLink;
310 Link *pPrev = pCur->pPrev;
311 if( pPrev == NULL )
312 {
313 va.destroy( pCur->pValue );
314 va.deallocate( pCur->pValue, sizeof( value ) );
315 pFirst = pCur->pNext;
316 la.destroy( pCur );
317 la.deallocate( pCur, sizeof( Link ) );
318 if( pFirst == NULL )
319 pLast = NULL;
320 nSize--;
321 i.pLink = pFirst;
322 }
323 else
324 {
325 va.destroy( pCur->pValue );
326 va.deallocate( pCur->pValue, sizeof( value ) );
327 Link *pTmp = pCur->pNext;
328 la.destroy( pCur );
329 la.deallocate( pCur, sizeof( Link ) );
330 pPrev->pNext = pTmp;
331 if( pTmp != NULL )
332 pTmp->pPrev = pPrev;
333 nSize--;
334 i.pLink = pTmp;
335 }
336 }
337
338 int getSize() const
161 { 339 {
162 int j = 0; 340 return nSize;
163 for( Link *pCur = pFirst; pCur; pCur = pCur->pNext )
164 j++;
165 return j;
166 } 341 }
167 342
168 private: 343 private:
@@ -170,6 +345,7 @@ namespace Bu
170 Link *pLast; 345 Link *pLast;
171 linkalloc la; 346 linkalloc la;
172 valuealloc va; 347 valuealloc va;
348 int nSize;
173 }; 349 };
174} 350}
175 351
diff --git a/src/old/sptr.cpp b/src/old/sptr.cpp
deleted file mode 100644
index 7f5e894..0000000
--- a/src/old/sptr.cpp
+++ /dev/null
@@ -1 +0,0 @@
1#include "sptr.h"
diff --git a/src/old/sptr.h b/src/old/sptr.h
deleted file mode 100644
index deae94d..0000000
--- a/src/old/sptr.h
+++ /dev/null
@@ -1,99 +0,0 @@
1#ifndef SPTR_H
2#define SPTR_H
3
4#include <stdint.h>
5#include <stdio.h>
6
7template<typename T>
8class SPtr
9{
10public:
11 SPtr() :
12 pRefCnt( NULL ),
13 pData( NULL )
14 {
15 }
16
17 ~SPtr()
18 {
19 decCount();
20 }
21
22 SPtr( const SPtr<T> &src ) :
23 pRefCnt( src.pRefCnt ),
24 pData( src.pData )
25 {
26 if( pRefCnt )
27 (*pRefCnt) += 1;
28 }
29
30 SPtr( T *pSrc ) :
31 pRefCnt( NULL ),
32 pData( pSrc )
33 {
34 pRefCnt = new int32_t;
35 (*pRefCnt) = 1;
36 }
37
38 int32_t count()
39 {
40 return *pRefCnt;
41 }
42
43 T *operator->() const
44 {
45 return pData;
46 }
47
48 T *operator*() const
49 {
50 return pData;
51 }
52
53 SPtr<T> operator=( const SPtr<T> &src )
54 {
55 decCount();
56 pRefCnt = src.pRefCnt;
57 pData = src.pData;
58 (*pRefCnt) += 1;
59
60 return *this;
61 }
62
63 bool operator==( const SPtr<T> &src )
64 {
65 return pData == src.pData;
66 }
67
68 operator bool()
69 {
70 return pRefCnt != NULL;
71 }
72
73 bool isSet()
74 {
75 return pRefCnt != NULL;
76 }
77
78private:
79 void decCount()
80 {
81 if( pRefCnt )
82 {
83 (*pRefCnt) -= 1;
84 //printf("Decrementing ref-count to %d\n", *pRefCnt );
85 if( (*pRefCnt) == 0 )
86 {
87 delete pRefCnt;
88 delete pData;
89 pRefCnt = NULL;
90 pData = NULL;
91 }
92 }
93 }
94
95 int32_t *pRefCnt;
96 T *pData;
97};
98
99#endif
diff --git a/src/serversocket.h b/src/serversocket.h
index 9a26e2d..f146d91 100644
--- a/src/serversocket.h
+++ b/src/serversocket.h
@@ -2,13 +2,19 @@
2#define SERVER_SOCKET_H 2#define SERVER_SOCKET_H
3 3
4#include <stdint.h> 4#include <stdint.h>
5#include "fstring.h" 5#include "bu/fstring.h"
6#include "socket.h"
7 6
8namespace Bu 7namespace Bu
9{ 8{
10 /** 9 /**
10 * A single tcp/ip server socket. When created the server socket will bind
11 * to the specified interface and port, and immediately begin listening for
12 * connections. When connections come in they are pooled by the networking
13 * drivers in the kernel until they are accepted, this means that failure
14 * to keep space in the connection pool will result in connection refusals.
11 * 15 *
16 * Although the accept function returns an integral file descriptor, it is
17 * designed to be used with the Socket class.
12 */ 18 */
13 class ServerSocket 19 class ServerSocket
14 { 20 {
diff --git a/src/sptr.cpp b/src/sptr.cpp
new file mode 100644
index 0000000..8ea7f8f
--- /dev/null
+++ b/src/sptr.cpp
@@ -0,0 +1 @@
#include "bu/sptr.h"
diff --git a/src/sptr.h b/src/sptr.h
new file mode 100644
index 0000000..faa8524
--- /dev/null
+++ b/src/sptr.h
@@ -0,0 +1,147 @@
1#ifndef SPTR_H
2#define SPTR_H
3
4#include <stdint.h>
5#include <stdio.h>
6
7namespace Bu
8{
9 template<typename T> class SPtr;
10 template< typename Tb, typename Ta > SPtr<Tb> SPtrCast( SPtr<Ta> src );
11
12 template<typename T>
13 class SPtr
14 {
15 template<typename Tb, typename Ta>
16 friend SPtr<Tb> SPtrCast( SPtr<Ta> pt );
17 public:
18 SPtr() :
19 pRefCnt( NULL ),
20 pData( NULL )
21 {
22 }
23
24 ~SPtr()
25 {
26 decCount();
27 }
28
29 SPtr( const SPtr<T> &src ) :
30 pRefCnt( src.pRefCnt ),
31 pData( src.pData )
32 {
33 if( pRefCnt )
34 (*pRefCnt) += 1;
35 }
36
37 SPtr( T *pSrc ) :
38 pRefCnt( NULL ),
39 pData( pSrc )
40 {
41 if( pData )
42 {
43 pRefCnt = new int32_t;
44 (*pRefCnt) = 1;
45 }
46 }
47
48 int32_t count() const
49 {
50 return *pRefCnt;
51 }
52
53 const T *operator->() const
54 {
55 return pData;
56 }
57
58 const T &operator*() const
59 {
60 return *pData;
61 }
62
63 T *operator->()
64 {
65 return pData;
66 }
67
68 T &operator*()
69 {
70 return *pData;
71 }
72
73 SPtr<T> operator=( const SPtr<T> &src )
74 {
75 decCount();
76 pRefCnt = src.pRefCnt;
77 pData = src.pData;
78 if( pRefCnt )
79 (*pRefCnt) += 1;
80
81 return *this;
82 }
83
84 const SPtr<T> operator=( const SPtr<T> &src ) const
85 {
86 decCount();
87 pRefCnt = src.pRefCnt;
88 pData = src.pData;
89 if( pRefCnt )
90 (*pRefCnt) += 1;
91
92 return *this;
93 }
94
95 bool operator==( const SPtr<T> &src ) const
96 {
97 return pData == src.pData;
98 }
99
100 bool operator==( const T *src ) const
101 {
102 return pData == src;
103 }
104
105 operator bool() const
106 {
107 return pRefCnt != NULL;
108 }
109
110 bool isSet() const
111 {
112 return pRefCnt != NULL;
113 }
114
115 private:
116 void decCount() const
117 {
118 if( pRefCnt )
119 {
120 (*pRefCnt) -= 1;
121 //printf("Decrementing ref-count to %d\n", *pRefCnt );
122 if( (*pRefCnt) == 0 )
123 {
124 delete pRefCnt;
125 delete pData;
126 pRefCnt = NULL;
127 pData = NULL;
128 }
129 }
130 }
131
132 mutable int32_t *pRefCnt;
133 mutable T *pData;
134 };
135
136 template< typename Tb, typename Ta > SPtr<Tb> SPtrCast( SPtr<Ta> src )
137 {
138 SPtr<Tb> ret;
139 ret.pRefCnt = src.pRefCnt;
140 ret.pData = dynamic_cast<Tb *>(src.pData);
141 if( ret.pRefCnt )
142 (*(ret.pRefCnt)) += 1;
143 return ret;
144 }
145}
146
147#endif
diff --git a/src/tests/list.cpp b/src/tests/list.cpp
index 34ab656..12807a5 100644
--- a/src/tests/list.cpp
+++ b/src/tests/list.cpp
@@ -1,4 +1,10 @@
1#include "bu/list.h" 1#include "bu/list.h"
2#include <list>
3
4typedef struct Bob
5{
6 int nID;
7} Bob;
2 8
3int main() 9int main()
4{ 10{
@@ -16,7 +22,32 @@ int main()
16 { 22 {
17 printf("%d ", *i ); 23 printf("%d ", *i );
18 } 24 }
25 printf("\n");
26 for( Bu::List<int>::iterator i = l.begin(); i != l.end(); i++ )
27 {
28 l.erase( i );
29 if( i != l.end() )
30 printf("%d ", *i );
31 }
32
33 printf("\n\n");
19 34
35 Bu::List<Bob> lb;
36 for( int j = 0; j < 10; j++ )
37 {
38 Bob b;
39 b.nID = j;
40 lb.append( b );
41 }
42
43 const Bu::List<Bob> rb = lb;
44
45 for( Bu::List<Bob>::const_iterator i = rb.begin(); i != rb.end(); i++ )
46 {
47 //i->nID += 2;
48 //(*i).nID = 4;
49 printf("%d ", i->nID );
50 }
20 printf("\n\n"); 51 printf("\n\n");
21} 52}
22 53