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_OSCMessage.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
29OSCMessage::OSCMessage (const OSCAddressPattern& ap) noexcept : addressPattern (ap)
30{
31}
32
33//==============================================================================
35{
36 addressPattern = ap;
37}
38
40{
41 return addressPattern;
42}
43
44//==============================================================================
45int OSCMessage::size() const noexcept
46{
47 return arguments.size();
48}
49
51{
52 return arguments.isEmpty();
53}
54
56{
57 return arguments.getReference (i);
58}
59
60const OSCArgument& OSCMessage::operator[] (const int i) const noexcept
61{
62 return arguments.getReference (i);
63}
64
66{
67 return arguments.begin();
68}
69
71{
72 return arguments.begin();
73}
74
76{
77 return arguments.end();
78}
79
81{
82 return arguments.end();
83}
84
86{
87 arguments.clear();
88}
89
90//==============================================================================
91void OSCMessage::addInt32 (int32 value) { arguments.add (OSCArgument (value)); }
92void OSCMessage::addFloat32 (float value) { arguments.add (OSCArgument (value)); }
93void OSCMessage::addString (const String& value) { arguments.add (OSCArgument (value)); }
94void OSCMessage::addBlob (MemoryBlock blob) { arguments.add (OSCArgument (std::move (blob))); }
95void OSCMessage::addColour (OSCColour colour) { arguments.add (OSCArgument (colour)); }
96void OSCMessage::addArgument (OSCArgument arg) { arguments.add (arg); }
97
98
99//==============================================================================
100//==============================================================================
101#if JUCE_UNIT_TESTS
102
103class OSCMessageTests final : public UnitTest
104{
105public:
107 : UnitTest ("OSCMessage class", UnitTestCategories::osc)
108 {}
109
110 void runTest() override
111 {
112 beginTest ("Basic usage");
113 {
114 OSCMessage msg ("/test/param0");
115 expectEquals (msg.size(), 0);
116 expect (msg.getAddressPattern().toString() == "/test/param0");
117
118 const int numTestArgs = 5;
119
120 const int testInt = 42;
121 const float testFloat = 3.14159f;
122 const String testString = "Hello, World!";
123 const OSCColour testColour = { 10, 20, 150, 200 };
124
125 const uint8 testBlobData[5] = { 0xBB, 0xCC, 0xDD, 0xEE, 0xFF };
126 const MemoryBlock testBlob (testBlobData, sizeof (testBlobData));
127
128 msg.addInt32 (testInt);
129 msg.addFloat32 (testFloat);
130 msg.addString (testString);
131 msg.addBlob (testBlob);
132 msg.addColour (testColour);
133
134 expectEquals (msg.size(), numTestArgs);
135
136 expectEquals (msg[0].getType(), OSCTypes::int32);
137 expectEquals (msg[1].getType(), OSCTypes::float32);
138 expectEquals (msg[2].getType(), OSCTypes::string);
139 expectEquals (msg[3].getType(), OSCTypes::blob);
140 expectEquals (msg[4].getType(), OSCTypes::colour);
141
142 expect (msg[0].isInt32());
143 expect (msg[1].isFloat32());
144 expect (msg[2].isString());
145 expect (msg[3].isBlob());
146 expect (msg[4].isColour());
147
148 expectEquals (msg[0].getInt32(), testInt);
149 expectEquals (msg[1].getFloat32(), testFloat);
150 expectEquals (msg[2].getString(), testString);
151 expect (msg[3].getBlob() == testBlob);
152 expect (msg[4].getColour().toInt32() == testColour.toInt32());
153
154 expect (msg.begin() + numTestArgs == msg.end());
155
156 auto arg = msg.begin();
157 expect (arg->isInt32());
158 expectEquals (arg->getInt32(), testInt);
159 ++arg;
160 expect (arg->isFloat32());
161 expectEquals (arg->getFloat32(), testFloat);
162 ++arg;
163 expect (arg->isString());
164 expectEquals (arg->getString(), testString);
165 ++arg;
166 expect (arg->isBlob());
167 expect (arg->getBlob() == testBlob);
168 ++arg;
169 expect (arg->isColour());
170 expect (arg->getColour().toInt32() == testColour.toInt32());
171 ++arg;
172 expect (arg == msg.end());
173 }
174
175
176 beginTest ("Initialisation with argument list (C++11 only)");
177 {
178 int testInt = 42;
179 float testFloat = 5.5;
180 String testString = "Hello, World!";
181
182 {
183 OSCMessage msg ("/test", testInt);
184 expect (msg.getAddressPattern().toString() == String ("/test"));
185 expectEquals (msg.size(), 1);
186 expect (msg[0].isInt32());
187 expectEquals (msg[0].getInt32(), testInt);
188 }
189 {
190 OSCMessage msg ("/test", testFloat);
191 expect (msg.getAddressPattern().toString() == String ("/test"));
192 expectEquals (msg.size(), 1);
193 expect (msg[0].isFloat32());
194 expectEquals (msg[0].getFloat32(), testFloat);
195 }
196 {
197 OSCMessage msg ("/test", testString);
198 expect (msg.getAddressPattern().toString() == String ("/test"));
199 expectEquals (msg.size(), 1);
200 expect (msg[0].isString());
201 expectEquals (msg[0].getString(), testString);
202 }
203 {
204 OSCMessage msg ("/test", testInt, testFloat, testString, testFloat, testInt);
205 expect (msg.getAddressPattern().toString() == String ("/test"));
206 expectEquals (msg.size(), 5);
207 expect (msg[0].isInt32());
208 expect (msg[1].isFloat32());
209 expect (msg[2].isString());
210 expect (msg[3].isFloat32());
211 expect (msg[4].isInt32());
212
213 expectEquals (msg[0].getInt32(), testInt);
214 expectEquals (msg[1].getFloat32(), testFloat);
215 expectEquals (msg[2].getString(), testString);
216 expectEquals (msg[3].getFloat32(), testFloat);
217 expectEquals (msg[4].getInt32(), testInt);
218 }
219 }
220 }
221};
222
224
225#endif
226
227} // namespace juce
A class to hold a resizable block of raw data.
An OSC address pattern.
An OSC argument.
void addInt32(int32 value)
Creates a new OSCArgument of type int32 with the given value, and adds it to the OSCMessage object.
OSCArgument & operator[](int i) noexcept
Returns a reference to the OSCArgument at index i in the OSCMessage object.
void addString(const String &value)
Creates a new OSCArgument of type string with the given value, and adds it to the OSCMessage object.
void addFloat32(float value)
Creates a new OSCArgument of type float32 with the given value, and adds it to the OSCMessage object.
void setAddressPattern(const OSCAddressPattern &ap) noexcept
Sets the address pattern of the OSCMessage.
OSCMessage(const OSCAddressPattern &ap) noexcept
Constructs an OSCMessage object with the given address pattern and no arguments.
OSCAddressPattern getAddressPattern() const noexcept
Returns the address pattern of the OSCMessage.
OSCArgument * end() noexcept
Returns a pointer to the last OSCArgument in the OSCMessage object.
void clear()
Removes all arguments from the OSCMessage.
bool isEmpty() const noexcept
Returns true if the OSCMessage contains no OSCArgument objects; false otherwise.
void addBlob(MemoryBlock blob)
Creates a new OSCArgument of type blob with binary data content copied from the given MemoryBlock.
OSCArgument * begin() noexcept
Returns a pointer to the first OSCArgument in the OSCMessage object.
void addArgument(OSCArgument argument)
Adds the OSCArgument argument to the OSCMessage object.
int size() const noexcept
Returns the number of OSCArgument objects that belong to this OSCMessage.
void addColour(OSCColour colour)
Creates a new OSCArgument of type colour with the given value, and adds it to the OSCMessage object.
The JUCE String class!
Definition juce_String.h:53
This is a base class for classes that perform a unit test.
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 char uint8
A platform-independent 8-bit unsigned integer type.
Holds a 32-bit RGBA colour for passing to and from an OSCArgument.