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.

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