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.

219 lines
7.4KB

  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() const noexcept
  51. {
  52. return arguments.begin();
  53. }
  54. OSCArgument* OSCMessage::end() const noexcept
  55. {
  56. return arguments.end();
  57. }
  58. void OSCMessage::clear()
  59. {
  60. arguments.clear();
  61. }
  62. //==============================================================================
  63. void OSCMessage::addInt32 (int32 value) { arguments.add (OSCArgument (value)); }
  64. void OSCMessage::addFloat32 (float value) { arguments.add (OSCArgument (value)); }
  65. void OSCMessage::addString (const String& value) { arguments.add (OSCArgument (value)); }
  66. void OSCMessage::addBlob (MemoryBlock blob) { arguments.add (OSCArgument (std::move (blob))); }
  67. void OSCMessage::addColour (OSCColour colour) { arguments.add (OSCArgument (colour)); }
  68. void OSCMessage::addArgument (OSCArgument arg) { arguments.add (arg); }
  69. //==============================================================================
  70. //==============================================================================
  71. #if JUCE_UNIT_TESTS
  72. class OSCMessageTests : public UnitTest
  73. {
  74. public:
  75. OSCMessageTests()
  76. : UnitTest ("OSCMessage class", UnitTestCategories::osc)
  77. {}
  78. void runTest()
  79. {
  80. beginTest ("Basic usage");
  81. {
  82. OSCMessage msg ("/test/param0");
  83. expectEquals (msg.size(), 0);
  84. expect (msg.getAddressPattern().toString() == "/test/param0");
  85. const int numTestArgs = 5;
  86. const int testInt = 42;
  87. const float testFloat = 3.14159f;
  88. const String testString = "Hello, World!";
  89. const OSCColour testColour = { 10, 20, 150, 200 };
  90. const uint8 testBlobData[5] = { 0xBB, 0xCC, 0xDD, 0xEE, 0xFF };
  91. const MemoryBlock testBlob (testBlobData, sizeof (testBlobData));
  92. msg.addInt32 (testInt);
  93. msg.addFloat32 (testFloat);
  94. msg.addString (testString);
  95. msg.addBlob (testBlob);
  96. msg.addColour (testColour);
  97. expectEquals (msg.size(), numTestArgs);
  98. expectEquals (msg[0].getType(), OSCTypes::int32);
  99. expectEquals (msg[1].getType(), OSCTypes::float32);
  100. expectEquals (msg[2].getType(), OSCTypes::string);
  101. expectEquals (msg[3].getType(), OSCTypes::blob);
  102. expectEquals (msg[4].getType(), OSCTypes::colour);
  103. expect (msg[0].isInt32());
  104. expect (msg[1].isFloat32());
  105. expect (msg[2].isString());
  106. expect (msg[3].isBlob());
  107. expect (msg[4].isColour());
  108. expectEquals (msg[0].getInt32(), testInt);
  109. expectEquals (msg[1].getFloat32(), testFloat);
  110. expectEquals (msg[2].getString(), testString);
  111. expect (msg[3].getBlob() == testBlob);
  112. expect (msg[4].getColour().toInt32() == testColour.toInt32());
  113. expect (msg.begin() + numTestArgs == msg.end());
  114. auto arg = msg.begin();
  115. expect (arg->isInt32());
  116. expectEquals (arg->getInt32(), testInt);
  117. ++arg;
  118. expect (arg->isFloat32());
  119. expectEquals (arg->getFloat32(), testFloat);
  120. ++arg;
  121. expect (arg->isString());
  122. expectEquals (arg->getString(), testString);
  123. ++arg;
  124. expect (arg->isBlob());
  125. expect (arg->getBlob() == testBlob);
  126. ++arg;
  127. expect (arg->isColour());
  128. expect (arg->getColour().toInt32() == testColour.toInt32());
  129. ++arg;
  130. expect (arg == msg.end());
  131. }
  132. beginTest ("Initialisation with argument list (C++11 only)");
  133. {
  134. int testInt = 42;
  135. float testFloat = 5.5;
  136. String testString = "Hello, World!";
  137. {
  138. OSCMessage msg ("/test", testInt);
  139. expect (msg.getAddressPattern().toString() == String ("/test"));
  140. expectEquals (msg.size(), 1);
  141. expect (msg[0].isInt32());
  142. expectEquals (msg[0].getInt32(), testInt);
  143. }
  144. {
  145. OSCMessage msg ("/test", testFloat);
  146. expect (msg.getAddressPattern().toString() == String ("/test"));
  147. expectEquals (msg.size(), 1);
  148. expect (msg[0].isFloat32());
  149. expectEquals (msg[0].getFloat32(), testFloat);
  150. }
  151. {
  152. OSCMessage msg ("/test", testString);
  153. expect (msg.getAddressPattern().toString() == String ("/test"));
  154. expectEquals (msg.size(), 1);
  155. expect (msg[0].isString());
  156. expectEquals (msg[0].getString(), testString);
  157. }
  158. {
  159. OSCMessage msg ("/test", testInt, testFloat, testString, testFloat, testInt);
  160. expect (msg.getAddressPattern().toString() == String ("/test"));
  161. expectEquals (msg.size(), 5);
  162. expect (msg[0].isInt32());
  163. expect (msg[1].isFloat32());
  164. expect (msg[2].isString());
  165. expect (msg[3].isFloat32());
  166. expect (msg[4].isInt32());
  167. expectEquals (msg[0].getInt32(), testInt);
  168. expectEquals (msg[1].getFloat32(), testFloat);
  169. expectEquals (msg[2].getString(), testString);
  170. expectEquals (msg[3].getFloat32(), testFloat);
  171. expectEquals (msg[4].getInt32(), testInt);
  172. }
  173. }
  174. }
  175. };
  176. static OSCMessageTests OSCMessageUnitTests;
  177. #endif
  178. } // namespace juce