1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
/*
GuiDataExchangeTest.cpp - TEMPORARY / PROTOTYPE / DEMO
Copyright 2024, Ichthyostega
This file is part of yoshimi, which is free software: you can redistribute
it and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.
yoshimi is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License (version 2
or later) for more details.
You should have received a copy of the GNU General Public License
along with yoshimi. If not, see <http://www.gnu.org/licenses/>.
*/
/* ============================================================================================== */
/* ================ 1/24 This is a demonstration how GuiDataExchange works ===================== */
#include "Interface/GuiDataExchange.h"
#include "Interface/InterChange.h"
#include "Misc/MirrorData.h"
#include "Misc/FormatFuncs.h"
#include <iostream>
#include <algorithm>
#include <string>
#include <array>
using std::cout;
using std::endl;
using std::string;
#define CHECK(COND) \
if (not (COND)) {\
cout << "FAIL: Line "<<__LINE__<<": " #COND <<endl; \
std::terminate();\
}
/** some »strange« test data we want to transport into the GUI */
class Heffalump
: public std::array<char,20>
{
public:
Heffalump()
{
auto h = "Heffalump.."+func::asHexString(rand());
std::copy (h.begin(),h.end(), this->begin());
back() = '\0';
}
};
void run_GuiDataExchangeTest()
{
srand(time(0));
cout << "\n■□■□■□■□■□■□■□■□◆•Gui-Data-Exchange-Test•◆□■□■□■□■□■□■□■□■\n"<<endl;
// =============================================== verify Heffalump (test data)
Heffalump h1,h2;
cout << "Hello " << h1.data() << endl;
CHECK (sizeof(Heffalump) == 20);
// all Heffalumps are unique (and can be compared)
CHECK (h1 != h2);
// Heffalumps can be copied and assigned
h2 = h1;
CHECK (h1 == h2);
h2 = Heffalump();
CHECK (h1 != h2);
// =============================================== setup (fake) communication infrastructure (for this test)
// use a dummy-ringbuffer for this test...
static constexpr size_t commandBlockSize = sizeof (CommandBlock);
RingBuffer <10, log2 (commandBlockSize)> simulatedGUI;
auto sendData = [&simulatedGUI](CommandBlock const& block)
{
simulatedGUI.write(block.bytes);
};
auto pullData = [&simulatedGUI]() -> CommandBlock
{
CommandBlock getData;
simulatedGUI.read(getData.bytes);
return getData;
};
// *Gui-Data-Exchange* : central facility to manage exchange connections
GuiDataExchange guiDataExchange(sendData);
// =============================================== setup a connection-identity
auto con = guiDataExchange.createConnection<Heffalump>();
// has unique identity
CHECK (con != guiDataExchange.createConnection<Heffalump>());
CHECK (con != guiDataExchange.createConnection<float>());
// can be copied and assigned
GuiDataExchange::Connection<Heffalump> c2(con);
CHECK (con == c2);
c2 = guiDataExchange.createConnection<Heffalump>();
CHECK (con != c2);
// can not be assigned with the wrong data buffer type
// c2 = guiDataExchange.createConnection<float>(); //////// does not compile due to different template argument DAT=float
// =============================================== setup a receiver
MirrorData<Heffalump> receiver(con);
// holds default-constructed data
Heffalump& receivedData = receiver.get();
CHECK (receivedData != h1);
CHECK (receivedData != h2);
// =============================================== Core publishes data
con.publish(h1);
// not transported to the GUI yet
CHECK (receivedData != h1);
// =============================================== GUI loop pulls and dispatches updates
guiDataExchange.dispatchUpdates(pullData());
// buffer contents were push-updated
CHECK (receivedData == h1);
// =============================================== dynamic registration of multiple receivers
{ //nested scope
MirrorData<Heffalump> receiver2(con);
CHECK (h1 != receiver2.get());
CHECK (h1 == receiver.get());
con.publish(h2);
CHECK (h2 != receiver2.get());
CHECK (h2 != receiver.get());
CHECK (h1 == receiver.get());
guiDataExchange.dispatchUpdates(pullData());
CHECK (h2 == receiver2.get());
CHECK (h2 == receiver.get());
con.publish(h1);
CHECK (h2 == receiver2.get());
CHECK (h2 == receiver.get());
}//(End)nested scope
// receiver2 does not exist anymore...
guiDataExchange.dispatchUpdates(pullData());
CHECK (h1 == receiver.get());
// =============================================== bootstrap a new receiver from a published data block
size_t slotIDX = con.emplace(h2);
// the following happens »elsewhere« (e.g. in the GUI)
GuiDataExchange::Connection<Heffalump> c3 = guiDataExchange.bootstrapConnection<Heffalump>(slotIDX);
MirrorData<Heffalump> receiver3{c3};
CHECK (h1 != receiver3.get());
CHECK (h2 != receiver3.get());
CHECK (h1 == receiver.get());
// cause a push directly from given index
guiDataExchange.pushUpdates(slotIDX);
CHECK (h2 == receiver.get());
CHECK (h2 == receiver3.get());
// the new connection is fully usable for publishing
c3.publish(h1);
guiDataExchange.dispatchUpdates(pullData());
CHECK (h1 == receiver.get());
CHECK (h1 == receiver3.get());
// =============================================== can install a hook to be activated on each push
string proofMark{};
receiver3.onUpdate([&](Heffalump const& h)
{
proofMark = string{h.data()};
});
// on next push-update...
con.publish(h2);
guiDataExchange.dispatchUpdates(pullData());
CHECK (proofMark == h2.data());
cout << "Bye Bye "<<receiver.get().data() <<endl;
}
|