Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
slice.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Roc 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/slice.h
10 //! @brief Slice.
11 
12 #ifndef ROC_CORE_SLICE_H_
13 #define ROC_CORE_SLICE_H_
14 
15 #include "roc_core/buffer.h"
16 #include "roc_core/print_buffer.h"
17 #include "roc_core/shared_ptr.h"
18 
19 namespace roc {
20 namespace core {
21 
22 //! Slice.
23 template <class T> class Slice {
24 public:
25  //! Construct empty slice.
27  : buffer_()
28  , data_(NULL)
29  , size_(0) {
30  }
31 
32  //! Construct slice pointing to a buffer.
33  Slice(Buffer<T>* buffer) {
34  buffer_ = buffer;
35  if (buffer_) {
36  data_ = buffer->data();
37  size_ = buffer->size();
38  } else {
39  data_ = NULL;
40  size_ = 0;
41  }
42  }
43 
44  //! Construct slice pointing to a part of a buffer.
45  Slice(Buffer<T>& buffer, size_t from, size_t to) {
46  if (from > to) {
47  roc_panic("slice: invalid range: [%lu,%lu)", (unsigned long)from,
48  (unsigned long)to);
49  }
50  if (to > buffer.size()) {
51  roc_panic("slice: out of bounds: available=[%lu,%lu), requested=[%lu,%lu)",
52  (unsigned long)0, (unsigned long)buffer.size(), (unsigned long)from,
53  (unsigned long)to);
54  }
55  buffer_ = &buffer;
56  data_ = buffer.data() + from;
57  size_ = to - from;
58  }
59 
60  //! Get slice data.
61  T* data() const {
62  if (data_ == NULL) {
63  roc_panic("slice: null slice");
64  }
65  return data_;
66  }
67 
68  //! Get number of elements in slice.
69  size_t size() const {
70  return size_;
71  }
72 
73  //! Get maximum possible number of elements in slice.
74  size_t capacity() const {
75  if (data_ == NULL) {
76  return 0;
77  } else {
78  return buffer_->size() - size_t(data_ - buffer_->data());
79  }
80  }
81 
82  //! Change slice size, up to the available capacity.
83  void resize(size_t new_size) {
84  const size_t cap = capacity();
85  if (new_size > cap) {
86  roc_panic("slice: out of bounds: available=%lu, requested=%lu",
87  (unsigned long)cap, (unsigned long)new_size);
88  }
89  size_ = new_size;
90  }
91 
92  //! Construct a slice pointing to a part of this slice.
93  Slice range(size_t from, size_t to) const {
94  if (from > to) {
95  roc_panic("slice: invalid range: [%lu,%lu)", (unsigned long)from,
96  (unsigned long)to);
97  }
98  if (to > size_) {
99  roc_panic("slice: out of bounds: available=[%lu,%lu), requested=[%lu,%lu)",
100  (unsigned long)0, (unsigned long)size_, (unsigned long)from,
101  (unsigned long)to);
102  }
103  Slice ret;
104  ret.buffer_ = buffer_;
105  ret.data_ = data_ + from;
106  ret.size_ = to - from;
107  return ret;
108  }
109 
110  //! Print slice to stderr.
111  void print() const {
112  if (buffer_) {
113  core::print_buffer_slice(data_, size_, buffer_->data(), buffer_->size());
114  } else {
115  core::print_buffer_slice(data_, size_, NULL, 0);
116  }
117  }
118 
119  //! Convert to bool.
120  //! @returns
121  //! true if the slice is attached to buffer, even if it has zero length.
122  operator const struct unspecified_bool*() const {
123  return (const unspecified_bool*)data_;
124  }
125 
126 private:
127  SharedPtr<Buffer<T> > buffer_;
128  T* data_;
129  size_t size_;
130 };
131 
132 } // namespace core
133 } // namespace roc
134 
135 #endif // ROC_CORE_SLICE_H_
Slice range(size_t from, size_t to) const
Construct a slice pointing to a part of this slice.
Definition: slice.h:93
T * data()
Get buffer data.
Definition: buffer.h:32
void resize(size_t new_size)
Change slice size, up to the available capacity.
Definition: slice.h:83
Slice(Buffer< T > *buffer)
Construct slice pointing to a buffer.
Definition: slice.h:33
Root namespace.
size_t size() const
Get number of elements in slice.
Definition: slice.h:69
Shared ownership intrusive pointer.
Buffer.
Definition: buffer.h:23
size_t size() const
Get maximum number of elements.
Definition: buffer.h:37
#define roc_panic(...)
Print error message and terminate program gracefully.
Definition: panic.h:42
Shared ownership intrusive pointer.
Definition: ownership.h:18
T * data() const
Get slice data.
Definition: slice.h:61
void print_buffer_slice(const uint8_t *inner, size_t inner_size, const uint8_t *outer, size_t outer_size)
Print a slice of bytes buffer.
Buffer.
void print() const
Print slice to stderr.
Definition: slice.h:111
Slice(Buffer< T > &buffer, size_t from, size_t to)
Construct slice pointing to a part of a buffer.
Definition: slice.h:45
Slice()
Construct empty slice.
Definition: slice.h:26
Slice.
Definition: slice.h:23
size_t capacity() const
Get maximum possible number of elements in slice.
Definition: slice.h:74