Skip to content

Commit 5e5ee60

Browse files
feat: std::format support for dpp::snowflake (#1203)
2 parents 53be231 + bc01f2f commit 5e5ee60

File tree

5 files changed

+55
-11
lines changed

5 files changed

+55
-11
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ option(DPP_INSTALL "Generate the install target" ON)
2525
option(DPP_BUILD_TEST "Build the test program" ON)
2626
option(DPP_NO_VCPKG "No VCPKG" OFF)
2727
option(DPP_CORO "Experimental support for C++20 coroutines" OFF)
28+
option(DPP_FORMATTERS "Support for C++20 formatters" OFF)
2829
option(DPP_USE_EXTERNAL_JSON "Use an external installation of nlohmann::json" OFF)
2930
option(DPP_USE_PCH "Use precompiled headers to speed up compilation" OFF)
3031
option(AVX_TYPE "Force AVX type for speeding up audio mixing" OFF)

include/dpp/snowflake.h

+24
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
#include <cstdint>
2727
#include <type_traits>
2828

29+
#ifdef DPP_FORMATTERS
30+
#include <format>
31+
#endif
32+
2933
/**
3034
* @brief The main namespace for D++ functions. classes and types
3135
*/
@@ -281,3 +285,23 @@ struct std::hash<dpp::snowflake>
281285
return std::hash<uint64_t>{}(s.value);
282286
}
283287
};
288+
289+
#ifdef DPP_FORMATTERS
290+
/*
291+
* @brief implementation of formater for dpp::snowflake for std::format support
292+
* https://en.cppreference.com/w/cpp/utility/format/formatter
293+
*/
294+
template <>
295+
struct std::formatter<dpp::snowflake>
296+
{
297+
template<class TP>
298+
constexpr typename TP::iterator parse(TP& ctx) {
299+
return ctx.begin();
300+
}
301+
302+
template<class TF>
303+
typename TF::iterator format(const dpp::snowflake& snowflake, TF& ctx) const {
304+
return std::format_to(ctx.out(), "{}", snowflake.str());
305+
}
306+
};
307+
#endif //DPP_FORMATTERS

library/CMakeLists.txt

+5-1
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,10 @@ if(DPP_CORO)
341341
COMMAND php buildtools/make_struct.php "\\Dpp\\Generator\\CoroGenerator")
342342
endif()
343343

344+
if(DPP_FORMATTERS)
345+
target_compile_definitions(dpp PUBLIC DPP_FORMATTERS)
346+
endif()
347+
344348
if (DPP_BUILD_TEST)
345349
enable_testing(${CMAKE_CURRENT_SOURCE_DIR}/..)
346350
file(GLOB testnamelist "${CMAKE_CURRENT_SOURCE_DIR}/../src/*")
@@ -351,7 +355,7 @@ if (DPP_BUILD_TEST)
351355
set (testsrc "")
352356
file(GLOB testsrc "${modules_dir}/${testname}/*.cpp")
353357
add_executable(${testname} ${testsrc})
354-
if (DPP_CORO)
358+
if (DPP_CORO OR DPP_FORMATTERS)
355359
target_compile_features(${testname} PRIVATE cxx_std_20)
356360
else()
357361
target_compile_features(${testname} PRIVATE cxx_std_17)

src/unittest/test.cpp

+23-10
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,19 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b
379379
set_test(SNOWFLAKE, success);
380380
}
381381

382+
{ // test snowflake: std::format support
383+
384+
#ifdef DPP_FORMATTERS
385+
set_test(SNOWFLAKE_STD_FORMAT,
386+
std::format("{}",dpp::snowflake{}) == "0" &&
387+
std::format("{}",dpp::snowflake{12345}) == "12345" &&
388+
std::format("{} hello {}", dpp::snowflake{12345}, dpp::snowflake{54321}) == "12345 hello 54321"
389+
);
390+
#else
391+
set_status(SNOWFLAKE_STD_FORMAT,ts_skipped);
392+
#endif // DPP_FORMATTERS
393+
};
394+
382395
{ // test dpp::json_interface
383396
start_test(JSON_INTERFACE);
384397
struct fillable : dpp::json_interface<fillable> {
@@ -776,7 +789,7 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b
776789
m5.get_url() == "" &&
777790
m6.get_url() == "" &&
778791
m7.get_url() == "" &&
779-
m8.get_url() == ""
792+
m8.get_url() == ""
780793
);
781794
}
782795

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

