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: Coro is now default enabled. #1368

Merged
merged 10 commits into from
Feb 7, 2025
3 changes: 2 additions & 1 deletion .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@
"chrono",
"ciphersuite",
"rmap",
"WSAPOLLFD"
"WSAPOLLFD",
"DDPP"
],
"flagWords": [
"hte"
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ docs/doxygen_sqlite3.db
build
buildtools/composer.phar
src/build
cmake-build-debug
cmake-build-*
docpages/example_code/build

# tests
Expand Down
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ option(DPP_BUILD_TEST "Build the test program" ON)
option(DPP_NO_VCPKG "No VCPKG" OFF)
option(DPP_NO_CONAN "No Conan" OFF)
option(CONAN_EXPORTED "Exported via Conan - DO NOT SET MANUALLY" OFF)
option(DPP_CORO "Support for C++20 coroutines" OFF)
option(DPP_NO_CORO "Remove Support for C++20 coroutines" OFF)
option(DPP_FORMATTERS "Support for C++20 formatters" OFF)
option(DPP_USE_EXTERNAL_JSON "Use an external installation of nlohmann::json" OFF)
option(DPP_USE_PCH "Use precompiled headers to speed up compilation" OFF)
Expand Down Expand Up @@ -60,9 +60,9 @@ if (DPP_TEST_VCPKG)
message("-- ${Red}DEVELOPER WARNING${ColourReset}: Running in ${Red}VCPKG test mode${ColourReset}: EMULATING A VCPKG BUILD WITHOUT VCPKG")
else()
if (DPP_NO_VCPKG)
message("-- INFO: Explicitly disabling VCPKG as running inside the CI action.")
message("${ColourReset}-- INFO: Explicitly disabling VCPKG as running inside the CI action.")
else()
message("-- INFO: Using VCPKG if detected")
message("${ColourReset}-- INFO: Using VCPKG if detected")
endif()
endif()

Expand Down
3 changes: 1 addition & 2 deletions Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -1936,8 +1936,7 @@ INCLUDE_FILE_PATTERNS =

#SHOW_HEADERFILE = NO

PREDEFINED = _DOXYGEN_ \
DPP_CORO
PREDEFINED = _DOXYGEN_

# If the MACRO_EXPANSION and EXPAND_ONLY_PREDEF tags are set to YES then this
# tag can be used to specify a list of macro names that should be expanded. The
Expand Down
2 changes: 1 addition & 1 deletion buildtools/classes/Generator/CoroGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function generateHeaderStart(): string
public function generateCppStart(): string
{
return $this->generateHeaderStart() . <<<EOT
#ifdef DPP_CORO
#ifndef DPP_NO_CORO

#include <dpp/export.h>
#include <dpp/snowflake.h>
Expand Down
6 changes: 3 additions & 3 deletions docpages/01_frequently_asked_questions.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ For a very small bot, you can get the memory usage as low as **6 megabytes** on

## How do I use this library in Windows?

The easiest way is to use our [template project](https://github.com/brainboxdotcc/windows-bot-template). If you are unable to do this, download the precompiled latest release from our GitHub releases, and take the DLLs, `.lib` file, and header files (`bin`, `lib` and `include` directories), placing them in a easily accessible place on your computer. Go into Visual Studio project settings in a new project, and point the project directories (notably the library directories and and include directories) at the correct locations. Add the `include` folder you extracted to your include directories, and add `dpp.lib` to your library directories. Ensure the project is set to C++17 standard or later in the settings. You should then be able to compile example programs within that project. When you run the program you have compiled you must ensure that all the dll files from the `bin` directory exist in the same directory as your executable.
The easiest way is to use our [template project](https://github.com/brainboxdotcc/windows-bot-template). If you are unable to do this, download the precompiled latest release from our GitHub releases, and take the DLLs, `.lib` file, and header files (`bin`, `lib` and `include` directories), placing them in a easily accessible place on your computer. Go into Visual Studio project settings in a new project, and point the project directories (notably the library directories and and include directories) at the correct locations. Add the `include` folder you extracted to your include directories, and add `dpp.lib` to your library directories. Ensure the project is set to C++20 standard or later in the settings (If you're using VS2019, you need to use C++17). You should then be able to compile example programs within that project. When you run the program you have compiled you must ensure that all the dll files from the `bin` directory exist in the same directory as your executable.

## Does this library support Visual Studio 2022?

Expand Down Expand Up @@ -44,7 +44,7 @@ If you don't understand something then feel free to ask in the [Discord server](

## Do I need to be an expert in C++ to use this library?

NO! Definitely not! We have tried to keep things as simple as possible. We only use language features where they make sense, not just because they exist. Take a look at the example program (`test.cpp` and you'll see just how simple it is to get up and running quickly). We use a small subset of C++17 and C++14 features.
NO! Definitely not! We have tried to keep things as simple as possible. We only use language features where they make sense, not just because they exist. Take a look at the example program (`test.cpp` and you'll see just how simple it is to get up and running quickly). We use a small subset of C++20, C++17, and C++14 features.

## Why is D++ also called DPP

Expand Down Expand Up @@ -88,7 +88,7 @@ Yes! D++ supports Discord threads. You can create, edit and delete threads and a

## Does D++ require C++20 support?

No, the library only requires C++17. We have some optional features such as \ref using-coroutines "coroutines" that do require C++20, but they are disabled by default.
By default, Yes. However, you can specify `-DDPP_NO_CORO` to make the library use C++17 features. You can read more about it at \ref using-coroutines "coroutines".

## When I start my bot I get an error: "error while loading shared libraries: libdpp.so: cannot open shared object file: No such file or directory"

Expand Down
2 changes: 1 addition & 1 deletion docpages/advanced_reference/coding_style_standards.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ Do not introduce platform specific (e.g. Windows only) code or libc functions. I

## C++ Version

The code must work with the C++17 standard, unless, for an optional feature that can be enabled and that uses a more recent standard (e.g. Coroutines).
The code must work with the C++17 standard, however, you can use C++20 as long as it's wrapped in a DPP_NO_CORO (but you should NOT make non-coro code wrapped in this just for C++20 functionality!), or, for an optional feature that can be enabled. This may be subject to change as we move closer to the C++20 standard.

## Select the Right Size Type for Numeric Types

Expand Down
4 changes: 2 additions & 2 deletions docpages/building/freebsd.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ Then once the build is complete, run `sudo make install` to install to the locat
Once installed, you can make use of the library in standalone programs simply by including it and linking to it:

```bash
clang++ -std=c++17 -L/usr/local/lib -I/usr/local/include -ldpp bot.cpp -o dppbot
clang++ -std=c++20 -L/usr/local/lib -I/usr/local/include -ldpp bot.cpp -o dppbot
```

The important flags in this command-line are:

* `-std=c++17` - Required to compile the headers
* `-std=c++20` - Required to compile the headers
* `-L/usr/local/lib` - Required to tell the linker where libdpp is located.
* `-I/usr/local/include` - Required to tell the linker where dpp headers are located.
* `-ldpp` - Link to `libdpp.so`.
Expand Down
4 changes: 2 additions & 2 deletions docpages/building/linux.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ Then once the build is complete, run `make install` to install to the location y
Once installed to the `/usr/local` directory, you can make use of the library in standalone programs simply by including it and linking to it:

```bash
g++ -std=c++17 mydppbot.cpp -o dppbot -ldpp
g++ -std=c++20 mydppbot.cpp -o dppbot -ldpp
```

The important flags in this command-line are:

* `-std=c++17` - Required to compile the headers
* `-std=c++20` - Required to compile the headers
* `-ldpp` - Link to libdpp.so
* `mydppbot.cpp` - Your source code
* `dppbot` - The name of the executable to make
Expand Down
4 changes: 2 additions & 2 deletions docpages/building/osx.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ Then once the build is complete, run `sudo make install` to install to the locat
Once installed, you can make use of the library in standalone programs simply by including it and linking to it:

```bash
clang++ -std=c++17 -ldpp mydppbot.cpp -o dppbot
clang++ -std=c++20 -ldpp mydppbot.cpp -o dppbot
```

The important flags in this command-line are:

* `-std=c++17` - Required to compile the headers
* `-std=c++20` - Required to compile the headers
* `-ldpp` - Link to libdpp.dylib
* `mydppbot.cpp` - Your source code
* `dppbot` - The name of the executable to make
Expand Down
2 changes: 1 addition & 1 deletion docpages/example_code/eval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ int main() {
*/
double compile_start = dpp::utility::time_f();
dpp::utility::exec("g++", {
"-std=c++17",
"-std=c++20",
"-shared", /* Build the output as a .so file */
"-fPIC",
std::string("-o") + std::to_string(event.msg.author.id) + "_" + std::to_string(event.msg.id) + ".so",
Expand Down
2 changes: 1 addition & 1 deletion docpages/example_programs/misc/eval.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ This is the main body of the example program.
To compile this program you must link against `libdl`. It is also critically important to include the `-rdynamic` flag. For example:

```
g++ -std=c++17 -rdynamic -oeval eval.cpp -ldpp -ldl
g++ -std=c++20 -rdynamic -oeval eval.cpp -ldpp -ldl
```

## Example usage
Expand Down
2 changes: 1 addition & 1 deletion docpages/example_programs/music_and_audio/mp3.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ To stream MP3 files via D++ you need to link an additional dependency to your bo
To compile this program you must remember to specify `libmpg123` alongside `libdpp` in the build command, for example:

```bash
g++ -std=c++17 -o musictest musictest.cpp -lmpg123 -ldpp
g++ -std=c++20 -o musictest musictest.cpp -lmpg123 -ldpp
```

\include{doc} install_prebuilt_footer.dox
2 changes: 1 addition & 1 deletion docpages/example_programs/the_basics/firstbot.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ The parameter which we set to false indicates if the function should return once

## 7. Compile and run your bot

Compile your bot using `g++ -std=c++17 -o bot bot.cpp -ldpp` (if your .cpp file is called `bot.cpp`) and run it with `./bot`.
Compile your bot using `g++ -std=c++20 -o bot bot.cpp -ldpp` (if your .cpp file is called `bot.cpp`) and run it with `./bot`.

## 8. Inviting your bot to your server

Expand Down
12 changes: 8 additions & 4 deletions docpages/example_programs/using_coroutines.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ One of the most anticipated features of C++20 is the addition of coroutines: in
* \subpage awaiting-events
* \subpage expiring-buttons

Coroutines are currently disabled by default; to use them you will need to build D++ \ref install-from-source "from source" and use the option `-DDPP_CORO=on` in your CMake command.
Your application also needs to enable C++20 and define DPP_CORO, by using:
- `-std=c++20 -DDPP_CORO` in your build command if building manually, or
- if using CMake, add `target_compile_definitions(my_program PUBLIC DPP_CORO)` and `target_compile_features(my_program PUBLIC cxx_std_20)`.
Coroutines require you to use C++20. You can do this by adding
- `-std=c++20` in your build command (before specifying files) if building manually, or,
- if using CMake, by adding `target_compile_features(my_program PUBLIC cxx_std_20)` in your `CMakeLists.txt`.

If you don't want to use Coroutines, You can either add
- `-DDPP_NO_CORO` in your build command, or, if using CMake,
- `target_compile_definitions(my_program PUBLIC DPP_NO_CORO)`.
- Additionally, you can build D++ without Coroutines with the same above.
2 changes: 1 addition & 1 deletion docpages/include/coro_warn.dox
Original file line number Diff line number Diff line change
@@ -1 +1 @@
\warning D++ Coroutines are a very new feature and are currently only supported by D++ on g++ 13, clang/LLVM 14, and MSVC 19.37 or above. Additionally, D++ must be built with the CMake option DPP_CORO, and your program must both define the macro DPP_CORO and use C++20 or above.
\warning D++ Coroutines are currently only supported by D++ on g++ 13, clang/LLVM 14, and MSVC 19.37 or above. Additionally, your program has to support C++20.
2 changes: 1 addition & 1 deletion docpages/install/install-brew.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ If it can't detect libdpp, please do `brew update` and repeat the steps above.
You will now be able to use D++ by including its library on the command line:

```bash
clang++ -std=c++17 -L/opt/homebrew/lib -I/opt/homebrew/include -ldpp mybot.cpp -o mybot
clang++ -std=c++20 -L/opt/homebrew/lib -I/opt/homebrew/include -ldpp mybot.cpp -o mybot
```

\include{doc} install_prebuilt_footer.dox
Expand Down
2 changes: 1 addition & 1 deletion docpages/install/install-windows-vs-zip.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ To add D++ to a Visual Studio project, using **Visual Studio 2019** or **Visual
10. Double check at this point that all the directories are filled in correctly. They should look generally like the ones in the screenshot below:
\image html zip_vsproj_9.png

11. Go to the general section in the same window now, and look for the drop down list labelled "C++ Language Standard". Make sure the selected option is **C++17 Standard (/std:c++17)** or later.
11. Go to the general section in the same window now, and look for the drop down list labelled "C++ Language Standard". If you're on VS2022, make sure the selected option is **C++20 Standard (/std:c++20)** or later. If you're on VS2019, make sure the selected option is **C++17 Standard (/std:c++17)** or later.
\image html zip_vsproj_10.png

12. Again within the same window, go to the input section, under the linker category, and add '**dpp.lib;**' to the start of the libraries to include, as shown below:
Expand Down
4 changes: 2 additions & 2 deletions docpages/make_a_bot/meson.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ Add the following line in the executable section of your `meson.build` file.
dependencies: [dpp]
```

Change the `cpp_std` value in the `project()` to `c++17`. Your `meson.build` should look like this:
Change the `cpp_std` value in the `project()` to `c++20`. Your `meson.build` should look like this:

your meson.build should look like this.
~~~~~~~~~~~~~~yml
project('discord-bot', 'cpp',
version : '0.1',
default_options : ['warning_level=3',
'cpp_std=c++17'])
'cpp_std=c++20'])

dpp = dependency('dpp')

Expand Down
2 changes: 1 addition & 1 deletion docpages/make_a_bot/replit.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ dpkg -x libdpp.deb .
```
3. Compile your bot, note that you should be sure to include the `pthread` library explicitly and reference the extracted dpp installation you just put into the repl:
```bash
g++ -o bot main.cpp -ldpp -lpthread -L./usr/lib -I./usr/include -std=c++17
g++ -o bot main.cpp -ldpp -lpthread -L./usr/lib -I./usr/include -std=c++20
```
4. Run your bot! Note that you will need to set `LD_PRELOAD` to reference `libdpp.so` as it will be located in `$HOME` and not `/usr/lib`:
```bash
Expand Down
2 changes: 2 additions & 0 deletions docpages/make_a_bot/windows_vs.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ If you prefer a video tutorial, you can watch the video below! Otherwise, scroll

## Text Tutorial

\note If you wish to use Coroutines, you must use VS 2022. If wish to use VS 2019, you must compile DPP with `DPP_NO_CORO.

1. Make sure you have Visual Studio 2019 or 2022. Community, Professional or Enterprise work fine. These instructions are not for Visual Studio Code. You can [download the correct version here](https://visualstudio.microsoft.com/downloads/). Note that older versions of Visual Studio will not work as they do not support enough of the C++17 standard.
2. Clone the [template project](https://github.com/brainboxdotcc/windows-bot-template/). Be sure to clone the entire project and not just copy and paste the `.cpp` file.
3. Double click on the `MyBot.sln` file in the folder you just cloned:
Expand Down
2 changes: 1 addition & 1 deletion docpages/make_a_bot/windows_wsl.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ This tutorial teaches you how to create a lightweight environment for D++ develo
7. Create a new directory, inside your home directory, using `mkdir MyBot`. Then, you want to open that directory using `cd MyBot`.
8. Now that you've a directory to work in, type `touch mybot.cxx` to create a file you can work in!
9. Now, head on over to Visual Studio Code. Press `CTRL+SHIFT+P` and type `Remote-WSL: New WSL Window` (You don't have to type all of it, it will auto-suggest it!). This will bring up a new window. In the new window, choose `open folder` and choose the directory you've created prior (It should be within your home directory). Press OK and now you have your Folder opened as a Workspace!
10. Add code to your CXX file (We suggest using the \ref firstbot "first bot page" if this is your first time!) and compile it by running `g++ -std=c++17 *.cxx -o bot -ldpp` in the same folder as your cxx file. This will create a "bot" file!
10. Add code to your CXX file (We suggest using the \ref firstbot "first bot page" if this is your first time!) and compile it by running `g++ -std=c++20 *.cxx -o bot -ldpp` in the same folder as your cxx file. This will create a "bot" file!
11. You can now start your bot by typing `./bot`!

If everything was done right, you should be able to see your bot working!
10 changes: 5 additions & 5 deletions include/dpp/cluster.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class DPP_EXPORT cluster {
*/
using slashcommand_handler_t = std::function<void(const slashcommand_t &)>;

#ifdef DPP_CORO
#ifndef DPP_NO_CORO
/**
* @brief Typedef for coroutines based slashcommand handler type
*/
Expand Down Expand Up @@ -445,7 +445,7 @@ class DPP_EXPORT cluster {
*/
timer start_timer(timer_callback_t on_tick, uint64_t frequency, timer_callback_t on_stop = {});

#ifdef DPP_CORO
#ifndef DPP_NO_CORO
/**
* @brief Start a coroutine timer. Every `frequency` seconds, the callback is called.
*
Expand Down Expand Up @@ -481,7 +481,7 @@ class DPP_EXPORT cluster {
*/
bool stop_timer(timer t);

#ifdef DPP_CORO
#ifndef DPP_NO_CORO
/**
* @brief Get an awaitable to wait a certain amount of seconds. Use the co_await keyword on its return value to suspend the coroutine until the timer ends
*
Expand Down Expand Up @@ -576,7 +576,7 @@ class DPP_EXPORT cluster {
*/
size_t active_requests();

#ifdef DPP_CORO
#ifndef DPP_NO_CORO
/**
* @brief Register a coroutine-based slash command handler.
*
Expand Down Expand Up @@ -4071,7 +4071,7 @@ class DPP_EXPORT cluster {
*/
void channel_set_voice_status(snowflake channel_id, const std::string& status, command_completion_event_t callback = utility::log_error());

#ifdef DPP_CORO
#ifndef DPP_NO_CORO
#include <dpp/cluster_coro_calls.h>
#endif

Expand Down
4 changes: 2 additions & 2 deletions include/dpp/coro/async.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct async_dummy : awaitable_dummy {

}

#ifdef DPP_CORO
#ifndef DPP_NO_CORO

#include "coro.h"

Expand Down Expand Up @@ -186,4 +186,4 @@ DPP_CHECK_ABI_COMPAT(async<>, async_dummy);

}

#endif /* DPP_CORO */
#endif /* DPP_NO_CORO */
4 changes: 2 additions & 2 deletions include/dpp/coro/awaitable.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct awaitable_dummy {

}

#ifdef DPP_CORO
#ifndef DPP_NO_CORO

#include <dpp/coro/coro.h>

Expand Down Expand Up @@ -732,4 +732,4 @@ void spawn_sync_wait_job(auto* awaitable, std::condition_variable &cv, auto&& re

}

#endif /* DPP_CORO */
#endif /* DPP_NO_CORO */
4 changes: 2 additions & 2 deletions include/dpp/coro/coro.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
************************************************************************************/

#pragma once
#ifdef DPP_CORO
#ifndef DPP_NO_CORO

#if (defined(_LIBCPP_VERSION) and !defined(__cpp_impl_coroutine)) // if libc++ experimental implementation (LLVM < 14)
# define STDCORO_EXPERIMENTAL_HEADER
Expand Down Expand Up @@ -198,5 +198,5 @@ inline int coro_alloc_count = 0;

} // namespace dpp

#endif /* DPP_CORO */
#endif /* DPP_NO_CORO */

4 changes: 2 additions & 2 deletions include/dpp/coro/coroutine.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct coroutine_dummy {

}

#ifdef DPP_CORO
#ifndef DPP_NO_CORO

#include <dpp/coro/coro.h>
#include <dpp/coro/awaitable.h>
Expand Down Expand Up @@ -403,4 +403,4 @@ struct dpp::detail::std_coroutine::coroutine_traits<dpp::coroutine<R>, Args...>
using promise_type = dpp::detail::coroutine::promise_t<R>;
};

#endif /* DPP_CORO */
#endif /* DPP_NO_CORO */
4 changes: 2 additions & 2 deletions include/dpp/coro/job.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ struct job_dummy {

}

#ifdef DPP_CORO
#ifndef DPP_NO_CORO

#include "coro.h"

Expand Down Expand Up @@ -142,4 +142,4 @@ struct dpp::detail::std_coroutine::coroutine_traits<dpp::job, Args...> {
using promise_type = dpp::detail::job::promise<Args...>;
};

#endif /* DPP_CORO */
#endif /* DPP_NO_CORO */
4 changes: 2 additions & 2 deletions include/dpp/coro/task.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct task_dummy : awaitable_dummy {

}

#ifdef DPP_CORO
#ifndef DPP_NO_CORO

#include <dpp/coro/coro.h>

Expand Down Expand Up @@ -443,4 +443,4 @@ struct dpp::detail::std_coroutine::coroutine_traits<dpp::task<T>, Args...> {
using promise_type = dpp::detail::task::promise_t<T>;
};

#endif /* DPP_CORO */
#endif /* DPP_NO_CORO */
Loading
Loading