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.

221 lines
7.3KB

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