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