The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

228 lines
7.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. OSCMessage::OSCMessage (const OSCAddressPattern& ap) noexcept : addressPattern (ap)
  21. {
  22. }
  23. //==============================================================================
  24. void OSCMessage::setAddressPattern (const OSCAddressPattern& ap) noexcept
  25. {
  26. addressPattern = ap;
  27. }
  28. OSCAddressPattern OSCMessage::getAddressPattern() const noexcept
  29. {
  30. return addressPattern;
  31. }
  32. //==============================================================================
  33. int OSCMessage::size() const noexcept
  34. {
  35. return arguments.size();
  36. }
  37. bool OSCMessage::isEmpty() const noexcept
  38. {
  39. return arguments.isEmpty();
  40. }
  41. OSCArgument& OSCMessage::operator[] (const int i) noexcept
  42. {
  43. return arguments.getReference (i);
  44. }
  45. const OSCArgument& OSCMessage::operator[] (const int i) const noexcept
  46. {
  47. return arguments.getReference (i);
  48. }
  49. OSCArgument* OSCMessage::begin() noexcept
  50. {
  51. return arguments.begin();
  52. }
  53. const OSCArgument* OSCMessage::begin() const noexcept
  54. {
  55. return arguments.begin();
  56. }
  57. OSCArgument* OSCMessage::end() noexcept
  58. {
  59. return arguments.end();
  60. }
  61. const OSCArgument* OSCMessage::end() const noexcept
  62. {
  63. return arguments.end();
  64. }
  65. void OSCMessage::clear()
  66. {
  67. arguments.clear();
  68. }
  69. //==============================================================================
  70. void OSCMessage::addInt32 (int32 value) { arguments.add (OSCArgument (value)); }
  71. void OSCMessage::addFloat32 (float value) { arguments.add (OSCArgument (value)); }
  72. void OSCMessage::addString (const String& value) { arguments.add (OSCArgument (value)); }
  73. void OSCMessage::addBlob (MemoryBlock blob) { arguments.add (OSCArgument (std::move (blob))); }
  74. void OSCMessage::addColour (OSCColour colour) { arguments.add (OSCArgument (colour)); }
  75. void OSCMessage::addArgument (OSCArgument arg) { arguments.add (arg); }
  76. //==============================================================================
  77. //==============================================================================
  78. #if JUCE_UNIT_TESTS
  79. class OSCMessageTests : public UnitTest
  80. {
  81. public:
  82. OSCMessageTests()
  83. : UnitTest ("OSCMessage class", UnitTestCategories::osc)
  84. {}
  85. void runTest()
  86. {
  87. beginTest ("Basic usage");
  88. {
  89. OSCMessage msg ("/test/param0");
  90. expectEquals (msg.size(), 0);
  91. expect (msg.getAddressPattern().toString() == "/test/param0");
  92. const int numTestArgs = 5;
  93. const int testInt = 42;
  94. const float testFloat = 3.14159f;
  95. const String testString = "Hello, World!";
  96. const OSCColour testColour = { 10, 20, 150, 200 };
  97. const uint8 testBlobData[5] = { 0xBB, 0xCC, 0xDD, 0xEE, 0xFF };
  98. const MemoryBlock testBlob (testBlobData, sizeof (testBlobData));
  99. msg.addInt32 (testInt);
  100. msg.addFloat32 (testFloat);
  101. msg.addString (testString);
  102. msg.addBlob (testBlob);
  103. msg.addColour (testColour);
  104. expectEquals (msg.size(), numTestArgs);
  105. expectEquals (msg[0].getType(), OSCTypes::int32);
  106. expectEquals (msg[1].getType(), OSCTypes::float32);
  107. expectEquals (msg[2].getType(), OSCTypes::string);
  108. expectEquals (msg[3].getType(), OSCTypes::blob);
  109. expectEquals (msg[4].getType(), OSCTypes::colour);
  110. expect (msg[0].isInt32());
  111. expect (msg[1].isFloat32());
  112. expect (msg[2].isString());
  113. expect (msg[3].isBlob());
  114. expect (msg[4].isColour());
  115. expectEquals (msg[0].getInt32(), testInt);
  116. expectEquals (msg[1].getFloat32(), testFloat);
  117. expectEquals (msg[2].getString(), testString);
  118. expect (msg[3].getBlob() == testBlob);
  119. expect (msg[4].getColour().toInt32() == testColour.toInt32());
  120. expect (msg.begin() + numTestArgs == msg.end());
  121. auto arg = msg.begin();
  122. expect (arg->isInt32());
  123. expectEquals (arg->getInt32(), testInt);
  124. ++arg;
  125. expect (arg->isFloat32());
  126. expectEquals (arg->getFloat32(), testFloat);
  127. ++arg;
  128. expect (arg->isString());
  129. expectEquals (arg->getString(), testString);
  130. ++arg;
  131. expect (arg->isBlob());
  132. expect (arg->getBlob() == testBlob);
  133. ++arg;
  134. expect (arg->isColour());
  135. expect (arg->getColour().toInt32() == testColour.toInt32());
  136. ++arg;
  137. expect (arg == msg.end());
  138. }
  139. beginTest ("Initialisation with argument list (C++11 only)");
  140. {
  141. int testInt = 42;
  142. float testFloat = 5.5;
  143. String testString = "Hello, World!";
  144. {
  145. OSCMessage msg ("/test", testInt);
  146. expect (msg.getAddressPattern().toString() == String ("/test"));
  147. expectEquals (msg.size(), 1);
  148. expect (msg[0].isInt32());
  149. expectEquals (msg[0].getInt32(), testInt);
  150. }
  151. {
  152. OSCMessage msg ("/test", testFloat);
  153. expect (msg.getAddressPattern().toString() == String ("/test"));
  154. expectEquals (msg.size(), 1);
  155. expect (msg[0].isFloat32());
  156. expectEquals (msg[0].getFloat32(), testFloat);
  157. }
  158. {
  159. OSCMessage msg ("/test", testString);
  160. expect (msg.getAddressPattern().toString() == String ("/test"));
  161. expectEquals (msg.size(), 1);
  162. expect (msg[0].isString());
  163. expectEquals (msg[0].getString(), testString);
  164. }
  165. {
  166. OSCMessage msg ("/test", testInt, testFloat, testString, testFloat, testInt);
  167. expect (msg.getAddressPattern().toString() == String ("/test"));
  168. expectEquals (msg.size(), 5);
  169. expect (msg[0].isInt32());
  170. expect (msg[1].isFloat32());
  171. expect (msg[2].isString());
  172. expect (msg[3].isFloat32());
  173. expect (msg[4].isInt32());
  174. expectEquals (msg[0].getInt32(), testInt);
  175. expectEquals (msg[1].getFloat32(), testFloat);
  176. expectEquals (msg[2].getString(), testString);
  177. expectEquals (msg[3].getFloat32(), testFloat);
  178. expectEquals (msg[4].getInt32(), testInt);
  179. }
  180. }
  181. }
  182. };
  183. static OSCMessageTests OSCMessageUnitTests;
  184. #endif
  185. } // namespace juce