ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libptytty/src/estl.h
(Generate patch)

Comparing libptytty/src/estl.h (file contents):
Revision 1.17 by sf-exg, Sat May 19 13:12:07 2012 UTC vs.
Revision 1.28 by sf-exg, Mon Nov 10 11:32:00 2014 UTC

20 return first; 20 return first;
21} 21}
22 22
23#include <new> 23#include <new>
24 24
25#if __cplusplus >= 201103L 25#if ECB_CPP11
26 #include <type_traits> 26 #include <type_traits>
27#endif 27#endif
28
29namespace estl
30{
31#if ESTL_LARGE_MEMORY_MODEL
32 // should use size_t/ssize_t, but that's not portable enough for us
33 typedef unsigned long size_type;
34 typedef long difference_type;
35#else
36 typedef uint32_t size_type;
37 typedef int32_t difference_type;
38#endif
39
40 template<typename T>
41 struct scoped_ptr
42 {
43 T *p;
44
45 scoped_ptr () : p (0) { }
46
47 explicit
48 scoped_ptr (T *a) : p (a) { }
49
50 ~scoped_ptr ()
51 {
52 delete p;
53 }
54
55 void reset (T *a)
56 {
57 delete p;
58 p = a;
59 }
60
61 T *operator ->() const { return p; }
62 T &operator *() const { return *p; }
63
64 operator T *() { return p; }
65 T *get () const { return p; }
66 };
67
68 template<typename T>
69 struct scoped_array
70 {
71 T *p;
72
73 scoped_array () : p (0) { }
74
75 explicit
76 scoped_array (T *a) : p (a) { }
77
78 ~scoped_array ()
79 {
80 delete [] p;
81 }
82
83 void reset (T *a)
84 {
85 delete [] p;
86 p = a;
87 }
88
89 T & operator [](size_type idx) const { return p[idx]; }
90
91 operator T *() { return p; }
92 T *get () const { return p; }
93 };
94}
28 95
29// original version taken from MICO, but this has been completely rewritten 96// original version taken from MICO, but this has been completely rewritten
30// known limitations w.r.t. std::vector 97// known limitations w.r.t. std::vector
31// - many methods missing 98// - many methods missing
32// - no error checking, no exceptions thrown (e.g. at()) 99// - no error checking, no exceptions thrown (e.g. at())
36// - we don't care about namespaces and stupid macros the user might define 103// - we don't care about namespaces and stupid macros the user might define
37// - no bool specialisation 104// - no bool specialisation
38template<class T> 105template<class T>
39struct simplevec 106struct simplevec
40{ 107{
41#if ESTL_BIG_VECTOR 108 typedef estl::size_type size_type;
42 // shoudl use size_t/ssize_t, but that's not portable enough for us
43 typedef unsigned long size_type;
44 typedef long difference_type;
45#else
46 typedef uint32_t size_type;
47 typedef int32_t difference_type;
48#endif
49 109
50 typedef T value_type; 110 typedef T value_type;
51 typedef T *iterator; 111 typedef T *iterator;
52 typedef const T *const_iterator; 112 typedef const T *const_iterator;
53 typedef T *pointer; 113 typedef T *pointer;
63 123
64 // we shamelessly optimise for "simple" types. everything 124 // we shamelessly optimise for "simple" types. everything
65 // "not simple enough" will use the slow path. 125 // "not simple enough" will use the slow path.
66 static bool is_simple_enough () 126 static bool is_simple_enough ()
67 { 127 {
68 #if __cplusplus >= 201103L 128 #if ECB_CPP11
69 return std::is_trivially_assignable<T, T>::value 129 return std::is_trivially_assignable<T, T>::value
70 && std::is_trivially_constructable<T>::value 130 && std::is_trivially_constructible<T>::value
71 && std::is_trivially_copyable<T>::value 131 && std::is_trivially_copyable<T>::value
72 && std::is_trivially_destructible<T>::value; 132 && std::is_trivially_destructible<T>::value;
73 #elif ECB_GCC_VERSION(4,4) 133 #elif ECB_GCC_VERSION(4,4) || ECB_CLANG_VERSION(2,8)
74 return __has_trivial_assign (T) 134 return __has_trivial_assign (T)
75 && __has_trivial_constructor (T) 135 && __has_trivial_constructor (T)
76 && __has_trivial_copy (T) 136 && __has_trivial_copy (T)
77 && __has_trivial_destructor (T); 137 && __has_trivial_destructor (T);
78 #else 138 #else
145 buf = nbuf; 205 buf = nbuf;
146 } 206 }
147 207
148 construct (buf + sze, n); 208 construct (buf + sze, n);
149 209
150 sze += n;
151
152 iterator src = buf + pos; 210 iterator src = buf + pos;
153 if (is_simple_enough ()) 211 if (is_simple_enough ())
154 memmove (src + n, src, sizeof (T) * n); 212 memmove (src + n, src, sizeof (T) * (sze - pos));
155 else 213 else
156 for (size_type i = n; i--; ) 214 for (size_type i = sze - pos; i--; )
157 cop_set (src + n + i, src + i); 215 cop_set (src + n + i, src + i);
216
217 sze += n;
158 } 218 }
159 219
160public: 220public:
161 size_type capacity () const { return res; } 221 size_type capacity () const { return res; }
162 size_type size () const { return sze; } 222 size_type size () const { return sze; }
216 276
217 while (n--) 277 while (n--)
218 new (buf + n) T (t); 278 new (buf + n) T (t);
219 } 279 }
220 280
221 template<class I> 281 simplevec (const_iterator first, const_iterator last)
222 simplevec (I first, I last)
223 { 282 {
224 sze = res = last - first; 283 sze = res = last - first;
225 buf = alloc (sze); 284 buf = alloc (sze);
226 copy (buf, first, sze, cop_new); 285 copy (buf, first, sze, cop_new);
227 } 286 }
267 reference operator [](size_type idx) { return buf[idx]; } 326 reference operator [](size_type idx) { return buf[idx]; }
268 327
269 const_reference at (size_type idx) const { return buf [idx]; } 328 const_reference at (size_type idx) const { return buf [idx]; }
270 reference at (size_type idx) { return buf [idx]; } 329 reference at (size_type idx) { return buf [idx]; }
271 330
272 template<class I> 331 void assign (const_iterator first, const_iterator last)
273 void assign (I first, I last)
274 { 332 {
275 swap (simplevec<T> (first, last)); 333 simplevec<T> v (first, last);
334 swap (v);
276 } 335 }
277 336
278 void assign (size_type n, const T &t) 337 void assign (size_type n, const T &t)
279 { 338 {
280 swap (simplevec<T> (n, t)); 339 simplevec<T> v (n, t);
340 swap (v);
281 } 341 }
282 342
283 simplevec<T> &operator= (const simplevec<T> &v) 343 simplevec<T> &operator= (const simplevec<T> &v)
284 { 344 {
285 assign (v.begin (), v.end ()); 345 assign (v.begin (), v.end ());
294 buf [at] = t; 354 buf [at] = t;
295 355
296 return buf + at; 356 return buf + at;
297 } 357 }
298 358
299 template<class I>
300 iterator insert (iterator pos, I first, I last) 359 iterator insert (iterator pos, const_iterator first, const_iterator last)
301 { 360 {
302 size_type n = last - first; 361 size_type n = last - first;
303 size_type at = pos - begin (); 362 size_type at = pos - begin ();
304 363
305 ins (pos, n); 364 ins (pos, n);
318 *i++ = t; 377 *i++ = t;
319 378
320 return buf + at; 379 return buf + at;
321 } 380 }
322 381
323 void erase (iterator first, iterator last) 382 iterator erase (iterator first, iterator last)
324 { 383 {
325 size_t n = last - first; 384 size_type n = last - first;
385 size_type c = end () - last;
326 386
327 if (is_simple_enough ()) 387 if (is_simple_enough ())
328 memmove (first, last, sizeof (T) * n); 388 memmove (first, last, sizeof (T) * c);
329 else 389 else
330 copy<iterator> (first, last, n, cop_new); 390 copy (first, last, c, cop_set);
331 391
332 sze -= n; 392 sze -= n;
333 destruct (buf + sze, n); 393 destruct (buf + sze, n);
334 }
335 394
395 return first;
396 }
397
336 void erase (iterator pos) 398 iterator erase (iterator pos)
337 { 399 {
338 if (pos != end ()) 400 if (pos != end ())
339 erase (pos, pos + 1); 401 erase (pos, pos + 1);
402
403 return pos;
340 } 404 }
341}; 405};
342 406
343template<class T> 407template<class T>
344bool operator ==(const simplevec<T> &v1, const simplevec<T> &v2) 408bool operator ==(const simplevec<T> &v1, const simplevec<T> &v2)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines