Build system improvements
[ustl.git] / memlink.h
blobd1a9cfb9c0fa038961ede954a237901242b09cd8
1 // This file is part of the ustl library, an STL implementation.
2 //
3 // Copyright (C) 2005 by Mike Sharov <msharov@users.sourceforge.net>
4 // This file is free software, distributed under the MIT License.
5 //
6 // memlink.h
8 #ifndef MEMLINK_H_798D25827C8E322D2D7E734B169FF5FC
9 #define MEMLINK_H_798D25827C8E322D2D7E734B169FF5FC
11 #include "cmemlink.h"
12 #include "ualgo.h"
14 namespace ustl {
16 /// \class memlink memlink.h ustl.h
17 /// \ingroup MemoryManagement
18 ///
19 /// \brief Wrapper for pointer to block with size.
20 ///
21 /// Use this class the way you would a pointer to an allocated unstructured block.
22 /// The pointer and block size are available through member functions and cast operator.
23 ///
24 /// Example usage:
25 /// \code
26 /// void* p = malloc (46721);
27 /// memlink a, b;
28 /// a.link (p, 46721);
29 /// assert (a.size() == 46721));
30 /// b = a;
31 /// assert (b.size() == 46721));
32 /// assert (b.begin() + 34 == a.begin + 34);
33 /// assert (0 == memcmp (a, b, 12));
34 /// a.fill (673, b, 42, 67);
35 /// b.erase (87, 12);
36 /// \endcode
37 ///
38 class memlink : public cmemlink {
39 public:
40 typedef value_type* pointer;
41 typedef cmemlink::pointer const_pointer;
42 typedef cmemlink::const_iterator const_iterator;
43 typedef pointer iterator;
44 typedef const memlink& rcself_t;
45 public:
46 inline memlink (void) : cmemlink() {}
47 inline memlink (void* p, size_type n) : cmemlink (p, n) {}
48 inline memlink (const void* p, size_type n) : cmemlink (p, n) {}
49 inline memlink (rcself_t l) : cmemlink (l) {}
50 inline explicit memlink (const cmemlink& l) : cmemlink (l) {}
51 inline pointer data (void) { return (const_cast<pointer>(cdata())); }
52 inline iterator begin (void) { return (iterator (data())); }
53 inline iterator iat (size_type i) { assert (i <= size()); return (begin() + i); }
54 inline iterator end (void) { return (iat (size())); }
55 inline const_iterator begin (void) const { return (cmemlink::begin()); }
56 inline const_iterator end (void) const { return (cmemlink::end()); }
57 inline const_iterator iat (size_type i) const { return (cmemlink::iat (i)); }
58 size_type writable_size (void) const { return (size()); }
59 inline rcself_t operator= (const cmemlink& l) { cmemlink::operator= (l); return (*this); }
60 inline rcself_t operator= (rcself_t l) { cmemlink::operator= (l); return (*this); }
61 inline void link (const void* p, size_type n) { cmemlink::link (p, n); }
62 inline void link (void* p, size_type n) { cmemlink::link (p, n); }
63 inline void link (const cmemlink& l) { cmemlink::link (l); }
64 inline void link (memlink& l) { cmemlink::link (l); }
65 inline void link (const void* first, const void* last) { link (first, distance (first, last)); }
66 inline void link (void* first, void* last) { link (first, distance (first, last)); }
67 inline void relink (const void* p, size_type n) { cmemlink::relink (p, n); }
68 inline void relink (void* p, size_type n) { cmemlink::relink (p, n); }
69 inline void copy (const cmemlink& l) { copy (begin(), l.cdata(), l.size()); }
70 inline void copy (const void* p, size_type n) { copy (begin(), p, n); }
71 void copy (iterator offset, const void* p, size_type n);
72 inline void swap (memlink& l) { cmemlink::swap (l); }
73 void fill (iterator start, const void* p, size_type elsize, size_type elCount = 1);
74 inline void insert (iterator start, size_type size);
75 inline void erase (iterator start, size_type size);
76 void read (istream& is);
79 /// Shifts the data in the linked block from \p start to \p start + \p n.
80 /// The contents of the uncovered bytes is undefined.
81 inline void memlink::insert (iterator start, size_type n)
83 assert (data() || !n);
84 assert (cmemlink::begin() || !n);
85 assert (start >= begin() && start + n <= end());
86 rotate (start, end() - n, end());
89 /// Shifts the data in the linked block from \p start + \p n to \p start.
90 /// The contents of the uncovered bytes is undefined.
91 inline void memlink::erase (iterator start, size_type n)
93 assert (data() || !n);
94 assert (cmemlink::begin() || !n);
95 assert (start >= begin() && start + n <= end());
96 rotate (start, start + n, end());
99 /// Use with memlink-derived classes to allocate and link to stack space.
100 #define alloca_link(m,n) (m).link (alloca (n), (n))
102 } // namespace ustl
104 #endif