JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_OSCBundle.cpp
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - 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 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-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
26namespace juce
27{
28
32
34{
35}
36
37// Note: The class invariant of OSCBundle::Element is that
38// at least one of the pointers bundle and message is nullptr
39// and the other one always points to a valid object.
40
42 : message (new OSCMessage (m)), bundle (nullptr)
43{
44}
45
47 : message (nullptr), bundle (new OSCBundle (b))
48{
49}
50
51//==============================================================================
53{
54 if (this != &other)
55 {
56 message = nullptr;
57 bundle = nullptr;
58
59 if (other.isMessage())
60 message.reset (new OSCMessage (other.getMessage()));
61 else
62 bundle.reset (new OSCBundle (other.getBundle()));
63 }
64}
65
66//==============================================================================
68{
69 bundle = nullptr;
70 message = nullptr;
71}
72
73//==============================================================================
75{
76 return message != nullptr;
77}
78
80{
81 return bundle != nullptr;
82}
83
84//==============================================================================
86{
87 if (message == nullptr)
88 {
89 // This element is not a bundle! You must check this first before accessing.
91 throw OSCInternalError ("Access error in OSC bundle element.");
92 }
93
94 return *message;
95}
96
97//==============================================================================
99{
100 if (bundle == nullptr)
101 {
102 // This element is not a bundle! You must check this first before accessing.
104 throw OSCInternalError ("Access error in OSC bundle element.");
105 }
106
107 return *bundle;
108}
109
110
111//==============================================================================
112//==============================================================================
113#if JUCE_UNIT_TESTS
114
115class OSCBundleTests final : public UnitTest
116{
117public:
118 OSCBundleTests()
119 : UnitTest ("OSCBundle class", UnitTestCategories::osc)
120 {}
121
122 void runTest() override
123 {
124 beginTest ("Construction");
125 {
126 OSCBundle bundle;
127 expect (bundle.getTimeTag().isImmediately());
128 }
129
130 beginTest ("Construction with time tag");
131 {
132 Time in100Seconds = (Time (Time::currentTimeMillis()) + RelativeTime (100.0));
133 OSCBundle bundle (in100Seconds);
134 expect (! bundle.getTimeTag().isImmediately());
135 expect (bundle.getTimeTag().toTime() == in100Seconds);
136 }
137
138 beginTest ("Usage when containing messages");
139 {
140 OSCBundle testBundle = generateTestBundle();
141 expectBundleEqualsTestBundle (testBundle);
142
143 }
144
145 beginTest ("Usage when containing other bundles (recursively)");
146 {
147 OSCBundle complexTestBundle;
148 complexTestBundle.addElement (generateTestBundle());
149 complexTestBundle.addElement (OSCMessage ("/test/"));
150 complexTestBundle.addElement (generateTestBundle());
151
152 expect (complexTestBundle.size() == 3);
153
154 OSCBundle::Element* elements = complexTestBundle.begin();
155
156 expect (! elements[0].isMessage());
157 expect (elements[0].isBundle());
158 expect (elements[1].isMessage());
159 expect (! elements[1].isBundle());
160 expect (! elements[2].isMessage());
161 expect (elements[2].isBundle());
162
163 expectBundleEqualsTestBundle (elements[0].getBundle());
164 expect (elements[1].getMessage().size() == 0);
165 expect (elements[1].getMessage().getAddressPattern().toString() == "/test");
166 expectBundleEqualsTestBundle (elements[2].getBundle());
167 }
168 }
169
170private:
171
172 int testInt = 127;
173 float testFloat = 1.5;
174
175 OSCBundle generateTestBundle()
176 {
177 OSCBundle bundle;
178
179 OSCMessage msg1 ("/test/fader");
180 msg1.addInt32 (testInt);
181
182 OSCMessage msg2 ("/test/foo");
183 msg2.addString ("bar");
184 msg2.addFloat32 (testFloat);
185
186 bundle.addElement (msg1);
187 bundle.addElement (msg2);
188
189 return bundle;
190 }
191
192 void expectBundleEqualsTestBundle (const OSCBundle& bundle)
193 {
194 expect (bundle.size() == 2);
195 expect (bundle[0].isMessage());
196 expect (! bundle[0].isBundle());
197 expect (bundle[1].isMessage());
198 expect (! bundle[1].isBundle());
199
200 int numElementsCounted = 0;
201 for (auto& element : bundle)
202 {
203 expect (element.isMessage());
204 expect (! element.isBundle());
205 ++numElementsCounted;
206 }
207 expectEquals (numElementsCounted, 2);
208
209 auto* e = bundle.begin();
210 expect (e[0].getMessage().size() == 1);
211 expect (e[0].getMessage().begin()->getInt32() == testInt);
212 expect (e[1].getMessage().size() == 2);
213 expectEquals (e[1].getMessage()[1].getFloat32(), testFloat);
214 }
215};
216
218
219//==============================================================================
220class OSCBundleElementTests final : public UnitTest
221{
222public:
223 OSCBundleElementTests()
224 : UnitTest ("OSCBundle::Element class", UnitTestCategories::osc)
225 {}
226
227 void runTest() override
228 {
229 beginTest ("Construction from OSCMessage");
230 {
231 float testFloat = -0.125;
232 OSCMessage msg ("/test");
233 msg.addFloat32 (testFloat);
234
235 OSCBundle::Element element (msg);
236
237 expect (element.isMessage());
238 expect (element.getMessage().size() == 1);
239 expect (element.getMessage()[0].getType() == OSCTypes::float32);
240 expectEquals (element.getMessage()[0].getFloat32(), testFloat);
241 }
242 }
243};
244
246
247#endif
248
249} // namespace juce
T begin(T... args)
An OSC bundle element.
bool isBundle() const noexcept
Returns true if the OSCBundle element is an OSCBundle.
bool isMessage() const noexcept
Returns true if the OSCBundle element is an OSCMessage.
Element(OSCMessage message)
Constructs an OSCBundle Element from an OSCMessage.
const OSCMessage & getMessage() const
Returns a reference to the contained OSCMessage.
const OSCBundle & getBundle() const
Returns a reference to the contained OSCBundle.
An OSC bundle.
OSCTimeTag getTimeTag() const noexcept
Returns the OSCBundle's OSC time tag.
OSCBundle()
Constructs an OSCBundle with no content and a default time tag ("immediately").
An OSC Message.
An OSC time tag.
bool isImmediately() const noexcept
Returns true if the OSCTimeTag object has the special value representing "immediately".
This is a base class for classes that perform a unit test.
#define jassertfalse
This will always cause an assertion failure.
JUCE Namespace.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
T size(T... args)
std::u16string toString(NumberT value)
convert an number to an UTF-16 string
Exception type thrown in cases of unexpected errors in the OSC module.