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.

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