]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Use move semantics when updating the content of the StateHolder
authorRemi Gacogne <remi.gacogne@powerdns.com>
Mon, 18 Nov 2019 09:11:58 +0000 (10:11 +0100)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Mon, 18 Nov 2019 09:11:58 +0000 (10:11 +0100)
pdns/sholder.hh

index 75837c30f1461280db8544bc3e1662faf91116ca..ce5da43ce38cba2b7e79769bb9985ea376aaa45c 100644 (file)
@@ -92,12 +92,22 @@ public:
     return LocalStateHolder<T>(this);
   }
 
-  void setState(T state) //!< Safely & slowly change the global state
+  void setState(const T& state) //!< Safely & slowly change the global state
   {
     std::shared_ptr<T> newState = std::make_shared<T>(state);
     {
       std::lock_guard<std::mutex> l(d_lock);
-      d_state = newState;
+      d_state = std::move(newState);
+      d_generation++;
+    }
+  }
+
+  void setState(T&& state) //!< Safely & slowly change the global state
+  {
+    std::shared_ptr<T> newState = std::make_shared<T>(std::move(state));
+    {
+      std::lock_guard<std::mutex> l(d_lock);
+      d_state = std::move(newState);
       d_generation++;
     }
   }
@@ -111,10 +121,10 @@ public:
   //! Safely & slowly modify the global state
   template<typename F>
   void modify(F act) {
-    std::lock_guard<std::mutex> l(d_lock); 
+    std::lock_guard<std::mutex> l(d_lock);
     auto state=*d_state; // and yes, these three steps are necessary, can't ever modify state in place, even when locked!
     act(state);
-    d_state = std::make_shared<T>(state);
+    d_state = std::make_shared<T>(std::move(state));
     ++d_generation;
   }