Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: std::format support for dpp::snowflake #1203

Merged
merged 31 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
61eb600
std::format support for dpp::snowflake
ruslan-ilesik Jul 18, 2024
9bdb1fd
fixed padding in tests
ruslan-ilesik Jul 18, 2024
15ada2e
spelling fix
ruslan-ilesik Jul 18, 2024
2aca0ad
fixing test not running fail on older compilers and theoretical fix f…
ruslan-ilesik Jul 18, 2024
0e31ace
one more fix of conditions for formatting
ruslan-ilesik Jul 18, 2024
eb743d5
one more fix of conditions for formatting
ruslan-ilesik Jul 18, 2024
0659f8b
one more fix of conditions for formatting
ruslan-ilesik Jul 18, 2024
115f1dd
one more fix of conditions for formatting
ruslan-ilesik Jul 18, 2024
89d5f60
Merge branch 'dev' into dev
braindigitalis Jul 18, 2024
1a70ef2
one more fix of conditions for formatting
ruslan-ilesik Jul 18, 2024
792e1d0
try to specify types to fix g++ compilation
ruslan-ilesik Jul 18, 2024
fd4bd76
clean addition cmake
ruslan-ilesik Jul 18, 2024
7a9d8e0
g++ compilation fix
ruslan-ilesik Jul 19, 2024
1d1c419
Merge branch 'dev' into dev
ruslan-ilesik Jul 19, 2024
e1dec28
introduced DPP_HAS_FORMAT definition
ruslan-ilesik Jul 19, 2024
fa7a831
cmake check for format
ruslan-ilesik Jul 19, 2024
bc364e1
cmake clear
ruslan-ilesik Jul 19, 2024
a07ee92
check for MSVC
ruslan-ilesik Jul 19, 2024
64cce72
check for MSVC
ruslan-ilesik Jul 19, 2024
ed54d29
fix cmake checks
ruslan-ilesik Jul 19, 2024
f535187
fix version check
ruslan-ilesik Jul 19, 2024
c3dd88d
cmake clean
ruslan-ilesik Jul 19, 2024
67de142
allow higher version of c++ then c++20
ruslan-ilesik Jul 19, 2024
ba4aff2
making requested changes
ruslan-ilesik Jul 19, 2024
22a3735
making requested changes
ruslan-ilesik Jul 19, 2024
51f7492
utilising cmake config file to make defenitions
ruslan-ilesik Jul 20, 2024
25977dc
fix new line
ruslan-ilesik Jul 21, 2024
c11da4f
add addtitional check for c++20 in autoconfig
ruslan-ilesik Jul 22, 2024
bc0766a
add addtitional check for c++20 in autoconfig
ruslan-ilesik Jul 22, 2024
d21fa02
Merge branch 'brainboxdotcc:dev' into dev
ruslan-ilesik Aug 2, 2024
bc01f2f
removed autconfig and adjusted dpp for formatters without it
ruslan-ilesik Aug 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,48 @@ else()
# that made no sense, it seems they may have changed their parsing rules somehow.
message("-- Using bundled nlohmann::json")
endif()


if(CMAKE_CXX_STANDARD GREATER_EQUAL 20 OR DPP_CORO)
include(CheckCXXSourceCompiles)
unset(HAS_FORMAT CACHE)
set(temp_flags "${CMAKE_REQUIRED_FLAGS}")
if (MSVC)
set(CMAKE_REQUIRED_FLAGS "/std:c++20")
else ()
set(CMAKE_REQUIRED_FLAGS "-std=c++20")
endif ()
check_cxx_source_compiles(
"
#include <format>
#include <string>

struct MyType {
int value;
std::string str() const { return std::to_string(value); }
};

template <>
struct std::formatter<MyType> : std::formatter<std::string> {
auto format(const MyType& item, std::format_context& ctx) const -> decltype(ctx.out()) {
return std::format_to(ctx.out(), \"{}\", item.str());
}
};

int main() {
MyType my{42};
auto s = std::format(\"{}\", my);
return 0;
}
"
HAS_FORMAT

)
set(CMAKE_REQUIRED_FLAGS ${temp_flags})
if(HAS_FORMAT)
message("-- INFO: Found std::format support")
target_compile_definitions(dpp PUBLIC DPP_HAS_FORMAT)
else ()
message("-- INFO: std::format support not found")
endif()
endif ()
ruslan-ilesik marked this conversation as resolved.
Show resolved Hide resolved
25 changes: 25 additions & 0 deletions include/dpp/snowflake.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
#include <cstdint>
#include <type_traits>

#ifdef DPP_HAS_FORMAT
#include <format>
#endif


ruslan-ilesik marked this conversation as resolved.
Show resolved Hide resolved
/**
* @brief The main namespace for D++ functions. classes and types
*/
Expand Down Expand Up @@ -281,3 +286,23 @@ struct std::hash<dpp::snowflake>
return std::hash<uint64_t>{}(s.value);
}
};

