cello
JUCE ValueTrees for Humans
Loading...
Searching...
No Matches
cello_query.cpp
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#include "cello_query.h"
21
22namespace cello
23{
24Query::Query (const juce::Identifier& resultType)
25: type { resultType }
26{
27}
28
29Query::Query (Predicate filter, const juce::Identifier& resultType)
30: Query { resultType }
31{
32 addFilter (filter);
33}
34
35Query& Query::addFilter (Predicate filter)
36{
37 filters.push_back (filter);
38 return *this;
39}
40
41juce::ValueTree Query::search (juce::ValueTree tree, bool deep, bool returnFirstFound) const
42{
43 juce::ValueTree result { type };
44 for (auto child : tree)
45 {
46 if (filter (child))
47 {
48 auto childCopy { juce::ValueTree { child.getType () } };
49 if (deep)
50 childCopy.copyPropertiesAndChildrenFrom (child, nullptr);
51 else
52 childCopy.copyPropertiesFrom (child, nullptr);
53 if (returnFirstFound)
54 return childCopy;
55 result.appendChild (childCopy, nullptr);
56 }
57 }
58
59 if (returnFirstFound)
60 return {};
61
62 return sort (result);
63 // return result;
64}
65
66bool Query::filter (juce::ValueTree tree) const
67{
68 if (filters.size () > 0)
69 {
70 for (auto fn : filters)
71 {
72 if (!fn (tree))
73 return false;
74 }
75 }
76 return true;
77}
78
79Query& Query::addComparison (Comparison sorter)
80{
81 sorters.push_back (sorter);
82 return *this;
83}
84
85juce::ValueTree Query::sort (juce::ValueTree tree, juce::UndoManager* undo, bool stableSort) const
86{
87 if (sorters.size () > 0)
88 tree.sort (*this, undo, stableSort);
89 return tree;
90}
91
92int Query::compareElements (const juce::ValueTree& left, const juce::ValueTree& right) const
93{
94 for (auto sorter : sorters)
95 {
96 auto sortOrder { sorter (left, right) };
97 if (sortOrder != 0)
98 return sortOrder;
99 }
100 return 0;
101}
102} // namespace cello
103
104#if RUN_UNIT_TESTS
105#include "test/test_cello_query.inl"
106#endif
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:85
Query & addComparison(Comparison sorter)
Add a comparison function to the list we use to sort a list of children.
Definition cello_query.cpp:79
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
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