Skip to content

Commit

Permalink
[struct_pack][fix] fix warning
Browse files Browse the repository at this point in the history
  • Loading branch information
poor-circle committed Aug 13, 2024
1 parent 8fec9b1 commit 7959ac7
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 58 deletions.
24 changes: 14 additions & 10 deletions cmake/build.cmake
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
message(STATUS "-------------YLT COMPILE SETTING------------")

# CPP Standard
foreach(i ${CMAKE_CXX_COMPILE_FEATURES})
if (i STREQUAL cxx_std_20)
set(has_cxx_std_20 TRUE)
endif()
endforeach()
if (has_cxx_std_20)
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++20" COMPILER_SUPPORTS_CXX20)
if (NOT COMPILER_SUPPORTS_CXX20)
CHECK_CXX_COMPILER_FLAG("-std=c++17" COMPILER_SUPPORTS_CXX17)
endif()
if (COMPILER_SUPPORTS_CXX20)
option(ENABLE_CPP_20 "Enable CPP 20" ON)
else ()
elseif(COMPILER_SUPPORTS_CXX17)
option(ENABLE_CPP_20 "Enable CPP 20" OFF)
else()
message(FATAL_ERROR "Compiler ${CMAKE_CXX_COMPILER} has no C++17 support.")
endif()
if (ENABLE_CPP_20)
set(CMAKE_CXX_STANDARD 20)
Expand Down Expand Up @@ -39,14 +41,16 @@ else()
message(STATUS "ENDIAN: LITTLE")
endif()

# force use lld if your compiler is clang

# When using coro_rpc_client to send request, only remote function declarations are required.
# In the examples, RPC function declaration and definition are divided.
# However, clang + ld(gold linker) + '-g' will report 'undefined reference to xxx'.
# So you can use lld when compiler is clang by this code:

if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
add_link_options(-fuse-ld=lld)
check_lld(has_lld)
if (has_lld)
add_link_options(-fuse-ld=lld)
endif()
endif()

# ccache
Expand Down
15 changes: 15 additions & 0 deletions cmake/utils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ int main()
{
return 0;
}
]====]
${_RESULT}
)
unset(CMAKE_REQUIRED_FLAGS)
endmacro()

macro(check_lld _RESULT)
include(CheckCXXSourceRuns)
set(CMAKE_REQUIRED_FLAGS "-fuse-ld=lld")
check_cxx_source_runs(
[====[
int main()
{
return 0;
}
]====]
${_RESULT}
)
Expand Down
3 changes: 2 additions & 1 deletion include/ylt/standalone/iguana/define.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
#define IGUANA__INLINE_LAMBDA constexpr __attribute__((always_inline))
#endif

