DSL
Buffer.h
1 //@AUTOHEADER@BEGIN@
2 /***********************************************************************\
3 | Drift Standard Libraries v1.01 |
4 | Copyright 2010-2023 Drift Solutions / Indy Sams |
5 | Docs and more information available at https://www.driftsolutions.dev |
6 | This file released under the 3-clause BSD license, |
7 | see included DSL.LICENSE.TXT file for details. |
8 \***********************************************************************/
9 //@AUTOHEADER@END@
10 
11 #ifndef __DSL_BUFFER_H__
12 #define __DSL_BUFFER_H__
13 
14 #include <drift/Mutex.h>
15 
16 #define DSL_BUFFER_USE_VECTOR
17 /* Other CRT's untested at this point but under MSVC 2022 vector is 18-25% faster in tests */
18 
30 struct DSL_BUFFER {
31  DSL_Mutex * hMutex;
32  union {
33  char * data;
34  uint8 * udata;
35  };
36  int64 len;
37 #ifdef DSL_BUFFER_USE_VECTOR
38  vector<uint8> * vec;
39 #endif
40 };
41 
42 DSL_API void DSL_CC buffer_init(DSL_BUFFER * buf, bool useMutex = false);
43 DSL_API void DSL_CC buffer_free(DSL_BUFFER * buf);
44 DSL_API_CLASS string buffer_as_string(DSL_BUFFER * buf);
45 
46 DSL_API void DSL_CC buffer_clear(DSL_BUFFER * buf);
47 DSL_API void DSL_CC buffer_set(DSL_BUFFER * buf, const char * ptr, int64 len);
48 DSL_API void DSL_CC buffer_resize(DSL_BUFFER * buf, int64 len);
49 
50 DSL_API void DSL_CC buffer_remove_front(DSL_BUFFER * buf, int64 len);
51 DSL_API void DSL_CC buffer_remove_end(DSL_BUFFER * buf, int64 len);
52 DSL_API bool DSL_CC buffer_prepend(DSL_BUFFER * buf, const char * ptr, int64 len);
53 DSL_API bool DSL_CC buffer_append(DSL_BUFFER * buf, const char * ptr, int64 len);
54 
59 template <typename T> bool buffer_append_int(DSL_BUFFER * buf, T y) { return buffer_append(buf, (char *)&y, sizeof(y)); }
60 
63 class DSL_API_CLASS DSL_Buffer {
64 private:
65  DSL_Mutex hMutex;
66  char * data;
67  uint32 len;
68 public:
69  DSL_Buffer();
70  ~DSL_Buffer();
71 
72  void Clear();
73  void RemoveFromEnd(uint32 len);
74  void RemoveFromBeginning(uint32 len);
75  char * Get();
76  uint32 GetLen();
77  bool Get(char * buf, uint32 * size);//the call will set size to the current length of data
78  //and will return true if the buffer was big enough and the copy was successful
79  //otherwise will return false and copy up to size into your buffer
80  bool Get(char * buf, uint32 size); //the call will fill the buffer with your data up to size
81  //and will return true if the buffer was big enough and the copy was successful
82  //otherwise will return false and copy up to size into your buffer
83 
84  void Set(const char * buf, uint32 len=0xFFFFFFFF);
85  void Append(const char * buf, uint32 len=0xFFFFFFFF);
86  void Prepend(const char * buf, uint32 len=0xFFFFFFFF);
87 
88  void Append_int8(int8 val);
89  void Append_uint8(uint8 val);
90  void Append_int16(int16 val);
91  void Append_uint16(uint16 val);
92  void Append_int32(int32 val);
93  void Append_uint32(uint32 val);
94  void Append_int64(int64 val);
95  void Append_uint64(uint64 val);
96 };
97 
98 #endif // __DSL_BUFFER_H__
bool buffer_append_int(DSL_BUFFER *buf, T y)
Definition: Buffer.h:59
DSL_API void DSL_CC buffer_remove_front(DSL_BUFFER *buf, int64 len)
Remove data from the beginning of the buffer.
Definition: Buffer.cpp:167
DSL_API void DSL_CC buffer_init(DSL_BUFFER *buf, bool useMutex=false)
Initialize the buffer, optionally with a mutex protecting it. If you don't use the mutex you need to ...
Definition: Buffer.cpp:123
DSL_API void DSL_CC buffer_set(DSL_BUFFER *buf, const char *ptr, int64 len)
Sets the buffer to the specified data, discarding anything existing.
Definition: Buffer.cpp:152
DSL_API_CLASS string buffer_as_string(DSL_BUFFER *buf)
Gets the buffer as a string.
Definition: Buffer.cpp:134
DSL_API void DSL_CC buffer_free(DSL_BUFFER *buf)
Free the buffer when you are done with it.
Definition: Buffer.cpp:128
DSL_API void DSL_CC buffer_clear(DSL_BUFFER *buf)
Sets the buffer length to 0 and clears the data. It is still ready to be used unlike buffer_free.
Definition: Buffer.cpp:144
DSL_API void DSL_CC buffer_remove_end(DSL_BUFFER *buf, int64 len)
Remove data from the end of the buffer.
Definition: Buffer.cpp:179
DSL_API bool DSL_CC buffer_append(DSL_BUFFER *buf, const char *ptr, int64 len)
Add data to the end of the buffer.
Definition: Buffer.cpp:202
DSL_API bool DSL_CC buffer_prepend(DSL_BUFFER *buf, const char *ptr, int64 len)
Add data to the beginning of the buffer.
Definition: Buffer.cpp:190
DSL_API void DSL_CC buffer_resize(DSL_BUFFER *buf, int64 len)
Resize the buffer, if the length is longer then the existing data the added byte values are undefined...
Definition: Buffer.cpp:160
DSL_Mutex * hMutex
Handle to the mutex protecting this buffer, if enabled.
Definition: Buffer.h:31