GRPC C++  1.26.0
slice_weak_hash_table.h
Go to the documentation of this file.
1 /*
2  * Copyright 2016 gRPC authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef GRPC_CORE_LIB_SLICE_SLICE_WEAK_HASH_TABLE_H
18 #define GRPC_CORE_LIB_SLICE_SLICE_WEAK_HASH_TABLE_H
19 
21 
26 
36 
37 namespace grpc_core {
38 
39 template <typename T, size_t Size>
40 class SliceWeakHashTable : public RefCounted<SliceWeakHashTable<T, Size>> {
41  public:
44  return MakeRefCounted<SliceWeakHashTable<T, Size>>();
45  }
46 
48  SliceWeakHashTable() = default;
49  ~SliceWeakHashTable() = default;
50 
53  void Add(const grpc_slice& key, T value) {
54  const size_t idx = grpc_slice_hash_internal(key) % Size;
55  entries_[idx].Set(key, std::move(value));
56  return;
57  }
58 
61  const T* Get(const grpc_slice& key) const {
62  const size_t idx = grpc_slice_hash_internal(key) % Size;
63  const auto& entry = entries_[idx];
64  return grpc_slice_eq(entry.key(), key) ? entry.value() : nullptr;
65  }
66 
67  private:
69  class Entry {
70  public:
71  Entry() = default;
72  ~Entry() {
73  if (is_set_) grpc_slice_unref_internal(key_);
74  }
75  const grpc_slice& key() const { return key_; }
76 
78  const T* value() const {
79  if (!is_set_) return nullptr;
80  return &value_;
81  }
82 
84  void Set(const grpc_slice& key, T&& value) {
85  if (is_set_) grpc_slice_unref_internal(key_);
86  key_ = key;
87  value_ = std::move(value);
88  is_set_ = true;
89  }
90 
91  private:
92  grpc_slice key_;
93  T value_;
94  bool is_set_ = false;
95  };
96 
97  Entry entries_[Size];
98 };
99 
100 } // namespace grpc_core
101 
102 #endif /* GRPC_CORE_LIB_SLICE_SLICE_WEAK_HASH_TABLE_H */
static RefCountedPtr< SliceWeakHashTable > Create()
Creates a new table of at most size entries.
Definition: slice_weak_hash_table.h:43
A grpc_slice s, if initialized, represents the byte range s.bytes[0..s.length-1]. ...
Definition: slice.h:60
Internal thread interface.
Definition: backoff.h:26
void grpc_slice_unref_internal(const grpc_slice &slice)
Definition: slice_internal.h:272
GPRAPI int grpc_slice_eq(grpc_slice a, grpc_slice b)
Definition: slice_weak_hash_table.h:40
Definition: ref_counted_ptr.h:35
uint32_t grpc_slice_hash_internal(const grpc_slice &s)
Definition: slice_internal.h:333
Definition: ref_counted.h:248
void Add(const grpc_slice &key, T value)
Add a mapping from key to value, taking ownership of key.
Definition: slice_weak_hash_table.h:53
SliceWeakHashTable()=default
Use Create function instead of using this directly.
const T * Get(const grpc_slice &key) const
Returns the value from the table associated with / key or null if not found.
Definition: slice_weak_hash_table.h:61