Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
list_node.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 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/list_node.h
10 //! @brief Linked list node.
11 
12 #ifndef ROC_CORE_LIST_NODE_H_
13 #define ROC_CORE_LIST_NODE_H_
14 
15 #include "roc_core/macro_helpers.h"
16 #include "roc_core/noncopyable.h"
17 #include "roc_core/panic.h"
18 #include "roc_core/stddefs.h"
19 
20 namespace roc {
21 namespace core {
22 
23 //! List node internal data.
24 struct ListData {
25  //! Previous list element.
27 
28  //! Next list element.
30 
31  //! The list this node is member of.
32  //! @remarks
33  //! NULL if node is not member of any list.
34  void* list;
35 
36  ListData()
37  : prev(NULL)
38  , next(NULL)
39  , list(NULL) {
40  }
41 };
42 
43 //! Base class for List element.
44 //! @remarks
45 //! Object should inherit this class to be able to be a member of List.
46 //! Tag allows to inherit multiple copies of ListNode and include same
47 //! object into multiple lists.
48 template <class Tag = void> class ListNode : public NonCopyable<ListNode<Tag> > {
49 public:
50  ~ListNode() {
51  if (list_data_.list != NULL) {
52  roc_panic("list node: attempt to destroy node while it's still in queue");
53  }
54  }
55 
56  //! Get pointer to parent node from pointer to internal data.
57  static ListNode* list_node(ListData* data) {
58  return ROC_CONTAINER_OF(data, ListNode, list_data_);
59  }
60 
61  //! Get pointer to internal data.
62  ListData* list_data() const {
63  return &list_data_;
64  }
65 
66 private:
67  mutable ListData list_data_;
68 };
69 
70 } // namespace core
71 } // namespace roc
72 
73 #endif // ROC_CORE_LIST_NODE_H_
Base class for List element.
Definition: list_node.h:48
ListData * list_data() const
Get pointer to internal data.
Definition: list_node.h:62
static ListNode * list_node(ListData *data)
Get pointer to parent node from pointer to internal data.
Definition: list_node.h:57
Base class for non-copyable objects.
Definition: noncopyable.h:23
Helper macros.
#define ROC_CONTAINER_OF(ptr, type, member)
Cast a member of a structure out to the containing structure.
Definition: macro_helpers.h:37
Root namespace.
Non-copyable object.
Panic.
#define roc_panic(...)
Print error message and terminate program gracefully.
Definition: panic.h:50
Commonly used types and functions.
List node internal data.
Definition: list_node.h:24
ListData * next
Next list element.
Definition: list_node.h:29
void * list
The list this node is member of.
Definition: list_node.h:34
ListData * prev
Previous list element.
Definition: list_node.h:26