cello
JUCE ValueTrees for Humans
Loading...
Searching...
No Matches
cello_sync.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
22#include <deque>
23#include <juce_core/juce_core.h>
24#include <juce_data_structures/juce_data_structures.h>
25
26namespace cello
27{
28
29class Object;
30
31class UpdateQueue
32{
33public:
34 UpdateQueue (Object& consumer, juce::Thread* thread);
35 virtual ~UpdateQueue () {}
36 UpdateQueue (const UpdateQueue&) = delete;
37 UpdateQueue& operator= (const UpdateQueue&) = delete;
38 UpdateQueue (UpdateQueue&&) = delete;
39 UpdateQueue& operator= (UpdateQueue&&) = delete;
40
44 int getPendingUpdateCount () const;
45
49 void performAllUpdates ();
50
55 void performNextUpdate ();
56
63 bool isDestinationThread (juce::Thread* thread) const { return thread == destThread; }
64
65protected:
66 void pushUpdate (juce::MemoryBlock&& update);
67
75 virtual void startUpdate (void* data, size_t size) = 0;
79 virtual void endUpdate () = 0;
80
81private:
83 Object& dest;
85 juce::Thread* destThread;
87 juce::CriticalSection mutex;
89 std::deque<juce::MemoryBlock> queue;
90};
91
92class SyncController;
93
108struct SyncData
109{
110 const char* data { nullptr };
111 size_t size { 0 };
112
113 SyncData () = default;
114 SyncData (const void* data_, size_t size_)
115 : data (static_cast<const char*> (data_))
116 , size (size_)
117 {
118 }
119 bool operator== (const SyncData& other) const
120 {
121 if (size != other.size)
122 return false;
123 for (size_t i { 0 }; i < size; ++i)
124 if (data[i] != other.data[i])
125 return false;
126 return true;
127 }
128 bool operator!= (const SyncData& other) const { return !(*this == other); }
129};
130
131class Sync : public UpdateQueue,
132 public juce::ValueTreeSynchroniser
133{
134public:
146 Sync (Object& producer, Object& consumer, juce::Thread* thread, SyncController* controller = nullptr);
147
148 Sync (const Sync&) = delete;
149 Sync& operator= (const Sync&) = delete;
150
151private:
162 void stateChanged (const void* encodedChange, size_t encodedChangeSize) override;
163
164 void startUpdate (void* data, size_t size) override;
165 void endUpdate () override;
166
167 SyncController* controller { nullptr };
168};
169
176{
177public:
186 SyncController (Object& obj1, juce::Thread* threadForObj1, Object& obj2, juce::Thread* threadForObj2);
187 ~SyncController () = default;
188
189 SyncController (const SyncController&) = delete;
190 SyncController& operator= (const SyncController&) = delete;
191 SyncController (SyncController&&) = delete;
192 SyncController& operator= (SyncController&&) = delete;
193
199 void performNextUpdate (juce::Thread* thread);
200
204 void performAllUpdates (juce::Thread* thread);
205
206private:
207 Sync sync1to2;
208 Sync sync2to1;
209
210 SyncData update1to2;
211 SyncData update2to1;
212
213 friend class Sync;
221 void startUpdate (Sync* sync, void* data, size_t size);
227 void endUpdate (Sync* sync);
228
237 bool shouldHandleUpdate (Sync* sync, void* data, size_t size) const;
238
239};
240
241} // namespace cello
Definition cello_object.h:34
Class to manage bi-directional sync between two Objects in different threads, preventing feedback loo...
Definition cello_sync.h:176
void performNextUpdate(juce::Thread *thread)
Perform the next update for the given thread.
Definition cello_sync.cpp:163
SyncController(Object &obj1, juce::Thread *threadForObj1, Object &obj2, juce::Thread *threadForObj2)
Construct a new SyncController object.
Definition cello_sync.cpp:125
void performAllUpdates(juce::Thread *thread)
Perform all updates for the given thread.
Definition cello_sync.cpp:174
Sync(Object &producer, Object &consumer, juce::Thread *thread, SyncController *controller=nullptr)
Construct a new Sync object.
Definition cello_sync.cpp:88
bool isDestinationThread(juce::Thread *thread) const
Check if the given thread is the destination thread for this update queue.
Definition cello_sync.h:63
void performAllUpdates()
Execute each of the updates that are ready.
Definition cello_sync.cpp:38
virtual void endUpdate()=0
Called when the update is complete. clear the update data.
int getPendingUpdateCount() const
Definition cello_sync.cpp:32
virtual void startUpdate(void *data, size_t size)=0
Called when a new update is pushed onto the queue. We use this to prevent feedback loops.
void performNextUpdate()
Pop the next event from the queue and apply the change to the destination value tree.
Definition cello_sync.cpp:44
Data structure for holding synchronization update information.
Definition cello_sync.h:109