The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
3.5KB

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