| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- #ifndef REMOTEAR3_UTILITY_HPP
- #define REMOTEAR3_UTILITY_HPP
- #include <spdlog/spdlog.h>
- #include <chrono>
- // https://en.cppreference.com/w/cpp/utility/unreachable
- [[noreturn]] inline void unreachable() {
- // Uses compiler specific extensions if possible.
- // Even if no extension is used, undefined behavior is still raised by
- // an empty function body and the noreturn attribute.
- #ifdef __GNUC__ // GCC, Clang, ICC
- __builtin_unreachable();
- // #elifdef _MSC_VER // MSVC
- #else
- __assume(false);
- #endif
- }
- #define RET_ERROR \
- assert(false);\
- unreachable()
- #define RET_ERROR_B \
- assert(false); \
- return false
- inline bool check_function_call(bool function_ret, unsigned int line_number,
- const char *file_name, const char *function_call_str) {
- if (function_ret) [[likely]] return true;
- SPDLOG_ERROR("Function call {} failed at {}:{}.",
- function_call_str, file_name, line_number);
- RET_ERROR_B;
- }
- #define CALL_CHECK(function_call) \
- check_function_call( \
- function_call, __LINE__, __FILE__, #function_call)
- struct log_timer {
- void reset() {
- last_ts = clock_type::now();
- }
- void record(std::string_view name) {
- SPDLOG_TRACE("{} reached at {}ms", name,
- std::chrono::duration_cast<time_res>(clock_type::now() - last_ts).count());
- }
- private:
- using clock_type = std::chrono::high_resolution_clock;
- using time_res = std::chrono::milliseconds;
- clock_type::time_point last_ts;
- };
- extern log_timer global_timer;
- #define RESET_TIMER global_timer.reset()
- #define RECORD_TIME(name) global_timer.record(name)
- inline auto system_timestamp() {
- using namespace std::chrono;
- auto time_point = high_resolution_clock::now();
- return duration_cast<microseconds>(time_point.time_since_epoch()).count();
- }
- inline auto now_since_epoch_in_seconds() {
- using namespace std::chrono;
- return time_point_cast<seconds, system_clock>(system_clock::now());
- }
- template<typename T>
- struct smart_buffer {
- T *ptr = nullptr;
- size_t length = 0;
- ~smart_buffer() {
- delete ptr;
- }
- void create(size_t req_length) {
- if (req_length > capacity) [[unlikely]] {
- delete ptr;
- ptr = new T[req_length];
- capacity = req_length;
- }
- length = req_length;
- }
- private:
- size_t capacity = 0;
- };
- #endif //REMOTEAR3_UTILITY_HPP
|