cello
JUCE ValueTrees for Humans
Loading...
Searching...
No Matches
cello_query.h
1/*
2 Copyright (c) 2023 Brett g Porter
3 Permission is hereby granted, free of charge, to any person obtaining a copy
4 of this software and associated documentation files (the "Software"), to deal
5 in the Software without restriction, including without limitation the rights
6 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 copies of the Software, and to permit persons to whom the Software is
8 furnished to do so, subject to the following conditions:
9 The above copyright notice and this permission notice shall be included in all
10 copies or substantial portions of the Software.
11 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17 SOFTWARE.
18*/
19
20#pragma once
21#include <juce_core/juce_core.h>
22#include <juce_data_structures/juce_data_structures.h>
23
24namespace cello
25{
26
27class Query
28{
29public:
31 static inline const juce::Identifier Result { "result" };
32
33 // query function, returns true if the tree it is passed should
34 // be included in the result set.
35 using Predicate = std::function<bool (juce::ValueTree)>;
36
37 // comparison/sort function.
38 // return 0 if the two trees should sort equally.
39 // return -1 if left should come before right
40 // return +1 if right should come before left.
41 using Comparison = std::function<int (const juce::ValueTree&, const juce::ValueTree&)>;
42
48 Query (const juce::Identifier& resultType = Result);
49
58 Query (Predicate filter, const juce::Identifier& resultType = Result);
59
60 ~Query () = default;
61 Query (const Query&) = default;
62 Query& operator= (const Query&) = default;
63 Query (Query&&) = default;
64 Query& operator= (Query&&) = default;
65
74 Query& addFilter (Predicate filter);
75
89 juce::ValueTree search (juce::ValueTree tree, bool deep, bool returnFirstFound = false) const;
90
97 int remove (juce::ValueTree tree) const;
98
106 Query& addComparison (Comparison sorter);
107
118 juce::ValueTree sort (juce::ValueTree tree, juce::UndoManager* undo = nullptr, bool stableSort = false) const;
119
120private:
128 bool filter (juce::ValueTree tree) const;
129
130 // ValueTree needs to be able to use our compareElements method.
131 friend class juce::ValueTree;
140 int compareElements (const juce::ValueTree& left, const juce::ValueTree& right) const;
141
142private:
144 juce::Identifier type;
146 std::vector<Predicate> filters;
148 std::vector<Comparison> sorters;
149};
150
151} // namespace cello
Definition cello_query.h:28
juce::ValueTree sort(juce::ValueTree tree, juce::UndoManager *undo=nullptr, bool stableSort=false) const
Use the list of comparison functions to sort the tree arg into its desired order.
Definition cello_query.cpp:100
static const juce::Identifier Result
The default identifier for the query results tree.
Definition cello_query.h:31
Query & addComparison(Comparison sorter)
Add a comparison function to the list we use to sort a list of children.
Definition cello_query.cpp:94
Query & addFilter(Predicate filter)
Append a filter predicate to the end of our list; these are executed in the sequence they're added,...
Definition cello_query.cpp:35
Query(const juce::Identifier &resultType=Result)
Construct a new Query object.
Definition cello_query.cpp:24
int remove(juce::ValueTree tree) const
Remove all children from the tree that match the query.
Definition cello_query.cpp:66
juce::ValueTree search(juce::ValueTree tree, bool deep, bool returnFirstFound=false) const
Execute the query we're programmed for – iterate through the children of tree, returning a new tree o...
Definition cello_query.cpp:41