zxs 6 days ago
parent
commit
9d26566f3e
2 changed files with 32 additions and 7 deletions
  1. 28 3
      robot/robotics/cache_queue.hpp
  2. 4 4
      robot/robotics/linq.hpp

+ 28 - 3
robot/robotics/cache_queue.hpp

@@ -25,8 +25,8 @@ namespace robotics {
 			 * @param name
 			 * @param values
 			 */
-			cache_queue(std::string const& name, std::vector<_Type>& values) :
-				v3::linq<_Type>(values),
+			cache_queue(std::string const& name) :
+				v3::linq<_Type>(source_data_),
 				file_name_(name) {
 			}
 			/**
@@ -34,6 +34,29 @@ namespace robotics {
 			 */
 			~cache_queue() {
 			}
+			/**
+			 * @brief Ìí¼Ó
+			 * @param value 
+			 */
+			void push(_Type const&value) {
+				this->append(value);
+				save();
+			}
+			/**
+			 * @brief ÒƳý
+			 * @return
+			 */
+			_Type pop() {
+				_Type value;
+				{
+					std::lock_guard<std::mutex> locker(this->mutex_);
+					value = source_data_.front();
+					source_data_.erase(source_data_.begin());
+				}
+				this->reset();
+				save();
+				return value;
+			}
 			/**
 			 * @brief ¼ÓÔØ
 			 */
@@ -78,7 +101,8 @@ namespace robotics {
 					std::filesystem::create_directory(directory);
 				}
 				v3::archive::stream stream;
-				for (auto& it : this->source()) {
+				auto datas = source_data_;
+				for (auto& it : datas) {
 					stream << it;
 				}
 				std::fstream file("data/" + file_name_, std::ios::binary | std::ios::out);
@@ -93,6 +117,7 @@ namespace robotics {
 				return true;
 			}
 		private:
+			std::vector<_Type> source_data_;
 			std::string file_name_;
         };
     }

+ 4 - 4
robot/robotics/linq.hpp

@@ -900,9 +900,9 @@ namespace robotics {
              * @brief 改变事件
              */
             std::vector<std::function<void(_Type const&, _Type const&, linq_change_type, bool)>> changed_event;
-        private:
+        protected:
             std::mutex                                               mutex_;
-            std::vector<_Type>& source_;
+            std::vector<_Type>&                                      source_;
             std::vector<bool>                                        source_ok_;
             std::vector<std::tuple<_Type, _Type, linq_change_type>>  changed_list_;
         };
@@ -1784,10 +1784,10 @@ namespace robotics {
              * @brief 改变事件
              */
             std::vector<std::function<void(_Type const&, _Type const&, linq_change_type, bool)>> changed_event;
-        private:
+        protected:
             std::mutex                                               mutex_;
             std::vector<bool>                                        source_ok_;
-            std::vector<std::shared_ptr<_Type>>& source_;
+            std::vector<std::shared_ptr<_Type>>&                     source_;
             std::vector<std::tuple<_Type, _Type, linq_change_type>>  changed_list_;
         };
         /**