mirror of
				https://github.com/ciphervance/supercell-wx.git
				synced 2025-10-31 16:40:04 +00:00 
			
		
		
		
	Add a maximum number of forward/backward time steps that can be queued
This commit is contained in:
		
							parent
							
								
									3537a233ca
								
							
						
					
					
						commit
						97693fdace
					
				
					 4 changed files with 133 additions and 4 deletions
				
			
		
							
								
								
									
										64
									
								
								scwx-qt/source/scwx/qt/util/queue_counter.hpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								scwx-qt/source/scwx/qt/util/queue_counter.hpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,64 @@ | |||
| #pragma once | ||||
| 
 | ||||
| #include <memory> | ||||
| #include <atomic> | ||||
| 
 | ||||
| namespace scwx::qt::util | ||||
| { | ||||
| 
 | ||||
| class QueueCounter | ||||
| { | ||||
| public: | ||||
|    /**
 | ||||
|     * Counts the number of items in a queue, and prevents it from exceeding a | ||||
|     * count in a thread safe manor. This is lock free, assuming | ||||
|     * std::atomic<size_t> supports lock free fetch_add and fetch_sub. | ||||
|     */ | ||||
| 
 | ||||
|    /**
 | ||||
|     * Construct a QueueCounter with a given maximum count | ||||
|     * | ||||
|     * @param maxCount The maximum number of items in the queue | ||||
|     */ | ||||
|    explicit QueueCounter(size_t maxCount); | ||||
| 
 | ||||
|    ~QueueCounter(); | ||||
|    QueueCounter(const QueueCounter&)            = delete; | ||||
|    QueueCounter(QueueCounter&&)                 = delete; | ||||
|    QueueCounter& operator=(const QueueCounter&) = delete; | ||||
|    QueueCounter& operator=(QueueCounter&&)      = delete; | ||||
| 
 | ||||
|    /**
 | ||||
|     * Called before adding an item. If it returns true, it is ok to add. If it | ||||
|     * returns false, it should not be added | ||||
|     * | ||||
|     * @return true if it is ok to add, false if the queue is full | ||||
|     */ | ||||
|    bool add(); | ||||
| 
 | ||||
|    /**
 | ||||
|     * Called when item is removed from the queue. Should only be called after a | ||||
|     * corresponding and successful call to add. | ||||
|     */ | ||||
|    void remove(); | ||||
| 
 | ||||
|    /**
 | ||||
|     * Tells if this instance is lock free | ||||
|     * | ||||
|     * @return true if it is lock free, false otherwise | ||||
|     */ | ||||
|    bool is_lock_free(); | ||||
| 
 | ||||
|    /**
 | ||||
|     * Tells if this class is always lock free. True if it is lock free, false | ||||
|     * otherwise | ||||
|     */ | ||||
|    static constexpr bool is_always_lock_free = | ||||
|       std::atomic<size_t>::is_always_lock_free; | ||||
| 
 | ||||
| private: | ||||
|    class Impl; | ||||
|    std::unique_ptr<Impl> p; | ||||
| }; | ||||
| 
 | ||||
| } // namespace scwx::qt::util
 | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 AdenKoperczak
						AdenKoperczak