Selaa lähdekoodia

#修复linq死锁问题

zxs 1 viikko sitten
vanhempi
commit
997b1fe68b
1 muutettua tiedostoa jossa 22 lisäystä ja 10 poistoa
  1. 22 10
      robot/robotics/linq.hpp

+ 22 - 10
robot/robotics/linq.hpp

@@ -807,6 +807,7 @@ namespace robotics {
              */
             template<bool _Unlock = true>
             bool contains(_Type const& value) {
+                bool result = false;
                 typename std::vector<_Type>::const_iterator it;
                 for (size_t i = 0; i < source_ok_.size(); ++i) {
                     if (!source_ok_[i]) {
@@ -814,7 +815,8 @@ namespace robotics {
                     }
                     it = source_.begin() + i;
                     if (*it == value) {
-                        return true;
+                        result = true;
+                        break;
                     }
                 }
                 if constexpr (_Unlock) {
@@ -829,6 +831,7 @@ namespace robotics {
              */
             template<bool _Unlock = true>
             bool contains(std::function<bool(typename std::vector<_Type>::const_iterator)>const& fn) {
+                bool result = false;
                 typename std::vector<_Type>::const_iterator it;
                 for (size_t i = 0; i < source_ok_.size(); ++i) {
                     if (!source_ok_[i]) {
@@ -836,7 +839,8 @@ namespace robotics {
                     }
                     it = source_.begin() + i;
                     if (fn(it)) {
-                        return true;
+                        result = true;
+                        break;
                     }
                 }
                 if constexpr (_Unlock) {
@@ -1001,15 +1005,17 @@ namespace robotics {
              */
             template<bool _Unlock = true>
             bool empty() {
+                bool result = true;
                 for (bool it : source_ok_) {
                     if (it) {
-                        return false;
+                        result = false;
+                        break;
                     }
                 }
                 if constexpr (_Unlock) {
                     unlock();
                 }
-                return true;
+                return result;
             }
             /**
              * @brief 改变事件
@@ -1823,6 +1829,7 @@ namespace robotics {
              */
             template<bool _Unlock = true>
             bool contains(std::shared_ptr<_Type> const& value) {
+                bool result = false;
                 typename std::vector<std::shared_ptr<_Type>>::const_iterator it;
                 for (size_t i = 0; i < source_ok_.size(); ++i) {
                     if (!source_ok_[i]) {
@@ -1830,13 +1837,14 @@ namespace robotics {
                     }
                     it = source_.begin() + i;
                     if (**it == *value) {
-                        return true;
+                        result =  true;
+                        break;
                     }
                 }
                 if constexpr (_Unlock) {
                     unlock();
                 }
-                return false;
+                return result;
             }
             /**
              * @brief 是否存在
@@ -1845,6 +1853,7 @@ namespace robotics {
              */
             template<bool _Unlock = true>
             bool contains(std::function<bool(std::shared_ptr<const _Type> const&)>const& fn) {
+                bool result = false;
                 typename std::vector<std::shared_ptr<_Type>>::const_iterator it;
                 for (size_t i = 0; i < source_ok_.size(); ++i) {
                     if (!source_ok_[i]) {
@@ -1852,13 +1861,14 @@ namespace robotics {
                     }
                     it = source_.begin() + i;
                     if (fn(*it)) {
-                        return true;
+                        result = true;
+                        break;
                     }
                 }
                 if constexpr (_Unlock) {
                     unlock();
                 }
-                return false;
+                return result;
             }
             /**
              * @brief 选择
@@ -2003,15 +2013,17 @@ namespace robotics {
              */
             template<bool _Unlock = true>
             bool empty() {
+                bool result = true;
                 for (bool it : source_ok_) {
                     if (it) {
-                        return false;
+                        result = false;
+                        break;
                     }
                 }
                 if constexpr (_Unlock) {
                     unlock();
                 }
-                return true;
+                return result;
             }
             /**
              * @brief 获取值