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.

204 lines
6.8KB

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