887900
set_test(UTILITY_USER_URL, false);
888901
auto user_url = dpp::utility::user_url(123);
889-
set_test(UTILITY_USER_URL,
890-
user_url == dpp::utility::url_host + "/users/123" &&
902+
set_test(UTILITY_USER_URL,
903+
user_url == dpp::utility::url_host + "/users/123" &&
891904
dpp::utility::user_url(0) == ""
892905
);
893906

894907
set_test(UTILITY_MESSAGE_URL, false);
895908
auto message_url = dpp::utility::message_url(1,2,3);
896909
set_test(UTILITY_MESSAGE_URL,
897-
message_url == dpp::utility::url_host+ "/channels/1/2/3" &&
910+
message_url == dpp::utility::url_host+ "/channels/1/2/3" &&
898911
dpp::utility::message_url(0,2,3) == "" &&
899-
dpp::utility::message_url(1,0,3) == "" &&
912+
dpp::utility::message_url(1,0,3) == "" &&
900913
dpp::utility::message_url(1,2,0) == "" &&
901914
dpp::utility::message_url(0,0,3) == "" &&
902915
dpp::utility::message_url(0,2,0) == "" &&
903916
dpp::utility::message_url(1,0,0) == "" &&
904-
dpp::utility::message_url(0,0,0) == ""
917+
dpp::utility::message_url(0,0,0) == ""
905918
);
906919

907920
set_test(UTILITY_CHANNEL_URL, false);
908921
auto channel_url = dpp::utility::channel_url(1,2);
909-
set_test(UTILITY_CHANNEL_URL,
922+
set_test(UTILITY_CHANNEL_URL,
910923
channel_url == dpp::utility::url_host+ "/channels/1/2" &&
911924
dpp::utility::channel_url(0,2) == "" &&
912925
dpp::utility::channel_url(1,0) == "" &&
913-
dpp::utility::channel_url(0,0) == ""
926+
dpp::utility::channel_url(0,0) == ""
914927
);
915928

916929
set_test(UTILITY_THREAD_URL, false);
@@ -919,7 +932,7 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b
919932
thread_url == dpp::utility::url_host+ "/channels/1/2" &&
920933
dpp::utility::thread_url(0,2) == "" &&
921934
dpp::utility::thread_url(1,0) == "" &&
922-
dpp::utility::thread_url(0,0) == ""
935+
dpp::utility::thread_url(0,0) == ""
923936
);
924937
}
925938

@@ -1648,7 +1661,7 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b
16481661
set_test(TIMESTAMP, false);
16491662
}
16501663
} else {
1651-
set_test(MESSAGESGET, false);
1664+
set_test(MESSAGESGET, false);
16521665
}
16531666
} else {
16541667
set_test(MESSAGESGET, false);

src/unittest/test.h

+2
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,8 @@ DPP_TEST(CORO_EVENT_HANDLER, "coro: online event handler", tf_online | tf_coro);
264264
DPP_TEST(CORO_API_CALLS, "coro: online api calls", tf_online | tf_coro);
265265
DPP_TEST(CORO_MUMBO_JUMBO, "coro: online mumbo jumbo in event handler", tf_online | tf_coro | tf_extended);
266266

267+
DPP_TEST(SNOWFLAKE_STD_FORMAT, "snowflake: std::format support", tf_offline);
268+
267269
void coro_offline_tests();
268270
void coro_online_tests(dpp::cluster *bot);
269271

0 commit comments

Comments
 (0)