Po-Hsien Wang | 00777b2 | 2019-04-24 16:37:09 -0700 | [diff] [blame] | 1 | // Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef __UTILS_H__ |
| 6 | #define __UTILS_H__ |
| 7 | |
| 8 | #include <stdarg.h> |
Po-Hsien Wang | 42e116c | 2020-06-09 16:10:27 -0700 | [diff] [blame^] | 9 | #include <stdio.h> |
Po-Hsien Wang | 00777b2 | 2019-04-24 16:37:09 -0700 | [diff] [blame] | 10 | #include <sys/time.h> |
Po-Hsien Wang | 42e116c | 2020-06-09 16:10:27 -0700 | [diff] [blame^] | 11 | #include <functional> |
Po-Hsien Wang | 00777b2 | 2019-04-24 16:37:09 -0700 | [diff] [blame] | 12 | #include <string> |
| 13 | #include <vector> |
| 14 | #include <vulkan/vulkan.hpp> |
| 15 | |
| 16 | extern int g_verbose; |
| 17 | void PrintDateTime(); |
| 18 | std::vector<std::string> SplitString(const std::string& kInput, char delimiter); |
Po-Hsien Wang | 00777b2 | 2019-04-24 16:37:09 -0700 | [diff] [blame] | 19 | |
| 20 | inline uint64_t GetUTime() { |
| 21 | struct timeval tv = {}; |
| 22 | gettimeofday(&tv, nullptr); |
| 23 | return tv.tv_usec + 1000000ULL * static_cast<uint64_t>(tv.tv_sec); |
| 24 | } |
| 25 | |
| 26 | bool IsItemInVector(const std::vector<std::string>& list, |
| 27 | const char* value, |
| 28 | bool empty_value); |
| 29 | |
| 30 | inline void DbgPrintf(const char* filename, |
| 31 | int line, |
| 32 | FILE* fileid, |
| 33 | const char* format, |
| 34 | ...) { |
| 35 | va_list args; |
| 36 | va_start(args, format); |
| 37 | char string_format[strlen(format) + 1024]; |
| 38 | char debug_header[1024] = "\0"; |
| 39 | if (g_verbose) { |
| 40 | sprintf(debug_header, "[%s:%d]", filename, line); |
| 41 | sprintf(string_format, "%30s %s\n", debug_header, format); |
| 42 | } else { |
| 43 | sprintf(string_format, "%s\n", format); |
| 44 | } |
| 45 | vfprintf(fileid, string_format, args); |
| 46 | va_end(args); |
| 47 | } |
| 48 | |
| 49 | #define DEBUG(fmt, ...) \ |
| 50 | { \ |
| 51 | if (g_verbose) { \ |
| 52 | LOG(fmt, ##__VA_ARGS__); \ |
| 53 | } \ |
| 54 | } |
| 55 | #define LOG(fmt, ...) \ |
| 56 | { DbgPrintf(__FILE__, __LINE__, stdout, fmt, ##__VA_ARGS__); } |
| 57 | #define ERROR(fmt, ...) \ |
| 58 | { DbgPrintf(__FILE__, __LINE__, stderr, fmt, ##__VA_ARGS__); } |
| 59 | #define ABORT(code, fmt, ...) \ |
| 60 | { \ |
| 61 | ERROR(fmt, ##__VA_ARGS__); \ |
| 62 | exit(code); \ |
| 63 | } |
| 64 | |
| 65 | // Put this in the declarations for a class to be uncopyable. |
| 66 | #define DISALLOW_COPY(TypeName) TypeName(const TypeName&) = delete |
| 67 | |
| 68 | // Put this in the declarations for a class to be unassignable. |
| 69 | #define DISALLOW_ASSIGN(TypeName) TypeName& operator=(const TypeName&) = delete |
| 70 | |
| 71 | // Put this in the declarations for a class to be uncopyable and unassignable. |
| 72 | #define DISALLOW_COPY_AND_ASSIGN(TypeName) \ |
| 73 | DISALLOW_COPY(TypeName); \ |
| 74 | DISALLOW_ASSIGN(TypeName) |
| 75 | |
| 76 | // Ignore warning for linter for unused variable. |
| 77 | #define UNUSED(x) (void)(x) |
| 78 | |
Po-Hsien Wang | 42e116c | 2020-06-09 16:10:27 -0700 | [diff] [blame^] | 79 | class ScopeGuard { |
| 80 | public: |
| 81 | template <class Callable> |
| 82 | ScopeGuard(Callable&& fn) : fn_(std::forward<Callable>(fn)) {} |
| 83 | |
| 84 | ScopeGuard(ScopeGuard&& other) : fn_(std::move(other.fn_)) { |
| 85 | other.fn_ = nullptr; |
| 86 | } |
| 87 | |
| 88 | ~ScopeGuard() { |
| 89 | // must not throw |
| 90 | if (fn_) |
| 91 | fn_(); |
| 92 | } |
| 93 | |
| 94 | ScopeGuard(const ScopeGuard&) = delete; |
| 95 | void operator=(const ScopeGuard&) = delete; |
| 96 | |
| 97 | private: |
| 98 | std::function<void()> fn_; |
| 99 | }; |
| 100 | |
| 101 | // Used to defer a function call for cleanup |
| 102 | #define CONCAT_(a, b) a##b |
| 103 | #define CONCAT(a, b) CONCAT_(a, b) |
| 104 | #define DEFER(fn) ScopeGuard CONCAT(__defer__, __LINE__) = [&]() { fn; } |
| 105 | |
Po-Hsien Wang | 00777b2 | 2019-04-24 16:37:09 -0700 | [diff] [blame] | 106 | #endif |