Skip to content

Commit e91eaa8

Browse files
Add C++ modules support and update nlohmann::json
1 parent 56d87ca commit e91eaa8

File tree

21 files changed

+5703
-3078
lines changed

21 files changed

+5703
-3078
lines changed

CMakeLists.txt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
cmake_minimum_required (VERSION 3.16)
2020
set(CMAKE_CXX_STANDARD 17)
21-
21+
2222
option(BUILD_SHARED_LIBS "Build shared libraries" ON)
2323
option(BUILD_VOICE_SUPPORT "Build voice support" ON)
2424
option(RUN_LDCONFIG "Run ldconfig after installation" ON)
@@ -33,6 +33,7 @@ option(DPP_USE_EXTERNAL_JSON "Use an external installation of nlohmann::json" OF
3333
option(DPP_USE_PCH "Use precompiled headers to speed up compilation" OFF)
3434
option(AVX_TYPE "Force AVX type for speeding up audio mixing" OFF)
3535
option(DPP_TEST_VCPKG "Force VCPKG build without VCPKG installed (for development use only!)" OFF)
36+
option(DPP_MODULES "Support for C++20 modules" OFF)
3637

3738
include(CheckCXXSymbolExists)
3839
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
@@ -159,3 +160,12 @@ endif()
159160
if (NOT WIN32)
160161
target_link_libraries(dpp PRIVATE std::filesystem)
161162
endif()
163+
164+
if (DPP_MODULES)
165+
add_subdirectory(src/modules)
166+
target_compile_definitions(dpp PUBLIC DPP_MODULES)
167+
message("-- C++20 Modules support: ${Green}ENABLED${ColourReset}")
168+
else()
169+
message("-- C++20 Modules support: ${Red}DISABLED${ColourReset} (enable with -DDPP_MODULES=ON)")
170+
endif()
171+

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ D++ is a lightweight and efficient library for **Discord** written in **modern C
3333
* Stable [Windows support](https://dpp.dev/buildwindows.html)
3434
* Ready-made compiled packages for Windows, Raspberry Pi (ARM64/ARM7/ARMv6), Debian x86/x64, and RPM based distributions
3535
* Highly scalable for large amounts of guilds and users
36+
* Support for C++ modules
3637

3738
Want to help? Drop me a line or send a PR.
3839

docpages/example_code/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ project(documentation_tests)
3333

3434
string(ASCII 27 Esc)
3535

36-
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DDPP_CORO -std=c++20 -pthread -O0 -fPIC -rdynamic -DFMT_HEADER_ONLY -Wall -Wextra -Wpedantic -Werror -Wno-unused-parameter -Wno-deprecated-declarations")
36+
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DDPP_CORO -DPP_MODULES -std=c++20 -pthread -O0 -fPIC -rdynamic -DFMT_HEADER_ONLY -Wall -Wextra -Wpedantic -Werror -Wno-unused-parameter -Wno-deprecated-declarations")
3737
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0")
3838

3939
file(GLOB example_list ./*.cpp)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* If import std; is supported, use it instead */
2+
#include <cstdlib>
3+
4+
import dpp;
5+
6+
using dpp::cluster;
7+
using dpp::slashcommand;
8+
using dpp::start_type;
9+
10+
int main() {
11+
cluster bot(std::getenv("BOT_TOKEN"));
12+
13+
bot.on_slashcommand([](auto event) {
14+
if (event.command.get_command_name() == "ping") {
15+
event.reply("Pong!");
16+
}
17+
});
18+
19+
bot.on_ready([&bot](auto event) {
20+
if (dpp::run_once<struct register_bot_commands>()) {
21+
bot.global_command_create(
22+
slashcommand("ping", "Ping pong!", bot.me.id)
23+
);
24+
}
25+
});
26+
27+
bot.start(start_type::st_wait);
28+
return 0;
29+
}

docpages/example_programs/misc.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ This section lists examples that do not fit neatly into any of the categories ab
1313
* \subpage setting_status
1414
* \subpage using-emojis
1515
* \subpage using_timers
16+
* \subpage using_modules
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
\page using_modules Using D++ with C++ modules
2+
3+
D++ is offered as a C++ module, which offers improved compile times over a traditional header.
4+
5+
In order to enable support, you should be using C++20+ with any module-supporting compiler. To activate the feature, pass the `DPP_MODULES` flag to CMake. Ensure that the generated build system supports modules (for CMake, this is usually Ninja).
6+
7+
Once this is done, simply `import dpp;` and we're good to go!
8+
9+
\include{cpp} using_modules.cpp

include/dpp/cluster.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ namespace dpp {
5858
* requests to Discord. This is useful for bots that do not need to receive websocket events as it will save a lot of
5959
* resources.
6060
*/
61-
constexpr uint32_t NO_SHARDS = ~0U;
61+
inline constexpr uint32_t NO_SHARDS = ~0U;
6262

6363
/**
6464
* @brief Types of startup for cluster::start()

include/dpp/colors.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace dpp {
3131
* @brief predefined color constants.
3232
*/
3333
namespace colors {
34-
static constexpr uint32_t
34+
inline constexpr uint32_t
3535
white = 0xFFFFFF,
3636
discord_white = 0xFFFFFE,
3737
light_gray = 0xC0C0C0,

include/dpp/discordclient.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class cluster;
5757
* @brief How many seconds to wait between (re)connections. DO NOT change this.
5858
* It is mandated by the Discord API spec!
5959
*/
60-
constexpr time_t RECONNECT_INTERVAL = 5;
60+
inline constexpr time_t RECONNECT_INTERVAL = 5;
6161

6262
/**
6363
* @brief Represents different event opcodes sent and received on a shard websocket

include/dpp/etf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ namespace dpp {
4242
/**
4343
* @brief Current ETF format version in use
4444
*/
45-
const uint8_t FORMAT_VERSION = 131;
45+
inline const uint8_t FORMAT_VERSION = 131;
4646

4747
/**
4848
* @brief Represents a token which identifies the type of value which follows it

0 commit comments

Comments
 (0)