Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
string_list.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 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_list.h
10 //! @brief Dynamic list of strings.
11 
12 #ifndef ROC_CORE_STRING_LIST_H_
13 #define ROC_CORE_STRING_LIST_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/stddefs.h"
20 
21 namespace roc {
22 namespace core {
23 
24 //! Dynamic list of strings.
25 //!
26 //! Strings are stored in a countinous dynamically-growing array.
27 //! Each string is stored in a block with a header and footer,
28 //! which both store block length. This allow fast iteration
29 //! in both directions.
30 //!
31 //! @code
32 //! ++--------+--------+---------+--------++-----------
33 //! || Header | string | padding | Footer || Header ...
34 //! ++--------+--------+---------+--------++-----------
35 //! @endcode
36 class StringList : public NonCopyable<> {
37 public:
38  //! Initialize empty string list.
39  explicit StringList(IArena& arena);
40 
41  //! Get number of elements.
42  size_t size() const;
43 
44  //! Check if list is empty.
45  bool is_empty() const;
46 
47  //! Get first string.
48  //! @returns
49  //! the first string in the list or NULL if it is empty.
50  const char* front() const;
51 
52  //! Get last string.
53  //! @returns
54  //! the last string in the list or NULL if it is empty.
55  const char* back() const;
56 
57  //! Get next string.
58  //! @returns
59  //! the first string of the given string or NULL if it is the last string.
60  //! @remarks
61  //! @p str should be a pointer returned by front(), nextof(), or prevof().
62  //! These pointers are invalidated by methods that modify the list.
63  const char* nextof(const char* str) const;
64 
65  //! Get previous string.
66  //! @returns
67  //! the last string of the given string or NULL if it is the first string.
68  //! @remarks
69  //! @p str should be a pointer returned by back(), nextof(), or prevof().
70  //! These pointers are invalidated by methods that modify the list.
71  const char* prevof(const char* str) const;
72 
73  //! Clear the list.
74  void clear();
75 
76  //! Append string to the list.
77  //! @remarks
78  //! Reallocates memory if necessary.
79  //! @returns
80  //! false if allocation failed.
81  ROC_ATTR_NODISCARD bool push_back(const char* str);
82 
83  //! Append string from a range to the list.
84  //! @remarks
85  //! Reallocates memory if necessary.
86  //! @returns
87  //! false if allocation failed.
88  ROC_ATTR_NODISCARD bool push_back(const char* str_begin, const char* str_end);
89 
90  //! Find string in the list.
91  //! @returns
92  //! the string in the list or NULL if it is not found.
93  ROC_ATTR_NODISCARD const char* find(const char* str);
94 
95  //! Find string in the list.
96  //! @returns
97  //! the string in the list or NULL if it is not found.
98  ROC_ATTR_NODISCARD const char* find(const char* str_begin, const char* str_end);
99 
100 private:
101  enum { MinCapacity = 128 };
102 
103  struct Header {
104  uint32_t len;
105  char str[];
106  };
107 
108  struct Footer {
109  uint32_t len;
110  };
111 
112  void check_member_(const char* str) const;
113  bool grow_(size_t size);
114 
115  core::Array<char> data_;
116  Header* front_;
117  Header* back_;
118  size_t size_;
119 };
120 
121 } // namespace core
122 } // namespace roc
123 
124 #endif // ROC_CORE_STRING_LIST_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
Dynamic list of strings.
Definition: string_list.h:36
const char * back() const
Get last string.
const char * nextof(const char *str) const
Get next string.
bool is_empty() const
Check if list is empty.
const char * front() const
Get first string.
ROC_ATTR_NODISCARD const char * find(const char *str_begin, const char *str_end)
Find string in the list.
void clear()
Clear the list.
const char * prevof(const char *str) const
Get previous string.
ROC_ATTR_NODISCARD bool push_back(const char *str)
Append string to the list.
StringList(IArena &arena)
Initialize empty string list.
ROC_ATTR_NODISCARD const char * find(const char *str)
Find string in the list.
ROC_ATTR_NODISCARD bool push_back(const char *str_begin, const char *str_end)
Append string from a range to the list.
size_t size() const
Get number of elements.
Memory arena interface.
@ Header
PayloadID comes before payload.
Definition: headers.h:32
@ Footer
PayloadID comes after payload.
Definition: headers.h:33
Root namespace.
Non-copyable object.
Commonly used types and functions.