Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
buffer.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 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/buffer.h
10 //! @brief Buffer.
11 
12 #ifndef ROC_CORE_BUFFER_H_
13 #define ROC_CORE_BUFFER_H_
14 
15 #include "roc_core/buffer_pool.h"
16 #include "roc_core/refcnt.h"
17 #include "roc_core/stddefs.h"
18 
19 namespace roc {
20 namespace core {
21 
22 //! Buffer.
23 template <class T> class Buffer : public RefCnt<Buffer<T> > {
24 public:
25  //! Initialize empty buffer.
26  explicit Buffer(BufferPool<T>& pool)
27  : pool_(pool) {
28  new (data()) T[size()];
29  }
30 
31  //! Get buffer data.
32  T* data() {
33  return (T*)(((char*)this) + sizeof(Buffer));
34  }
35 
36  //! Get maximum number of elements.
37  size_t size() const {
38  return pool_.buffer_size();
39  }
40 
41  //! Get pointer to buffer from the pointer to its data.
42  static Buffer* container_of(void* data) {
43  return (Buffer*)((char*)data - sizeof(Buffer));
44  }
45 
46 private:
47  friend class RefCnt<Buffer>;
48 
49  void destroy() {
50  pool_.destroy(*this);
51  }
52 
53  BufferPool<T>& pool_;
54 };
55 
56 } // namespace core
57 } // namespace roc
58 
59 #endif // ROC_CORE_BUFFER_H_
Buffer(BufferPool< T > &pool)
Initialize empty buffer.
Definition: buffer.h:26
T * data()
Get buffer data.
Definition: buffer.h:32
Root namespace.
Buffer pool.
Definition: buffer_pool.h:23
Buffer.
Definition: buffer.h:23
Commonly used types and functions.
size_t size() const
Get maximum number of elements.
Definition: buffer.h:37
Base class for reference countable objects.
Definition: refcnt.h:25
Base class for reference countable objects.
Buffer pool.
static Buffer * container_of(void *data)
Get pointer to buffer from the pointer to its data.
Definition: buffer.h:42