asyncio_utils.h 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #pragma once
  2. #include <string>
  3. #include <algorithm>
  4. #include <iostream>
  5. template <typename... Args>
  6. std::string format(const std::string &fmt, Args... args)
  7. {
  8. // C++11 specify that string store elements continously
  9. std::string ret;
  10. auto sz = std::snprintf(nullptr, 0, fmt.c_str(), args...);
  11. ret.reserve(sz + 1);
  12. ret.resize(sz); // to be sure there have room for \0
  13. std::snprintf(&ret.front(), ret.capacity() + 1, fmt.c_str(), args...);
  14. return ret;
  15. }
  16. template <typename... Args>
  17. bool set_this_thread_name(const std::string &name, Args &&... args)
  18. {
  19. auto new_name = format(name, std::forward<Args>(args)...);
  20. #ifdef __APPLE__
  21. return pthread_setname_np(new_name.c_str()) == 0;
  22. #else
  23. pthread_t pth = pthread_self();
  24. return pthread_setname_np(pth, new_name.c_str()) == 0;
  25. #endif
  26. }
  27. /**
  28. * Parse host:port pairs
  29. */
  30. void url_parse_host(std::string host,
  31. std::string &host_out, int &port_out,
  32. const std::string def_host, const int def_port);
  33. /**
  34. * Parse ?ids=sid,cid
  35. */
  36. void url_parse_query(std::string query);