Commit 3380dbb4 authored by captainwong's avatar captainwong

update

parent 3751fb7a
.vscode/
# Compiled Object files # Compiled Object files
*.slo *.slo
*.lo *.lo
......
#pragma once
#include <memory>
#include <functional>
namespace jlib {
//template <typename T>
//struct deleter : public std::default_delete<T>
//{};
template <typename T>
class auto_free
{
public:
typedef std::function<void(T*)> deleter;
explicit auto_free(T* data, deleter dter = {})
: data_(data), deleter_(dter)
{}
~auto_free() {
if (data_ && deleter_) {
deleter_(data_);
}
}
protected:
T* data_ = nullptr;
deleter deleter_ = nullptr;
};
}
#pragma once
#include <vector>
#include <string>
namespace jlib {
template <class V, class T>
bool is_contain(const V& v, const T& t) {
for (auto i : v) { if (i == t) { return true; } }
return false;
}
template <class V>
std::vector<std::wstring> get_other(const V& v, const std::wstring& t) {
std::vector<std::wstring> ret = {};
for (auto i : v) { if (i != t) { ret.push_back(t); } }
return ret;
}
template <class V>
std::vector<std::string> get_other(const V& v, const std::string& t) {
std::vector<std::string> ret = {};
for (auto i : v) { if (i != t) { ret.push_back(t); } }
return ret;
}
}
...@@ -4,27 +4,51 @@ ...@@ -4,27 +4,51 @@
namespace jlib{ namespace jlib{
inline bool toClipboard(HWND hwnd, const std::string &s) inline bool to_clipboard(const std::wstring &s)
{ {
if (!OpenClipboard(hwnd)) return false; if (!OpenClipboard(nullptr)) return false;
if (!EmptyClipboard()) return false; if (!EmptyClipboard()) return false;
HGLOBAL hg = GlobalAlloc(GMEM_DDESHARE, s.size() + 1); size_t sz = (s.length() + 1) * 2;
if (!hg) { HGLOBAL hg = GlobalAlloc(GMEM_MOVEABLE, sz);
CloseClipboard(); if (!hg) {
return false; CloseClipboard();
} return false;
LPVOID hMem = GlobalLock(hg); }
if (!hMem) { LPVOID hMem = GlobalLock(hg);
CloseClipboard(); if (!hMem) {
return false; CloseClipboard();
} return false;
memcpy(hMem, s.c_str(), s.size() + 1); }
GlobalUnlock(hg); memset(hMem, 0, sz);
SetClipboardData(CF_TEXT, hg); memcpy(hMem, s.data(), s.size() * 2);
CloseClipboard(); GlobalUnlock(hg);
GlobalFree(hg); SetClipboardData(CF_UNICODETEXT, hg);
return true; CloseClipboard();
GlobalFree(hg);
return true;
} }
inline std::wstring from_clipboard()
{
std::wstring text;
do {
if (!OpenClipboard(nullptr))
break;
HANDLE hData = GetClipboardData(CF_UNICODETEXT);
if (hData == nullptr)
break;
auto pszText = static_cast<const wchar_t*>(GlobalLock(hData));
if (pszText == nullptr)
break;
text = pszText;
GlobalUnlock(hData);
CloseClipboard();
} while (false);
return text;
}
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment