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.

243 lines
6.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 7 technical preview.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For the technical preview this file cannot be licensed commercially.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. OSCBundle::OSCBundle()
  16. {
  17. }
  18. OSCBundle::OSCBundle (OSCTimeTag t) : timeTag (t)
  19. {
  20. }
  21. // Note: The class invariant of OSCBundle::Element is that
  22. // at least one of the pointers bundle and message is nullptr
  23. // and the other one always points to a valid object.
  24. OSCBundle::Element::Element (OSCMessage m)
  25. : message (new OSCMessage (m)), bundle (nullptr)
  26. {
  27. }
  28. OSCBundle::Element::Element (OSCBundle b)
  29. : message (nullptr), bundle (new OSCBundle (b))
  30. {
  31. }
  32. //==============================================================================
  33. OSCBundle::Element::Element (const Element& other)
  34. {
  35. if (this != &other)
  36. {
  37. message = nullptr;
  38. bundle = nullptr;
  39. if (other.isMessage())
  40. message.reset (new OSCMessage (other.getMessage()));
  41. else
  42. bundle.reset (new OSCBundle (other.getBundle()));
  43. }
  44. }
  45. //==============================================================================
  46. OSCBundle::Element::~Element()
  47. {
  48. bundle = nullptr;
  49. message = nullptr;
  50. }
  51. //==============================================================================
  52. bool OSCBundle::Element::isMessage() const noexcept
  53. {
  54. return message != nullptr;
  55. }
  56. bool OSCBundle::Element::isBundle() const noexcept
  57. {
  58. return bundle != nullptr;
  59. }
  60. //==============================================================================
  61. const OSCMessage& OSCBundle::Element::getMessage() const
  62. {
  63. if (message == nullptr)
  64. {
  65. // This element is not a bundle! You must check this first before accessing.
  66. jassertfalse;
  67. throw OSCInternalError ("Access error in OSC bundle element.");
  68. }
  69. return *message;
  70. }
  71. //==============================================================================
  72. const OSCBundle& OSCBundle::Element::getBundle() const
  73. {
  74. if (bundle == nullptr)
  75. {
  76. // This element is not a bundle! You must check this first before accessing.
  77. jassertfalse;
  78. throw OSCInternalError ("Access error in OSC bundle element.");
  79. }
  80. return *bundle;
  81. }
  82. //==============================================================================
  83. //==============================================================================
  84. #if JUCE_UNIT_TESTS
  85. class OSCBundleTests : public UnitTest
  86. {
  87. public:
  88. OSCBundleTests()
  89. : UnitTest ("OSCBundle class", UnitTestCategories::osc)
  90. {}
  91. void runTest()
  92. {
  93. beginTest ("Construction");
  94. {
  95. OSCBundle bundle;
  96. expect (bundle.getTimeTag().isImmediately());
  97. }
  98. beginTest ("Construction with time tag");
  99. {
  100. Time in100Seconds = (Time (Time::currentTimeMillis()) + RelativeTime (100.0));
  101. OSCBundle bundle (in100Seconds);
  102. expect (! bundle.getTimeTag().isImmediately());
  103. expect (bundle.getTimeTag().toTime() == in100Seconds);
  104. }
  105. beginTest ("Usage when containing messages");
  106. {
  107. OSCBundle testBundle = generateTestBundle();
  108. expectBundleEqualsTestBundle (testBundle);
  109. }
  110. beginTest ("Usage when containing other bundles (recursively)");
  111. {
  112. OSCBundle complexTestBundle;
  113. complexTestBundle.addElement (generateTestBundle());
  114. complexTestBundle.addElement (OSCMessage ("/test/"));
  115. complexTestBundle.addElement (generateTestBundle());
  116. expect (complexTestBundle.size() == 3);
  117. OSCBundle::Element* elements = complexTestBundle.begin();
  118. expect (! elements[0].isMessage());
  119. expect (elements[0].isBundle());
  120. expect (elements[1].isMessage());
  121. expect (! elements[1].isBundle());
  122. expect (! elements[2].isMessage());
  123. expect (elements[2].isBundle());
  124. expectBundleEqualsTestBundle (elements[0].getBundle());
  125. expect (elements[1].getMessage().size() == 0);
  126. expect (elements[1].getMessage().getAddressPattern().toString() == "/test");
  127. expectBundleEqualsTestBundle (elements[2].getBundle());
  128. }
  129. }
  130. private:
  131. int testInt = 127;
  132. float testFloat = 1.5;
  133. OSCBundle generateTestBundle()
  134. {
  135. OSCBundle bundle;
  136. OSCMessage msg1 ("/test/fader");
  137. msg1.addInt32 (testInt);
  138. OSCMessage msg2 ("/test/foo");
  139. msg2.addString ("bar");
  140. msg2.addFloat32 (testFloat);
  141. bundle.addElement (msg1);
  142. bundle.addElement (msg2);
  143. return bundle;
  144. }
  145. void expectBundleEqualsTestBundle (const OSCBundle& bundle)
  146. {
  147. expect (bundle.size() == 2);
  148. expect (bundle[0].isMessage());
  149. expect (! bundle[0].isBundle());
  150. expect (bundle[1].isMessage());
  151. expect (! bundle[1].isBundle());
  152. int numElementsCounted = 0;
  153. for (auto& element : bundle)
  154. {
  155. expect (element.isMessage());
  156. expect (! element.isBundle());
  157. ++numElementsCounted;
  158. }
  159. expectEquals (numElementsCounted, 2);
  160. auto* e = bundle.begin();
  161. expect (e[0].getMessage().size() == 1);
  162. expect (e[0].getMessage().begin()->getInt32() == testInt);
  163. expect (e[1].getMessage().size() == 2);
  164. expect (e[1].getMessage()[1].getFloat32() == testFloat);
  165. }
  166. };
  167. static OSCBundleTests OSCBundleUnitTests;
  168. //==============================================================================
  169. class OSCBundleElementTests : public UnitTest
  170. {
  171. public:
  172. OSCBundleElementTests()
  173. : UnitTest ("OSCBundle::Element class", UnitTestCategories::osc)
  174. {}
  175. void runTest()
  176. {
  177. beginTest ("Construction from OSCMessage");
  178. {
  179. float testFloat = -0.125;
  180. OSCMessage msg ("/test");
  181. msg.addFloat32 (testFloat);
  182. OSCBundle::Element element (msg);
  183. expect (element.isMessage());
  184. expect (element.getMessage().size() == 1);
  185. expect (element.getMessage()[0].getType() == OSCTypes::float32);
  186. expect (element.getMessage()[0].getFloat32() == testFloat);
  187. }
  188. }
  189. };
  190. static OSCBundleElementTests OSCBundleElementUnitTests;
  191. #endif
  192. } // namespace juce