You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@brpc.apache.org by ja...@apache.org on 2022/12/13 02:47:53 UTC

[incubator-brpc] branch master updated: FlatMap's value supports unique_ptr

This is an automated email from the ASF dual-hosted git repository.

jamesge pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-brpc.git


The following commit(s) were added to refs/heads/master by this push:
     new 50e41d0a FlatMap's value supports unique_ptr
50e41d0a is described below

commit 50e41d0a0b36634b4f275d2bc204ed3ec908bcde
Author: gejun.0 <ge...@bytedance.com>
AuthorDate: Tue Dec 13 10:47:38 2022 +0800

    FlatMap's value supports unique_ptr
---
 src/butil/containers/flat_map.h     | 8 ++++++++
 src/butil/containers/flat_map_inl.h | 8 ++++----
 2 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/src/butil/containers/flat_map.h b/src/butil/containers/flat_map.h
index 6f2deafe..6789c3c7 100644
--- a/src/butil/containers/flat_map.h
+++ b/src/butil/containers/flat_map.h
@@ -361,11 +361,15 @@ public:
     //                                             ^^^^^^^^^^^
     const K& first_ref() const { return _key; }
     T& second_ref() { return _value; }
+    T&& second_movable_ref() { return std::move(_value); }
     value_type& value_ref() { return *reinterpret_cast<value_type*>(this); }
     inline static const K& first_ref_from_value(const value_type& v)
     { return v.first; }
     inline static const T& second_ref_from_value(const value_type& v)
     { return v.second; }
+    inline static T&& second_movable_ref_from_value(value_type& v)
+    { return std::move(v.second); }
+
 private:
     const K _key;
     T _value;
@@ -378,12 +382,16 @@ public:
     explicit FlatMapElement(const K& k) : _key(k) {}
     const K& first_ref() const { return _key; }
     FlatMapVoid& second_ref() { return second_ref_from_value(_key); }
+    FlatMapVoid& second_movable_ref() { return second_ref(); }
     value_type& value_ref() { return _key; }
     inline static const K& first_ref_from_value(value_type& v) { return v; }
     inline static FlatMapVoid& second_ref_from_value(value_type&) {
         static FlatMapVoid dummy;
         return dummy;
     }
+    inline static const FlatMapVoid& second_movable_ref_from_value(value_type& v) {
+        return second_ref_from_value(v);
+    }
 private:
     K _key;
 };
diff --git a/src/butil/containers/flat_map_inl.h b/src/butil/containers/flat_map_inl.h
index ad5598c2..e98c88b5 100644
--- a/src/butil/containers/flat_map_inl.h
+++ b/src/butil/containers/flat_map_inl.h
@@ -394,7 +394,7 @@ size_t FlatMap<_K, _T, _H, _E, _S>::erase(const K2& key, _T* old_value) {
     }
     if (_eql(first_node.element().first_ref(), key)) {
         if (old_value) {
-            *old_value = first_node.element().second_ref();
+            *old_value = first_node.element().second_movable_ref();
         }
         if (first_node.next == NULL) {
             first_node.element().~Element();
@@ -420,7 +420,7 @@ size_t FlatMap<_K, _T, _H, _E, _S>::erase(const K2& key, _T* old_value) {
             first_node.next = p->next;
             const_cast<_K&>(first_node.element().first_ref()) =
                 p->element().first_ref();
-            first_node.element().second_ref() = p->element().second_ref();
+            first_node.element().second_ref() = p->element().second_movable_ref();
             p->element().~Element();
             _pool.back(p);
         }
@@ -432,7 +432,7 @@ size_t FlatMap<_K, _T, _H, _E, _S>::erase(const K2& key, _T* old_value) {
     while (p) {
         if (_eql(p->element().first_ref(), key)) {
             if (old_value) {
-                *old_value = p->element().second_ref();
+                *old_value = p->element().second_movable_ref();
             }
             last_p->next = p->next;
             p->element().~Element();
@@ -616,7 +616,7 @@ bool FlatMap<_K, _T, _H, _E, _S>::resize(size_t nbucket2) {
     }
     for (iterator it = begin(); it != end(); ++it) {
         new_map[Element::first_ref_from_value(*it)] = 
-            Element::second_ref_from_value(*it);
+            Element::second_movable_ref_from_value(*it);
     }
     new_map.swap(*this);
     return true;


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org