diff --git a/CMakeLists.txt b/CMakeLists.txt index 5e1d81ce..7c118c55 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,7 @@ if(${CMAKE_VERSION} VERSION_GREATER_EQUAL 3.24) endif() set(CMAKE_EXPORT_COMPILE_COMMANDS ON) +set(CMAKE_CXX_STANDARD 20) # See: https://github.com/cpm-cmake/CPM.cmake include("cmake/CPM.cmake") diff --git a/build.gradle b/build.gradle index 8522b7d2..ca43aa74 100644 --- a/build.gradle +++ b/build.gradle @@ -19,8 +19,8 @@ apply plugin: 'com.android.application' android { compileSdkVersion 29 - buildToolsVersion "30.0.3" - ndkVersion "25.1.8937393" + buildToolsVersion '33.0.0' + ndkVersion "26.1.10909125" defaultConfig { applicationId "org.meumeu.wivrn" diff --git a/client/decoder/shard_accumulator.cpp b/client/decoder/shard_accumulator.cpp index f9b76d65..9a21af2d 100644 --- a/client/decoder/shard_accumulator.cpp +++ b/client/decoder/shard_accumulator.cpp @@ -22,13 +22,11 @@ #include "rs.h" #include "scenes/stream.h" #include "spdlog/spdlog.h" -#include "utils/typename.h" #include "wivrn_serialization.h" using namespace xrt::drivers::wivrn::to_headset; using shard_set = shard_accumulator::shard_set; using data_shard = shard_accumulator::data_shard; -using parity_shard = shard_accumulator::parity_shard; shard_set::shard_set(uint8_t stream_index) { @@ -40,7 +38,6 @@ void shard_set::reset(uint64_t frame_index) num_shards = 0; min_for_reconstruction = -1; data.clear(); - parity.clear(); uint8_t stream_index = feedback.stream_index; feedback = {}; @@ -79,125 +76,9 @@ uint16_t shard_set::insert(data_shard && shard) if (not data[idx]) ++num_shards; data[idx] = std::move(shard); - if (auto i = try_reconstruct()) - return *i; return idx; } -std::optional shard_set::insert(parity_shard && shard) -{ - XrTime now = application::now(); - if (empty()) - feedback.received_first_packet = now; - feedback.received_last_packet = now; - - auto idx = shard.parity_element; - min_for_reconstruction = shard.data_shard_count; - if (idx >= parity.size()) - parity.resize(idx + 1); - if (not parity[idx]) - ++num_shards; - parity[idx] = std::move(shard); - - return try_reconstruct(); -} - -parity_shard & shard_set::get_parity() -{ - for (auto & shard: parity) - { - if (shard) - return *shard; - } - // this function shall be called only when at least one parity shard exists - assert(false); - - __builtin_unreachable(); -} - -std::optional shard_set::try_reconstruct() -{ - if (parity.empty()) - return {}; - if (num_shards < min_for_reconstruction) - return {}; - - static std::once_flag once; - std::call_once(once, reed_solomon_init); - auto & p = get_parity(); - std::unique_ptr rs( - reed_solomon_new(p.data_shard_count, p.num_parity_elements), reed_solomon_release); - - size_t shard_size = p.payload.size(); - - std::vector rs_shards; - std::vector rs_marks; - - std::vector> data_raw; - data.resize(p.data_shard_count); - data_raw.reserve(p.data_shard_count); - for (auto & shard: data) - { - if (shard) - { - xrt::drivers::wivrn::serialization_packet s; - s.serialize(*shard); - data_raw.emplace_back(std::move(s)).resize(shard_size); - rs_marks.push_back(0); - } - else - { - data_raw.emplace_back(shard_size, 0); - rs_marks.push_back(1); - } - rs_shards.push_back(data_raw.back().data()); - } - // WARNING: do not use p after this line, the array it points to has been modified - parity.resize(p.num_parity_elements); - std::vector> dummy; - dummy.reserve(p.num_parity_elements); - for (auto & shard: parity) - { - if (shard) - { - rs_shards.push_back(shard->payload.data()); - rs_marks.push_back(0); - } - else - { - auto d = dummy.emplace_back(shard_size).data(); - rs_shards.push_back(d); - rs_marks.push_back(1); - } - } - - int err = reed_solomon_reconstruct(rs.get(), rs_shards.data(), rs_marks.data(), data.size() + parity.size(), shard_size); - if (err) - { - spdlog::info("reed_solomon_reconstruct failed"); - return {}; - } - std::optional first_reconstructed; - for (size_t i = 0; i < data.size(); ++i) - { - if (not data[i]) - { - xrt::drivers::wivrn::deserialization_packet d(data_raw[i]); - data[i] = d.deserialize(); - if (not first_reconstructed) - first_reconstructed = i; - } - } - assert(first_reconstructed); - spdlog::info("reed_solomon_reconstruct success frame {}, reconstructed {} (out of {} shards)", - data[0]->frame_idx, - *first_reconstructed, - data.size()); - - feedback.reconstructed = application::now(); - return first_reconstructed; -} - static void debug_why_not_sent(const shard_set & shards) { const auto & frame = shards.data; @@ -207,10 +88,7 @@ static void debug_why_not_sent(const shard_set & shards) return; } int frame_idx = -1; - int expected_data = -1; - int expected_parity = -1; size_t data = 0; - size_t parity = 0; for (const auto & shard: frame) { if (shard) @@ -219,18 +97,8 @@ static void debug_why_not_sent(const shard_set & shards) ++data; } } - for (const auto & shard: shards.parity) - { - if (shard) - { - frame_idx = shard->frame_idx; - expected_data = shard->data_shard_count; - expected_parity = shard->num_parity_elements; - ++parity; - } - } - spdlog::info("frame {} was not sent with {}/{} data, {}/{} parity shards)", frame_idx, data, expected_data, parity, expected_parity); + spdlog::info("frame {} was not sent with {} data shards", frame_idx, data); } void shard_accumulator::advance() @@ -239,8 +107,7 @@ void shard_accumulator::advance() next.reset(current.frame_index() + 1); } -template -void shard_accumulator::push_shard(Shard && shard) +void shard_accumulator::push_shard(video_stream_data_shard&& shard) { assert(current.frame_index() + 1 == next.frame_index()); @@ -251,7 +118,6 @@ void shard_accumulator::push_shard(Shard && shard) } else if (frame_diff == 0) { - // Due to error correction, inserting a shard might create a valid one before auto shard_idx = current.insert(std::move(shard)); try_submit_frame(shard_idx); } @@ -290,9 +156,6 @@ void shard_accumulator::push_shard(Shard && shard) } } -template void shard_accumulator::push_shard(parity_shard && shard); -template void shard_accumulator::push_shard(video_stream_data_shard && shard); - void shard_accumulator::try_submit_frame(std::optional shard_idx) { if (shard_idx) diff --git a/client/decoder/shard_accumulator.h b/client/decoder/shard_accumulator.h index 6400bf82..babb0844 100644 --- a/client/decoder/shard_accumulator.h +++ b/client/decoder/shard_accumulator.h @@ -30,9 +30,6 @@ using decoder_impl = ::ffmpeg::decoder; #endif #include "wivrn_packets.h" -#include -#include -#include #include #include @@ -42,21 +39,15 @@ class shard_accumulator public: using data_shard = xrt::drivers::wivrn::to_headset::video_stream_data_shard; - using parity_shard = xrt::drivers::wivrn::to_headset::video_stream_parity_shard; struct shard_set { size_t num_shards = 0; size_t min_for_reconstruction = -1; std::vector> data; - std::vector> parity; void reset(uint64_t frame_index); bool empty() const; uint16_t insert(data_shard &&); - std::optional insert(parity_shard &&); - - parity_shard & get_parity(); - std::optional try_reconstruct(); xrt::drivers::wivrn::from_headset::feedback feedback{}; @@ -93,8 +84,7 @@ public: next.reset(1); } - template - void push_shard(Shard &&); + void push_shard(xrt::drivers::wivrn::to_headset::video_stream_data_shard&&); auto & desc() const { diff --git a/client/scenes/stream.cpp b/client/scenes/stream.cpp index 808c0d9b..685e2077 100644 --- a/client/scenes/stream.cpp +++ b/client/scenes/stream.cpp @@ -543,17 +543,13 @@ void scenes::stream::video() { auto shard = shard_queue.pop(); - std::visit( - [this](auto & shard) { - if (shard.stream_item_idx >= decoders.size()) - { - // We don't know (yet?) about this stream, ignore packet - return; - } - auto idx = shard.stream_item_idx; - decoders[idx].decoder->push_shard(std::move(shard)); - }, - shard); + if (shard.stream_item_idx >= decoders.size()) + { + // We don't know (yet?) about this stream, ignore packet + return; + } + auto idx = shard.stream_item_idx; + decoders[idx].decoder->push_shard(std::move(shard)); } catch (utils::sync_queue_closed &) { diff --git a/client/scenes/stream.h b/client/scenes/stream.h index b33e46d4..64bf9592 100644 --- a/client/scenes/stream.h +++ b/client/scenes/stream.h @@ -69,7 +69,7 @@ class stream : public scene, public std::enable_shared_from_this std::optional tracking_thread; std::thread video_thread; - utils::sync_queue> shard_queue; + utils::sync_queue shard_queue; std::mutex decoder_mutex; std::vector decoders; // Locked by decoder_mutex @@ -102,7 +102,6 @@ public: void operator()(to_headset::handshake&&) {}; void operator()(to_headset::video_stream_data_shard &&); - void operator()(to_headset::video_stream_parity_shard &&); void operator()(to_headset::haptics &&); void operator()(to_headset::timesync_query &&); void operator()(to_headset::audio_stream_description &&); diff --git a/client/scenes/stream_network.cpp b/client/scenes/stream_network.cpp index 96b96f3e..159162e1 100644 --- a/client/scenes/stream_network.cpp +++ b/client/scenes/stream_network.cpp @@ -43,12 +43,6 @@ void scenes::stream::operator()(to_headset::video_stream_data_shard && shard) shard_queue.push(std::move(shard)); } -void scenes::stream::operator()(to_headset::video_stream_parity_shard && shard) -{ - video_started_ = true; - shard_queue.push(std::move(shard)); -} - void scenes::stream::operator()(to_headset::audio_stream_description && desc) { audio_handle.emplace(desc, *network_session, instance); diff --git a/client/vk/details/enumerate.h b/client/vk/details/enumerate.h index 5e6cccf3..03b4eb49 100644 --- a/client/vk/details/enumerate.h +++ b/client/vk/details/enumerate.h @@ -31,7 +31,7 @@ namespace vk::details template struct structure_traits_base { - static constexpr inline VkStructureType type = static_cast(-1); + static constexpr inline VkStructureType type = VK_STRUCTURE_TYPE_MAX_ENUM; using base = T; }; @@ -53,7 +53,7 @@ auto enumerate(F f, Args &&... args) -> auto if (result >= 0) { - if constexpr (structure_type == static_cast(-1)) + if constexpr (structure_type == VK_STRUCTURE_TYPE_MAX_ENUM) { array.resize(count); } diff --git a/client/wivrn_client.cpp b/client/wivrn_client.cpp index 37396da1..c449836f 100644 --- a/client/wivrn_client.cpp +++ b/client/wivrn_client.cpp @@ -19,10 +19,7 @@ #include "wivrn_client.h" #include "spdlog/common.h" -#include "version.h" #include "wivrn_packets.h" -#include "wivrn_serialization.h" -#include #include #include #include @@ -60,7 +57,7 @@ void wivrn_session::handshake() if (std::holds_alternative(*packet)) { // If this packet is lost, a tracking packet will do the job to establish the connection - send_stream(from_headset::stream_packets(from_headset::handshake{})); + send_stream(from_headset::handshake{}); return; } @@ -137,13 +134,3 @@ wivrn_session::wivrn_session(in_addr address, int port) : // // return {}; // } - -void wivrn_session::send_control(const from_headset::control_packets & packet) -{ - control.send(packet); -} - -void wivrn_session::send_stream(const from_headset::stream_packets & packet) -{ - stream.send(packet); -} diff --git a/client/wivrn_client.h b/client/wivrn_client.h index 429ce1aa..9c1a6fb7 100644 --- a/client/wivrn_client.h +++ b/client/wivrn_client.h @@ -21,7 +21,6 @@ #include "wivrn_packets.h" #include "wivrn_sockets.h" -#include #include using namespace xrt::drivers::wivrn; @@ -41,8 +40,16 @@ public: wivrn_session(const wivrn_session &) = delete; wivrn_session & operator=(const wivrn_session &) = delete; - void send_control(const from_headset::control_packets & packet); - void send_stream(const from_headset::stream_packets & packet); + template + void send_control(T && packet) + { + control.send(std::forward(packet)); + } + template + void send_stream(T && packet) + { + stream.send(std::forward(packet)); + } template int poll(T && visitor, std::chrono::milliseconds timeout) diff --git a/cmake/FreetypeHarfbuzz.cmake b/cmake/FreetypeHarfbuzz.cmake index b27cfbf9..719470dc 100644 --- a/cmake/FreetypeHarfbuzz.cmake +++ b/cmake/FreetypeHarfbuzz.cmake @@ -7,15 +7,15 @@ if (USE_SYSTEM_FREETYPE) target_link_libraries(FreetypeHarfbuzz INTERFACE Freetype::Freetype harfbuzz::harfbuzz) else() - set(HARFBUZZ_VERSION 5.3.1) - set(FREETYPE_VERSION 2.12.1) + set(HARFBUZZ_VERSION 8.3.0) + set(FREETYPE_VERSION 2.13.2) if(NOT EXISTS ${CMAKE_BINARY_DIR}/freetype-${FREETYPE_VERSION}.tar.xz) if (EXISTS ${CMAKE_SOURCE_DIR}/freetype-${FREETYPE_VERSION}.tar.xz) file(CREATE_LINK ${CMAKE_SOURCE_DIR}/freetype-${FREETYPE_VERSION}.tar.xz ${CMAKE_BINARY_DIR}/freetype-${FREETYPE_VERSION}.tar.xz SYMBOLIC) else() file(DOWNLOAD https://download.savannah.gnu.org/releases/freetype/freetype-${FREETYPE_VERSION}.tar.xz ${CMAKE_BINARY_DIR}/freetype-${FREETYPE_VERSION}.tar.xz - EXPECTED_HASH SHA256=4766f20157cc4cf0cd292f80bf917f92d1c439b243ac3018debf6b9140c41a7f) + EXPECTED_HASH SHA256=12991c4e55c506dd7f9b765933e62fd2be2e06d421505d7950a132e4f1bb484d) endif() endif() @@ -24,7 +24,7 @@ else() file(CREATE_LINK ${CMAKE_SOURCE_DIR}/harfbuzz-${HARFBUZZ_VERSION}.tar.xz ${CMAKE_BINARY_DIR}/harfbuzz-${HARFBUZZ_VERSION}.tar.xz SYMBOLIC) else() file(DOWNLOAD https://github.com/harfbuzz/harfbuzz/releases/download/${HARFBUZZ_VERSION}/harfbuzz-${HARFBUZZ_VERSION}.tar.xz ${CMAKE_BINARY_DIR}/harfbuzz-${HARFBUZZ_VERSION}.tar.xz - EXPECTED_HASH SHA256=4a6ce097b75a8121facc4ba83b5b083bfec657f45b003cd5a3424f2ae6b4434d) + EXPECTED_HASH SHA256=109501eaeb8bde3eadb25fab4164e993fbace29c3d775bcaa1c1e58e2f15f847) endif() endif() diff --git a/common/wivrn_packets.h b/common/wivrn_packets.h index 443b06b4..d492f754 100644 --- a/common/wivrn_packets.h +++ b/common/wivrn_packets.h @@ -29,12 +29,11 @@ #include #include +#include "wivrn_serialization_types.h" + namespace xrt::drivers::wivrn { -template -struct serialization_traits; - // Default port for server to listen, both TCP and UDP static const int default_port = 9757; @@ -180,9 +179,7 @@ struct feedback std::array real_pose; uint8_t data_packets; - uint8_t parity_packets; uint8_t received_data_packets; - uint8_t received_parity_packets; }; using control_packets = std::variant; @@ -270,22 +267,11 @@ public: std::array fov; }; std::optional view_info; - - // Container for the data, read payload instead - std::vector data; // Actual video data, may contain multiple NAL units std::span payload; -}; -struct video_stream_parity_shard -{ - uint8_t stream_item_idx; - uint64_t frame_idx; - uint16_t data_shard_count; - uint8_t num_parity_elements; - uint8_t parity_element; - // Parity data - std::vector payload; + // Container for the data, read payload instead + data_holder data; }; struct haptics @@ -302,49 +288,8 @@ struct timesync_query }; using control_packets = std::variant; -using stream_packets = std::variant; +using stream_packets = std::variant; } // namespace to_headset -template <> -struct serialization_traits -{ - template - static constexpr void type_hash(T & h) - { - h.feed("video_v0"); - } - - template - static void serialize(const to_headset::video_stream_data_shard & shard, T & packet) - { - packet.reserve(to_headset::video_stream_data_shard::max_payload_size + 20); - packet.serialize(shard.stream_item_idx); - packet.serialize(shard.frame_idx); - packet.serialize(shard.shard_idx); - packet.serialize(shard.flags); - packet.serialize(shard.view_info); - uint16_t size = shard.payload.size(); - packet.serialize(size); - packet.write(shard.payload.data(), shard.payload.size_bytes()); - } - - template - static to_headset::video_stream_data_shard deserialize(T & packet) - { - to_headset::video_stream_data_shard shard; - packet.deserialize(shard.stream_item_idx); - packet.deserialize(shard.frame_idx); - packet.deserialize(shard.shard_idx); - packet.deserialize(shard.flags); - packet.deserialize(shard.view_info); - uint16_t size; - packet.deserialize(size); - size_t read_index; - std::tie(read_index, shard.data) = packet.steal_buffer(); - shard.payload = std::span(shard.data.data() + read_index, shard.data.size() - read_index); - return shard; - } -}; - } // namespace xrt::drivers::wivrn diff --git a/common/wivrn_serialization.h b/common/wivrn_serialization.h index cab1ff15..236fffef 100644 --- a/common/wivrn_serialization.h +++ b/common/wivrn_serialization.h @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -35,6 +36,8 @@ #include #include +#include "wivrn_serialization_types.h" + namespace xrt::drivers::wivrn { @@ -99,12 +102,9 @@ class serialization_packet std::vector buffer; public: - serialization_packet() = default; + std::span span; // Will be sent at the end, untouched - void reserve(size_t n) - { - buffer.reserve(n); - } + serialization_packet() = default; void write(const void * data, size_t size) { @@ -113,24 +113,17 @@ public: memcpy(&buffer[index], data, size); } - const uint8_t * data() const - { - return buffer.data(); - } - size_t size() const - { - return buffer.size(); - } - template void serialize(const T & value) { serialization_traits::serialize(value, *this); } - operator std::vector &&() && + operator std::vector> () { - return std::move(buffer); + if (span.empty()) + return {buffer}; + return {buffer, span}; } }; @@ -154,6 +147,14 @@ public: read_index += size; } + std::span read_span(size_t size) + { + check_remaining_size(size); + std::span res(&buffer[read_index], size); + read_index += size; + return res; + } + size_t remaining() const { return buffer.size() - read_index; @@ -455,7 +456,7 @@ struct serialization_traits> static void serialize(const std::variant & value, serialization_packet & packet) { - packet.serialize(value.index()); + packet.serialize(value.index()); std::visit([&](const auto & x) { packet.serialize(x); }, value); } @@ -472,7 +473,7 @@ struct serialization_traits> static std::variant deserialize(deserialization_packet & packet) { - uint32_t type_index = packet.deserialize(); + uint8_t type_index = packet.deserialize(); if (type_index >= sizeof...(T)) throw deserialization_error(); @@ -506,6 +507,48 @@ struct serialization_traits> } }; +template <> +struct serialization_traits> +{ + static constexpr void type_hash(details::hash_context & h) + { + h.feed("span"); + } + + static void serialize(const std::span& value, serialization_packet & packet) + { + packet.serialize(value.size()); + //FIXME: nocopy + packet.write(value.data(), value.size_bytes()); + } + + static std::span deserialize(deserialization_packet & packet) + { + size_t size = packet.deserialize(); + return packet.read_span(size); + } +}; + +template <> +struct serialization_traits +{ + static constexpr void type_hash(details::hash_context & h) + { + } + + static void serialize(const data_holder&, serialization_packet &) + { + } + + static data_holder deserialize(deserialization_packet & packet) + { + data_holder value; + size_t size; + std::tie(size, value.c) = packet.steal_buffer(); + return value; + } +}; + namespace unit_tests { template diff --git a/common/wivrn_serialization_types.h b/common/wivrn_serialization_types.h new file mode 100644 index 00000000..5a6f9a4d --- /dev/null +++ b/common/wivrn_serialization_types.h @@ -0,0 +1,40 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2023 Guillaume Meunier + * Copyright (C) 2023 Patrick Nicolas + * + * 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 3 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 . + */ +#pragma once + +#include +#include + +namespace xrt::drivers::wivrn +{ + +// Intended to be the last element of a serializable type +// contains the data referenced by spans +struct data_holder +{ + data_holder() = default; + data_holder(const data_holder&) = delete; + data_holder(data_holder&&) = default; + data_holder& operator=(const data_holder&) = delete; + data_holder& operator=(data_holder&&) = default; + + std::vector c; +}; + +} diff --git a/common/wivrn_sockets.cpp b/common/wivrn_sockets.cpp index 55aa0ead..7823825c 100644 --- a/common/wivrn_sockets.cpp +++ b/common/wivrn_sockets.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include #include @@ -282,6 +283,16 @@ void xrt::drivers::wivrn::UDP::send_raw(const std::vector & data) throw std::system_error{errno, std::generic_category()}; } +void xrt::drivers::wivrn::UDP::send_raw(const std::vector> & data) +{ + std::vector spans; + for (const auto& span: data) + spans.emplace_back((void*)span.data(), span.size()); + + if (::writev(fd, spans.data(), spans.size()) < 0) + throw std::system_error{errno, std::generic_category()}; +} + xrt::drivers::wivrn::deserialization_packet xrt::drivers::wivrn::TCP::receive_raw() { size_t expected_size; @@ -323,10 +334,12 @@ xrt::drivers::wivrn::deserialization_packet xrt::drivers::wivrn::TCP::receive_ra return deserialization_packet{std::move(new_buffer), 4}; } -void xrt::drivers::wivrn::TCP::send_raw(const std::vector & data) +void xrt::drivers::wivrn::TCP::send_raw(const std::vector> & spans) { std::lock_guard lock(*mutex); + assert(spans.size() == 1); + const auto& data = spans[0]; uint32_t size = data.size(); ssize_t sent = ::send(fd, &size, sizeof(size), MSG_NOSIGNAL); diff --git a/common/wivrn_sockets.h b/common/wivrn_sockets.h index 27ed3daa..5ce8f06d 100644 --- a/common/wivrn_sockets.h +++ b/common/wivrn_sockets.h @@ -26,6 +26,7 @@ #include #include #include +#include #include #include @@ -90,6 +91,7 @@ public: deserialization_packet receive_raw(); std::pair receive_from_raw(); void send_raw(const std::vector & data); + void send_raw(const std::vector> & data); void connect(in6_addr address, int port); void connect(in_addr address, int port); @@ -114,7 +116,7 @@ public: explicit TCP(int fd); deserialization_packet receive_raw(); - void send_raw(const std::vector & data); + void send_raw(const std::vector> & data); }; class TCPListener : public fd_base @@ -137,9 +139,27 @@ public: return {T{fd2}, addr}; } }; - template -class typed_socket : public Socket +class typed_socket; + +namespace details +{ +template +struct Index; + +template +struct Index> { + static const std::size_t value = 0; +}; + +template +struct Index> { + static const std::size_t value = 1 + Index>::value; +}; +} + +template +class typed_socket> : public Socket { public: template @@ -156,12 +176,14 @@ public: return packet.deserialize(); } - template >> - void send(const T & data) + template + void send(T && data) { serialization_packet p; - p.serialize(data); - this->send_raw(std::move(p)); + uint8_t index = details::Index, std::tuple>::value; + p.serialize(index); + p.serialize(std::forward(data)); + this->send_raw(p); } }; diff --git a/external/magic_enum.hpp b/external/magic_enum.hpp index 59e58666..a4ea49ae 100644 --- a/external/magic_enum.hpp +++ b/external/magic_enum.hpp @@ -5,11 +5,11 @@ // | | | | (_| | (_| | | (__ | |____| | | | |_| | | | | | | | |____|_| |_| // |_| |_|\__,_|\__, |_|\___| |______|_| |_|\__,_|_| |_| |_| \_____| // __/ | https://github.com/Neargye/magic_enum -// |___/ version 0.8.1 +// |___/ version 0.9.5 // // Licensed under the MIT License . // SPDX-License-Identifier: MIT -// Copyright (c) 2019 - 2022 Daniil Goncharov . +// Copyright (c) 2019 - 2023 Daniil Goncharov . // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -33,48 +33,57 @@ #define NEARGYE_MAGIC_ENUM_HPP #define MAGIC_ENUM_VERSION_MAJOR 0 -#define MAGIC_ENUM_VERSION_MINOR 8 -#define MAGIC_ENUM_VERSION_PATCH 1 +#define MAGIC_ENUM_VERSION_MINOR 9 +#define MAGIC_ENUM_VERSION_PATCH 5 #include -#include -#include #include -#include +#include +#include #include #include #include -#include #if defined(MAGIC_ENUM_CONFIG_FILE) -#include MAGIC_ENUM_CONFIG_FILE +# include MAGIC_ENUM_CONFIG_FILE #endif #if !defined(MAGIC_ENUM_USING_ALIAS_OPTIONAL) -#include +# include #endif #if !defined(MAGIC_ENUM_USING_ALIAS_STRING) -#include +# include #endif #if !defined(MAGIC_ENUM_USING_ALIAS_STRING_VIEW) -#include +# include +#endif + +#if defined(MAGIC_ENUM_NO_ASSERT) +# define MAGIC_ENUM_ASSERT(...) static_cast(0) +#else +# include +# define MAGIC_ENUM_ASSERT(...) assert((__VA_ARGS__)) #endif #if defined(__clang__) # pragma clang diagnostic push +# pragma clang diagnostic ignored "-Wunknown-warning-option" +# pragma clang diagnostic ignored "-Wenum-constexpr-conversion" +# pragma clang diagnostic ignored "-Wuseless-cast" // suppresses 'static_cast('\0')' for char_type = char (common on Linux). #elif defined(__GNUC__) # pragma GCC diagnostic push # pragma GCC diagnostic ignored "-Wmaybe-uninitialized" // May be used uninitialized 'return {};'. +# pragma GCC diagnostic ignored "-Wuseless-cast" // suppresses 'static_cast('\0')' for char_type = char (common on Linux). #elif defined(_MSC_VER) # pragma warning(push) -# pragma warning(disable : 26495) // Variable 'static_string::chars_' is uninitialized. +# pragma warning(disable : 26495) // Variable 'static_str::chars_' is uninitialized. # pragma warning(disable : 28020) // Arithmetic overflow: Using operator '-' on a 4 byte value and then casting the result to a 8 byte value. # pragma warning(disable : 26451) // The expression '0<=_Param_(1)&&_Param_(1)<=1-1' is not true at this call. # pragma warning(disable : 4514) // Unreferenced inline function has been removed. #endif // Checks magic_enum compiler compatibility. -#if defined(__clang__) && __clang_major__ >= 5 || defined(__GNUC__) && __GNUC__ >= 9 || defined(_MSC_VER) && _MSC_VER >= 1910 +#if defined(__clang__) && __clang_major__ >= 5 || defined(__GNUC__) && __GNUC__ >= 9 || defined(_MSC_VER) && _MSC_VER >= 1910 || defined(__RESHARPER__) # undef MAGIC_ENUM_SUPPORTED # define MAGIC_ENUM_SUPPORTED 1 #endif @@ -94,7 +103,20 @@ // Enum value must be less or equals than MAGIC_ENUM_RANGE_MAX. By default MAGIC_ENUM_RANGE_MAX = 128. // If need another max range for all enum types by default, redefine the macro MAGIC_ENUM_RANGE_MAX. #if !defined(MAGIC_ENUM_RANGE_MAX) -# define MAGIC_ENUM_RANGE_MAX 128 +# define MAGIC_ENUM_RANGE_MAX 127 +#endif + +// Improve ReSharper C++ intellisense performance with builtins, avoiding unnecessary template instantiations. +#if defined(__RESHARPER__) +# undef MAGIC_ENUM_GET_ENUM_NAME_BUILTIN +# undef MAGIC_ENUM_GET_TYPE_NAME_BUILTIN +# if __RESHARPER__ >= 20230100 +# define MAGIC_ENUM_GET_ENUM_NAME_BUILTIN(V) __rscpp_enumerator_name(V) +# define MAGIC_ENUM_GET_TYPE_NAME_BUILTIN(T) __rscpp_type_name() +# else +# define MAGIC_ENUM_GET_ENUM_NAME_BUILTIN(V) nullptr +# define MAGIC_ENUM_GET_TYPE_NAME_BUILTIN(T) nullptr +# endif #endif namespace magic_enum { @@ -120,6 +142,23 @@ MAGIC_ENUM_USING_ALIAS_STRING using std::string; #endif +using char_type = string_view::value_type; +static_assert(std::is_same_v, "magic_enum::customize requires same string_view::value_type and string::value_type"); +static_assert([] { + if constexpr (std::is_same_v) { + constexpr const char c[] = "abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789|"; + constexpr const wchar_t wc[] = L"abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789|"; + static_assert(std::size(c) == std::size(wc), "magic_enum::customize identifier characters are multichars in wchar_t."); + + for (std::size_t i = 0; i < std::size(c); ++i) { + if (c[i] != wc[i]) { + return false; + } + } + } + return true; +} (), "magic_enum::customize wchar_t is not compatible with ASCII."); + namespace customize { // Enum value must be in range [MAGIC_ENUM_RANGE_MIN, MAGIC_ENUM_RANGE_MAX]. By default MAGIC_ENUM_RANGE_MIN = -128, MAGIC_ENUM_RANGE_MAX = 128. @@ -137,16 +176,28 @@ static_assert(MAGIC_ENUM_RANGE_MAX > MAGIC_ENUM_RANGE_MIN, "MAGIC_ENUM_RANGE_MAX static_assert((MAGIC_ENUM_RANGE_MAX - MAGIC_ENUM_RANGE_MIN) < (std::numeric_limits::max)(), "MAGIC_ENUM_RANGE must be less than UINT16_MAX."); namespace detail { -enum class default_customize_tag {}; -enum class invalid_customize_tag {}; + +enum class customize_tag { + default_tag, + invalid_tag, + custom_tag +}; + } // namespace magic_enum::customize::detail -using customize_t = std::variant; +class customize_t : public std::pair { + public: + constexpr customize_t(string_view srt) : std::pair{detail::customize_tag::custom_tag, srt} {} + constexpr customize_t(const char_type* srt) : customize_t{string_view{srt}} {} + constexpr customize_t(detail::customize_tag tag) : std::pair{tag, string_view{}} { + MAGIC_ENUM_ASSERT(tag != detail::customize_tag::custom_tag); + } +}; // Default customize. -inline constexpr auto default_tag = detail::default_customize_tag{}; +inline constexpr auto default_tag = customize_t{detail::customize_tag::default_tag}; // Invalid customize. -inline constexpr auto invalid_tag = detail::invalid_customize_tag{}; +inline constexpr auto invalid_tag = customize_t{detail::customize_tag::invalid_tag}; // If need custom names for enum, add specialization enum_name for necessary enum type. template @@ -164,12 +215,6 @@ constexpr customize_t enum_type_name() noexcept { namespace detail { -template >>> -using enum_constant = std::integral_constant, V>; - -template -inline constexpr bool always_false_v = false; - template struct supported #if defined(MAGIC_ENUM_SUPPORTED) && MAGIC_ENUM_SUPPORTED || defined(MAGIC_ENUM_NO_CHECK_SUPPORT) @@ -178,6 +223,12 @@ struct supported : std::false_type {}; #endif +template , std::enable_if_t, int> = 0> +using enum_constant = std::integral_constant; + +template +inline constexpr bool always_false_v = false; + template struct has_is_flags : std::false_type {}; @@ -196,14 +247,23 @@ struct range_max : std::integral_constant {}; template struct range_max::max)>> : std::integral_constant::max), customize::enum_range::max> {}; +struct str_view { + const char* str_ = nullptr; + std::size_t size_ = 0; +}; + template -class static_string { +class static_str { public: - constexpr explicit static_string(string_view str) noexcept : static_string{str, std::make_integer_sequence{}} { - assert(str.size() == N); + constexpr explicit static_str(str_view str) noexcept : static_str{str.str_, std::make_integer_sequence{}} { + MAGIC_ENUM_ASSERT(str.size_ == N); } - constexpr const char* data() const noexcept { return chars_; } + constexpr explicit static_str(string_view str) noexcept : static_str{str.data(), std::make_integer_sequence{}} { + MAGIC_ENUM_ASSERT(str.size() == N); + } + + constexpr const char_type* data() const noexcept { return chars_; } constexpr std::uint16_t size() const noexcept { return N; } @@ -211,69 +271,44 @@ class static_string { private: template - constexpr static_string(string_view str, std::integer_sequence) noexcept : chars_{str[I]..., '\0'} {} + constexpr static_str(const char* str, std::integer_sequence) noexcept : chars_{static_cast(str[I])..., static_cast('\0')} {} - char chars_[N + 1]; + template + constexpr static_str(string_view str, std::integer_sequence) noexcept : chars_{str[I]..., static_cast('\0')} {} + + char_type chars_[static_cast(N) + 1]; }; template <> -class static_string<0> { +class static_str<0> { public: - constexpr explicit static_string() = default; + constexpr explicit static_str() = default; - constexpr explicit static_string(string_view) noexcept {} + constexpr explicit static_str(str_view) noexcept {} - constexpr const char* data() const noexcept { return nullptr; } + constexpr explicit static_str(string_view) noexcept {} + + constexpr const char_type* data() const noexcept { return nullptr; } constexpr std::uint16_t size() const noexcept { return 0; } constexpr operator string_view() const noexcept { return {}; } }; -constexpr string_view pretty_name(string_view name) noexcept { - for (std::size_t i = name.size(); i > 0; --i) { - if (!((name[i - 1] >= '0' && name[i - 1] <= '9') || - (name[i - 1] >= 'a' && name[i - 1] <= 'z') || - (name[i - 1] >= 'A' && name[i - 1] <= 'Z') || -#if defined(MAGIC_ENUM_ENABLE_NONASCII) - (name[i - 1] & 0x80) || -#endif - (name[i - 1] == '_'))) { - name.remove_prefix(i); - break; - } - } - - if (name.size() > 0 && ((name.front() >= 'a' && name.front() <= 'z') || - (name.front() >= 'A' && name.front() <= 'Z') || -#if defined(MAGIC_ENUM_ENABLE_NONASCII) - (name.front() & 0x80) || -#endif - (name.front() == '_'))) { - return name; - } - - return {}; // Invalid name. -} - +template > class case_insensitive { - static constexpr char to_lower(char c) noexcept { - return (c >= 'A' && c <= 'Z') ? static_cast(c + ('a' - 'A')) : c; + static constexpr char_type to_lower(char_type c) noexcept { + return (c >= static_cast('A') && c <= static_cast('Z')) ? static_cast(c + (static_cast('a') - static_cast('A'))) : c; } public: template - constexpr auto operator()([[maybe_unused]] L lhs, [[maybe_unused]] R rhs) const noexcept -> std::enable_if_t, char> && std::is_same_v, char>, bool> { -#if defined(MAGIC_ENUM_ENABLE_NONASCII) - static_assert(always_false_v, "magic_enum::case_insensitive not supported Non-ASCII feature."); - return false; -#else - return to_lower(lhs) == to_lower(rhs); -#endif + constexpr auto operator()(L lhs, R rhs) const noexcept -> std::enable_if_t, char_type> && std::is_same_v, char_type>, bool> { + return Op{}(to_lower(lhs), to_lower(rhs)); } }; -constexpr std::size_t find(string_view str, char c) noexcept { +constexpr std::size_t find(string_view str, char_type c) noexcept { #if defined(__clang__) && __clang_major__ < 9 && defined(__GLIBCXX__) || defined(_MSC_VER) && _MSC_VER < 1920 && !defined(__clang__) // https://stackoverflow.com/questions/56484834/constexpr-stdstring-viewfind-last-of-doesnt-work-on-clang-8-with-libstdc // https://developercommunity.visualstudio.com/content/problem/360432/vs20178-regression-c-failed-in-test.html @@ -291,15 +326,10 @@ constexpr std::size_t find(string_view str, char c) noexcept { return string_view::npos; } else { - return str.find_first_of(c); + return str.find(c); } } -template -constexpr std::array, N> to_array(T (&a)[N], std::index_sequence) noexcept { - return {{a[I]...}}; -} - template constexpr bool is_default_predicate() noexcept { return std::is_same_v, std::equal_to> || @@ -309,7 +339,7 @@ constexpr bool is_default_predicate() noexcept { template constexpr bool is_nothrow_invocable() { return is_default_predicate() || - std::is_nothrow_invocable_r_v; + std::is_nothrow_invocable_r_v; } template @@ -365,7 +395,7 @@ constexpr I log2(I value) noexcept { static_assert(std::is_integral_v, "magic_enum::detail::log2 requires integral type."); if constexpr (std::is_same_v) { // bool special case - return assert(false), value; + return MAGIC_ENUM_ASSERT(false), value; } else { auto ret = I{0}; for (; value > I{1}; value >>= I{1}, ++ret) {} @@ -374,6 +404,15 @@ constexpr I log2(I value) noexcept { } } +#if defined(__cpp_lib_array_constexpr) && __cpp_lib_array_constexpr >= 201603L +# define MAGIC_ENUM_ARRAY_CONSTEXPR 1 +#else +template +constexpr std::array, N> to_array(T (&a)[N], std::index_sequence) noexcept { + return {{a[I]...}}; +} +#endif + template inline constexpr bool is_enum_v = std::is_enum_v && std::is_same_v>; @@ -381,83 +420,257 @@ template constexpr auto n() noexcept { static_assert(is_enum_v, "magic_enum::detail::n requires enum type."); - [[maybe_unused]] constexpr auto custom = customize::enum_type_name(); - static_assert(std::is_same_v, customize::customize_t>, "magic_enum::customize requires customize_t type."); - if constexpr (custom.index() == 0) { - constexpr auto name = std::get(custom); - static_assert(!name.empty(), "magic_enum::customize requires not empty string."); - return static_string{name}; - } else if constexpr (custom.index() == 1 && supported::value) { -#if defined(__clang__) || defined(__GNUC__) - constexpr auto name = pretty_name({__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 2}); + if constexpr (supported::value) { +#if defined(MAGIC_ENUM_GET_TYPE_NAME_BUILTIN) + constexpr auto name_ptr = MAGIC_ENUM_GET_TYPE_NAME_BUILTIN(E); + constexpr auto name = name_ptr ? str_view{name_ptr, std::char_traits::length(name_ptr)} : str_view{}; +#elif defined(__clang__) + str_view name; + if constexpr (sizeof(__PRETTY_FUNCTION__) == sizeof(__FUNCTION__)) { + static_assert(always_false_v, "magic_enum::detail::n requires __PRETTY_FUNCTION__."); + return str_view{}; + } else { + name.size_ = sizeof(__PRETTY_FUNCTION__) - 36; + name.str_ = __PRETTY_FUNCTION__ + 34; + } +#elif defined(__GNUC__) + auto name = str_view{__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1}; + if constexpr (sizeof(__PRETTY_FUNCTION__) == sizeof(__FUNCTION__)) { + static_assert(always_false_v, "magic_enum::detail::n requires __PRETTY_FUNCTION__."); + return str_view{}; + } else if (name.str_[name.size_ - 1] == ']') { + name.size_ -= 50; + name.str_ += 49; + } else { + name.size_ -= 40; + name.str_ += 37; + } #elif defined(_MSC_VER) - constexpr auto name = pretty_name({__FUNCSIG__, sizeof(__FUNCSIG__) - 17}); + // CLI/C++ workaround (see https://github.com/Neargye/magic_enum/issues/284). + str_view name; + name.str_ = __FUNCSIG__; + name.str_ += 40; + name.size_ += sizeof(__FUNCSIG__) - 57; #else - constexpr auto name = string_view{}; + auto name = str_view{}; #endif - return static_string{name}; + std::size_t p = 0; + for (std::size_t i = name.size_; i > 0; --i) { + if (name.str_[i] == ':') { + p = i + 1; + break; + } + } + if (p > 0) { + name.size_ -= p; + name.str_ += p; + } + return name; } else { - return static_string<0>{}; // Unsupported compiler or Invalid customize. + return str_view{}; // Unsupported compiler or Invalid customize. } } template -inline constexpr auto type_name_v = n(); +constexpr auto type_name() noexcept { + [[maybe_unused]] constexpr auto custom = customize::enum_type_name(); + static_assert(std::is_same_v, customize::customize_t>, "magic_enum::customize requires customize_t type."); + if constexpr (custom.first == customize::detail::customize_tag::custom_tag) { + constexpr auto name = custom.second; + static_assert(!name.empty(), "magic_enum::customize requires not empty string."); + return static_str{name}; + } else if constexpr (custom.first == customize::detail::customize_tag::invalid_tag) { + return static_str<0>{}; + } else if constexpr (custom.first == customize::detail::customize_tag::default_tag) { + constexpr auto name = n(); + return static_str{name}; + } else { + static_assert(always_false_v, "magic_enum::customize invalid."); + } +} +template +inline constexpr auto type_name_v = type_name(); + +template +constexpr auto n() noexcept { + static_assert(is_enum_v, "magic_enum::detail::n requires enum type."); + + if constexpr (supported::value) { +#if defined(MAGIC_ENUM_GET_ENUM_NAME_BUILTIN) + constexpr auto name_ptr = MAGIC_ENUM_GET_ENUM_NAME_BUILTIN(V); + auto name = name_ptr ? str_view{name_ptr, std::char_traits::length(name_ptr)} : str_view{}; +#elif defined(__clang__) + str_view name; + if constexpr (sizeof(__PRETTY_FUNCTION__) == sizeof(__FUNCTION__)) { + static_assert(always_false_v, "magic_enum::detail::n requires __PRETTY_FUNCTION__."); + return str_view{}; + } else { + name.size_ = sizeof(__PRETTY_FUNCTION__) - 36; + name.str_ = __PRETTY_FUNCTION__ + 34; + } + if (name.size_ > 22 && name.str_[0] == '(' && name.str_[1] == 'a' && name.str_[10] == ' ' && name.str_[22] == ':') { + name.size_ -= 23; + name.str_ += 23; + } + if (name.str_[0] == '(' || name.str_[0] == '-' || (name.str_[0] >= '0' && name.str_[0] <= '9')) { + name = str_view{}; + } +#elif defined(__GNUC__) + auto name = str_view{__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1}; + if constexpr (sizeof(__PRETTY_FUNCTION__) == sizeof(__FUNCTION__)) { + static_assert(always_false_v, "magic_enum::detail::n requires __PRETTY_FUNCTION__."); + return str_view{}; + } else if (name.str_[name.size_ - 1] == ']') { + name.size_ -= 55; + name.str_ += 54; + } else { + name.size_ -= 40; + name.str_ += 37; + } + if (name.str_[0] == '(') { + name = str_view{}; + } +#elif defined(_MSC_VER) + str_view name; + if ((__FUNCSIG__[5] == '_' && __FUNCSIG__[35] != '(') || (__FUNCSIG__[5] == 'c' && __FUNCSIG__[41] != '(')) { + // CLI/C++ workaround (see https://github.com/Neargye/magic_enum/issues/284). + name.str_ = __FUNCSIG__; + name.str_ += 35; + name.size_ = sizeof(__FUNCSIG__) - 52; + } +#else + auto name = str_view{}; +#endif + std::size_t p = 0; + for (std::size_t i = name.size_; i > 0; --i) { + if (name.str_[i] == ':') { + p = i + 1; + break; + } + } + if (p > 0) { + name.size_ -= p; + name.str_ += p; + } + return name; + } else { + return str_view{}; // Unsupported compiler or Invalid customize. + } +} + +#if defined(_MSC_VER) && !defined(__clang__) && _MSC_VER < 1920 +# define MAGIC_ENUM_VS_2017_WORKAROUND 1 +#endif + +#if defined(MAGIC_ENUM_VS_2017_WORKAROUND) template constexpr auto n() noexcept { static_assert(is_enum_v, "magic_enum::detail::n requires enum type."); +# if defined(MAGIC_ENUM_GET_ENUM_NAME_BUILTIN) + constexpr auto name_ptr = MAGIC_ENUM_GET_ENUM_NAME_BUILTIN(V); + auto name = name_ptr ? str_view{name_ptr, std::char_traits::length(name_ptr)} : str_view{}; +# else + // CLI/C++ workaround (see https://github.com/Neargye/magic_enum/issues/284). + str_view name; + name.str_ = __FUNCSIG__; + name.size_ = sizeof(__FUNCSIG__) - 17; + std::size_t p = 0; + for (std::size_t i = name.size_; i > 0; --i) { + if (name.str_[i] == ',' || name.str_[i] == ':') { + p = i + 1; + break; + } + } + if (p > 0) { + name.size_ -= p; + name.str_ += p; + } + if (name.str_[0] == '(' || name.str_[0] == '-' || (name.str_[0] >= '0' && name.str_[0] <= '9')) { + name = str_view{}; + } + return name; +# endif +} +#endif + +template +constexpr auto enum_name() noexcept { [[maybe_unused]] constexpr auto custom = customize::enum_name(V); static_assert(std::is_same_v, customize::customize_t>, "magic_enum::customize requires customize_t type."); - if constexpr (custom.index() == 0) { - constexpr auto name = std::get(custom); + if constexpr (custom.first == customize::detail::customize_tag::custom_tag) { + constexpr auto name = custom.second; static_assert(!name.empty(), "magic_enum::customize requires not empty string."); - return static_string{name}; - } else if constexpr (custom.index() == 1 && supported::value) { -#if defined(__clang__) || defined(__GNUC__) - constexpr auto name = pretty_name({__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 2}); -#elif defined(_MSC_VER) - constexpr auto name = pretty_name({__FUNCSIG__, sizeof(__FUNCSIG__) - 17}); + return static_str{name}; + } else if constexpr (custom.first == customize::detail::customize_tag::invalid_tag) { + return static_str<0>{}; + } else if constexpr (custom.first == customize::detail::customize_tag::default_tag) { +#if defined(MAGIC_ENUM_VS_2017_WORKAROUND) + constexpr auto name = n(); #else - constexpr auto name = string_view{}; + constexpr auto name = n(); #endif - return static_string{name}; + return static_str{name}; } else { - return static_string<0>{}; // Unsupported compiler or Invalid customize. + static_assert(always_false_v, "magic_enum::customize invalid."); } } template -inline constexpr auto enum_name_v = n(); +inline constexpr auto enum_name_v = enum_name(); template constexpr bool is_valid() noexcept { - static_assert(is_enum_v, "magic_enum::detail::is_valid requires enum type."); - - return n(V)>().size() != 0; -} - -template > -constexpr E value(std::size_t i) noexcept { - static_assert(is_enum_v, "magic_enum::detail::value requires enum type."); - - if constexpr (std::is_same_v) { // bool special case - static_assert(O == 0, "magic_enum::detail::value requires valid offset."); - - return static_cast(i); - } else if constexpr (IsFlags) { - return static_cast(U{1} << static_cast(static_cast(i) + O)); +#if defined(__clang__) && __clang_major__ >= 16 + // https://reviews.llvm.org/D130058, https://reviews.llvm.org/D131307 + constexpr E v = __builtin_bit_cast(E, V); +#else + constexpr E v = static_cast(V); +#endif + [[maybe_unused]] constexpr auto custom = customize::enum_name(v); + static_assert(std::is_same_v, customize::customize_t>, "magic_enum::customize requires customize_t type."); + if constexpr (custom.first == customize::detail::customize_tag::custom_tag) { + constexpr auto name = custom.second; + static_assert(!name.empty(), "magic_enum::customize requires not empty string."); + return name.size() != 0; + } else if constexpr (custom.first == customize::detail::customize_tag::default_tag) { +#if defined(MAGIC_ENUM_VS_2017_WORKAROUND) + return n().size_ != 0; +#else + return n().size_ != 0; +#endif } else { - return static_cast(static_cast(i) + O); + return false; } } -template > -constexpr int reflected_min() noexcept { - static_assert(is_enum_v, "magic_enum::detail::reflected_min requires enum type."); +enum class enum_subtype { + common, + flags +}; - if constexpr (IsFlags) { +template > +constexpr U ualue(std::size_t i) noexcept { + if constexpr (std::is_same_v) { // bool special case + static_assert(O == 0, "magic_enum::detail::ualue requires valid offset."); + + return static_cast(i); + } else if constexpr (S == enum_subtype::flags) { + return static_cast(U{1} << static_cast(static_cast(i) + O)); + } else { + return static_cast(static_cast(i) + O); + } +} + +template > +constexpr E value(std::size_t i) noexcept { + return static_cast(ualue(i)); +} + +template > +constexpr int reflected_min() noexcept { + if constexpr (S == enum_subtype::flags) { return 0; } else { constexpr auto lhs = range_min::value; @@ -471,11 +684,9 @@ constexpr int reflected_min() noexcept { } } -template > +template > constexpr int reflected_max() noexcept { - static_assert(is_enum_v, "magic_enum::detail::reflected_max requires enum type."); - - if constexpr (IsFlags) { + if constexpr (S == enum_subtype::flags) { return std::numeric_limits::digits - 1; } else { constexpr auto lhs = range_max::value; @@ -489,155 +700,182 @@ constexpr int reflected_max() noexcept { } } -template -inline constexpr auto reflected_min_v = reflected_min(); +#define MAGIC_ENUM_FOR_EACH_256(T) \ + T( 0)T( 1)T( 2)T( 3)T( 4)T( 5)T( 6)T( 7)T( 8)T( 9)T( 10)T( 11)T( 12)T( 13)T( 14)T( 15)T( 16)T( 17)T( 18)T( 19)T( 20)T( 21)T( 22)T( 23)T( 24)T( 25)T( 26)T( 27)T( 28)T( 29)T( 30)T( 31) \ + T( 32)T( 33)T( 34)T( 35)T( 36)T( 37)T( 38)T( 39)T( 40)T( 41)T( 42)T( 43)T( 44)T( 45)T( 46)T( 47)T( 48)T( 49)T( 50)T( 51)T( 52)T( 53)T( 54)T( 55)T( 56)T( 57)T( 58)T( 59)T( 60)T( 61)T( 62)T( 63) \ + T( 64)T( 65)T( 66)T( 67)T( 68)T( 69)T( 70)T( 71)T( 72)T( 73)T( 74)T( 75)T( 76)T( 77)T( 78)T( 79)T( 80)T( 81)T( 82)T( 83)T( 84)T( 85)T( 86)T( 87)T( 88)T( 89)T( 90)T( 91)T( 92)T( 93)T( 94)T( 95) \ + T( 96)T( 97)T( 98)T( 99)T(100)T(101)T(102)T(103)T(104)T(105)T(106)T(107)T(108)T(109)T(110)T(111)T(112)T(113)T(114)T(115)T(116)T(117)T(118)T(119)T(120)T(121)T(122)T(123)T(124)T(125)T(126)T(127) \ + T(128)T(129)T(130)T(131)T(132)T(133)T(134)T(135)T(136)T(137)T(138)T(139)T(140)T(141)T(142)T(143)T(144)T(145)T(146)T(147)T(148)T(149)T(150)T(151)T(152)T(153)T(154)T(155)T(156)T(157)T(158)T(159) \ + T(160)T(161)T(162)T(163)T(164)T(165)T(166)T(167)T(168)T(169)T(170)T(171)T(172)T(173)T(174)T(175)T(176)T(177)T(178)T(179)T(180)T(181)T(182)T(183)T(184)T(185)T(186)T(187)T(188)T(189)T(190)T(191) \ + T(192)T(193)T(194)T(195)T(196)T(197)T(198)T(199)T(200)T(201)T(202)T(203)T(204)T(205)T(206)T(207)T(208)T(209)T(210)T(211)T(212)T(213)T(214)T(215)T(216)T(217)T(218)T(219)T(220)T(221)T(222)T(223) \ + T(224)T(225)T(226)T(227)T(228)T(229)T(230)T(231)T(232)T(233)T(234)T(235)T(236)T(237)T(238)T(239)T(240)T(241)T(242)T(243)T(244)T(245)T(246)T(247)T(248)T(249)T(250)T(251)T(252)T(253)T(254)T(255) -template -inline constexpr auto reflected_max_v = reflected_max(); - -template -constexpr std::size_t values_count(const bool (&valid)[N]) noexcept { - auto count = std::size_t{0}; - for (std::size_t i = 0; i < N; ++i) { - if (valid[i]) { - ++count; - } +template +constexpr void valid_count(bool* valid, std::size_t& count) noexcept { +#define MAGIC_ENUM_V(O) \ + if constexpr ((I + O) < Size) { \ + if constexpr (is_valid(I + O)>()) { \ + valid[I + O] = true; \ + ++count; \ + } \ } - return count; + MAGIC_ENUM_FOR_EACH_256(MAGIC_ENUM_V) + + if constexpr ((I + 256) < Size) { + valid_count(valid, count); + } +#undef MAGIC_ENUM_V } -template -constexpr auto values(std::index_sequence) noexcept { - static_assert(is_enum_v, "magic_enum::detail::values requires enum type."); - constexpr bool valid[sizeof...(I)] = {is_valid(I)>()...}; - constexpr std::size_t count = values_count(valid); +template +struct valid_count_t { + std::size_t count = 0; + bool valid[N] = {}; +}; - if constexpr (count > 0) { - E values[count] = {}; - for (std::size_t i = 0, v = 0; v < count; ++i) { - if (valid[i]) { - values[v++] = value(i); +template +constexpr auto valid_count() noexcept { + valid_count_t vc; + valid_count(vc.valid, vc.count); + return vc; +} + +template +constexpr auto values() noexcept { + constexpr auto vc = valid_count(); + + if constexpr (vc.count > 0) { +#if defined(MAGIC_ENUM_ARRAY_CONSTEXPR) + std::array values = {}; +#else + E values[vc.count] = {}; +#endif + for (std::size_t i = 0, v = 0; v < vc.count; ++i) { + if (vc.valid[i]) { + values[v++] = value(i); } } - - return to_array(values, std::make_index_sequence{}); +#if defined(MAGIC_ENUM_ARRAY_CONSTEXPR) + return values; +#else + return to_array(values, std::make_index_sequence{}); +#endif } else { return std::array{}; } } -template > +template > constexpr auto values() noexcept { - static_assert(is_enum_v, "magic_enum::detail::values requires enum type."); - constexpr auto min = reflected_min_v; - constexpr auto max = reflected_max_v; + constexpr auto min = reflected_min(); + constexpr auto max = reflected_max(); constexpr auto range_size = max - min + 1; static_assert(range_size > 0, "magic_enum::enum_range requires valid size."); static_assert(range_size < (std::numeric_limits::max)(), "magic_enum::enum_range requires valid size."); - return values>(std::make_index_sequence{}); + return values(); } template > -constexpr bool is_flags_enum() noexcept { - static_assert(is_enum_v, "magic_enum::detail::is_flags_enum requires enum type."); - - if constexpr (has_is_flags::value) { - return customize::enum_range::is_flags; - } else if constexpr (std::is_same_v) { // bool special case - return false; +constexpr enum_subtype subtype(std::true_type) noexcept { + if constexpr (std::is_same_v) { // bool special case + return enum_subtype::common; + } else if constexpr (has_is_flags::value) { + return customize::enum_range::is_flags ? enum_subtype::flags : enum_subtype::common; } else { -#if defined(MAGIC_ENUM_NO_CHECK_FLAGS) - return false; -#else - constexpr auto flags_values = values(); - constexpr auto default_values = values(); +#if defined(MAGIC_ENUM_AUTO_IS_FLAGS) + constexpr auto flags_values = values(); + constexpr auto default_values = values(); if (flags_values.size() == 0 || default_values.size() > flags_values.size()) { - return false; + return enum_subtype::common; } for (std::size_t i = 0; i < default_values.size(); ++i) { const auto v = static_cast(default_values[i]); if (v != 0 && (v & (v - 1)) != 0) { - return false; + return enum_subtype::common; } } - return flags_values.size() > 0; + return enum_subtype::flags; +#else + return enum_subtype::common; #endif } } -template -inline constexpr bool is_flags_v = is_flags_enum(); - -template -inline constexpr std::array values_v = values>(); +template +constexpr enum_subtype subtype(std::false_type) noexcept { + // For non-enum type return default common subtype. + return enum_subtype::common; +} template > -using values_t = decltype((values_v)); +inline constexpr auto subtype_v = subtype(std::is_enum{}); -template -inline constexpr auto count_v = values_v.size(); +template +inline constexpr auto values_v = values(); -template > -inline constexpr auto min_v = (count_v > 0) ? static_cast(values_v.front()) : U{0}; +template > +using values_t = decltype((values_v)); -template > -inline constexpr auto max_v = (count_v > 0) ? static_cast(values_v.back()) : U{0}; +template +inline constexpr auto count_v = values_v.size(); -template +template > +inline constexpr auto min_v = (count_v > 0) ? static_cast(values_v.front()) : U{0}; + +template > +inline constexpr auto max_v = (count_v > 0) ? static_cast(values_v.back()) : U{0}; + +template constexpr auto names(std::index_sequence) noexcept { - static_assert(is_enum_v, "magic_enum::detail::names requires enum type."); - - return std::array{{enum_name_v[I]>...}}; + constexpr auto names = std::array{{enum_name_v[I]>...}}; + return names; } -template -inline constexpr std::array names_v = names(std::make_index_sequence>{}); +template +inline constexpr auto names_v = names(std::make_index_sequence>{}); -template > -using names_t = decltype((names_v)); +template > +using names_t = decltype((names_v)); -template +template constexpr auto entries(std::index_sequence) noexcept { - static_assert(is_enum_v, "magic_enum::detail::entries requires enum type."); - - return std::array, sizeof...(I)>{{{values_v[I], enum_name_v[I]>}...}}; + constexpr auto entries = std::array, sizeof...(I)>{{{values_v[I], enum_name_v[I]>}...}}; + return entries; } -template -inline constexpr std::array entries_v = entries(std::make_index_sequence>{}); +template +inline constexpr auto entries_v = entries(std::make_index_sequence>{}); -template > -using entries_t = decltype((entries_v)); +template > +using entries_t = decltype((entries_v)); -template > +template > constexpr bool is_sparse() noexcept { - static_assert(is_enum_v, "magic_enum::detail::is_sparse requires enum type."); - - if constexpr (count_v == 0) { + if constexpr (count_v == 0) { return false; } else if constexpr (std::is_same_v) { // bool special case return false; } else { - constexpr auto max = is_flags_v ? log2(max_v) : max_v; - constexpr auto min = is_flags_v ? log2(min_v) : min_v; + constexpr auto max = (S == enum_subtype::flags) ? log2(max_v) : max_v; + constexpr auto min = (S == enum_subtype::flags) ? log2(min_v) : min_v; constexpr auto range_size = max - min + 1; - return range_size != count_v; + return range_size != count_v; } } -template -inline constexpr bool is_sparse_v = is_sparse(); +template > +inline constexpr bool is_sparse_v = is_sparse(); -template > +template > constexpr U values_ors() noexcept { - static_assert(is_enum_v, "magic_enum::detail::values_ors requires enum type."); + static_assert(S == enum_subtype::flags, "magic_enum::detail::values_ors requires valid subtype."); auto ors = U{0}; - for (std::size_t i = 0; i < count_v; ++i) { - ors |= static_cast(values_v[i]); + for (std::size_t i = 0; i < count_v; ++i) { + ors |= static_cast(values_v[i]); } return ors; @@ -652,10 +890,10 @@ struct enable_if_enum { static_assert(supported::value, "magic_enum unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility)."); }; -template > -using enable_if_t = typename enable_if_enum> && std::is_invocable_r_v, R>::type; +template , typename D = std::decay_t> +using enable_if_t = typename enable_if_enum && std::is_invocable_r_v, R>::type; -template >>> +template >, int> = 0> using enum_concept = T; template > @@ -676,6 +914,8 @@ struct underlying_type {}; template struct underlying_type : std::underlying_type> {}; +#if defined(MAGIC_ENUM_ENABLE_HASH) || defined(MAGIC_ENUM_ENABLE_HASH_SWITCH) + template struct constexpr_hash_t; @@ -748,7 +988,7 @@ struct constexpr_hash_t -[[maybe_unused]] constexpr static Hash hash_v{}; +inline constexpr Hash hash_v{}; template constexpr auto calculate_cases(std::size_t Page) noexcept { @@ -796,7 +1036,8 @@ constexpr R invoke_r(F&& f, Args&&... args) noexcept(std::is_nothrow_invocable_r } enum class case_call_t { - index, value + index, + value }; template @@ -806,7 +1047,7 @@ template <> inline constexpr auto default_result_type_lambda = []() noexcept {}; template -constexpr bool no_duplicate() noexcept { +constexpr bool has_duplicate() noexcept { using value_t = std::decay_t; using hash_value_t = std::invoke_result_t; std::arraysize()> hashes{}; @@ -828,50 +1069,43 @@ constexpr bool no_duplicate() noexcept { return true; } -#define MAGIC_ENUM_FOR_EACH_256(T) T(0)T(1)T(2)T(3)T(4)T(5)T(6)T(7)T(8)T(9)T(10)T(11)T(12)T(13)T(14)T(15)T(16)T(17)T(18)T(19)T(20)T(21)T(22)T(23)T(24)T(25)T(26)T(27)T(28)T(29)T(30)T(31) \ - T(32)T(33)T(34)T(35)T(36)T(37)T(38)T(39)T(40)T(41)T(42)T(43)T(44)T(45)T(46)T(47)T(48)T(49)T(50)T(51)T(52)T(53)T(54)T(55)T(56)T(57)T(58)T(59)T(60)T(61)T(62)T(63) \ - T(64)T(65)T(66)T(67)T(68)T(69)T(70)T(71)T(72)T(73)T(74)T(75)T(76)T(77)T(78)T(79)T(80)T(81)T(82)T(83)T(84)T(85)T(86)T(87)T(88)T(89)T(90)T(91)T(92)T(93)T(94)T(95) \ - T(96)T(97)T(98)T(99)T(100)T(101)T(102)T(103)T(104)T(105)T(106)T(107)T(108)T(109)T(110)T(111)T(112)T(113)T(114)T(115)T(116)T(117)T(118)T(119)T(120)T(121)T(122)T(123)T(124)T(125)T(126)T(127) \ - T(128)T(129)T(130)T(131)T(132)T(133)T(134)T(135)T(136)T(137)T(138)T(139)T(140)T(141)T(142)T(143)T(144)T(145)T(146)T(147)T(148)T(149)T(150)T(151)T(152)T(153)T(154)T(155)T(156)T(157)T(158)T(159) \ - T(160)T(161)T(162)T(163)T(164)T(165)T(166)T(167)T(168)T(169)T(170)T(171)T(172)T(173)T(174)T(175)T(176)T(177)T(178)T(179)T(180)T(181)T(182)T(183)T(184)T(185)T(186)T(187)T(188)T(189)T(190)T(191) \ - T(192)T(193)T(194)T(195)T(196)T(197)T(198)T(199)T(200)T(201)T(202)T(203)T(204)T(205)T(206)T(207)T(208)T(209)T(210)T(211)T(212)T(213)T(214)T(215)T(216)T(217)T(218)T(219)T(220)T(221)T(222)T(223) \ - T(224)T(225)T(226)T(227)T(228)T(229)T(230)T(231)T(232)T(233)T(234)T(235)T(236)T(237)T(238)T(239)T(240)T(241)T(242)T(243)T(244)T(245)T(246)T(247)T(248)T(249)T(250)T(251)T(252)T(253)T(254)T(255) - -#define MAGIC_ENUM_CASE(val) \ - case cases[val]: \ - if constexpr ((val) + Page < size) { \ - if (!pred(values[val + Page], searched)) { \ - break; \ - } \ - if constexpr (CallValue == case_call_t::index) { \ - if constexpr (std::is_invocable_r_v>) { \ - return detail::invoke_r(std::forward(lambda), std::integral_constant{}); \ - } else if constexpr (std::is_invocable_v>) { \ - assert(false && "magic_enum::detail::constexpr_switch wrong result type."); \ - } \ - } else if constexpr (CallValue == case_call_t::value) { \ - if constexpr (std::is_invocable_r_v>) { \ - return detail::invoke_r(std::forward(lambda), enum_constant{}); \ - } else if constexpr (std::is_invocable_r_v>) { \ - assert(false && "magic_enum::detail::constexpr_switch wrong result type."); \ - } \ - } \ - break; \ +#define MAGIC_ENUM_CASE(val) \ + case cases[val]: \ + if constexpr ((val) + Page < size) { \ + if (!pred(values[val + Page], searched)) { \ + break; \ + } \ + if constexpr (CallValue == case_call_t::index) { \ + if constexpr (std::is_invocable_r_v>) { \ + return detail::invoke_r(std::forward(lambda), std::integral_constant{}); \ + } else if constexpr (std::is_invocable_v>) { \ + MAGIC_ENUM_ASSERT(false && "magic_enum::detail::constexpr_switch wrong result type."); \ + } \ + } else if constexpr (CallValue == case_call_t::value) { \ + if constexpr (std::is_invocable_r_v>) { \ + return detail::invoke_r(std::forward(lambda), enum_constant{}); \ + } else if constexpr (std::is_invocable_r_v>) { \ + MAGIC_ENUM_ASSERT(false && "magic_enum::detail::constexpr_switch wrong result type."); \ + } \ + } \ + break; \ } else [[fallthrough]]; template ::value_type>, - typename Lambda, typename ResultGetterType = decltype(default_result_type_lambda<>), - typename BinaryPredicate = std::equal_to<>> -constexpr std::invoke_result_t constexpr_switch( + typename BinaryPredicate = std::equal_to<>, + typename Lambda, + typename ResultGetterType> +constexpr decltype(auto) constexpr_switch( Lambda&& lambda, typename std::decay_t::value_type searched, - ResultGetterType&& def = default_result_type_lambda<>, + ResultGetterType&& def, BinaryPredicate&& pred = {}) { using result_t = std::invoke_result_t; - using hash_t = std::conditional_t(), Hash, typename Hash::secondary_hash>; + using hash_t = std::conditional_t(), Hash, typename Hash::secondary_hash>; + static_assert(has_duplicate(), "magic_enum::detail::constexpr_switch duplicated hash found, please report it: https://github.com/Neargye/magic_enum/issues."); constexpr std::array values = *GlobValues; constexpr std::size_t size = values.size(); constexpr std::array cases = calculate_cases(Page); @@ -887,23 +1121,9 @@ constexpr std::invoke_result_t constexpr_switch( return def(); } -#undef MAGIC_ENUM_FOR_EACH_256 #undef MAGIC_ENUM_CASE -template -constexpr auto for_each(Lambda&& lambda, std::index_sequence) { - static_assert(is_enum_v, "magic_enum::detail::for_each requires enum type."); - constexpr bool has_void_return = (std::is_void_v[I]>>> || ...); - constexpr bool all_same_return = (std::is_same_v[0]>>, std::invoke_result_t[I]>>> && ...); - - if constexpr (has_void_return) { - (lambda(enum_constant[I]>{}), ...); - } else if constexpr (all_same_return) { - return std::array{lambda(enum_constant[I]>{})...}; - } else { - return std::tuple{lambda(enum_constant[I]>{})...}; - } -} +#endif } // namespace magic_enum::detail @@ -950,40 +1170,39 @@ template } // Returns number of enum values. -template +template > [[nodiscard]] constexpr auto enum_count() noexcept -> detail::enable_if_t { - return detail::count_v>; + return detail::count_v, S>; } // Returns enum value at specified index. // No bounds checking is performed: the behavior is undefined if index >= number of enum values. -template +template > [[nodiscard]] constexpr auto enum_value(std::size_t index) noexcept -> detail::enable_if_t> { using D = std::decay_t; - if constexpr (detail::is_sparse_v) { - return assert((index < detail::count_v)), detail::values_v[index]; + if constexpr (detail::is_sparse_v) { + return MAGIC_ENUM_ASSERT(index < detail::count_v), detail::values_v[index]; } else { - constexpr bool is_flag = detail::is_flags_v; - constexpr auto min = is_flag ? detail::log2(detail::min_v) : detail::min_v; + constexpr auto min = (S == detail::enum_subtype::flags) ? detail::log2(detail::min_v) : detail::min_v; - return assert((index < detail::count_v)), detail::value(index); + return MAGIC_ENUM_ASSERT(index < detail::count_v), detail::value(index); } } // Returns enum value at specified index. -template +template > [[nodiscard]] constexpr auto enum_value() noexcept -> detail::enable_if_t> { using D = std::decay_t; - static_assert(I < detail::count_v, "magic_enum::enum_value out of range."); + static_assert(I < detail::count_v, "magic_enum::enum_value out of range."); - return enum_value(I); + return enum_value(I); } // Returns std::array with enum values, sorted by enum value. -template -[[nodiscard]] constexpr auto enum_values() noexcept -> detail::enable_if_t> { - return detail::values_v>; +template > +[[nodiscard]] constexpr auto enum_values() noexcept -> detail::enable_if_t> { + return detail::values_v, S>; } // Returns integer value from enum value. @@ -1000,31 +1219,50 @@ template // Obtains index in enum values from enum value. // Returns optional with index. -template +template > [[nodiscard]] constexpr auto enum_index(E value) noexcept -> detail::enable_if_t> { using D = std::decay_t; using U = underlying_type_t; - if constexpr (detail::count_v == 0) { + if constexpr (detail::count_v == 0) { + static_cast(value); return {}; // Empty enum. - } else if constexpr (detail::is_sparse_v || detail::is_flags_v) { - return detail::constexpr_switch<&detail::values_v, detail::case_call_t::index>( + } else if constexpr (detail::is_sparse_v || (S == detail::enum_subtype::flags)) { +#if defined(MAGIC_ENUM_ENABLE_HASH) + return detail::constexpr_switch<&detail::values_v, detail::case_call_t::index>( [](std::size_t i) { return optional{i}; }, value, detail::default_result_type_lambda>); +#else + for (std::size_t i = 0; i < detail::count_v; ++i) { + if (enum_value(i) == value) { + return i; + } + } + return {}; // Invalid value or out of range. +#endif } else { const auto v = static_cast(value); - if (v >= detail::min_v && v <= detail::max_v) { - return static_cast(v - detail::min_v); + if (v >= detail::min_v && v <= detail::max_v) { + return static_cast(v - detail::min_v); } return {}; // Invalid value or out of range. } } +// Obtains index in enum values from enum value. +// Returns optional with index. +template +[[nodiscard]] constexpr auto enum_index(E value) noexcept -> detail::enable_if_t> { + using D = std::decay_t; + + return enum_index(value); +} + // Obtains index in enum values from static storage enum variable. -template +template >> [[nodiscard]] constexpr auto enum_index() noexcept -> detail::enable_if_t { - constexpr auto index = enum_index>(V); + constexpr auto index = enum_index, S>(V); static_assert(index, "magic_enum::enum_index enum value does not have a index."); return *index; @@ -1042,298 +1280,139 @@ template // Returns name from enum value. // If enum value does not have name or value out of range, returns empty string. -template +template > [[nodiscard]] constexpr auto enum_name(E value) noexcept -> detail::enable_if_t { using D = std::decay_t; - if (const auto i = enum_index(value)) { - return detail::names_v[*i]; + if (const auto i = enum_index(value)) { + return detail::names_v[*i]; } return {}; } -// Returns name from enum-flags value. -// If enum-flags value does not have name or value out of range, returns empty string. -template -[[nodiscard]] auto enum_flags_name(E value) -> detail::enable_if_t { +// Returns name from enum value. +// If enum value does not have name or value out of range, returns empty string. +template +[[nodiscard]] constexpr auto enum_name(E value) -> detail::enable_if_t { using D = std::decay_t; - using U = underlying_type_t; - if constexpr (detail::is_flags_v) { - string name; - auto check_value = U{0}; - for (std::size_t i = 0; i < detail::count_v; ++i) { - if (const auto v = static_cast(enum_value(i)); (static_cast(value) & v) != 0) { - check_value |= v; - const auto n = detail::names_v[i]; - if (!name.empty()) { - name.append(1, '|'); - } - name.append(n.data(), n.size()); - } - } - - if (check_value != 0 && check_value == static_cast(value)) { - return name; - } - - return {}; // Invalid value or out of range. - } else { - return string{enum_name(value)}; - } + return enum_name(value); } // Returns std::array with names, sorted by enum value. -template -[[nodiscard]] constexpr auto enum_names() noexcept -> detail::enable_if_t> { - return detail::names_v>; +template > +[[nodiscard]] constexpr auto enum_names() noexcept -> detail::enable_if_t> { + return detail::names_v, S>; } // Returns std::array with pairs (value, name), sorted by enum value. -template -[[nodiscard]] constexpr auto enum_entries() noexcept -> detail::enable_if_t> { - return detail::entries_v>; +template > +[[nodiscard]] constexpr auto enum_entries() noexcept -> detail::enable_if_t> { + return detail::entries_v, S>; } // Allows you to write magic_enum::enum_cast("bar", magic_enum::case_insensitive); -inline constexpr auto case_insensitive = detail::case_insensitive{}; +inline constexpr auto case_insensitive = detail::case_insensitive<>{}; // Obtains enum value from integer value. // Returns optional with enum value. -template +template > [[nodiscard]] constexpr auto enum_cast(underlying_type_t value) noexcept -> detail::enable_if_t>> { using D = std::decay_t; - using U = underlying_type_t; - if constexpr (detail::count_v == 0) { + if constexpr (detail::count_v == 0) { + static_cast(value); return {}; // Empty enum. - } else if constexpr (detail::is_sparse_v) { - if constexpr (detail::is_flags_v) { - constexpr auto count = detail::count_v; - auto check_value = U{0}; - for (std::size_t i = 0; i < count; ++i) { - if (const auto v = static_cast(enum_value(i)); (value & v) != 0) { - check_value |= v; - } - } - - if (check_value != 0 && check_value == value) { - return static_cast(value); - } - return {}; // Invalid value or out of range. - } else { - return detail::constexpr_switch<&detail::values_v, detail::case_call_t::value>( + } else { + if constexpr (detail::is_sparse_v || (S == detail::enum_subtype::flags)) { +#if defined(MAGIC_ENUM_ENABLE_HASH) + return detail::constexpr_switch<&detail::values_v, detail::case_call_t::value>( [](D v) { return optional{v}; }, static_cast(value), detail::default_result_type_lambda>); +#else + for (std::size_t i = 0; i < detail::count_v; ++i) { + if (value == static_cast>(enum_value(i))) { + return static_cast(value); + } + } + return {}; // Invalid value or out of range. +#endif + } else { + if (value >= detail::min_v && value <= detail::max_v) { + return static_cast(value); + } + return {}; // Invalid value or out of range. } - } else { - constexpr auto min = detail::min_v; - constexpr auto max = detail::is_flags_v ? detail::values_ors() : detail::max_v; - - if (value >= min && value <= max) { - return static_cast(value); - } - return {}; // Invalid value or out of range. } } // Obtains enum value from name. // Returns optional with enum value. -template > -[[nodiscard]] constexpr auto enum_cast(string_view value, [[maybe_unused]] BinaryPredicate&& p = {}) noexcept(detail::is_nothrow_invocable()) -> detail::enable_if_t>, BinaryPredicate> { - static_assert(std::is_invocable_r_v, "magic_enum::enum_cast requires bool(char, char) invocable predicate."); +template , typename BinaryPredicate = std::equal_to<>> +[[nodiscard]] constexpr auto enum_cast(string_view value, [[maybe_unused]] BinaryPredicate p = {}) noexcept(detail::is_nothrow_invocable()) -> detail::enable_if_t>, BinaryPredicate> { using D = std::decay_t; - using U = underlying_type_t; - if constexpr (detail::count_v == 0) { + if constexpr (detail::count_v == 0) { + static_cast(value); return {}; // Empty enum. - } else if constexpr (detail::is_flags_v) { - auto result = U{0}; - while (!value.empty()) { - const auto d = detail::find(value, '|'); - const auto s = (d == string_view::npos) ? value : value.substr(0, d); - auto f = U{0}; - for (std::size_t i = 0; i < detail::count_v; ++i) { - if (detail::cmp_equal(s, detail::names_v[i], p)) { - f = static_cast(enum_value(i)); - result |= f; - break; - } - } - if (f == U{0}) { - return {}; // Invalid value or out of range. - } - value.remove_prefix((d == string_view::npos) ? value.size() : d + 1); - } - - if (result != U{0}) { - return static_cast(result); - } - return {}; // Invalid value or out of range. - } else if constexpr (detail::count_v > 0) { - if constexpr (detail::is_default_predicate()) { - return detail::constexpr_switch<&detail::names_v, detail::case_call_t::index>( - [](std::size_t i) { return optional{detail::values_v[i]}; }, +#if defined(MAGIC_ENUM_ENABLE_HASH) + } else if constexpr (detail::is_default_predicate()) { + return detail::constexpr_switch<&detail::names_v, detail::case_call_t::index>( + [](std::size_t i) { return optional{detail::values_v[i]}; }, value, detail::default_result_type_lambda>, [&p](string_view lhs, string_view rhs) { return detail::cmp_equal(lhs, rhs, p); }); +#endif } else { - for (std::size_t i = 0; i < detail::count_v; ++i) { - if (detail::cmp_equal(value, detail::names_v[i], p)) { - return enum_value(i); - } + for (std::size_t i = 0; i < detail::count_v; ++i) { + if (detail::cmp_equal(value, detail::names_v[i], p)) { + return enum_value(i); } - return {}; // Invalid value or out of range. } + return {}; // Invalid value or out of range. } } -// Checks whether enum contains enumerator with such enum value. -template +// Checks whether enum contains value with such value. +template > [[nodiscard]] constexpr auto enum_contains(E value) noexcept -> detail::enable_if_t { using D = std::decay_t; using U = underlying_type_t; - return static_cast(enum_cast(static_cast(value))); + return static_cast(enum_cast(static_cast(value))); } -// Checks whether enum contains enumerator with such integer value. -template -[[nodiscard]] constexpr auto enum_contains(underlying_type_t value) noexcept -> detail::enable_if_t { - using D = std::decay_t; - - return static_cast(enum_cast(value)); -} - -// Checks whether enum contains enumerator with such name. -template > -[[nodiscard]] constexpr auto enum_contains(string_view value, BinaryPredicate&& p = {}) noexcept(detail::is_nothrow_invocable()) -> detail::enable_if_t { - static_assert(std::is_invocable_r_v, "magic_enum::enum_contains requires bool(char, char) invocable predicate."); - using D = std::decay_t; - - return static_cast(enum_cast(value, std::forward(p))); -} - -template -constexpr auto enum_switch(Lambda&& lambda, E value) -> detail::enable_if_t { - using D = std::decay_t; - - return detail::constexpr_switch<&detail::values_v, detail::case_call_t::value>( - std::forward(lambda), - value, - detail::default_result_type_lambda); -} - -template -constexpr auto enum_switch(Lambda&& lambda, E value, Result&& result) -> detail::enable_if_t { - using D = std::decay_t; - - return detail::constexpr_switch<&detail::values_v, detail::case_call_t::value>( - std::forward(lambda), - value, - [&result] { return std::forward(result); }); -} - -template , typename Lambda> -constexpr auto enum_switch(Lambda&& lambda, string_view name, BinaryPredicate&& p = {}) -> detail::enable_if_t { - static_assert(std::is_invocable_r_v, "magic_enum::enum_switch requires bool(char, char) invocable predicate."); - using D = std::decay_t; - - if (const auto v = enum_cast(name, std::forward(p))) { - return enum_switch(std::forward(lambda), *v); - } - return detail::default_result_type_lambda(); -} - -template , typename Lambda> -constexpr auto enum_switch(Lambda&& lambda, string_view name, Result&& result, BinaryPredicate&& p = {}) -> detail::enable_if_t { - static_assert(std::is_invocable_r_v, "magic_enum::enum_switch requires bool(char, char) invocable predicate."); - using D = std::decay_t; - - if (const auto v = enum_cast(name, std::forward(p))) { - return enum_switch(std::forward(lambda), *v, std::forward(result)); - } - return std::forward(result); -} - -template -constexpr auto enum_switch(Lambda&& lambda, underlying_type_t value) -> detail::enable_if_t { - using D = std::decay_t; - - if (const auto v = enum_cast(value)) { - return enum_switch(std::forward(lambda), *v); - } - return detail::default_result_type_lambda(); -} - -template -constexpr auto enum_switch(Lambda&& lambda, underlying_type_t value, Result&& result) -> detail::enable_if_t { - using D = std::decay_t; - - if (const auto v = enum_cast(value)) { - return enum_switch(std::forward(lambda), *v, std::forward(result)); - } - return std::forward(result); -} - -template -constexpr auto enum_for_each(Lambda&& lambda) { - using D = std::decay_t; - static_assert(std::is_enum_v, "magic_enum::enum_for_each requires enum type."); - - return detail::for_each(std::forward(lambda), std::make_index_sequence>{}); -} - -namespace ostream_operators { - -template = 0> -std::basic_ostream& operator<<(std::basic_ostream& os, E value) { +// Checks whether enum contains value with such value. +template +[[nodiscard]] constexpr auto enum_contains(E value) noexcept -> detail::enable_if_t { using D = std::decay_t; using U = underlying_type_t; - if constexpr (detail::supported::value) { - if (const auto name = enum_flags_name(value); !name.empty()) { - for (const auto c : name) { - os.put(c); - } - return os; - } - } - return (os << static_cast(value)); + return static_cast(enum_cast(static_cast(value))); } -template = 0> -std::basic_ostream& operator<<(std::basic_ostream& os, optional value) { - return value ? (os << *value) : os; -} - -} // namespace magic_enum::ostream_operators - -namespace istream_operators { - -template = 0> -std::basic_istream& operator>>(std::basic_istream& is, E& value) { +// Checks whether enum contains value with such integer value. +template > +[[nodiscard]] constexpr auto enum_contains(underlying_type_t value) noexcept -> detail::enable_if_t { using D = std::decay_t; - std::basic_string s; - is >> s; - if (const auto v = enum_cast(s)) { - value = *v; - } else { - is.setstate(std::basic_ios::failbit); - } - return is; + return static_cast(enum_cast(value)); } -} // namespace magic_enum::istream_operators +// Checks whether enum contains enumerator with such name. +template , typename BinaryPredicate = std::equal_to<>> +[[nodiscard]] constexpr auto enum_contains(string_view value, BinaryPredicate p = {}) noexcept(detail::is_nothrow_invocable()) -> detail::enable_if_t { + using D = std::decay_t; -namespace iostream_operators { + return static_cast(enum_cast(value, std::move(p))); +} -using namespace ostream_operators; -using namespace istream_operators; +template +inline constexpr auto as_flags = AsFlags ? detail::enum_subtype::flags : detail::enum_subtype::common; -} // namespace magic_enum::iostream_operators +template +inline constexpr auto as_common = AsFlags ? detail::enum_subtype::common : detail::enum_subtype::flags; namespace bitwise_operators { @@ -1384,4 +1463,10 @@ constexpr E& operator^=(E& lhs, E rhs) noexcept { # pragma warning(pop) #endif +#undef MAGIC_ENUM_GET_ENUM_NAME_BUILTIN +#undef MAGIC_ENUM_GET_TYPE_NAME_BUILTIN +#undef MAGIC_ENUM_VS_2017_WORKAROUND +#undef MAGIC_ENUM_ARRAY_CONSTEXPR +#undef MAGIC_ENUM_FOR_EACH_256 + #endif // NEARGYE_MAGIC_ENUM_HPP diff --git a/server/driver/video_encoder.cpp b/server/driver/video_encoder.cpp index af61a143..5f985683 100644 --- a/server/driver/video_encoder.cpp +++ b/server/driver/video_encoder.cpp @@ -112,10 +112,11 @@ void VideoEncoder::Encode(wivrn_session & cnx, } -void VideoEncoder::SendData(std::vector && data) +void VideoEncoder::SendData(std::vector && data_) { auto & max_payload_size = to_headset::video_stream_data_shard::max_payload_size; std::lock_guard lock(mutex); + data = std::move(data_); #if 0 std::ofstream debug("/tmp/video_dump-" + std::to_string(stream_idx), std::ios::app); debug.write((char*)data.data(), data.size()); @@ -138,7 +139,6 @@ void VideoEncoder::SendData(std::vector && data) begin = next; flags = 0; } - shard.data = std::move(data); } void VideoEncoder::PushShard(const std::span & payload, uint8_t flags) diff --git a/server/driver/video_encoder.h b/server/driver/video_encoder.h index f64bfbae..8b0d1d30 100644 --- a/server/driver/video_encoder.h +++ b/server/driver/video_encoder.h @@ -52,6 +52,8 @@ private: // shard waiting to be sent to_headset::video_stream_data_shard shard; + // and its data + std::vector data; public: static std::unique_ptr Create(vk_bundle * vk, diff --git a/server/driver/wivrn_connection.cpp b/server/driver/wivrn_connection.cpp index bbed8ae9..80f6fb24 100644 --- a/server/driver/wivrn_connection.cpp +++ b/server/driver/wivrn_connection.cpp @@ -47,7 +47,7 @@ wivrn_connection::wivrn_connection(TCP && tcp) : stream.bind(port); - control.send(to_headset::control_packets(to_headset::handshake{})); + control.send(to_headset::handshake{}); while (true) { @@ -98,16 +98,6 @@ wivrn_connection::wivrn_connection(TCP && tcp) : } } -void wivrn_connection::send_control(const to_headset::control_packets & packet) -{ - control.send(packet); -} - -void wivrn_connection::send_stream(const to_headset::stream_packets & packet) -{ - stream.send(packet); -} - std::optional wivrn_connection::poll_control(int timeout) { pollfd fds{}; diff --git a/server/driver/wivrn_connection.h b/server/driver/wivrn_connection.h index 245a5a2e..c8fe0564 100644 --- a/server/driver/wivrn_connection.h +++ b/server/driver/wivrn_connection.h @@ -36,8 +36,16 @@ public: wivrn_connection(const wivrn_connection &) = delete; wivrn_connection & operator=(const wivrn_connection &) = delete; - void send_control(const to_headset::control_packets & packet); - void send_stream(const to_headset::stream_packets & packet); + template + void send_control(T && packet) + { + control.send(std::forward(packet)); + } + template + void send_stream(T && packet) + { + stream.send(std::forward(packet)); + } std::optional poll_control(int timeout); diff --git a/server/driver/wivrn_session.h b/server/driver/wivrn_session.h index 6be15b33..e0e16198 100644 --- a/server/driver/wivrn_session.h +++ b/server/driver/wivrn_session.h @@ -82,15 +82,15 @@ public: void operator()(audio_data &&); template - void send_stream(const T & packet) + void send_stream(T && packet) { - connection.send_stream(packet); + connection.send_stream(std::forward(packet)); } template - void send_control(const T & packet) + void send_control(T && packet) { - connection.send_control(packet); + connection.send_control(std::forward(packet)); } std::array get_foveation_parameters();