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.

229 lines
6.1KB

  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. OSCArgument::OSCArgument (int32 value) noexcept
  20. : type (OSCTypes::int32), intValue (value)
  21. {
  22. }
  23. OSCArgument::OSCArgument (float value) noexcept
  24. : type (OSCTypes::float32), floatValue (value)
  25. {
  26. }
  27. OSCArgument::OSCArgument (const String& value) noexcept
  28. : type (OSCTypes::string), stringValue (value)
  29. {
  30. }
  31. OSCArgument::OSCArgument (const MemoryBlock& b)
  32. : type (OSCTypes::blob), blob (b)
  33. {
  34. }
  35. //==============================================================================
  36. String OSCArgument::getString() const noexcept
  37. {
  38. if (isString())
  39. return stringValue;
  40. jassertfalse; // you must check the type of an argument before attempting to get its value!
  41. return {};
  42. }
  43. int32 OSCArgument::getInt32() const noexcept
  44. {
  45. if (isInt32())
  46. return intValue;
  47. jassertfalse; // you must check the type of an argument before attempting to get its value!
  48. return 0;
  49. }
  50. float OSCArgument::getFloat32() const noexcept
  51. {
  52. if (isFloat32())
  53. return floatValue;
  54. jassertfalse; // you must check the type of an argument before attempting to get its value!
  55. return 0.0f;
  56. }
  57. const MemoryBlock& OSCArgument::getBlob() const noexcept
  58. {
  59. // you must check the type of an argument before attempting to get its value!
  60. jassert (isBlob());
  61. return blob;
  62. }
  63. //==============================================================================
  64. //==============================================================================
  65. #if JUCE_UNIT_TESTS
  66. class OSCArgumentTests : public UnitTest
  67. {
  68. public:
  69. OSCArgumentTests() : UnitTest ("OSCArgument class") {}
  70. MemoryBlock getMemoryBlockWithRandomData (size_t numBytes)
  71. {
  72. MemoryBlock block (numBytes);
  73. Random rng = getRandom();
  74. for (size_t i = 0; i < numBytes; ++i)
  75. block[i] = (char) rng.nextInt (256);
  76. return block;
  77. }
  78. void runTest()
  79. {
  80. runTestInitialisation();
  81. }
  82. void runTestInitialisation()
  83. {
  84. beginTest ("Int32");
  85. {
  86. int value = 123456789;
  87. OSCArgument arg (value);
  88. expect (arg.getType() == OSCTypes::int32);
  89. expect (arg.isInt32());
  90. expect (! arg.isFloat32());
  91. expect (! arg.isString());
  92. expect (! arg.isBlob());
  93. expect (arg.getInt32() == value);
  94. }
  95. beginTest ("Float32");
  96. {
  97. float value = 12345.6789f;
  98. OSCArgument arg (value);
  99. expect (arg.getType() == OSCTypes::float32);
  100. expect (! arg.isInt32());
  101. expect (arg.isFloat32());
  102. expect (! arg.isString());
  103. expect (! arg.isBlob());
  104. expect (arg.getFloat32() == value);
  105. }
  106. beginTest ("String");
  107. {
  108. String value = "Hello, World!";
  109. OSCArgument arg (value);
  110. expect (arg.getType() == OSCTypes::string);
  111. expect (! arg.isInt32());
  112. expect (! arg.isFloat32());
  113. expect (arg.isString());
  114. expect (! arg.isBlob());
  115. expect (arg.getString() == value);
  116. }
  117. beginTest ("String (from C string)");
  118. {
  119. OSCArgument arg ("Hello, World!");
  120. expect (arg.getType() == OSCTypes::string);
  121. expect (! arg.isInt32());
  122. expect (! arg.isFloat32());
  123. expect (arg.isString());
  124. expect (! arg.isBlob());
  125. expect (arg.getString() == String ("Hello, World!"));
  126. }
  127. beginTest ("Blob");
  128. {
  129. const size_t numBytes = 412;
  130. MemoryBlock blob = getMemoryBlockWithRandomData (numBytes);
  131. OSCArgument arg (blob);
  132. expect (arg.getType() == OSCTypes::blob);
  133. expect (! arg.isInt32());
  134. expect (! arg.isFloat32());
  135. expect (! arg.isString());
  136. expect (arg.isBlob());
  137. expect (arg.getBlob() == blob);
  138. }
  139. beginTest ("Copy, move and assignment");
  140. {
  141. {
  142. int value = -42;
  143. OSCArgument arg (value);
  144. OSCArgument copy = arg;
  145. expect (copy.getType() == OSCTypes::int32);
  146. expect (copy.getInt32() == value);
  147. OSCArgument assignment ("this will be overwritten!");
  148. assignment = copy;
  149. expect (assignment.getType() == OSCTypes::int32);
  150. expect (assignment.getInt32() == value);
  151. }
  152. {
  153. const size_t numBytes = 412;
  154. MemoryBlock blob = getMemoryBlockWithRandomData (numBytes);
  155. OSCArgument arg (blob);
  156. OSCArgument copy = arg;
  157. expect (copy.getType() == OSCTypes::blob);
  158. expect (copy.getBlob() == blob);
  159. OSCArgument assignment ("this will be overwritten!");
  160. assignment = copy;
  161. expect (assignment.getType() == OSCTypes::blob);
  162. expect (assignment.getBlob() == blob);
  163. }
  164. }
  165. }
  166. };
  167. static OSCArgumentTests OSCArgumentUnitTests;
  168. #endif // JUCE_UNIT_TESTS