/usr/include/ableton/discovery/Payload.hpp is in ableton-link-dev 1.0.0+dfsg-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | /* Copyright 2016, Ableton AG, Berlin. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* If you would like to incorporate Link into a proprietary software application,
* please contact <link-devs@ableton.com>.
*/
#pragma once
#include <ableton/discovery/NetworkByteStreamSerializable.hpp>
#include <functional>
#include <unordered_map>
namespace ableton
{
namespace discovery
{
struct PayloadEntryHeader
{
using Key = std::uint32_t;
using Size = std::uint32_t;
Key key;
Size size;
friend Size sizeInByteStream(const PayloadEntryHeader& header)
{
return sizeInByteStream(header.key) + sizeInByteStream(header.size);
}
template <typename It>
friend It toNetworkByteStream(const PayloadEntryHeader& header, It out)
{
return toNetworkByteStream(
header.size, toNetworkByteStream(header.key, std::move(out)));
}
template <typename It>
static std::pair<PayloadEntryHeader, It> fromNetworkByteStream(It begin, const It end)
{
using namespace std;
Key key;
Size size;
tie(key, begin) = Deserialize<Key>::fromNetworkByteStream(begin, end);
tie(size, begin) = Deserialize<Size>::fromNetworkByteStream(begin, end);
return make_pair(PayloadEntryHeader{move(key), move(size)}, move(begin));
}
};
template <typename EntryType>
struct PayloadEntry
{
PayloadEntry(EntryType entryVal)
: value(std::move(entryVal))
{
header = {EntryType::key, sizeInByteStream(value)};
}
PayloadEntryHeader header;
EntryType value;
friend std::uint32_t sizeInByteStream(const PayloadEntry& entry)
{
return sizeInByteStream(entry.header) + sizeInByteStream(entry.value);
}
template <typename It>
friend It toNetworkByteStream(const PayloadEntry& entry, It out)
{
return toNetworkByteStream(
entry.value, toNetworkByteStream(entry.header, std::move(out)));
}
};
namespace detail
{
template <typename It>
using HandlerMap =
std::unordered_map<typename PayloadEntryHeader::Key, std::function<void(It, It)>>;
// Given an index of handlers and a byte range, parse the bytes as a
// sequence of payload entries and invoke the appropriate handler for
// each entry type. Entries that are encountered that do not have a
// corresponding handler in the map are ignored. Throws
// std::runtime_error if parsing fails for any entry. Note that if an
// exception is thrown, some of the handlers may have already been called.
template <typename It>
void parseByteStream(HandlerMap<It>& map, It bsBegin, const It bsEnd)
{
using namespace std;
while (bsBegin < bsEnd)
{
// Try to parse an entry header at this location in the byte stream
PayloadEntryHeader header;
It valueBegin;
tie(header, valueBegin) =
Deserialize<PayloadEntryHeader>::fromNetworkByteStream(bsBegin, bsEnd);
// Ensure that the reported size of the entry does not exceed the
// length of the byte stream
It valueEnd = valueBegin + header.size;
if (bsEnd < valueEnd)
{
throw range_error("Partial payload entry with key: " + to_string(header.key));
}
// The next entry will start at the end of this one
bsBegin = valueEnd;
// Use the appropriate handler for this entry, if available
auto handlerIt = map.find(header.key);
if (handlerIt != end(map))
{
handlerIt->second(move(valueBegin), move(valueEnd));
}
}
}
} // namespace detail
// Payload encoding
template <typename... Entries>
struct Payload;
template <typename First, typename Rest>
struct Payload<First, Rest>
{
Payload(First first, Rest rest)
: mFirst(std::move(first))
, mRest(std::move(rest))
{
}
Payload(PayloadEntry<First> first, Rest rest)
: mFirst(std::move(first))
, mRest(std::move(rest))
{
}
template <typename RhsFirst, typename RhsRest>
using PayloadSum =
Payload<First, typename Rest::template PayloadSum<RhsFirst, RhsRest>>;
// Concatenate payloads together into a single payload
template <typename RhsFirst, typename RhsRest>
friend PayloadSum<RhsFirst, RhsRest> operator+(
Payload lhs, Payload<RhsFirst, RhsRest> rhs)
{
return {std::move(lhs.mFirst), std::move(lhs.mRest) + std::move(rhs)};
}
friend std::size_t sizeInByteStream(const Payload& payload)
{
return sizeInByteStream(payload.mFirst) + sizeInByteStream(payload.mRest);
}
template <typename It>
friend It toNetworkByteStream(const Payload& payload, It streamIt)
{
return toNetworkByteStream(
payload.mRest, toNetworkByteStream(payload.mFirst, std::move(streamIt)));
}
PayloadEntry<First> mFirst;
Rest mRest;
};
template <>
struct Payload<>
{
template <typename RhsFirst, typename RhsRest>
using PayloadSum = Payload<RhsFirst, RhsRest>;
template <typename RhsFirst, typename RhsRest>
friend PayloadSum<RhsFirst, RhsRest> operator+(Payload, Payload<RhsFirst, RhsRest> rhs)
{
return rhs;
}
friend std::size_t sizeInByteStream(const Payload&)
{
return 0;
}
template <typename It>
friend It toNetworkByteStream(const Payload&, It streamIt)
{
return streamIt;
}
};
template <typename... Entries>
struct PayloadBuilder;
// Payload factory function
template <typename... Entries>
auto makePayload(Entries... entries)
-> decltype(PayloadBuilder<Entries...>{}(std::move(entries)...))
{
return PayloadBuilder<Entries...>{}(std::move(entries)...);
}
template <typename First, typename... Rest>
struct PayloadBuilder<First, Rest...>
{
auto operator()(First first, Rest... rest)
-> Payload<First, decltype(makePayload(std::move(rest)...))>
{
return {std::move(first), makePayload(std::move(rest)...)};
}
};
template <>
struct PayloadBuilder<>
{
Payload<> operator()()
{
return {};
}
};
// Parse payloads to values
template <typename... Entries>
struct ParsePayload;
template <typename First, typename... Rest>
struct ParsePayload<First, Rest...>
{
template <typename It, typename... Handlers>
static void parse(It begin, It end, Handlers... handlers)
{
detail::HandlerMap<It> map;
collectHandlers(map, std::move(handlers)...);
detail::parseByteStream(map, std::move(begin), std::move(end));
}
template <typename It, typename FirstHandler, typename... RestHandlers>
static void collectHandlers(
detail::HandlerMap<It>& map, FirstHandler handler, RestHandlers... rest)
{
using namespace std;
map[First::key] = [handler](const It begin, const It end) {
const auto res = First::fromNetworkByteStream(begin, end);
if (res.second != end)
{
std::ostringstream stringStream;
stringStream << "Parsing payload entry " << First::key
<< " did not consume the expected number of bytes. "
<< " Expected: " << distance(begin, end)
<< ", Actual: " << distance(begin, res.second);
throw range_error(stringStream.str());
}
handler(res.first);
};
ParsePayload<Rest...>::collectHandlers(map, std::move(rest)...);
}
};
template <>
struct ParsePayload<>
{
template <typename It>
static void collectHandlers(detail::HandlerMap<It>&)
{
}
};
template <typename... Entries, typename It, typename... Handlers>
void parsePayload(It begin, It end, Handlers... handlers)
{
using namespace std;
ParsePayload<Entries...>::parse(move(begin), move(end), move(handlers)...);
}
} // namespace discovery
} // namespace ableton
|