Skip to content

feat: add coroutine support via ENABLE_CORO cmake flag#7

Merged
null8626 merged 1 commit into
top-gg-community:mainfrom
braindigitalis:main
Jul 9, 2024
Merged

feat: add coroutine support via ENABLE_CORO cmake flag#7
null8626 merged 1 commit into
top-gg-community:mainfrom
braindigitalis:main

Conversation

@braindigitalis
Copy link
Copy Markdown
Contributor

This PR adds support for D++ asynchronous coroutines.

For this to work you must:

  • Use a C++20 supporting compiler (g++11 onwards etc)
  • build and install D++ with the DPP_CORO cmake flag
  • build cpp-sdk with the ENABLE_CORO flag

Building with this flag will reveal new functions in the SDK client. Each of the functions such as get_user will have a coroutine equivalent starting with co_ (this is the naming convention of D++, copied from C++ standards). The coroutine equivalent will have no callback parameter and can be awaited.

So, the basic example would become instead:

#include <topgg/topgg.h>
#include <dpp/dpp.h>
#include <iostream>

int main() {
  dpp::cluster bot{"your bot token"};
  topgg::client topgg_client{&bot, "your top.gg token"};

  const auto result = co_await topgg_client.co_get_bot(264811613708746752);
  try {
    const auto topgg_bot = result.get();
    std::cout << topgg_bot.username << std::endl;
  } catch (const std::exception& ext) {
    std::cout << "error: " << ext.what() << std::endl;
  }

  return 0;
}

This is nicer, as it avoids callback hell, and program flow reads in a nicer way. This is of course completely optional and if you don't want to use coroutines, or if you can't, the existing API calls will work exactly as before.

@pixxies pixxies requested a review from null8626 July 8, 2024 16:50
@null8626
Copy link
Copy Markdown
Member

null8626 commented Jul 9, 2024

Thank you sooo much for this! I am not familiar with C++ coroutines hahahaha so this pull request is a huge blessing! ❤️

@null8626 null8626 merged commit 404f314 into top-gg-community:main Jul 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants