12 #ifndef ROC_CORE_ARRAY_H_
13 #define ROC_CORE_ARRAY_H_
32 , allocator_(allocator) {
56 roc_panic(
"array: subscript out of range: index=%lu size=%lu",
57 (
unsigned long)index, (
unsigned long)size_);
65 roc_panic(
"array: subscript out of range: index=%lu size=%lu",
66 (
unsigned long)index, (
unsigned long)size_);
76 roc_panic(
"array: attempting to call front() on empty array");
86 roc_panic(
"array: attempting to call front() on empty array");
96 roc_panic(
"array: attempting to call back() on empty array");
98 return data_[size_ - 1];
106 roc_panic(
"array: attempting to call back() on empty array");
108 return data_[size_ - 1];
115 if (size_ >= max_size_) {
116 roc_panic(
"array: attempting to append element to full array: size=%lu",
117 (
unsigned long)size_);
119 new (data_ + size_) T(value);
135 for (
size_t n = size_; n < sz; n++) {
140 for (
size_t n = size_; n > sz; n--) {
156 if (max_sz <= max_size_) {
160 T* new_data = (T*)allocator_.
allocate(max_sz *
sizeof(T));
162 roc_log(
LogError,
"array: can't allocate memory: old_size=%lu new_size=%lu",
163 (
unsigned long)max_size_, (
unsigned long)max_sz);
168 for (
size_t n = 0; n < size_; n++) {
169 new (new_data + n) T(data_[n]);
173 for (
size_t n = size_; n > 0; n--) {
T & operator[](size_t index)
Get element at given position.
bool grow(size_t max_sz)
Increase array maximum size.
void push_back(const T &value)
Append element to array.
const T & front() const
Get first element.
bool resize(size_t sz)
Set array size.
Array(IAllocator &allocator)
Initialize empty array.
const T & operator[](size_t index) const
Get element at given position.
T & front()
Get first element.
size_t max_size() const
Get maximum number of elements.
const T & back() const
Get last element.
T & back()
Get last element.
size_t size() const
Get number of elements.
Memory allocator interface.
virtual void deallocate(void *)=0
Deallocate previously allocated memory.
virtual void * allocate(size_t size)=0
Allocate memory.
Base class for non-copyable objects.
Memory allocator interface.
#define roc_log(...)
Print message to log.
#define roc_panic(...)
Print error message and terminate program gracefully.
Commonly used types and functions.