Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
string_buffer.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Roc Streaming authors
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
8 
9 //! @file roc_core/string_buffer.h
10 //! @brief String buffer.
11 
12 #ifndef ROC_CORE_STRING_BUFFER_H_
13 #define ROC_CORE_STRING_BUFFER_H_
14 
15 #include "roc_core/array.h"
16 #include "roc_core/attributes.h"
17 #include "roc_core/iarena.h"
18 #include "roc_core/noncopyable.h"
19 #include "roc_core/panic.h"
20 
21 namespace roc {
22 namespace core {
23 
24 //! String buffer.
25 //!
26 //! Dynamic array storing zero-terminated string. Works on top of Array,
27 //! but guarantees that the string is always zero-terminated.
28 //!
29 //! @tparam EmbeddedCapacity is the same as for Array.
30 class StringBuffer : public NonCopyable<> {
31 public:
32  //! Initialize empty buffer.
33  explicit StringBuffer(IArena& arena);
34 
35  //! Check if buffer is empty.
36  bool is_empty() const;
37 
38  //! Get string length, excluding terminating zero.
39  size_t len() const;
40 
41  //! Get zero-terminated string.
42  const char* c_str() const;
43 
44  //! Set buffer to empty string.
45  void clear();
46 
47  //! Copy given string into buffer.
48  //! @p str should be zero-terminated.
49  //! @returns
50  //! false if allocation failed.
51  ROC_ATTR_NODISCARD bool assign(const char* str);
52 
53  //! Copy given range into buffer.
54  //! Buffer will be automatically zero-terminated.
55  //! @returns
56  //! false if allocation failed.
57  ROC_ATTR_NODISCARD bool assign(const char* str_begin, const char* str_end);
58 
59  //! Extend buffer by requested number of characters.
60  //! @remarks
61  //! Characters are appended to the buffer and filled with zeros.
62  //! It's the caller responsibility to fill them.
63  //! @returns
64  //! NULL if allocation failed.
65  ROC_ATTR_NODISCARD char* extend(size_t n_chars);
66 
67  //! Grow capacity to be able to hold desired number of characters.
68  //! Capacity is increased linearly.
69  //! @returns
70  //! false if allocation failed.
71  ROC_ATTR_NODISCARD bool grow(size_t desired_len);
72 
73  //! Grow capacity to be able to hold desired number of characters.
74  //! Capacity is increased exponentially.
75  //! @returns
76  //! false if allocation failed.
77  ROC_ATTR_NODISCARD bool grow_exp(size_t desired_len);
78 
79 private:
80  Array<char, 32> data_;
81 };
82 
83 } // namespace core
84 } // namespace roc
85 
86 #endif // ROC_CORE_STRING_BUFFER_H_
Dynamic array.
Compiler attributes.
#define ROC_ATTR_NODISCARD
Emit warning if function result is not checked.
Definition: attributes.h:31
Memory arena interface.
Definition: iarena.h:23
Base class for non-copyable objects.
Definition: noncopyable.h:23
ROC_ATTR_NODISCARD bool grow_exp(size_t desired_len)
Grow capacity to be able to hold desired number of characters. Capacity is increased exponentially.
size_t len() const
Get string length, excluding terminating zero.
ROC_ATTR_NODISCARD bool assign(const char *str)
Copy given string into buffer. str should be zero-terminated.
void clear()
Set buffer to empty string.
bool is_empty() const
Check if buffer is empty.
ROC_ATTR_NODISCARD bool grow(size_t desired_len)
Grow capacity to be able to hold desired number of characters. Capacity is increased linearly.
ROC_ATTR_NODISCARD bool assign(const char *str_begin, const char *str_end)
Copy given range into buffer. Buffer will be automatically zero-terminated.
StringBuffer(IArena &arena)
Initialize empty buffer.
const char * c_str() const
Get zero-terminated string.
ROC_ATTR_NODISCARD char * extend(size_t n_chars)
Extend buffer by requested number of characters.
Memory arena interface.
Root namespace.
Non-copyable object.
Panic.