1 /*
2   ==============================================================================
3 
4    This file is part of the JUCE library.
5    Copyright (c) 2020 - Raw Material Software Limited
6 
7    JUCE is an open source library subject to commercial or open-source
8    licensing.
9 
10    By using JUCE, you agree to the terms of both the JUCE 6 End-User License
11    Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
12 
13    End User License Agreement: www.juce.com/juce-6-licence
14    Privacy Policy: www.juce.com/juce-privacy-policy
15 
16    Or: You may also use this code under the terms of the GPL v3 (see
17    www.gnu.org/licenses).
18 
19    JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20    EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21    DISCLAIMED.
22 
23   ==============================================================================
24 */
25 
26 namespace juce
27 {
28 
29 //==============================================================================
30 /**
31     This class can be used to watch for all changes to the state of a ValueTree,
32     and to convert them to a transmittable binary encoding.
33 
34     The purpose of this class is to allow two or more ValueTrees to be remotely
35     synchronised by transmitting encoded changes over some kind of transport
36     mechanism.
37 
38     To use it, you'll need to implement a subclass of ValueTreeSynchroniser
39     and implement the stateChanged() method to transmit the encoded change (maybe
40     via a network or other means) to a remote destination, where it can be
41     applied to a target tree.
42 
43     @tags{DataStructures}
44 */
45 class JUCE_API  ValueTreeSynchroniser  : private ValueTree::Listener
46 {
47 public:
48     /** Creates a ValueTreeSynchroniser that watches the given tree.
49 
50         After creating an instance of this class and somehow attaching it to
51         a target tree, you probably want to call sendFullSyncCallback() to
52         get them into a common starting state.
53     */
54     ValueTreeSynchroniser (const ValueTree& tree);
55 
56     /** Destructor. */
57     ~ValueTreeSynchroniser() override;
58 
59     /** This callback happens when the ValueTree changes and the given state-change message
60         needs to be applied to any other trees that need to stay in sync with it.
61         The data is an opaque blob of binary that you should transmit to wherever your
62         target tree lives, and use applyChange() to apply this to the target tree.
63     */
64     virtual void stateChanged (const void* encodedChange, size_t encodedChangeSize) = 0;
65 
66     /** Forces the sending of a full state message, which may be large, as it
67         encodes the entire ValueTree.
68 
69         This will internally invoke stateChanged() with the encoded version of the state.
70     */
71     void sendFullSyncCallback();
72 
73     /** Applies an encoded change to the given destination tree.
74 
75         When you implement a receiver for changes that were sent by the stateChanged()
76         message, this is the function that you'll need to call to apply them to the
77         target tree that you want to be synced.
78     */
79     static bool applyChange (ValueTree& target,
80                              const void* encodedChangeData, size_t encodedChangeDataSize,
81                              UndoManager* undoManager);
82 
83     /** Returns the root ValueTree that is being observed. */
getRoot()84     const ValueTree& getRoot() noexcept       { return valueTree; }
85 
86 private:
87     ValueTree valueTree;
88 
89     void valueTreePropertyChanged (ValueTree&, const Identifier&) override;
90     void valueTreeChildAdded (ValueTree&, ValueTree&) override;
91     void valueTreeChildRemoved (ValueTree&, ValueTree&, int) override;
92     void valueTreeChildOrderChanged (ValueTree&, int, int) override;
93 
94     JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ValueTreeSynchroniser)
95 };
96 
97 } // namespace juce
98