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