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