1 // This file is part of the ustl library, an STL implementation.
3 // Copyright (C) 2005 by Mike Sharov <msharov@users.sourceforge.net>
4 // This file is free software, distributed under the MIT License.
9 #ifndef CMEMLINK_H_7CFAB32C5C6732ED29B34EF00EA40A12
10 #define CMEMLINK_H_7CFAB32C5C6732ED29B34EF00EA40A12
12 #include "ualgobase.h"
14 /// The ustl namespace contains all ustl classes and algorithms.
21 /// \class cmemlink cmemlink.h ustl.h
22 /// \ingroup MemoryManagement
24 /// \brief A read-only pointer to a sized block of memory.
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.
32 /// void* p = malloc (46721);
34 /// a.link (p, 46721);
35 /// assert (a.size() == 46721));
37 /// assert (b.size() == 46721));
38 /// assert (b.DataAt(34) == a.DataAt(34));
39 /// assert (0 == memcmp (a, b, 12));
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
;
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 inline void link (const cmemlink
& l
) { link (l
.begin(), l
.size()); }
62 inline void link (const void* first
, const void* last
) { link (first
, distance (first
, last
)); }
63 inline void relink (const void* p
, size_type n
);
64 inline virtual void unlink (void) throw() { m_Data
= NULL
; m_Size
= 0; }
65 inline rcself_t
operator= (const cmemlink
& l
) { link (l
); return (*this); }
66 bool operator== (const cmemlink
& l
) const;
67 void swap (cmemlink
& l
);
68 inline size_type
size (void) const { return (m_Size
); }
69 inline size_type
max_size (void) const { return (size()); }
70 inline size_type
readable_size (void) const { return (size()); }
71 inline bool empty (void) const { return (!size()); }
72 inline const_pointer
cdata (void) const { return (m_Data
); }
73 inline iterator
begin (void) const { return (iterator (cdata())); }
74 inline iterator
iat (size_type i
) const { assert (i
<= size()); return (begin() + i
); }
75 inline iterator
end (void) const { return (iat (size())); }
76 inline void resize (size_type n
) { m_Size
= n
; }
77 inline void read (istream
&) { assert (!"ustl::cmemlink is a read-only object."); }
78 void write (ostream
& os
) const;
79 size_type
stream_size (void) const;
80 void text_write (ostringstream
& os
) const;
81 void write_file (const char* filename
, int mode
= 0644) const;
83 const_pointer m_Data
; ///< Pointer to the data block (const)
84 size_type m_Size
; ///< size of the data block
87 /// A fast alternative to link which can be used when relinking to the same block (i.e. when it is resized)
88 inline void cmemlink::relink (const void* p
, size_type n
)
90 m_Data
= reinterpret_cast<const_pointer
>(p
);
94 /// Use with cmemlink-derived classes to link to a static array
95 #define static_link(v) link (VectorBlock(v))