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.

209 lines
6.9KB

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