search.hh (4585B)
1 #ifndef __SEARCH_H_GUARD__ 2 #define __SEARCH_H_GUARD__ 3 4 #include "../winsys/common.hh" 5 #include "client.hh" 6 #include "workspace.hh" 7 8 #include <functional> 9 #include <string> 10 #include <utility> 11 12 typedef class SearchSelector final 13 { 14 public: 15 enum class SelectionCriterium 16 { 17 OnWorkspaceBySelector, 18 ByNameEquals, 19 ByClassEquals, 20 ByInstanceEquals, 21 ByNameContains, 22 ByClassContains, 23 ByInstanceContains, 24 ForCondition, 25 }; 26 27 SearchSelector(Index index, Workspace::ClientSelector::SelectionCriterium criterium) 28 : m_tag(SearchSelectorTag::OnWorkspaceBySelector), 29 m_workspace_selector(std::pair(index, criterium)) 30 {} 31 32 SearchSelector(SelectionCriterium criterium, std::string&& str_) 33 : m_string(str_) 34 { 35 switch (criterium) { 36 case SelectionCriterium::ByNameEquals: m_tag = SearchSelectorTag::ByNameEquals; return; 37 case SelectionCriterium::ByClassEquals: m_tag = SearchSelectorTag::ByClassEquals; return; 38 case SelectionCriterium::ByInstanceEquals: m_tag = SearchSelectorTag::ByInstanceEquals; return; 39 case SelectionCriterium::ByNameContains: m_tag = SearchSelectorTag::ByNameContains; return; 40 case SelectionCriterium::ByClassContains: m_tag = SearchSelectorTag::ByClassContains; return; 41 case SelectionCriterium::ByInstanceContains: m_tag = SearchSelectorTag::ByInstanceContains; return; 42 default: return; 43 } 44 } 45 46 SearchSelector(std::function<bool(const Client_ptr)>&& filter) 47 : m_tag(SearchSelectorTag::ForCondition), 48 m_filter(filter) 49 {} 50 51 ~SearchSelector() 52 { 53 switch (m_tag) { 54 case SearchSelectorTag::OnWorkspaceBySelector: 55 { 56 (&m_workspace_selector)->std::pair< 57 Index, 58 Workspace::ClientSelector::SelectionCriterium 59 >::pair::~pair(); 60 61 return; 62 } 63 case SearchSelectorTag::ByNameEquals: // fallthrough 64 case SearchSelectorTag::ByClassEquals: // fallthrough 65 case SearchSelectorTag::ByInstanceEquals: // fallthrough 66 case SearchSelectorTag::ByNameContains: // fallthrough 67 case SearchSelectorTag::ByClassContains: // fallthrough 68 case SearchSelectorTag::ByInstanceContains: 69 { 70 (&m_string)->std::string::~string(); 71 72 return; 73 } 74 case SearchSelectorTag::ForCondition: 75 { 76 (&m_filter)->std::function<bool(const Client_ptr)>::function::~function(); 77 78 return; 79 } 80 default: return; 81 } 82 } 83 84 SearchSelector(SearchSelector&&) = delete; 85 86 SelectionCriterium 87 criterium() const 88 { 89 switch (m_tag) { 90 case SearchSelectorTag::OnWorkspaceBySelector: return SelectionCriterium::OnWorkspaceBySelector; 91 case SearchSelectorTag::ByNameEquals: return SelectionCriterium::ByNameEquals; 92 case SearchSelectorTag::ByClassEquals: return SelectionCriterium::ByClassEquals; 93 case SearchSelectorTag::ByInstanceEquals: return SelectionCriterium::ByInstanceEquals; 94 case SearchSelectorTag::ByNameContains: return SelectionCriterium::ByNameContains; 95 case SearchSelectorTag::ByClassContains: return SelectionCriterium::ByClassContains; 96 case SearchSelectorTag::ByInstanceContains: return SelectionCriterium::ByInstanceContains; 97 case SearchSelectorTag::ForCondition: return SelectionCriterium::ForCondition; 98 default: Util::die("no associated criterium"); 99 } 100 101 return {}; 102 } 103 104 std::pair<Index, Workspace::ClientSelector::SelectionCriterium> const& 105 workspace_selector() const 106 { 107 return m_workspace_selector; 108 } 109 110 std::string const& 111 string_value() const 112 { 113 return m_string; 114 } 115 116 std::function<bool(const Client_ptr)> const& 117 filter() const 118 { 119 return m_filter; 120 } 121 122 private: 123 enum class SearchSelectorTag 124 { 125 OnWorkspaceBySelector, 126 ByNameEquals, 127 ByClassEquals, 128 ByInstanceEquals, 129 ByNameContains, 130 ByClassContains, 131 ByInstanceContains, 132 ForCondition, 133 }; 134 135 SearchSelectorTag m_tag; 136 137 union 138 { 139 std::pair<Index, Workspace::ClientSelector::SelectionCriterium> m_workspace_selector; 140 std::string m_string; 141 std::function<bool(const Client_ptr)> m_filter; 142 }; 143 144 }* SearchSelector_ptr; 145 146 #endif//__SEARCH_H_GUARD__