diff --git a/.cspell.json b/.cspell.json index 82c89a6eb2..7eaa3f7877 100644 --- a/.cspell.json +++ b/.cspell.json @@ -153,7 +153,8 @@ "chrono", "ciphersuite", "rmap", - "WSAPOLLFD" + "WSAPOLLFD", + "DDPP" ], "flagWords": [ "hte" diff --git a/.gitignore b/.gitignore index bbf269173b..3c4104c7d4 100644 --- a/.gitignore +++ b/.gitignore @@ -10,7 +10,7 @@ docs/doxygen_sqlite3.db build buildtools/composer.phar src/build -cmake-build-debug +cmake-build-* docpages/example_code/build # tests diff --git a/CMakeLists.txt b/CMakeLists.txt index 18b54f5d15..e493458016 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) @@ -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() diff --git a/Doxyfile b/Doxyfile index e0098049d0..5549327b56 100644 --- a/Doxyfile +++ b/Doxyfile @@ -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 diff --git a/buildtools/classes/Generator/CoroGenerator.php b/buildtools/classes/Generator/CoroGenerator.php index 1a68809a70..76fdc18fa6 100644 --- a/buildtools/classes/Generator/CoroGenerator.php +++ b/buildtools/classes/Generator/CoroGenerator.php @@ -54,7 +54,7 @@ public function generateHeaderStart(): string public function generateCppStart(): string { return $this->generateHeaderStart() . << #include diff --git a/docpages/01_frequently_asked_questions.md b/docpages/01_frequently_asked_questions.md index 62336d6ff9..0fae56ab44 100644 --- a/docpages/01_frequently_asked_questions.md +++ b/docpages/01_frequently_asked_questions.md @@ -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? @@ -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 @@ -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" diff --git a/docpages/advanced_reference/coding_style_standards.md b/docpages/advanced_reference/coding_style_standards.md index 6885a1d1f4..9f6fa17b2d 100644 --- a/docpages/advanced_reference/coding_style_standards.md +++ b/docpages/advanced_reference/coding_style_standards.md @@ -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 diff --git a/docpages/building/freebsd.md b/docpages/building/freebsd.md index b6ec50ed99..c71331d295 100644 --- a/docpages/building/freebsd.md +++ b/docpages/building/freebsd.md @@ -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`. diff --git a/docpages/building/linux.md b/docpages/building/linux.md index 9abad1ce41..50f47230a1 100644 --- a/docpages/building/linux.md +++ b/docpages/building/linux.md @@ -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 diff --git a/docpages/building/osx.md b/docpages/building/osx.md index 059c77fca0..8e0a32cca8 100644 --- a/docpages/building/osx.md +++ b/docpages/building/osx.md @@ -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 diff --git a/docpages/example_code/eval.cpp b/docpages/example_code/eval.cpp index 2c90503bfa..d2b7e0a719 100644 --- a/docpages/example_code/eval.cpp +++ b/docpages/example_code/eval.cpp @@ -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", diff --git a/docpages/example_programs/misc/eval.md b/docpages/example_programs/misc/eval.md index ec026214e3..63d774cd48 100644 --- a/docpages/example_programs/misc/eval.md +++ b/docpages/example_programs/misc/eval.md @@ -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 diff --git a/docpages/example_programs/music_and_audio/mp3.md b/docpages/example_programs/music_and_audio/mp3.md index b316a0cd68..9eaca9a480 100644 --- a/docpages/example_programs/music_and_audio/mp3.md +++ b/docpages/example_programs/music_and_audio/mp3.md @@ -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 diff --git a/docpages/example_programs/the_basics/firstbot.md b/docpages/example_programs/the_basics/firstbot.md index c0b4296a49..c1639ced18 100644 --- a/docpages/example_programs/the_basics/firstbot.md +++ b/docpages/example_programs/the_basics/firstbot.md @@ -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 diff --git a/docpages/example_programs/using_coroutines.md b/docpages/example_programs/using_coroutines.md index 8e239c3f3e..9b2481c145 100644 --- a/docpages/example_programs/using_coroutines.md +++ b/docpages/example_programs/using_coroutines.md @@ -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. diff --git a/docpages/include/coro_warn.dox b/docpages/include/coro_warn.dox index 5710006205..beeb5ad1c7 100644 --- a/docpages/include/coro_warn.dox +++ b/docpages/include/coro_warn.dox @@ -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. diff --git a/docpages/install/install-brew.md b/docpages/install/install-brew.md index 770e007c44..52bcfc6591 100644 --- a/docpages/install/install-brew.md +++ b/docpages/install/install-brew.md @@ -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 diff --git a/docpages/install/install-windows-vs-zip.md b/docpages/install/install-windows-vs-zip.md index 068a9c58ff..294f9ec92a 100644 --- a/docpages/install/install-windows-vs-zip.md +++ b/docpages/install/install-windows-vs-zip.md @@ -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: diff --git a/docpages/make_a_bot/meson.md b/docpages/make_a_bot/meson.md index 29b93a1afc..8a2a9e0839 100644 --- a/docpages/make_a_bot/meson.md +++ b/docpages/make_a_bot/meson.md @@ -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') diff --git a/docpages/make_a_bot/replit.md b/docpages/make_a_bot/replit.md index feb0e7ea82..e631730cbe 100644 --- a/docpages/make_a_bot/replit.md +++ b/docpages/make_a_bot/replit.md @@ -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 diff --git a/docpages/make_a_bot/windows_vs.md b/docpages/make_a_bot/windows_vs.md index 3376945fe1..e2466ea784 100644 --- a/docpages/make_a_bot/windows_vs.md +++ b/docpages/make_a_bot/windows_vs.md @@ -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: diff --git a/docpages/make_a_bot/windows_wsl.md b/docpages/make_a_bot/windows_wsl.md index e09ea7400e..5f5bf1d046 100644 --- a/docpages/make_a_bot/windows_wsl.md +++ b/docpages/make_a_bot/windows_wsl.md @@ -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! diff --git a/include/dpp/cluster.h b/include/dpp/cluster.h index 283d622a05..35fd8f817a 100644 --- a/include/dpp/cluster.h +++ b/include/dpp/cluster.h @@ -157,7 +157,7 @@ class DPP_EXPORT cluster { */ using slashcommand_handler_t = std::function; -#ifdef DPP_CORO +#ifndef DPP_NO_CORO /** * @brief Typedef for coroutines based slashcommand handler type */ @@ -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. * @@ -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 * @@ -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. * @@ -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 #endif diff --git a/include/dpp/coro/async.h b/include/dpp/coro/async.h index 2feac38d8a..6f42259738 100644 --- a/include/dpp/coro/async.h +++ b/include/dpp/coro/async.h @@ -32,7 +32,7 @@ struct async_dummy : awaitable_dummy { } -#ifdef DPP_CORO +#ifndef DPP_NO_CORO #include "coro.h" @@ -186,4 +186,4 @@ DPP_CHECK_ABI_COMPAT(async<>, async_dummy); } -#endif /* DPP_CORO */ +#endif /* DPP_NO_CORO */ diff --git a/include/dpp/coro/awaitable.h b/include/dpp/coro/awaitable.h index f27a39f4e5..4bc8cddfce 100644 --- a/include/dpp/coro/awaitable.h +++ b/include/dpp/coro/awaitable.h @@ -33,7 +33,7 @@ struct awaitable_dummy { } -#ifdef DPP_CORO +#ifndef DPP_NO_CORO #include @@ -732,4 +732,4 @@ void spawn_sync_wait_job(auto* awaitable, std::condition_variable &cv, auto&& re } -#endif /* DPP_CORO */ +#endif /* DPP_NO_CORO */ diff --git a/include/dpp/coro/coro.h b/include/dpp/coro/coro.h index 34827debdd..1592a02bd8 100644 --- a/include/dpp/coro/coro.h +++ b/include/dpp/coro/coro.h @@ -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 @@ -198,5 +198,5 @@ inline int coro_alloc_count = 0; } // namespace dpp -#endif /* DPP_CORO */ +#endif /* DPP_NO_CORO */ diff --git a/include/dpp/coro/coroutine.h b/include/dpp/coro/coroutine.h index 648ac2b79b..a2f541c2a4 100644 --- a/include/dpp/coro/coroutine.h +++ b/include/dpp/coro/coroutine.h @@ -30,7 +30,7 @@ struct coroutine_dummy { } -#ifdef DPP_CORO +#ifndef DPP_NO_CORO #include #include @@ -403,4 +403,4 @@ struct dpp::detail::std_coroutine::coroutine_traits, Args...> using promise_type = dpp::detail::coroutine::promise_t; }; -#endif /* DPP_CORO */ +#endif /* DPP_NO_CORO */ diff --git a/include/dpp/coro/job.h b/include/dpp/coro/job.h index ed6ab2518c..4b3ef50680 100644 --- a/include/dpp/coro/job.h +++ b/include/dpp/coro/job.h @@ -29,7 +29,7 @@ struct job_dummy { } -#ifdef DPP_CORO +#ifndef DPP_NO_CORO #include "coro.h" @@ -142,4 +142,4 @@ struct dpp::detail::std_coroutine::coroutine_traits { using promise_type = dpp::detail::job::promise; }; -#endif /* DPP_CORO */ +#endif /* DPP_NO_CORO */ diff --git a/include/dpp/coro/task.h b/include/dpp/coro/task.h index d57bc8ce7f..0477231892 100644 --- a/include/dpp/coro/task.h +++ b/include/dpp/coro/task.h @@ -31,7 +31,7 @@ struct task_dummy : awaitable_dummy { } -#ifdef DPP_CORO +#ifndef DPP_NO_CORO #include @@ -443,4 +443,4 @@ struct dpp::detail::std_coroutine::coroutine_traits, Args...> { using promise_type = dpp::detail::task::promise_t; }; -#endif /* DPP_CORO */ +#endif /* DPP_NO_CORO */ diff --git a/include/dpp/coro/when_any.h b/include/dpp/coro/when_any.h index 1e3fd5a359..7c3c311003 100644 --- a/include/dpp/coro/when_any.h +++ b/include/dpp/coro/when_any.h @@ -19,7 +19,7 @@ * ************************************************************************************/ -#ifdef DPP_CORO +#ifndef DPP_NO_CORO #pragma once #include "coro.h" diff --git a/include/dpp/dispatcher.h b/include/dpp/dispatcher.h index 6ea8989b33..915b9d1c79 100644 --- a/include/dpp/dispatcher.h +++ b/include/dpp/dispatcher.h @@ -50,9 +50,9 @@ #include #include -#ifdef DPP_CORO +#ifndef DPP_NO_CORO #include -#endif /* DPP_CORO */ +#endif /* DPP_NO_CORO */ namespace dpp { @@ -582,7 +582,7 @@ struct DPP_EXPORT interaction_create_t : public event_dispatch_t { */ void delete_original_response(command_completion_event_t callback = utility::log_error()) const; -#ifdef DPP_CORO +#ifndef DPP_NO_CORO /** * @brief Acknowledge interaction without displaying a message to the user, * for use with button and select menu components. @@ -680,7 +680,7 @@ struct DPP_EXPORT interaction_create_t : public event_dispatch_t { * On success the result will contain a dpp::confirmation object in confirmation_callback_t::value. On failure, the value is undefined and confirmation_callback_t::is_error() method will return true. You can obtain full error details with confirmation_callback_t::get_error(). */ dpp::async co_delete_original_response() const; -#endif /* DPP_CORO */ +#endif /* DPP_NO_CORO */ /** * @brief command interaction @@ -1696,7 +1696,7 @@ struct DPP_EXPORT message_create_t : public event_dispatch_t { */ void reply(message&& msg, bool mention_replied_user = false, command_completion_event_t callback = utility::log_error()) const; -#ifdef DPP_CORO +#ifndef DPP_NO_CORO /** * @brief Send a text to the same channel as the channel_id in received event. * @@ -1747,7 +1747,7 @@ struct DPP_EXPORT message_create_t : public event_dispatch_t { * On success the result will contain a dpp::confirmation object in confirmation_callback_t::value. On failure, the value is undefined and confirmation_callback_t::is_error() method will return true. You can obtain full error details with confirmation_callback_t::get_error(). */ dpp::async co_reply(message&& msg, bool mention_replied_user = false) const; -#endif /* DPP_CORO */ +#endif /* DPP_NO_CORO */ }; /** diff --git a/include/dpp/event_router.h b/include/dpp/event_router.h index 7a352f0453..656badfb45 100644 --- a/include/dpp/event_router.h +++ b/include/dpp/event_router.h @@ -39,7 +39,7 @@ namespace dpp { -#ifdef DPP_CORO +#ifndef DPP_NO_CORO template class event_router_t; @@ -212,7 +212,7 @@ template class event_router_t { */ using event_handler_abi_t = std::variant>; -#ifdef DPP_CORO +#ifndef DPP_NO_CORO friend class detail::event_router::awaitable; /** @brief dpp::task coro event handler */ @@ -247,7 +247,7 @@ template class event_router_t { */ std::map dispatch_container; -#ifdef DPP_CORO +#ifndef DPP_NO_CORO /** * @brief Mutex for messing with coro_awaiters. */ @@ -311,7 +311,7 @@ template class event_router_t { }; } -#ifdef DPP_CORO +#ifndef DPP_NO_CORO /** * @brief Handle an event as a coroutine, ensuring the lifetime of the event object. */ @@ -425,7 +425,7 @@ template class event_router_t { * This will be caught in this destructor, however, make sure no other exceptions are thrown in the coroutine after that or it will terminate. */ ~event_router_t() { -#ifdef DPP_CORO +#ifndef DPP_NO_CORO while (!coro_awaiters.empty()) { // cancel all awaiters. here we cannot do the usual loop as we'd need to lock coro_mutex, and cancel() locks and modifies coro_awaiters try { @@ -449,7 +449,7 @@ template class event_router_t { * @param event Class to pass as parameter to all listeners. */ void call(const T& event) const { -#ifdef DPP_CORO +#ifndef DPP_NO_CORO handle_coro(event); #else handle(event); @@ -463,14 +463,14 @@ template class event_router_t { * @param event Class to pass as parameter to all listeners. */ void call(T&& event) const { -#ifdef DPP_CORO +#ifndef DPP_NO_CORO handle_coro(std::move(event)); #else handle(std::move(event)); #endif }; -#ifdef DPP_CORO +#ifndef DPP_NO_CORO /** * @brief Obtain an awaitable object that refers to an event with a certain condition. * It can be co_await-ed to wait for the next event that satisfies this condition. @@ -542,7 +542,7 @@ template class event_router_t { * @retval false if there are some listeners */ [[nodiscard]] bool empty() const { -#ifdef DPP_CORO +#ifndef DPP_NO_CORO std::shared_lock lock{mutex}; std::shared_lock coro_lock{coro_mutex}; @@ -569,7 +569,7 @@ template class event_router_t { /** * @brief Attach a callable to the event, adding a listener. * The callable should either be of the form `void(const T&)` or - * `dpp::task(const T&)` (the latter requires DPP_CORO to be defined), + * `dpp::task(const T&)`, * where T is the event type for this event router. * * This has the exact same behavior as using \ref attach(F&&) "attach". @@ -585,7 +585,7 @@ template class event_router_t { /** * @brief Attach a callable to the event, adding a listener. * The callable should either be of the form `void(const T&)` or - * `dpp::task(const T&)` (the latter requires DPP_CORO to be defined), + * `dpp::task(const T&)`, * where T is the event type for this event router. * * @param fun Callable to attach to event @@ -595,7 +595,7 @@ template class event_router_t { template [[maybe_unused]] event_handle attach(F&& fun); #else /* not _DOXYGEN_ */ -# ifdef DPP_CORO +# ifndef DPP_NO_CORO /** * @brief Attach a callable to the event, adding a listener. * The callable should either be of the form `void(const T&)` or @@ -681,7 +681,7 @@ template class event_router_t { dispatch_container.emplace(h, std::forward(fun)); return h; } -# endif /* DPP_CORO */ +# endif /* DPP_NO_CORO */ #endif /* _DOXYGEN_ */ /** * @brief Detach a listener from the event using a previously obtained ID. @@ -698,7 +698,7 @@ template class event_router_t { } }; -#ifdef DPP_CORO +#ifndef DPP_NO_CORO namespace detail::event_router { diff --git a/include/dpp/exception.h b/include/dpp/exception.h index 9d6dd2d69d..c0d81a31fd 100644 --- a/include/dpp/exception.h +++ b/include/dpp/exception.h @@ -579,13 +579,13 @@ class exception : public std::exception * @note This is a stub for documentation purposes. For full information on supported methods please see dpp::exception. */ class invalid_token_exception : public dpp::rest_exception { }; -#ifdef DPP_CORO +#ifndef DPP_NO_CORO /** * @brief Represents the cancellation of a task. Will be thrown to the awaiter of a cancelled task. * @note This is a stub for documentation purposes. For full information on supported methods please see dpp::exception. */ class task_cancelled_exception : public dpp::exception { }; -#endif /* DPP_CORO */ +#endif /* DPP_NO_CORO */ #else derived_exception(logic_exception, exception); derived_exception(file_exception, exception); @@ -598,9 +598,9 @@ class exception : public std::exception derived_exception(length_exception, exception); derived_exception(parse_exception, exception); derived_exception(cache_exception, exception); -# ifdef DPP_CORO +# ifndef DPP_NO_CORO derived_exception(task_cancelled_exception, exception); -# endif /* DPP_CORO */ +# endif /* DPP_NO_CORO */ #endif } diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt index 0293afa631..8f0abd8def 100644 --- a/library/CMakeLists.txt +++ b/library/CMakeLists.txt @@ -129,7 +129,7 @@ if (UNIX) ) set_source_files_properties( "${CMAKE_CURRENT_SOURCE_DIR}/../include/dpp/unicode_emojis.h" PROPERTIES GENERATED TRUE ) - if (DPP_CORO) + if (NOT DPP_NO_CORO) execute_process( WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/.." COMMAND php buildtools/make_struct.php "\\Dpp\\Generator\\CoroGenerator" @@ -368,12 +368,18 @@ if(HAVE_PTHREAD_SETNAME_NP) target_compile_definitions(dpp PRIVATE HAVE_PTHREAD_SETNAME_NP) endif() -if(DPP_CORO) - message("-- ${Yellow}Enabled coroutine feature${ColourReset}") +if(NOT DPP_NO_CORO) + message("-- Attempting to enable coroutines feature") set(CMAKE_CXX_STANDARD 20) target_compile_features(dpp PUBLIC cxx_std_20) if(WIN32 AND NOT MINGW AND NOT DPP_CLANG_CL) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /await:strict") # https://learn.microsoft.com/en-us/cpp/build/reference/await-enable-coroutine-support?view=msvc-170 + if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC") + if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS_EQUAL 19.29.30158) # Taken from 2019 actions, as they seemingly fail to compile. + message("${BoldRed}Coroutines with MSVC (Visual Studio) require VS 2022 (Compiler Ver: 19.29.30158) or above. Forcing coroutines off.${ColourReset}") + set(DPP_NO_CORO ON) + endif() + endif() else() if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 14.0.0) # clang >= 14 has native support @@ -395,7 +401,8 @@ if(DPP_CORO) " IS_GLIBCXX) if(IS_GLIBCXX) if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 12.0.0) - message(FATAL_ERROR "${BoldRed}Clang with stdc++ and coroutines requires version 12.0.0 or above${ColourReset}") + message("${BoldRed}Clang with stdc++ and coroutines requires version 12.0.0 or above. Forcing coroutines off.${ColourReset}") + set(DPP_NO_CORO ON) endif() message("-- ${Yellow}Detected stdc++ - enabling mock std::experimental namespace${ColourReset}") target_compile_definitions(dpp PUBLIC "STDCORO_GLIBCXX_COMPAT") @@ -409,14 +416,21 @@ if(DPP_CORO) endif() elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU") if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 10.0) - message(FATAL_ERROR "${BoldRed}Coroutines with g++ require version 10 or above${ColourReset}") + message("${BoldRed}Coroutines with g++ require version 10 or above. Forcing coroutines off.${ColourReset}") + set(DPP_NO_CORO ON) elseif(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 11.0) message("-- ${Yellow}Note - coroutines in g++10 are experimental, upgrading to g++11 or above is recommended${ColourReset}") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fcoroutines") endif() endif() endif() - target_compile_definitions(dpp PUBLIC DPP_CORO) +endif() + +if(DPP_NO_CORO) + message("-- ${Yellow}Coroutines are disabled.${ColourReset}") + target_compile_definitions(dpp PUBLIC DPP_NO_CORO) +else() + message("-- ${Green}Coroutines are enabled!${ColourReset}") endif() if(DPP_FORMATTERS) @@ -452,7 +466,7 @@ if (DPP_BUILD_TEST) set (testsrc "") file(GLOB testsrc "${modules_dir}/${testname}/*.cpp") add_executable(${testname} ${testsrc}) - if (DPP_CORO OR DPP_FORMATTERS) + if ((NOT DPP_NO_CORO) OR DPP_FORMATTERS) target_compile_features(${testname} PRIVATE cxx_std_20) else() target_compile_features(${testname} PRIVATE cxx_std_17) diff --git a/src/coro_loop/coro_loop.cpp b/src/coro_loop/coro_loop.cpp index 159c801cf3..231741acc2 100644 --- a/src/coro_loop/coro_loop.cpp +++ b/src/coro_loop/coro_loop.cpp @@ -42,7 +42,7 @@ int64_t rss() { return proc_self_value("VmRSS:") * 1024; } -#ifdef DPP_CORO +#ifndef DPP_NO_CORO dpp::task test(dpp::cluster& cluster) { [[maybe_unused]] int test[102400]{}; test[60] = 5; @@ -51,7 +51,7 @@ dpp::task test(dpp::cluster& cluster) { #endif int main() { -#ifdef DPP_CORO +#ifndef DPP_NO_CORO char* t = getenv("DPP_UNIT_TEST_TOKEN"); if (t) { dpp::cluster coro_cluster(t, dpp::i_guilds, 1, 0, 1, true, dpp::cache_policy::cpol_none); diff --git a/src/dpp/cluster.cpp b/src/dpp/cluster.cpp index b569a3a918..dfea3d8c41 100644 --- a/src/dpp/cluster.cpp +++ b/src/dpp/cluster.cpp @@ -121,7 +121,7 @@ cluster::cluster(const std::string &_token, uint32_t _intents, uint32_t _shards, ); /* Add slashcommand callback for named commands. */ -#ifdef DPP_CORO +#ifndef DPP_NO_CORO on_slashcommand([this](const slashcommand_t& event) -> task { slashcommand_handler_variant copy; { diff --git a/src/dpp/cluster/timer.cpp b/src/dpp/cluster/timer.cpp index e7bbfea2c2..1844fd20d5 100644 --- a/src/dpp/cluster/timer.cpp +++ b/src/dpp/cluster/timer.cpp @@ -100,7 +100,7 @@ void cluster::tick_timers() { } while (true); } -#ifdef DPP_CORO +#ifndef DPP_NO_CORO async cluster::co_sleep(uint64_t seconds) { return async{[this, seconds] (auto &&cb) mutable { start_timer([this, cb] (dpp::timer handle) { diff --git a/src/dpp/cluster_coro_calls.cpp b/src/dpp/cluster_coro_calls.cpp index d04ad63c99..99840b7cdd 100644 --- a/src/dpp/cluster_coro_calls.cpp +++ b/src/dpp/cluster_coro_calls.cpp @@ -26,7 +26,7 @@ * * To re-generate this header file re-run the script! */ -#ifdef DPP_CORO +#ifndef DPP_NO_CORO #include #include diff --git a/src/dpp/dispatcher.cpp b/src/dpp/dispatcher.cpp index 986bc134c2..391105e55b 100644 --- a/src/dpp/dispatcher.cpp +++ b/src/dpp/dispatcher.cpp @@ -111,7 +111,7 @@ void message_create_t::reply(message&& msg, bool mention_replied_user, command_c owner->message_create(std::move(msg), std::move(callback)); } -#ifdef DPP_CORO +#ifndef DPP_NO_CORO async message_create_t::co_send(const std::string& m) const { return dpp::async{[&, this] (T&& cb) { this->send(m, std::forward(cb)); }}; } @@ -135,7 +135,7 @@ async message_create_t::co_reply(const message& msg, bo async message_create_t::co_reply(message&& msg, bool mention_replied_user) const { return dpp::async{[&, this] (T&& cb) { this->reply(std::move(msg), mention_replied_user, std::forward(cb)); }}; } -#endif /* DPP_CORO */ +#endif /* DPP_NO_CORO */ void interaction_create_t::reply(interaction_response_type t, const message& m, command_completion_event_t callback) const { owner->interaction_response_create(this->command.id, this->command.token, dpp::interaction_response(t, m), std::move(callback)); @@ -218,7 +218,7 @@ void interaction_create_t::delete_original_response(command_completion_event_t c } -#ifdef DPP_CORO +#ifndef DPP_NO_CORO async interaction_create_t::co_reply() const { return dpp::async{[this] (T&& cb) { this->reply(std::forward(cb)); }}; } @@ -266,7 +266,7 @@ async interaction_create_t::co_edit_original_response(c async interaction_create_t::co_delete_original_response() const { return dpp::async{[&, this] (T&& cb) { this->delete_original_response(std::forward(cb)); }}; } -#endif /* DPP_CORO */ +#endif /* DPP_NO_CORO */ command_value interaction_create_t::get_parameter(const std::string& name) const { const command_interaction ci = command.get_command_interaction(); diff --git a/src/dpp/utility.cpp b/src/dpp/utility.cpp index 79e51c646d..01330e92d6 100644 --- a/src/dpp/utility.cpp +++ b/src/dpp/utility.cpp @@ -119,7 +119,7 @@ avx_type_t voice_avx() { } bool is_coro_enabled() { -#ifdef DPP_CORO +#ifndef DPP_NO_CORO return true; #else return false; diff --git a/src/unittest/coro.cpp b/src/unittest/coro.cpp index 8ec42d89d2..cb44f6be77 100644 --- a/src/unittest/coro.cpp +++ b/src/unittest/coro.cpp @@ -4,7 +4,7 @@ #include "test.h" -#ifdef DPP_CORO +#ifndef DPP_NO_CORO namespace { diff --git a/src/unittest/test.h b/src/unittest/test.h index cf9f966a28..df2bdd889d 100644 --- a/src/unittest/test.h +++ b/src/unittest/test.h @@ -294,7 +294,7 @@ extern dpp::snowflake TEST_EVENT_ID; extern bool offline; /* True if we skip tt_extended tests */ extern bool extended; -#ifdef DPP_CORO +#ifndef DPP_NO_CORO inline constexpr bool coro = true; #else inline constexpr bool coro = false;