Last active
October 8, 2018 06:58
-
-
Save qh-huang/c3773cc198792704b688ea980a30e113 to your computer and use it in GitHub Desktop.
Commonly Used Code Pieces
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// thread sleep in C++11 | |
void ThreadSleep(int duration_ms) { | |
std::cout << "Thread Sleeping..." << std::endl; | |
auto start = std::chrono::high_resolution_clock::now(); | |
std::this_thread::sleep_for(std::chrono::milliseconds(duration_ms)); | |
auto end = std::chrono::high_resolution_clock::now(); | |
std::chrono::duration<double, std::milli> elapsed = end - start; | |
cout << << "Thread Awake. Sleeping for " << elapsed.count() << " ms." << std::cout; | |
} | |
// ==================== | |
// dbg.h | |
// ==================== | |
#ifndef __dbg_h__ | |
#define __dbg_h__ | |
#include <stdio.h> | |
#include <errno.h> | |
#include <string.h> | |
#ifdef NDEBUG | |
#define debug(M, ...) | |
#else | |
#define debug(M, ...) fprintf(stderr, "DEBUG %s:%d: " M "\n", __FILE__, __LINE__, ##__VA_ARGS__) | |
#endif | |
#define clean_errno() (errno == 0 ? "None" : strerror(errno)) | |
#define log_err(M, ...) fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " M "\n", __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__) | |
#define log_warn(M, ...) fprintf(stderr, "[WARN] (%s:%d: errno: %s) " M "\n", __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__) | |
#define log_info(M, ...) fprintf(stderr, "[INFO] (%s:%d) " M "\n", __FILE__, __LINE__, ##__VA_ARGS__) | |
#define check(A, M, ...) if(!(A)) { log_err(M, ##__VA_ARGS__); errno=0; goto error; }https://gist.github.com/qiao-tw/c3773cc198792704b688ea980a30e113 | |
#define sentinel(M, ...) { log_err(M, ##__VA_ARGS__); errno=0; goto error; } | |
#define check_mem(A) check((A), "Out of memory.") | |
#define check_debug(A, M, ...) if(!(A)) { debug(M, ##__VA_ARGS__); errno=0; goto error; } | |
#endif | |
// =================== | |
// debug.h | |
// =================== | |
#include <cstring> | |
#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) | |
#define TRACE_FUNC \ | |
do { \ | |
std::cout << __FILENAME__ << ":" << __LINE__ << " in " << __func__ << std::endl; \ | |
} while (0); | |
// == trace functions and estimate elpse time == | |
#include <miniglog/glog/logging.h> | |
#define DEXEC(fn) \ | |
do { \ | |
DLOG(INFO) << "[EXEC " << #fn << " START]"; \ | |
std::chrono::steady_clock::time_point begin = \ | |
std::chrono::steady_clock::now(); \ | |
fn; \ | |
std::chrono::steady_clock::time_point end = \ | |
std::chrono::steady_clock::now(); \ | |
DLOG(INFO) << "[EXEC " << #fn << " FINISHED in " \ | |
<< std::chrono::duration_cast<std::chrono::microseconds> \ | |
(end - begin).count() << " us]"; \ | |
} while (0); | |
// Usage: | |
// DEXEC(foo()); | |
// -- output -- | |
// foo.cpp: 123 [EXEC foo() START] | |
// foo.cpp: 123 [EXEC foo() FINISHED in 456 ms] | |
#define DTRACE DLOG(INFO) << __func__; | |
// Usage: | |
// void foo() { | |
// DTRACE | |
// } | |
// -- output -- | |
// foo.cpp: 123 void foo(void) | |
#define PAUSE(format, ...) \ | |
fprintf(stderr, format, ##__VA_ARGS__); \ | |
fgetc(stdin) | |
// Usage: | |
// void foo_pause() { | |
// PAUSE("%s: Press ENTER to continue...\n", __func__); | |
// } | |
// =================== | |
// file_operations.hpp | |
// =================== | |
#include <iostream> | |
#include <string> | |
bool GetFileDirectory (const string& file_path, string& dir_path) | |
{ | |
size_t found = file_path.find_last_of("/\\"); | |
if (found == string::npos) return false; | |
dir_path = file_path.substr(0,found); | |
return true; | |
} | |
bool GetFileName (const string& file_path, string& file_name) | |
{ | |
size_t found = file_path.find_first_of("/\\"); | |
if (found == string::npos) return false; | |
file_name = file_path.substr(found+1); | |
return true; | |
} | |
bool GetLinesFromFile(const string& file_path, vector<string>& lines) | |
{ | |
lines.clear(); | |
ifstream file(file_path.c_str(), ios::in); | |
if (file.good()) { | |
string str; | |
while(getline(file, str)) { | |
lines.push_back(str); | |
} | |
} | |
if (lines.empty()) return false; | |
return true; | |
} | |
//======================================================== |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment