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.

198 lines
6.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. //==============================================================================
  18. OSCMessage::OSCMessage (const OSCAddressPattern& ap) noexcept : addressPattern (ap)
  19. {
  20. }
  21. //==============================================================================
  22. void OSCMessage::setAddressPattern (const OSCAddressPattern& ap) noexcept
  23. {
  24. addressPattern = ap;
  25. }
  26. OSCAddressPattern OSCMessage::getAddressPattern() const noexcept
  27. {
  28. return addressPattern;
  29. }
  30. //==============================================================================
  31. int OSCMessage::size() const noexcept
  32. {
  33. return arguments.size();
  34. }
  35. bool OSCMessage::empty() const noexcept
  36. {
  37. return arguments.empty();
  38. }
  39. OSCArgument& OSCMessage::operator[] (const int i) const noexcept
  40. {
  41. return arguments.getReference (i);
  42. }
  43. OSCArgument* OSCMessage::begin() const noexcept
  44. {
  45. return arguments.begin();
  46. }
  47. OSCArgument* OSCMessage::end() const noexcept
  48. {
  49. return arguments.end();
  50. }
  51. void OSCMessage::clear()
  52. {
  53. arguments.clear();
  54. }
  55. //==============================================================================
  56. void OSCMessage::addInt32 (int32 value) { arguments.add (OSCArgument (value)); }
  57. void OSCMessage::addFloat32 (float value) { arguments.add (OSCArgument (value)); }
  58. void OSCMessage::addString (const String& value) { arguments.add (OSCArgument (value)); }
  59. void OSCMessage::addBlob (const MemoryBlock& blob) { arguments.add (OSCArgument (blob)); }
  60. void OSCMessage::addArgument (OSCArgument arg) { arguments.add (arg); }
  61. //==============================================================================
  62. //==============================================================================
  63. #if JUCE_UNIT_TESTS
  64. class OSCMessageTests : public UnitTest
  65. {
  66. public:
  67. OSCMessageTests() : UnitTest ("OSCMessage class") {}
  68. void runTest()
  69. {
  70. beginTest ("Basic usage");
  71. {
  72. OSCMessage msg ("/test/param0");
  73. expectEquals (msg.size(), 0);
  74. expect (msg.getAddressPattern().toString() == "/test/param0");
  75. const int numTestArgs = 4;
  76. const int testInt = 42;
  77. const float testFloat = 3.14159f;
  78. const String testString = "Hello, World!";
  79. const uint8 testBlobData[5] = { 0xBB, 0xCC, 0xDD, 0xEE, 0xFF };
  80. const MemoryBlock testBlob (testBlobData, sizeof (testBlobData));
  81. msg.addInt32 (testInt);
  82. msg.addFloat32 (testFloat);
  83. msg.addString (testString);
  84. msg.addBlob (testBlob);
  85. expectEquals (msg.size(), numTestArgs);
  86. expectEquals (msg[0].getType(), OSCTypes::int32);
  87. expectEquals (msg[1].getType(), OSCTypes::float32);
  88. expectEquals (msg[2].getType(), OSCTypes::string);
  89. expectEquals (msg[3].getType(), OSCTypes::blob);
  90. expect (msg[0].isInt32());
  91. expect (msg[1].isFloat32());
  92. expect (msg[2].isString());
  93. expect (msg[3].isBlob());
  94. expectEquals (msg[0].getInt32(), testInt);
  95. expectEquals (msg[1].getFloat32(), testFloat);
  96. expectEquals (msg[2].getString(), testString);
  97. expect (msg[3].getBlob() == testBlob);
  98. expect (msg.begin() + numTestArgs == msg.end());
  99. OSCArgument* arg = msg.begin();
  100. expect (arg->isInt32());
  101. expectEquals (arg->getInt32(), testInt);
  102. ++arg;
  103. expect (arg->isFloat32());
  104. expectEquals (arg->getFloat32(), testFloat);
  105. ++arg;
  106. expect (arg->isString());
  107. expectEquals (arg->getString(), testString);
  108. ++arg;
  109. expect (arg->isBlob());
  110. expect(arg->getBlob() == testBlob);
  111. ++arg;
  112. expect (arg == msg.end());
  113. }
  114. #if JUCE_COMPILER_SUPPORTS_VARIADIC_TEMPLATES && JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  115. beginTest ("Initialisation with argument list (C++11 only)");
  116. {
  117. int testInt = 42;
  118. float testFloat = 5.5;
  119. String testString = "Hello, World!";
  120. {
  121. OSCMessage msg ("/test", testInt);
  122. expect (msg.getAddressPattern().toString() == String ("/test"));
  123. expectEquals (msg.size(), 1);
  124. expect (msg[0].isInt32());
  125. expectEquals (msg[0].getInt32(), testInt);
  126. }
  127. {
  128. OSCMessage msg ("/test", testFloat);
  129. expect (msg.getAddressPattern().toString() == String ("/test"));
  130. expectEquals (msg.size(), 1);
  131. expect (msg[0].isFloat32());
  132. expectEquals (msg[0].getFloat32(), testFloat);
  133. }
  134. {
  135. OSCMessage msg ("/test", testString);
  136. expect (msg.getAddressPattern().toString() == String ("/test"));
  137. expectEquals (msg.size(), 1);
  138. expect (msg[0].isString());
  139. expectEquals (msg[0].getString(), testString);
  140. }
  141. {
  142. OSCMessage msg ("/test", testInt, testFloat, testString, testFloat, testInt);
  143. expect (msg.getAddressPattern().toString() == String ("/test"));
  144. expectEquals (msg.size(), 5);
  145. expect (msg[0].isInt32());
  146. expect (msg[1].isFloat32());
  147. expect (msg[2].isString());
  148. expect (msg[3].isFloat32());
  149. expect (msg[4].isInt32());
  150. expectEquals (msg[0].getInt32(), testInt);
  151. expectEquals (msg[1].getFloat32(), testFloat);
  152. expectEquals (msg[2].getString(), testString);
  153. expectEquals (msg[3].getFloat32(), testFloat);
  154. expectEquals (msg[4].getInt32(), testInt);
  155. }
  156. }
  157. #endif
  158. }
  159. };
  160. static OSCMessageTests OSCMessageUnitTests;
  161. #endif // JUCE_UNIT_TESTS