buffer.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: buffer.h
  3. // Purpose: interface of wxMemoryBuffer
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. wxScopedCharTypeBuffer<T> is a template class for storing characters.
  9. Data are stored in reference-counted buffer. In other words, making a copy
  10. of wxScopedCharTypeBuffer<T> will @em not make another copy of the stored
  11. string data, it will still point to the same location in memory.
  12. wxScopedCharTypeBuffer<T> supports two storage modes: owned and non-owned.
  13. "Owned" data buffer (created with CreateOwned() or wxCharTypeBuffer<T>
  14. derived class) owns the data and frees them when the last buffer pointing
  15. to them is destroyed.
  16. "Non-owned" buffer (created with CreateNonOwned()), on the other hand,
  17. references data owned by somebody else -- typical use is by
  18. wxString::mb_str() or wxString::wc_str(), which may return non-owned buffer
  19. pointing to wxString's internal store.
  20. Because of this, the validity of data stored in wxScopedCharTypeBuffer<T>
  21. is limited by the lifetime of the "parent" object that created the
  22. buffer (e.g. the wxString on which mb_str() was called).
  23. If you need to preserve the data for longer, assign it to
  24. wxCharTypeBuffer<T> instead of wxScopedCharTypeBuffer<T>. On the other
  25. hand, use wxScopedCharTypeBuffer<T> if the buffer is to be destroyed before
  26. the "parent" object -- typical use would be creating it on the stack and
  27. destroying when it goes out of scope (hence the class' name).
  28. @tparam T
  29. The type of the characters stored in this class.
  30. @since 2.9.0
  31. @nolibrary
  32. @category{data}
  33. */
  34. template <typename T>
  35. class wxScopedCharTypeBuffer
  36. {
  37. public:
  38. /// Stored characters type.
  39. typedef T CharType;
  40. /// Default constructor, creates NULL buffer.
  41. wxScopedCharTypeBuffer();
  42. /**
  43. Creates non-owned buffer from string data @a str.
  44. The buffer's destructor will not destroy @a str. The returned buffer's
  45. data is valid only as long as @a str is valid.
  46. @param str String data.
  47. @param len If specified, length of the string, otherwise the string
  48. is considered to be NUL-terminated.
  49. */
  50. static const wxScopedCharTypeBuffer CreateNonOwned(const CharType *str, size_t len = wxNO_LEN);
  51. /**
  52. Creates owned buffer from @a str and takes ownership of it.
  53. The buffer's destructor will free @a str when its reference count
  54. reaches zero (initial count is 1).
  55. @param str String data.
  56. @param len If specified, length of the string, otherwise the string
  57. is considered to be NUL-terminated.
  58. */
  59. static const wxScopedCharTypeBuffer CreateOwned(CharType *str, size_t len = wxNO_LEN);
  60. /**
  61. Copy constructor.
  62. Increases reference count on the data, does @em not make wxStrdup()
  63. copy of the data.
  64. */
  65. wxScopedCharTypeBuffer(const wxScopedCharTypeBuffer& src);
  66. /// Assignment operator behaves in the same way as the copy constructor.
  67. wxScopedCharTypeBuffer& operator=(const wxScopedCharTypeBuffer& src);
  68. /**
  69. Destructor. Frees stored data if it is in "owned" mode and data's
  70. reference count reaches zero.
  71. */
  72. ~wxScopedCharTypeBuffer();
  73. /// Resets the buffer to NULL, freeing the data if necessary.
  74. void reset();
  75. /// Returns pointer to the stored data.
  76. CharType *data();
  77. /// Returns const pointer to the stored data.
  78. const CharType *data() const;
  79. /// Returns length of the string stored.
  80. size_t length() const;
  81. /// Implicit conversion to C string.
  82. operator const CharType *() const;
  83. /// Random access to the stored C string.
  84. CharType operator[](size_t n) const;
  85. };
  86. /// Scoped char buffer.
  87. typedef wxScopedCharTypeBuffer<char> wxScopedCharBuffer;
  88. /// Scoped wchar_t buffer.
  89. typedef wxScopedCharTypeBuffer<wchar_t> wxScopedWCharBuffer;
  90. /**
  91. wxCharTypeBuffer<T> is a template class for storing characters.
  92. The difference from wxScopedCharTypeBuffer<T> is that this class
  93. doesn't have non-owned mode and the data stored in it are valid for
  94. as long as the buffer instance exists. Other than that, this class'
  95. behaviour is the same as wxScopedCharTypeBuffer<T>'s -- in particular,
  96. the data are reference-counted and copying the buffer is cheap.
  97. wxScopedCharTypeBuffer<T> buffers can be converted into wxCharTypeBuffer<T>.
  98. @tparam T
  99. The type of the characters stored in this class.
  100. @since 2.9.0
  101. @nolibrary
  102. @category{data}
  103. */
  104. template <typename T>
  105. class wxCharTypeBuffer : public wxScopedCharTypeBuffer<T>
  106. {
  107. public:
  108. /**
  109. Creates (owned) buffer from @a str and takes ownership of it.
  110. @param str String data.
  111. @param len If specified, length of the string, otherwise the string
  112. is considered to be NUL-terminated.
  113. @see wxScopedCharTypeBuffer<T>::CreateOwned()
  114. */
  115. wxCharTypeBuffer(const CharType *str = NULL, size_t len = wxNO_LEN);
  116. /**
  117. Creates (owned) buffer of size @a len.
  118. @see wxScopedCharTypeBuffer<T>::CreateOwned()
  119. */
  120. wxCharTypeBuffer(size_t len);
  121. /**
  122. Copy constructor.
  123. Increases reference count on the data, does @em not make wxStrdup()
  124. copy of the data.
  125. */
  126. wxCharTypeBuffer(const wxCharTypeBuffer& src);
  127. /**
  128. Makes a copy of scoped buffer @a src.
  129. If @a src is a non-owned buffer, a copy of its data is made using
  130. wxStrdup(). If @a src is an owned buffer, this constructor behaves
  131. in the usual way (reference count on buffer data is incremented).
  132. */
  133. wxCharTypeBuffer(const wxScopedCharTypeBuffer<T>& src);
  134. /**
  135. Assigns @a str to this buffer and takes ownership of it (i.e.\ the
  136. buffer becomes "owned").
  137. */
  138. wxCharTypeBuffer& operator=(const CharType *str);
  139. /// Assignment operator behaves in the same way as the copy constructor.
  140. wxCharTypeBuffer& operator=(const wxCharTypeBuffer& src);
  141. /**
  142. Assigns a scoped buffer to this buffer.
  143. If @a src is a non-owned buffer, a copy of its data is made using
  144. wxStrdup(). If @a src is an owned buffer, the assignment behaves
  145. in the usual way (reference count on buffer data is incremented).
  146. */
  147. wxCharTypeBuffer& operator=(const wxScopedCharTypeBuffer<T>& src);
  148. /**
  149. Extends the buffer to have size @a len.
  150. Can only be called on buffers that don't share data with another
  151. buffer (i.e. reference count of the data is 1).
  152. @see shrink()
  153. */
  154. bool extend(size_t len);
  155. /**
  156. Shrinks the buffer to have size @a len and NUL-terminates the string
  157. at this length.
  158. Can only be called on buffers that don't share data with another
  159. buffer (i.e. reference count of the data is 1).
  160. @param len Length to shrink to. Must not be larger than current length.
  161. @note The string is not reallocated to take less memory.
  162. @since 2.9.0
  163. @see extend()
  164. */
  165. bool shrink(size_t len);
  166. };
  167. /**
  168. This is a specialization of wxCharTypeBuffer<T> for @c char type.
  169. @nolibrary
  170. @category{data}
  171. */
  172. class wxCharBuffer : public wxCharTypeBuffer<char>
  173. {
  174. public:
  175. typedef wxCharTypeBuffer<char> wxCharTypeBufferBase;
  176. typedef wxScopedCharTypeBuffer<char> wxScopedCharTypeBufferBase;
  177. wxCharBuffer(const wxCharTypeBufferBase& buf);
  178. wxCharBuffer(const wxScopedCharTypeBufferBase& buf);
  179. wxCharBuffer(const CharType *str = NULL);
  180. wxCharBuffer(size_t len);
  181. wxCharBuffer(const wxCStrData& cstr);
  182. };
  183. /**
  184. This is a specialization of wxCharTypeBuffer<T> for @c wchar_t type.
  185. @nolibrary
  186. @category{data}
  187. */
  188. class wxWCharBuffer : public wxCharTypeBuffer<wchar_t>
  189. {
  190. public:
  191. typedef wxCharTypeBuffer<wchar_t> wxCharTypeBufferBase;
  192. typedef wxScopedCharTypeBuffer<wchar_t> wxScopedCharTypeBufferBase;
  193. wxWCharBuffer(const wxCharTypeBufferBase& buf);
  194. wxWCharBuffer(const wxScopedCharTypeBufferBase& buf);
  195. wxWCharBuffer(const CharType *str = NULL);
  196. wxWCharBuffer(size_t len);
  197. wxWCharBuffer(const wxCStrData& cstr);
  198. };
  199. /**
  200. @class wxMemoryBuffer
  201. A @b wxMemoryBuffer is a useful data structure for storing arbitrary sized
  202. blocks of memory. wxMemoryBuffer guarantees deletion of the memory block when
  203. the object is destroyed.
  204. @library{wxbase}
  205. @category{data}
  206. */
  207. class wxMemoryBuffer
  208. {
  209. public:
  210. /**
  211. Copy constructor, refcounting is used for performance, but wxMemoryBuffer
  212. is not a copy-on-write structure so changes made to one buffer effect all
  213. copies made from it.
  214. @see @ref overview_refcount
  215. */
  216. wxMemoryBuffer(const wxMemoryBuffer& src);
  217. /**
  218. Create a new buffer.
  219. @param size
  220. size of the new buffer, 1KiB by default.
  221. */
  222. wxMemoryBuffer(size_t size = 1024);
  223. /**
  224. Append a single byte to the buffer.
  225. @param data
  226. New byte to append to the buffer.
  227. */
  228. void AppendByte(char data);
  229. /**
  230. Single call to append a data block to the buffer.
  231. @param data
  232. Pointer to block to append to the buffer.
  233. @param len
  234. Length of data to append.
  235. */
  236. void AppendData(const void *data, size_t len);
  237. /**
  238. Clear the buffer contents.
  239. The buffer won't contain any data after this method is called.
  240. @see IsEmpty()
  241. @since 2.9.4
  242. */
  243. void Clear();
  244. /**
  245. Ensure that the buffer is big enough and return a pointer to the start
  246. of the empty space in the buffer. This pointer can be used to directly
  247. write data into the buffer, this new data will be appended to the
  248. existing data.
  249. @param sizeNeeded
  250. Amount of extra space required in the buffer for
  251. the append operation
  252. */
  253. void* GetAppendBuf(size_t sizeNeeded);
  254. /**
  255. Returns the size of the buffer.
  256. */
  257. size_t GetBufSize() const;
  258. /**
  259. Return a pointer to the data in the buffer.
  260. */
  261. void* GetData() const;
  262. /**
  263. Returns the length of the valid data in the buffer.
  264. */
  265. size_t GetDataLen() const;
  266. /**
  267. Ensure the buffer is big enough and return a pointer to the
  268. buffer which can be used to directly write into the buffer
  269. up to @a sizeNeeded bytes.
  270. */
  271. void* GetWriteBuf(size_t sizeNeeded);
  272. /**
  273. Returns true if the buffer contains no data.
  274. @see Clear()
  275. @since 2.9.4
  276. */
  277. bool IsEmpty() const;
  278. /**
  279. Ensures the buffer has at least @a size bytes available.
  280. */
  281. void SetBufSize(size_t size);
  282. /**
  283. Sets the length of the data stored in the buffer.
  284. Mainly useful for truncating existing data.
  285. @param size
  286. New length of the valid data in the buffer. This is
  287. distinct from the allocated size
  288. */
  289. void SetDataLen(size_t size);
  290. /**
  291. Update the length after completing a direct append, which
  292. you must have used GetAppendBuf() to initialise.
  293. @param sizeUsed
  294. This is the amount of new data that has been
  295. appended.
  296. */
  297. void UngetAppendBuf(size_t sizeUsed);
  298. /**
  299. Update the buffer after completing a direct write, which
  300. you must have used GetWriteBuf() to initialise.
  301. @param sizeUsed
  302. The amount of data written in to buffer
  303. by the direct write
  304. */
  305. void UngetWriteBuf(size_t sizeUsed);
  306. };