#if __has_cpp_attribute(likely) && __has_cpp_attribute(unlikely)
#if __has_cpp_attribute(likely) && __has_cpp_attribute(unlikely) && \
__cplusplus >= 202002L
#define IGUANA_LIKELY [[likely]]
#define IGUANA_UNLIKELY [[unlikely]]
#else
Expand Down
6 changes: 3 additions & 3 deletions include/ylt/struct_pack/calculate_size.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,15 +423,15 @@ get_serialize_runtime_info(const Args &...args) {
ret.len_ += sz_info.total;
}
else {
if SP_LIKELY (sz_info.max_size < (int64_t{1} << 8)) {
if SP_LIKELY (sz_info.max_size < (uint64_t{1} << 8)) {
ret.len_ += sz_info.total + sz_info.size_cnt;
}
else {
if (sz_info.max_size < (int64_t{1} << 16)) {
if (sz_info.max_size < (uint64_t{1} << 16)) {
ret.len_ += sz_info.total + sz_info.size_cnt * 2;
ret.metainfo_ = 0b01000;
}
else if (sz_info.max_size < (int64_t{1} << 32)) {
else if (sz_info.max_size < (uint64_t{1} << 32)) {
ret.len_ += sz_info.total + sz_info.size_cnt * 4;
ret.metainfo_ = 0b10000;
}
Expand Down
3 changes: 2 additions & 1 deletion include/ylt/struct_pack/marco.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
#define STRUCT_PACK_MAY_INLINE inline
#endif

#if __has_cpp_attribute(likely) && __has_cpp_attribute(unlikely)
#if __has_cpp_attribute(likely) && __has_cpp_attribute(unlikely) && \
__cplusplus >= 202002L
#define SP_LIKELY(expr) (expr) [[likely]]
#define SP_UNLIKELY(expr) (expr) [[unlikely]]
#elif __GNUC__
Expand Down
5 changes: 2 additions & 3 deletions include/ylt/struct_pack/packer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,6 @@ class packer {
if constexpr (trivially_copyable_container<type> &&
is_little_endian_copyable<sizeof(
typename type::value_type)>) {
using value_type = typename type::value_type;
write_bytes_array(writer_, (char *)item.data(),
item.size() * sizeof(typename type::value_type));
return;
Expand Down Expand Up @@ -441,13 +440,13 @@ class packer {
item, [this](auto &&...items) CONSTEXPR_INLINE_LAMBDA {
constexpr uint64_t tag =
get_parent_tag<type>(); // to pass msvc with c++17
serialize_fast_varint<tag>(items...);
this->serialize_fast_varint<tag>(items...);
});
}
visit_members(item, [this](auto &&...items) CONSTEXPR_INLINE_LAMBDA {
constexpr uint64_t tag =
get_parent_tag<type>(); // to pass msvc with c++17
serialize_many<size_type, version, tag>(items...);
this->serialize_many<size_type, version, tag>(items...);
});
}
}
Expand Down
32 changes: 11 additions & 21 deletions include/ylt/struct_pack/unpacker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,24 @@ struct memory_reader {
constexpr memory_reader(const char *beg, const char *end) noexcept
: now(beg), end(end) {}
bool read(char *target, size_t len) {
if SP_UNLIKELY (end - now < len) {
if SP_UNLIKELY (static_cast<size_t>(end - now) < len) {
return false;
}
memcpy(target, now, len);
now += len;
return true;
}
bool check(size_t len) { return end - now >= len; }
bool check(size_t len) { return static_cast<size_t>(end - now) >= len; }
const char *read_view(size_t len) {
if SP_UNLIKELY (end - now < len) {
if SP_UNLIKELY (static_cast<size_t>(end - now) < len) {
return nullptr;
}
auto ret = now;
now += len;
return ret;
}
bool ignore(size_t len) {
if SP_UNLIKELY (end - now < len) {
if SP_UNLIKELY (static_cast<size_t>(end - now) < len) {
return false;
}
now += len;
Expand Down Expand Up @@ -482,10 +482,7 @@ class unpacker {

STRUCT_PACK_INLINE std::pair<struct_pack::err_code, std::uint64_t>
deserialize_compatible(unsigned compatible_sz_len) {
constexpr std::size_t sz[] = {0, 2, 4, 8};
auto len_sz = sz[compatible_sz_len];
std::size_t data_len = 0;
bool result;
switch (compatible_sz_len) {
case 1:
if SP_LIKELY (low_bytes_read_wrapper<2>(reader_, data_len)) {
Expand Down Expand Up @@ -564,7 +561,7 @@ class unpacker {
}
else {
if constexpr (is_MD5_reader_wrapper<Reader>) {
reader_.read_head((char *)&current_types_code);
current_types_code = reader_.read_head();
if SP_LIKELY (current_types_code % 2 == 0) // unexist metainfo
{
size_type_ = 0;
Expand Down Expand Up @@ -876,7 +873,6 @@ class unpacker {
}
else if constexpr (container<type>) {
std::size_t size = 0;
bool result{};
if constexpr (size_type == 1) {
if SP_UNLIKELY (!low_bytes_read_wrapper<size_type>(reader_, size)) {
return struct_pack::errc::no_buffer_space;
Expand Down Expand Up @@ -909,7 +905,7 @@ class unpacker {
}
}
else {
std::uint64_t sz;
std::uint64_t sz{};
if SP_UNLIKELY (!low_bytes_read_wrapper<size_type>(reader_,
sz)) {
return struct_pack::errc::no_buffer_space;
Expand Down Expand Up @@ -1017,7 +1013,7 @@ class unpacker {
return errc::no_buffer_space;
}
}
std::size_t mem_sz = size * sizeof(value_type);
[[maybe_unused]] std::size_t mem_sz = size * sizeof(value_type);
if constexpr (NotSkip) {
if constexpr (string_view<type> || dynamic_span<type>) {
static_assert(
Expand All @@ -1037,7 +1033,7 @@ class unpacker {
}
resize(item, size);
if constexpr (is_little_endian_copyable<sizeof(value_type)>) {
auto ec =
[[maybe_unused]] auto ec =
read_bytes_array(reader_, (char *)item.data(), mem_sz);
assert(ec == true);
}
Expand Down Expand Up @@ -1218,7 +1214,7 @@ class unpacker {
item, [this](auto &&...items) CONSTEXPR_INLINE_LAMBDA {
constexpr uint64_t tag =
get_parent_tag<type>(); // to pass msvc with c++17
return deserialize_fast_varint<tag, NotSkip>(items...);
return this->deserialize_fast_varint<tag, NotSkip>(items...);
});
if SP_UNLIKELY (code) {
return code;
Expand All @@ -1228,7 +1224,7 @@ class unpacker {
item, [this](auto &&...items) CONSTEXPR_INLINE_LAMBDA {
constexpr uint64_t tag =
get_parent_tag<type>(); // to pass msvc with c++17
return deserialize_many<size_type, version, NotSkip, tag>(
return this->deserialize_many<size_type, version, NotSkip, tag>(
items...);
});
}
Expand Down Expand Up @@ -1433,13 +1429,7 @@ struct MD5_reader_wrapper : public Reader {
is_failed =
!read_wrapper<sizeof(head_chars)>(*(Reader *)this, (char *)&head_chars);
}
bool read_head(char *target) {
if SP_UNLIKELY (is_failed) {
return false;
}
memcpy(target, &head_chars, sizeof(head_chars));
return true;
}
uint32_t read_head() { return head_chars; }
Reader &&release_reader() { return std::move(*(Reader *)this); }
bool is_failed;
uint32_t get_md5() { return head_chars & 0xFFFFFFFE; }
Expand Down
1 change: 0 additions & 1 deletion include/ylt/struct_pack/user_helper.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once
#include <cstdint>
#include <span>

#include "ylt/struct_pack/calculate_size.hpp"
#include "ylt/struct_pack/packer.hpp"
Expand Down
13 changes: 1 addition & 12 deletions include/ylt/struct_pack/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,6 @@ inline constexpr std::string_view type_string() {
#endif
}

#if __cpp_concepts >= 201907L
constexpr bool is_string_reserve_shrink = requires { std::string{}.reserve(); };
#else
constexpr bool is_string_reserve_shrink = true;
#endif

template <typename T>
using remove_cvref_t = std::remove_cv_t<std::remove_reference_t<T>>;

Expand Down Expand Up @@ -201,12 +195,7 @@ inline void resize(std::basic_string<ch> &raw_str, std::size_t sz) {
raw_str.resize(sz);
#elif defined(__GLIBCXX__) || defined(_LIBCPP_VERSION) || \
defined(_MSVC_STL_VERSION)
if constexpr (is_string_reserve_shrink) {
if (sz > raw_str.capacity()) {
raw_str.reserve(sz);
}
}
else {
if (sz > raw_str.capacity()) {
raw_str.reserve(sz);
}
std::string &str = *reinterpret_cast<std::string *>(&raw_str);
Expand Down
1 change: 1 addition & 0 deletions src/struct_pack/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ else()
# include_directories(include)
# include_directories(include/ylt/thirdparty)
endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
add_executable(struct_pack_example basic_usage.cpp non_aggregated_type.cpp serialize_config.cpp user_defined_serialization.cpp derived_class.cpp main.cpp)
5 changes: 3 additions & 2 deletions src/struct_pack/examples/basic_usage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ void basic_usage() {
{
auto buffer = struct_pack::serialize_with_offset(/* offset = */ 2, p);
auto buffer2 = struct_pack::serialize(p);
bool result = std::string_view{buffer.data() + 2, buffer.size() - 2} ==
std::string_view{buffer2.data(), buffer2.size()};
[[maybe_unused]] bool result =
std::string_view{buffer.data() + 2, buffer.size() - 2} ==
std::string_view{buffer2.data(), buffer2.size()};
assert(result);
}
// api 6. serialize varadic param
Expand Down
4 changes: 2 additions & 2 deletions src/struct_pack/examples/derived_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void virtual_base_class_example(void) {
struct_pack::deserialize<std::vector<std::unique_ptr<base>>>(ret);
assert(result.has_value()); // check deserialize ok
assert(result->size() == 3); // check vector size
for (int i = 0; i < result->size(); ++i) {
for (size_t i = 0; i < result->size(); ++i) {
assert(checker[i] == result.value()[i]->hello()); // check type
}
}
Expand Down Expand Up @@ -123,7 +123,7 @@ void base_class_example(void) {
struct_pack::deserialize<std::vector<std::unique_ptr<base>>>(ret);
assert(result.has_value());
assert(result->size() == 4); // check vector size
for (int i = 0; i < result->size(); ++i) {
for (size_t i = 0; i < result->size(); ++i) {
assert(checker[i] == result.value()[i]->hello()); // check type
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/struct_pack/examples/serialize_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void serialize_config() {
// only need 4 bytes
assert(buffer.size() == 4);
// deserialize with config
auto result =
[[maybe_unused]] auto result =
struct_pack::deserialize<struct_pack::DISABLE_ALL_META_INFO, rect>(
buffer);
assert(result.value() == r);
Expand All @@ -75,6 +75,6 @@ void serialize_config_by_ADL() {
// only need 4 bytes
assert(buffer.size() == 4);
// deserialize with config
auto result = struct_pack::deserialize<rect>(buffer);
[[maybe_unused]] auto result = struct_pack::deserialize<rect>(buffer);
assert(result.value() == r);
}

0 comments on commit 7959ac7

Please sign in to comment.