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