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_OSCArgument.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
29OSCArgument::OSCArgument (int32 v) : type (OSCTypes::int32), intValue (v) {}
30OSCArgument::OSCArgument (float v) : type (OSCTypes::float32), floatValue (v) {}
31OSCArgument::OSCArgument (const String& s) : type (OSCTypes::string), stringValue (s) {}
32OSCArgument::OSCArgument (MemoryBlock b) : type (OSCTypes::blob), blob (std::move (b)) {}
33OSCArgument::OSCArgument (OSCColour c) : type (OSCTypes::colour), intValue ((int32) c.toInt32()) {}
34
35//==============================================================================
37{
38 if (isString())
39 return stringValue;
40
41 jassertfalse; // you must check the type of an argument before attempting to get its value!
42 return {};
43}
44
46{
47 if (isInt32())
48 return intValue;
49
50 jassertfalse; // you must check the type of an argument before attempting to get its value!
51 return 0;
52}
53
55{
56 if (isFloat32())
57 return floatValue;
58
59 jassertfalse; // you must check the type of an argument before attempting to get its value!
60 return 0.0f;
61}
62
64{
65 // you must check the type of an argument before attempting to get its value!
66 jassert (isBlob());
67
68 return blob;
69}
70
72{
73 if (isColour())
74 return OSCColour::fromInt32 ((uint32) intValue);
75
76 jassertfalse; // you must check the type of an argument before attempting to get its value!
77 return { 0, 0, 0, 0 };
78}
79
80
81//==============================================================================
82//==============================================================================
83#if JUCE_UNIT_TESTS
84
85class OSCArgumentTests final : public UnitTest
86{
87public:
89 : UnitTest ("OSCArgument class", UnitTestCategories::osc)
90 {}
91
92
93 MemoryBlock getMemoryBlockWithRandomData (size_t numBytes)
94 {
95 MemoryBlock block (numBytes);
96
97 Random rng = getRandom();
98
99 for (size_t i = 0; i < numBytes; ++i)
100 block[i] = (char) rng.nextInt (256);
101
102 return block;
103 }
104
105 void runTest() override
106 {
108 }
109
111 {
112 beginTest ("Int32");
113 {
114 int value = 123456789;
115
116 OSCArgument arg (value);
117
118 expect (arg.getType() == OSCTypes::int32);
119 expect (arg.isInt32());
120 expect (! arg.isFloat32());
121 expect (! arg.isString());
122 expect (! arg.isBlob());
123 expect (! arg.isColour());
124
125 expect (arg.getInt32() == value);
126 }
127
128 beginTest ("Float32");
129 {
130 float value = 12345.6789f;
131
132 OSCArgument arg (value);
133
134 expect (arg.getType() == OSCTypes::float32);
135 expect (! arg.isInt32());
136 expect (arg.isFloat32());
137 expect (! arg.isString());
138 expect (! arg.isBlob());
139 expect (! arg.isColour());
140
141 expectEquals (arg.getFloat32(), value);
142 }
143
144 beginTest ("String");
145 {
146 String value = "Hello, World!";
147 OSCArgument arg (value);
148
149 expect (arg.getType() == OSCTypes::string);
150 expect (! arg.isInt32());
151 expect (! arg.isFloat32());
152 expect (arg.isString());
153 expect (! arg.isBlob());
154 expect (! arg.isColour());
155
156 expect (arg.getString() == value);
157 }
158
159 beginTest ("String (from C string)");
160 {
161 OSCArgument arg ("Hello, World!");
162
163 expect (arg.getType() == OSCTypes::string);
164 expect (! arg.isInt32());
165 expect (! arg.isFloat32());
166 expect (arg.isString());
167 expect (! arg.isBlob());
168 expect (! arg.isColour());
169
170 expect (arg.getString() == "Hello, World!");
171 }
172
173 beginTest ("Blob");
174 {
175 auto blob = getMemoryBlockWithRandomData (413);
176 OSCArgument arg (blob);
177
178 expect (arg.getType() == OSCTypes::blob);
179 expect (! arg.isInt32());
180 expect (! arg.isFloat32());
181 expect (! arg.isString());
182 expect (arg.isBlob());
183 expect (! arg.isColour());
184
185 expect (arg.getBlob() == blob);
186 }
187
188 beginTest ("Colour");
189 {
190 Random rng = getRandom();
191
192 for (int i = 100; --i >= 0;)
193 {
194 OSCColour col = { (uint8) rng.nextInt (256),
195 (uint8) rng.nextInt (256),
196 (uint8) rng.nextInt (256),
197 (uint8) rng.nextInt (256) };
198
199 OSCArgument arg (col);
200
201 expect (arg.getType() == OSCTypes::colour);
202 expect (! arg.isInt32());
203 expect (! arg.isFloat32());
204 expect (! arg.isString());
205 expect (! arg.isBlob());
206 expect (arg.isColour());
207
208 expect (arg.getColour().toInt32() == col.toInt32());
209 }
210 }
211
212 beginTest ("Copy, move and assignment");
213 {
214 {
215 int value = -42;
216 OSCArgument arg (value);
217
218 OSCArgument copy = arg;
219 expect (copy.getType() == OSCTypes::int32);
220 expect (copy.getInt32() == value);
221
222 OSCArgument assignment ("this will be overwritten!");
224 expect (assignment.getType() == OSCTypes::int32);
225 expect (assignment.getInt32() == value);
226 }
227 {
228 const size_t numBytes = 412;
229 MemoryBlock blob = getMemoryBlockWithRandomData (numBytes);
230 OSCArgument arg (blob);
231
232 OSCArgument copy = arg;
233 expect (copy.getType() == OSCTypes::blob);
234 expect (copy.getBlob() == blob);
235
236 OSCArgument assignment ("this will be overwritten!");
238 expect (assignment.getType() == OSCTypes::blob);
239 expect (assignment.getBlob() == blob);
240 }
241 }
242 }
243};
244
246
247#endif
248
249} // namespace juce
A class to hold a resizable block of raw data.
OSCColour getColour() const noexcept
Returns the value of the OSCArgument as an OSCColour.
String getString() const noexcept
Returns the value of the OSCArgument as a string.
bool isFloat32() const noexcept
Returns whether the type of the OSCArgument is float.
bool isBlob() const noexcept
Returns whether the type of the OSCArgument is blob.
bool isColour() const noexcept
Returns whether the type of the OSCArgument is colour.
int32 getInt32() const noexcept
Returns the value of the OSCArgument as an int32.
OSCArgument(int32 value)
Constructs an OSCArgument with type int32 and a given value.
float getFloat32() const noexcept
Returns the value of the OSCArgument as a float32.
const MemoryBlock & getBlob() const noexcept
Returns the binary data contained in the blob and owned by the OSCArgument, as a reference to a JUCE ...
bool isInt32() const noexcept
Returns whether the type of the OSCArgument is int32.
bool isString() const noexcept
Returns whether the type of the OSCArgument is string.
The definitions of supported OSC types and their associated OSC type tags, as defined in the OpenSoun...
The JUCE String class!
Definition juce_String.h:53
This is a base class for classes that perform a unit test.
#define jassert(expression)
Platform-independent assertion macro.
#define jassertfalse
This will always cause an assertion failure.
@ copy
The command ID that should be used to send a "Copy to clipboard" command.
JUCE Namespace.
signed int int32
A platform-independent 32-bit signed integer type.
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
unsigned int uint32
A platform-independent 32-bit unsigned integer type.
unsigned char uint8
A platform-independent 8-bit unsigned integer type.
Holds a 32-bit RGBA colour for passing to and from an OSCArgument.