Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
hashmap_node.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 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/hashmap_node.h
10 //! @brief Hashmap node.
11 
12 #ifndef ROC_CORE_HASHMAP_NODE_H_
13 #define ROC_CORE_HASHMAP_NODE_H_
14 
15 #include "roc_core/hashsum.h"
16 #include "roc_core/macro_helpers.h"
17 #include "roc_core/noncopyable.h"
18 #include "roc_core/panic.h"
19 #include "roc_core/stddefs.h"
20 
21 namespace roc {
22 namespace core {
23 
24 //! Hashmap node internal data.
25 struct HashmapData {
26  //! Previous node in bucket.
28 
29  //! Next node in bucket.
31 
32  //! Previous node in list of all nodes.
34 
35  //! Next node in in list of all nodes.
37 
38  //! Cached node hash.
40 
41  //! The bucket this node belongs to.
42  //! @remarks
43  //! NULL if node is not member of any hashmap.
44  void* bucket;
45 
46  HashmapData()
47  : bucket_prev(NULL)
48  , bucket_next(NULL)
49  , all_prev(NULL)
50  , all_next(NULL)
51  , hash(0)
52  , bucket(NULL) {
53  }
54 };
55 
56 //! Base class for Hashmap element.
57 //! @remarks
58 //! Object should inherit this class to be able to be a member of Hashmap.
59 //! Tag allows to inherit multiple copies of ListNode and include same
60 //! object into multiple lists.
61 template <class Tag = void> class HashmapNode : public NonCopyable<HashmapNode<Tag> > {
62 public:
63  ~HashmapNode() {
64  if (hashmap_data_.bucket != NULL) {
65  roc_panic(
66  "hashmap node: attempt to destroy node while it's still in hashmap");
67  }
68  }
69 
70  //! Get pointer to parent node from pointer to internal data.
72  return ROC_CONTAINER_OF(data, HashmapNode, hashmap_data_);
73  }
74 
75  //! Get pointer to internal data.
77  return &hashmap_data_;
78  }
79 
80 private:
81  mutable HashmapData hashmap_data_;
82 };
83 
84 } // namespace core
85 } // namespace roc
86 
87 #endif // ROC_CORE_HASHMAP_NODE_H_
Base class for Hashmap element.
Definition: hashmap_node.h:61
static HashmapNode * hashmap_node(HashmapData *data)
Get pointer to parent node from pointer to internal data.
Definition: hashmap_node.h:71
HashmapData * hashmap_data() const
Get pointer to internal data.
Definition: hashmap_node.h:76
Base class for non-copyable objects.
Definition: noncopyable.h:23
Hash sum.
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
size_t hashsum_t
Hash type.
Definition: hashsum.h:21
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.
Hashmap node internal data.
Definition: hashmap_node.h:25
HashmapData * bucket_next
Next node in bucket.
Definition: hashmap_node.h:30
HashmapData * all_prev
Previous node in list of all nodes.
Definition: hashmap_node.h:33
HashmapData * all_next
Next node in in list of all nodes.
Definition: hashmap_node.h:36
void * bucket
The bucket this node belongs to.
Definition: hashmap_node.h:44
hashsum_t hash
Cached node hash.
Definition: hashmap_node.h:39
HashmapData * bucket_prev
Previous node in bucket.
Definition: hashmap_node.h:27