Audio plugin host https://kx.studio/carla
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.

134 lines
3.7KB

  1. /* Copyright 2016, Ableton AG, Berlin. All rights reserved.
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. * If you would like to incorporate Link into a proprietary software application,
  17. * please contact <link-devs@ableton.com>.
  18. */
  19. #pragma once
  20. #include <ableton/discovery/NetworkByteStreamSerializable.hpp>
  21. #include <cstdint>
  22. #include <tuple>
  23. #include <utility>
  24. namespace ableton
  25. {
  26. namespace discovery
  27. {
  28. namespace test
  29. {
  30. // Test payload entries
  31. // A fixed-size entry type
  32. struct Foo
  33. {
  34. static const std::int32_t key = '_foo';
  35. static_assert(key == 0x5f666f6f, "Unexpected byte order");
  36. std::int32_t fooVal;
  37. friend std::uint32_t sizeInByteStream(const Foo& foo)
  38. {
  39. // Namespace qualification is needed to avoid ambiguous function definitions
  40. return discovery::sizeInByteStream(foo.fooVal);
  41. }
  42. template <typename It>
  43. friend It toNetworkByteStream(const Foo& foo, It out)
  44. {
  45. return discovery::toNetworkByteStream(foo.fooVal, std::move(out));
  46. }
  47. template <typename It>
  48. static std::pair<Foo, It> fromNetworkByteStream(It begin, It end)
  49. {
  50. auto result = Deserialize<decltype(fooVal)>::fromNetworkByteStream(
  51. std::move(begin), std::move(end));
  52. return std::make_pair(Foo{std::move(result.first)}, std::move(result.second));
  53. }
  54. };
  55. // A variable-size entry type
  56. struct Bar
  57. {
  58. static const std::int32_t key = '_bar';
  59. static_assert(key == 0x5f626172, "Unexpected byte order");
  60. std::vector<std::uint64_t> barVals;
  61. friend std::uint32_t sizeInByteStream(const Bar& bar)
  62. {
  63. return discovery::sizeInByteStream(bar.barVals);
  64. }
  65. template <typename It>
  66. friend It toNetworkByteStream(const Bar& bar, It out)
  67. {
  68. return discovery::toNetworkByteStream(bar.barVals, out);
  69. }
  70. template <typename It>
  71. static std::pair<Bar, It> fromNetworkByteStream(It begin, It end)
  72. {
  73. auto result = Deserialize<decltype(barVals)>::fromNetworkByteStream(
  74. std::move(begin), std::move(end));
  75. return std::make_pair(Bar{std::move(result.first)}, std::move(result.second));
  76. }
  77. };
  78. // An entry type with two vectors
  79. struct Foobar
  80. {
  81. static const std::int32_t key = 'fbar';
  82. static_assert(key == 0x66626172, "Unexpected byte order");
  83. using FoobarVector = std::vector<std::uint64_t>;
  84. using FoobarTuple = std::tuple<FoobarVector, FoobarVector>;
  85. FoobarVector fooVals;
  86. FoobarVector barVals;
  87. friend std::uint32_t sizeInByteStream(const Foobar& foobar)
  88. {
  89. return discovery::sizeInByteStream(foobar.asTuple());
  90. }
  91. template <typename It>
  92. friend It toNetworkByteStream(const Foobar& foobar, It out)
  93. {
  94. return discovery::toNetworkByteStream(foobar.asTuple(), out);
  95. }
  96. template <typename It>
  97. static std::pair<Foobar, It> fromNetworkByteStream(It begin, It end)
  98. {
  99. const auto result =
  100. Deserialize<FoobarTuple>::fromNetworkByteStream(std::move(begin), std::move(end));
  101. const auto foobar = Foobar{std::get<0>(result.first), std::get<1>(result.first)};
  102. return std::make_pair(std::move(foobar), std::move(result.second));
  103. }
  104. FoobarTuple asTuple() const
  105. {
  106. return std::make_tuple(fooVals, barVals);
  107. }
  108. };
  109. } // namespace test
  110. } // namespace discovery
  111. } // namespace ableton