diff options
author | John Ankarström <john@ankarstrom.se> | 2022-08-24 03:01:10 +0200 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2022-08-24 03:01:10 +0200 |
commit | ccb7d14a8b1ad6d80308c14a3f0b209d66c3c88d (patch) | |
tree | db79708df0f22a9cb721bbadb644bbcb5f6e975b /c/util.h | |
parent | 9c237e1537238ebe95289ed6df82f6d43c482944 (diff) | |
download | EpisodeBrowser-ccb7d14a8b1ad6d80308c14a3f0b209d66c3c88d.tar.gz |
Add FetchScreenwriters. Improve thread communication.
Diffstat (limited to 'c/util.h')
-rw-r--r-- | c/util.h | 53 |
1 files changed, 35 insertions, 18 deletions
@@ -11,17 +11,14 @@ #define CONCAT(a, b) CONCAT_IMPL(a, b) #define _ CONCAT(unused_, __LINE__) -inline size_t Min(size_t a, size_t b) +template <typename F> +struct Finally { - return a < b? a: b; -} - -inline int Cmp(const int a, const int b) -{ - if (a == b) return 0; - if (a > b) return 1; - return -1; -} + F f; + inline Finally(F f) : f(f) {} + inline ~Finally() { f(); } +}; +#define FINALLY Finally _ = [=]() /* Generic RAII type. */ template <typename T, auto F, typename U, auto E = 0> @@ -45,33 +42,53 @@ template <typename T> struct Buf { T* data; - size_t size; - Buf(T* data, size_t size) : data(data), size(size) {} - Buf(std::basic_string<T>& s) : data(s.data()), size(s.capacity()) {} - template <size_t N> Buf(T (&data)[N]) : data(data), size(N) {} + size_t c; + Buf(T* data, size_t c) : data(data), c(c) {} + Buf(std::basic_string<T>& s) : data(s.data()), c(s.capacity()) {} + template <size_t N> Buf(T (&data)[N]) : data(data), c(N) {} operator T*() { return data; } T& operator *() { return *data; } T& operator [](size_t i) { return data[i]; } + Buf<T> operator +(size_t i) { return {data+i, c-i}; } + //T operator -(size_t i) { return {data-i, c+i}; } }; +inline int Cmp(const int a, const int b) +{ + if (a == b) return 0; + if (a > b) return 1; + return -1; +} + +inline size_t Min(size_t a, size_t b) +{ + return a < b? a: b; +} + +template <typename T, size_t N> +inline size_t Len(T (&)[N]) +{ + return N-1; +} + /* Format wide string. */ template<typename... T> inline int Swprintf(Buf<wchar_t> buf, const wchar_t* const fmt, T... xs) { - return _snwprintf_s(buf, buf.size, _TRUNCATE, fmt, xs...); + return _snwprintf_s(buf, buf.c, _TRUNCATE, fmt, xs...); } /* Format static narrow string. */ template<typename... T> inline int Sprintf(Buf<char> buf, const char* const fmt, T... xs) { - return _snprintf_s(buf, buf.size, _TRUNCATE, fmt, xs...); + return _snprintf_s(buf, buf.c, _TRUNCATE, fmt, xs...); } /* Copy to static wide string buffer. */ inline wchar_t* Wcscpy(Buf<wchar_t> dst, const wchar_t* const src) { - const size_t len = Min(dst.size, wcslen(src)+1); + const size_t len = Min(dst.c, wcslen(src)+1); memcpy(dst, src, len*sizeof(wchar_t)); dst[len-1] = 0; return dst; @@ -80,7 +97,7 @@ inline wchar_t* Wcscpy(Buf<wchar_t> dst, const wchar_t* const src) /* Copy to static narrow string buffer. */ inline char* Strcpy(Buf<char> dst, const char* const src) { - const size_t len = Min(dst.size, strlen(src)+1); + const size_t len = Min(dst.c, strlen(src)+1); memcpy(dst, src, len); dst[len-1] = 0; return dst; |