#ifdef DPP_HAS_FORMAT
/*
* @brief implementation of formater for dpp::snowflake for std::format support
* https://en.cppreference.com/w/cpp/utility/format/formatter
*/
template <>
struct std::formatter<dpp::snowflake>
{
template<class TP>
constexpr typename TP::iterator parse(TP& ctx) {
return ctx.begin();
}

template<class TF>
typename TF::iterator format(const dpp::snowflake& snowflake, TF& ctx) const {
return std::format_to(ctx.out(), "{}", snowflake.str());
}
};
#endif //DPP_HAS_FORMAT
ruslan-ilesik marked this conversation as resolved.
Show resolved Hide resolved
33 changes: 23 additions & 10 deletions src/unittest/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,19 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b
set_test(SNOWFLAKE, success);
}

{ // test snowflake: std::format support

#ifdef DPP_HAS_FORMAT
set_test(SNOWFLAKE_STD_FORMAT,
std::format("{}",dpp::snowflake{}) == "0" &&
std::format("{}",dpp::snowflake{12345}) == "12345" &&
std::format("{} hello {}", dpp::snowflake{12345}, dpp::snowflake{54321}) == "12345 hello 54321"
);
#else
set_status(SNOWFLAKE_STD_FORMAT,ts_skipped);
#endif // DPP_HAS_FORMAT
};

{ // test dpp::json_interface
start_test(JSON_INTERFACE);
struct fillable : dpp::json_interface<fillable> {
Expand Down Expand Up @@ -776,7 +789,7 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b
m5.get_url() == "" &&
m6.get_url() == "" &&
m7.get_url() == "" &&
m8.get_url() == ""
m8.get_url() == ""
);
}

Expand Down Expand Up @@ -886,31 +899,31 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b

set_test(UTILITY_USER_URL, false);
auto user_url = dpp::utility::user_url(123);
set_test(UTILITY_USER_URL,
user_url == dpp::utility::url_host + "/users/123" &&
set_test(UTILITY_USER_URL,
user_url == dpp::utility::url_host + "/users/123" &&
dpp::utility::user_url(0) == ""
);

set_test(UTILITY_MESSAGE_URL, false);
auto message_url = dpp::utility::message_url(1,2,3);
set_test(UTILITY_MESSAGE_URL,
message_url == dpp::utility::url_host+ "/channels/1/2/3" &&
message_url == dpp::utility::url_host+ "/channels/1/2/3" &&
dpp::utility::message_url(0,2,3) == "" &&
dpp::utility::message_url(1,0,3) == "" &&
dpp::utility::message_url(1,0,3) == "" &&
dpp::utility::message_url(1,2,0) == "" &&
dpp::utility::message_url(0,0,3) == "" &&
dpp::utility::message_url(0,2,0) == "" &&
dpp::utility::message_url(1,0,0) == "" &&
dpp::utility::message_url(0,0,0) == ""
dpp::utility::message_url(0,0,0) == ""
);

set_test(UTILITY_CHANNEL_URL, false);
auto channel_url = dpp::utility::channel_url(1,2);
set_test(UTILITY_CHANNEL_URL,
set_test(UTILITY_CHANNEL_URL,
channel_url == dpp::utility::url_host+ "/channels/1/2" &&
dpp::utility::channel_url(0,2) == "" &&
dpp::utility::channel_url(1,0) == "" &&
dpp::utility::channel_url(0,0) == ""
dpp::utility::channel_url(0,0) == ""
);

set_test(UTILITY_THREAD_URL, false);
Expand All @@ -919,7 +932,7 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b
thread_url == dpp::utility::url_host+ "/channels/1/2" &&
dpp::utility::thread_url(0,2) == "" &&
dpp::utility::thread_url(1,0) == "" &&
dpp::utility::thread_url(0,0) == ""
dpp::utility::thread_url(0,0) == ""
);
}

Expand Down Expand Up @@ -1648,7 +1661,7 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b
set_test(TIMESTAMP, false);
}
} else {
set_test(MESSAGESGET, false);
set_test(MESSAGESGET, false);
}
} else {
set_test(MESSAGESGET, false);
Expand Down
2 changes: 2 additions & 0 deletions src/unittest/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ DPP_TEST(CORO_EVENT_HANDLER, "coro: online event handler", tf_online | tf_coro);
DPP_TEST(CORO_API_CALLS, "coro: online api calls", tf_online | tf_coro);
DPP_TEST(CORO_MUMBO_JUMBO, "coro: online mumbo jumbo in event handler", tf_online | tf_coro | tf_extended);

DPP_TEST(SNOWFLAKE_STD_FORMAT, "snowflake: std::format support", tf_offline);

void coro_offline_tests();
void coro_online_tests(dpp::cluster *bot);

Expand Down
Loading