aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README4
-rw-r--r--c/common.cpp3
-rw-r--r--c/common.h15
3 files changed, 14 insertions, 8 deletions
diff --git a/README b/README
index 77e46d6..ebe22ef 100644
--- a/README
+++ b/README
@@ -76,6 +76,10 @@ Following is a summary of some coding conventions used in the project.
- wstr = ... (std::wstring)
- tstr = ... (std::basic_string<TCHAR>)
+The list above is non-exhaustive. Variables whose type is unknown (in
+templates) do not need prefixes. Some very common self-explanatory
+variables also do not need prefixes, e.g. len (usually size_t).
+
... TYPES ...
Here are some general guidelines for choosing what types to use:
diff --git a/c/common.cpp b/c/common.cpp
index ba52f2c..25ff899 100644
--- a/c/common.cpp
+++ b/c/common.cpp
@@ -4,6 +4,7 @@
/* Win32Error: Exception for Windows API errors. */
+Win32Error::Win32Error() : m_dwErr(GetLastError()) {}
Win32Error::Win32Error(const DWORD dwErr) : m_dwErr(dwErr) {}
Win32Error::~Win32Error()
@@ -48,7 +49,7 @@ Library::Library(const TCHAR* const tszLibrary)
{
m_hModule = LoadLibrary(tszLibrary);
if (!m_hModule)
- throw Win32Error(GetLastError());
+ throw Win32Error();
}
Library::~Library()
diff --git a/c/common.h b/c/common.h
index d84590d..24c98c8 100644
--- a/c/common.h
+++ b/c/common.h
@@ -15,6 +15,7 @@ int EBMessageBox(const TCHAR* tszText, const TCHAR* tszCaption, unsigned uType);
struct Win32Error : public std::exception
{
+ Win32Error();
Win32Error(DWORD dwErr);
~Win32Error();
template <typename T = char> const T* what() const noexcept;
@@ -57,19 +58,19 @@ std::optional<T> maybe_make(U... xs)
template <auto F, typename... T>
inline auto require(T... xs)
{
- auto r = F(xs...);
- if (!r) throw Win32Error(GetLastError());
- return r;
+ if (auto r = F(xs...)) return r;
+ else throw Win32Error();
}
/* Call Windows API function, showing a warning on NULL. */
template <auto F, typename... T>
inline auto prefer(T... xs)
{
- try {
- return require<F>(xs...);
- } catch (Win32Error& e) {
- EBMessageBox(e.what<TCHAR>(), TEXT("System Error"), MB_ICONWARNING);
+ if (auto r = F(xs...))
+ return r;
+ else {
+ EBMessageBox(Win32Error().what<TCHAR>(),
+ TEXT("System Error"), MB_ICONWARNING);
return (decltype(F(std::declval<T>()...)))NULL;
}
}