From 634428758b3647eed01bedce4e8376f97cd65df6 Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Mon, 10 Aug 2020 04:55:04 -0300 Subject: [PATCH] Fix build errors on gcc --- src/stream.h | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/src/stream.h b/src/stream.h index 265d6ef..cbea1d4 100644 --- a/src/stream.h +++ b/src/stream.h @@ -15,6 +15,10 @@ #include #include +#ifndef __cpp_lib_bit_cast +#include +#endif + #include #include "common_types.h" @@ -24,8 +28,8 @@ namespace Sirit { class Declarations; struct OpId { - spv::Op opcode; - Id result_type; + spv::Op opcode{}; + Id result_type{}; }; struct EndOp {}; @@ -38,7 +42,7 @@ inline void InsertStringView(std::vector& words, size_t& insert_index, std::string_view string) { const size_t size = string.size(); const auto read = [string, size](size_t offset) { - return offset < size ? static_cast(string[offset]) : u8(0); + return offset < size ? static_cast(string[offset]) : 0u; }; for (size_t i = 0; i < size; i += sizeof(u32)) { @@ -106,11 +110,25 @@ public: } Stream& operator<<(float value) { +#ifdef __cpp_lib_bit_cast return *this << std::bit_cast(value); +#else + static_assert(sizeof(float) == sizeof(u32)); + u32 int_value; + std::memcpy(&int_value, &value, sizeof(int_value)); + return *this << int_value; +#endif } Stream& operator<<(double value) { +#ifdef __cpp_lib_bit_cast return *this << std::bit_cast(value); +#else + static_assert(sizeof(double) == sizeof(u64)); + u64 int_value; + std::memcpy(&int_value, &value, sizeof(int_value)); + return *this << int_value; +#endif } Stream& operator<<(bool value) { @@ -131,6 +149,10 @@ public: return *this; } + Stream& operator<<(const char* string) { + return *this << std::string_view{string}; + } + template requires std::is_enum_v Stream& operator<<(T value) { static_assert(sizeof(T) == sizeof(u32)); @@ -188,7 +210,7 @@ public: } Id operator<<(EndOp) { - const auto begin = stream.words.begin(); + const auto begin = stream.words.data(); std::vector declarations(begin + stream.op_index, begin + stream.insert_index); // Normalize result id for lookups