.. _program_listing_file__tmp_ws_src_mavros_libmavconn_include_mavconn_thread_utils.hpp: Program Listing for File thread_utils.hpp ========================================= |exhale_lsh| :ref:`Return to documentation for file ` (``/tmp/ws/src/mavros/libmavconn/include/mavconn/thread_utils.hpp``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp // // libmavconn // Copyright 2014,2015,2016,2021 Vladimir Ermakov, All rights reserved. // // This file is part of the mavros package and subject to the license terms // in the top-level LICENSE file of the mavros repository. // https://github.com/mavlink/mavros/tree/master/LICENSE.md // #pragma once #ifndef MAVCONN__THREAD_UTILS_HPP_ #define MAVCONN__THREAD_UTILS_HPP_ #include #include #include #include #include #include namespace mavconn { namespace utils { template std::string format(const std::string & fmt, Args... args) { // C++11 specify that string store elements continously std::string ret; auto sz = std::snprintf(nullptr, 0, fmt.c_str(), args ...); ret.reserve(sz + 1); ret.resize(sz); // to be sure there have room for \0 std::snprintf(&ret.front(), ret.capacity() + 1, fmt.c_str(), args ...); return ret; } template bool set_this_thread_name(const std::string & name, Args && ... args) { auto new_name = format(name, std::forward(args)...); #ifdef __APPLE__ return pthread_setname_np(new_name.c_str()) == 0; #else pthread_t pth = pthread_self(); return pthread_setname_np(pth, new_name.c_str()) == 0; #endif } template inline const std::string to_string_ss(T & obj) { std::ostringstream ss; ss << obj; return ss.str(); } constexpr size_t operator"" _KiB(unsigned long long sz) // NOLINT { return sz * 1024; } } // namespace utils } // namespace mavconn #endif // MAVCONN__THREAD_UTILS_HPP_