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 // cmemlink.h 7 // 8 9 #ifndef CMEMLINK_H_7CFAB32C5C6732ED29B34EF00EA40A12 10 #define CMEMLINK_H_7CFAB32C5C6732ED29B34EF00EA40A12 11 12 #include "ualgobase.h" 13 14 /// The ustl namespace contains all ustl classes and algorithms. 15 namespace ustl { 16 17 class istream; 18 class ostream; 19 class ostringstream; 20 21 /// \class cmemlink cmemlink.h ustl.h 22 /// \ingroup MemoryManagement 23 /// 24 /// \brief A read-only pointer to a sized block of memory. 25 /// 26 /// Use this class the way you would a const pointer to an allocated unstructured block. 27 /// The pointer and block size are available through member functions and cast operator. 28 /// 29 /// Example usage: 30 /// 31 /// \code 32 /// void* p = malloc (46721); 33 /// cmemlink a, b; 34 /// a.link (p, 46721); 35 /// assert (a.size() == 46721)); 36 /// b = a; 37 /// assert (b.size() == 46721)); 38 /// assert (b.DataAt(34) == a.DataAt(34)); 39 /// assert (0 == memcmp (a, b, 12)); 40 /// \endcode 41 /// 42 class cmemlink { 43 public: 44 typedef char value_type; 45 typedef const value_type* pointer; 46 typedef const value_type* const_pointer; 47 typedef value_type reference; 48 typedef value_type const_reference; 49 typedef size_t size_type; 50 typedef uint32_t written_size_type; 51 typedef ptrdiff_t difference_type; 52 typedef const_pointer const_iterator; 53 typedef const_iterator iterator; 54 typedef const cmemlink& rcself_t; 55 public: 56 inline cmemlink (void) : m_Data (NULL), m_Size (0) { } 57 inline cmemlink (const void* p, size_type n) : m_Data (const_pointer(p)), m_Size (n) { assert (p || !n); } 58 inline cmemlink (const cmemlink& l) : m_Data (l.m_Data), m_Size (l.m_Size) {} 59 inline virtual ~cmemlink (void) {} 60 void link (const void* p, size_type n); 61 OVERLOAD_POINTER_AND_SIZE_T_V2(link, const void*) 62 inline void link (const cmemlink& l) { link (l.begin(), l.size()); } 63 inline void link (const void* first, const void* last) { link (first, distance (first, last)); } 64 inline void relink (const void* p, size_type n); 65 inline virtual void unlink (void) { m_Data = NULL; m_Size = 0; } 66 inline rcself_t operator= (const cmemlink& l) { link (l); return (*this); } 67 bool operator== (const cmemlink& l) const; 68 void swap (cmemlink& l); 69 inline size_type size (void) const { return (m_Size); } 70 inline size_type max_size (void) const { return (size()); } 71 inline size_type readable_size (void) const { return (size()); } 72 inline bool empty (void) const { return (!size()); } 73 inline const_pointer cdata (void) const { return (m_Data); } 74 inline iterator begin (void) const { return (iterator (cdata())); } 75 inline iterator iat (size_type i) const { assert (i <= size()); return (begin() + i); } 76 inline iterator end (void) const { return (iat (size())); } 77 inline void resize (size_type n) { m_Size = n; } 78 inline void read (istream&) { assert (!"ustl::cmemlink is a read-only object."); } 79 void write (ostream& os) const; 80 size_type stream_size (void) const; 81 void text_write (ostringstream& os) const; 82 void write_file (const char* filename, int mode = 0644) const; 83 private: 84 const_pointer m_Data; ///< Pointer to the data block (const) 85 size_type m_Size; ///< size of the data block 86 }; 87 88 /// A fast alternative to link which can be used when relinking to the same block (i.e. when it is resized) 89 inline void cmemlink::relink (const void* p, size_type n) 90 { 91 m_Data = reinterpret_cast<const_pointer>(p); 92 m_Size = n; 93 } 94 95 /// Use with cmemlink-derived classes to link to a static array 96 #define static_link(v) link (VectorBlock(v)) 97 98 } // namespace ustl 99 100 #endif 101 102