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.

229 lines
7.5KB

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