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 //! Base class for hashmap element.
25 //! @remarks
26 //! Object should inherit this class to be able to be a member of Hashmap.
27 class HashmapNode : public NonCopyable<HashmapNode> {
28 public:
29  //! Hashmap node data.
30  struct HashmapNodeData {
31  //! Previous node in bucket.
33 
34  //! Next node in bucket.
36 
37  //! Previous node in list of all nodes.
39 
40  //! Next node in in list of all nodes.
42 
43  //! Cached node hash.
45 
46  //! The bucket this node belongs to.
47  //! @remarks
48  //! NULL if node is not member of any hashmap.
49  void* bucket;
50 
52  : bucket_prev(NULL)
53  , bucket_next(NULL)
54  , all_prev(NULL)
55  , all_next(NULL)
56  , hash(0)
57  , bucket(NULL) {
58  }
59 
60  //! Get HashmapNode object that contains this HashmapData object.
62  return ROC_CONTAINER_OF(this, HashmapNode, hashmap_data_);
63  }
64  };
65 
66  ~HashmapNode() {
67  if (hashmap_data_.bucket != NULL) {
68  roc_panic("hashmap node:"
69  " can't call destructor for an element that is still in hashmap");
70  }
71  }
72 
73  //! Get hashmap node data.
75  return &hashmap_data_;
76  }
77 
78 private:
79  mutable HashmapNodeData hashmap_data_;
80 };
81 
82 } // namespace core
83 } // namespace roc
84 
85 #endif // ROC_CORE_HASHMAP_NODE_H_
Base class for hashmap element.
Definition: hashmap_node.h:27
HashmapNodeData * hashmap_node_data() const
Get hashmap node data.
Definition: hashmap_node.h:74
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.
HashmapNodeData * all_next
Next node in in list of all nodes.
Definition: hashmap_node.h:41
void * bucket
The bucket this node belongs to.
Definition: hashmap_node.h:49
HashmapNode * container_of()
Get HashmapNode object that contains this HashmapData object.
Definition: hashmap_node.h:61
hashsum_t hash
Cached node hash.
Definition: hashmap_node.h:44
HashmapNodeData * bucket_prev
Previous node in bucket.
Definition: hashmap_node.h:32
HashmapNodeData * all_prev
Previous node in list of all nodes.
Definition: hashmap_node.h:38
HashmapNodeData * bucket_next
Next node in bucket.
Definition: hashmap_node.h:35