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.

207 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) 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. beginTest ("Initialisation with argument list (C++11 only)");
  122. {
  123. int testInt = 42;
  124. float testFloat = 5.5;
  125. String testString = "Hello, World!";
  126. {
  127. OSCMessage msg ("/test", testInt);
  128. expect (msg.getAddressPattern().toString() == String ("/test"));
  129. expectEquals (msg.size(), 1);
  130. expect (msg[0].isInt32());
  131. expectEquals (msg[0].getInt32(), testInt);
  132. }
  133. {
  134. OSCMessage msg ("/test", testFloat);
  135. expect (msg.getAddressPattern().toString() == String ("/test"));
  136. expectEquals (msg.size(), 1);
  137. expect (msg[0].isFloat32());
  138. expectEquals (msg[0].getFloat32(), testFloat);
  139. }
  140. {
  141. OSCMessage msg ("/test", testString);
  142. expect (msg.getAddressPattern().toString() == String ("/test"));
  143. expectEquals (msg.size(), 1);
  144. expect (msg[0].isString());
  145. expectEquals (msg[0].getString(), testString);
  146. }
  147. {
  148. OSCMessage msg ("/test", testInt, testFloat, testString, testFloat, testInt);
  149. expect (msg.getAddressPattern().toString() == String ("/test"));
  150. expectEquals (msg.size(), 5);
  151. expect (msg[0].isInt32());
  152. expect (msg[1].isFloat32());
  153. expect (msg[2].isString());
  154. expect (msg[3].isFloat32());
  155. expect (msg[4].isInt32());
  156. expectEquals (msg[0].getInt32(), testInt);
  157. expectEquals (msg[1].getFloat32(), testFloat);
  158. expectEquals (msg[2].getString(), testString);
  159. expectEquals (msg[3].getFloat32(), testFloat);
  160. expectEquals (msg[4].getInt32(), testInt);
  161. }
  162. }
  163. }
  164. };
  165. static OSCMessageTests OSCMessageUnitTests;
  166. #endif // JUCE_UNIT_TESTS
  167. } // namespace juce