search.h (1570B)
1 #pragma once 2 3 #include <functional> 4 #include <string> 5 #include <shared_mutex> 6 7 #include "datasource.h" 8 #include "tags.h" 9 10 namespace pluginLib::patchDB 11 { 12 struct Search; 13 14 struct SearchRequest 15 { 16 std::string name; 17 TypedTags tags; 18 DataSourceNodePtr sourceNode; 19 PatchPtr patch; // used by the UI to restore selection of a patch, the data source of this request patch will be null, the result will tell the UI which datasource it is in 20 SourceType sourceType = SourceType::Invalid; 21 std::set<TagType> anyTagOfType; 22 std::set<TagType> noTagOfType; 23 std::function<bool(const Patch&)> customCompareFunc; 24 25 bool match(const Patch& _patch) const; 26 bool isValid() const; 27 bool operator == (const SearchRequest& _r) const; 28 }; 29 30 using SearchResult = std::set<PatchPtr>; 31 using SearchCallback = std::function<void(const Search&)>; 32 33 enum class SearchState 34 { 35 NotStarted, 36 Running, 37 Cancelled, 38 Completed 39 }; 40 41 struct Search 42 { 43 SearchHandle handle = g_invalidSearchHandle; 44 45 SearchRequest request; 46 47 SearchCallback callback; 48 49 SearchResult results; 50 51 mutable std::shared_mutex resultsMutex; 52 53 SearchState state = SearchState::NotStarted; 54 55 size_t getResultSize() const 56 { 57 std::shared_lock searchLock(resultsMutex); 58 return results.size(); 59 } 60 61 SourceType getSourceType() const 62 { 63 if(request.sourceNode) 64 return request.sourceNode->type; 65 return request.sourceType; 66 } 67 68 void setCompleted() 69 { 70 state = SearchState::Completed; 71 72 if(!callback) 73 return; 74 75 std::shared_lock searchLock(resultsMutex); 76 callback(*this); 77 } 78 }; 79 }