mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Sadly the linux build VMs dont support c++20 so we cant use lambdas as template arguments
This commit is contained in:
parent
17450ddc1c
commit
039b684269
@ -17,7 +17,7 @@
|
|||||||
|
|
||||||
#define arraysz(x) (sizeof(x)/sizeof(x[0]))
|
#define arraysz(x) (sizeof(x)/sizeof(x[0]))
|
||||||
|
|
||||||
template<typename T, void free_T(T), T null=static_cast<T>(NULL)>
|
template<typename T=void*, void free_T(T)=free, T null=static_cast<T>(NULL)>
|
||||||
class generic_raii {
|
class generic_raii {
|
||||||
private:
|
private:
|
||||||
generic_raii( const generic_raii & ) noexcept;
|
generic_raii( const generic_raii & ) noexcept;
|
||||||
@ -43,23 +43,37 @@ class generic_raii {
|
|||||||
void attach(T val) noexcept { release(); handle = val; }
|
void attach(T val) noexcept { release(); handle = val; }
|
||||||
T* unsafe_address() noexcept { return &handle; }
|
T* unsafe_address() noexcept { return &handle; }
|
||||||
explicit operator bool() const noexcept { return handle != null; }
|
explicit operator bool() const noexcept { return handle != null; }
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class wchar_raii : public generic_raii<wchar_t*, [](wchar_t* x){PyMem_Free(x);}> {
|
static inline void wchar_raii_free(wchar_t *x) { PyMem_Free(x); }
|
||||||
#if __cplusplus >= 201703L
|
|
||||||
|
#if (defined(__GNUC__) && !defined(__clang__))
|
||||||
|
#pragma GCC diagnostic push
|
||||||
|
#pragma GCC diagnostic ignored "-Wsubobject-linkage"
|
||||||
|
#endif
|
||||||
|
class wchar_raii : public generic_raii<wchar_t*, wchar_raii_free, static_cast<wchar_t*>(NULL)> {
|
||||||
private:
|
private:
|
||||||
Py_ssize_t sz;
|
Py_ssize_t sz;
|
||||||
public:
|
public:
|
||||||
|
wchar_raii(PyObject *obj) {
|
||||||
|
from_unicode(obj);
|
||||||
|
}
|
||||||
|
wchar_raii(wchar_t *obj, size_t sz) : generic_raii(obj), sz(sz) { }
|
||||||
int from_unicode(PyObject *obj) {
|
int from_unicode(PyObject *obj) {
|
||||||
wchar_t *buf = PyUnicode_AsWideCharString(obj, &sz);
|
wchar_t *buf = PyUnicode_AsWideCharString(obj, &sz);
|
||||||
if (!buf) return 0;
|
if (!buf) return 0;
|
||||||
attach(buf);
|
attach(buf);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
#if __cplusplus >= 201703L
|
||||||
std::wstring_view as_view() const { return std::wstring_view(handle, sz); }
|
std::wstring_view as_view() const { return std::wstring_view(handle, sz); }
|
||||||
std::wstring as_copy() const { return std::wstring(handle, sz); }
|
|
||||||
#endif
|
#endif
|
||||||
|
std::wstring as_copy() const { return std::wstring(handle, sz); }
|
||||||
};
|
};
|
||||||
|
#if (defined(__GNUC__) && !defined(__clang__))
|
||||||
|
#pragma GCC diagnostic pop
|
||||||
|
#endif
|
||||||
|
|
||||||
typedef generic_raii<PyObject*, Py_DecRef> pyobject_raii;
|
typedef generic_raii<PyObject*, Py_DecRef> pyobject_raii;
|
||||||
|
|
||||||
@ -91,26 +105,8 @@ class generic_raii_array {
|
|||||||
const T operator[](size_t i) const noexcept { return array[i]; }
|
const T operator[](size_t i) const noexcept { return array[i]; }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
static inline int
|
static inline int
|
||||||
py_to_wchar(PyObject *obj, wchar_raii *output) {
|
py_to_wchar_(PyObject *obj, wchar_raii *output) {
|
||||||
if (!PyUnicode_Check(obj)) {
|
|
||||||
if (obj == Py_None) { output->release(); return 1; }
|
|
||||||
PyErr_SetString(PyExc_TypeError, "unicode object expected");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
wchar_t *buf = PyUnicode_AsWideCharString(obj, NULL);
|
|
||||||
if (!buf) { PyErr_NoMemory(); return 0; }
|
|
||||||
output->attach(buf);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline int
|
|
||||||
py_to_wchar_no_none(PyObject *obj, wchar_raii *output) {
|
|
||||||
if (!PyUnicode_Check(obj)) {
|
|
||||||
PyErr_SetString(PyExc_TypeError, "unicode object expected");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
#if __cplusplus >= 201703L
|
#if __cplusplus >= 201703L
|
||||||
return output->from_unicode(obj);
|
return output->from_unicode(obj);
|
||||||
#else
|
#else
|
||||||
@ -120,3 +116,22 @@ py_to_wchar_no_none(PyObject *obj, wchar_raii *output) {
|
|||||||
return 1;
|
return 1;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline int
|
||||||
|
py_to_wchar(PyObject *obj, wchar_raii *output) {
|
||||||
|
if (!PyUnicode_Check(obj)) {
|
||||||
|
if (obj == Py_None) { output->release(); return 1; }
|
||||||
|
PyErr_SetString(PyExc_TypeError, "unicode object expected");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return py_to_wchar_(obj, output);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline int
|
||||||
|
py_to_wchar_no_none(PyObject *obj, wchar_raii *output) {
|
||||||
|
if (!PyUnicode_Check(obj)) {
|
||||||
|
PyErr_SetString(PyExc_TypeError, "unicode object expected");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return py_to_wchar_(obj, output);
|
||||||
|
}
|
||||||
|
@ -58,8 +58,11 @@ class scoped_com_initializer { // {{{
|
|||||||
|
|
||||||
#define INITIALIZE_COM_IN_FUNCTION scoped_com_initializer com; if (!com) return com.set_python_error();
|
#define INITIALIZE_COM_IN_FUNCTION scoped_com_initializer com; if (!com) return com.set_python_error();
|
||||||
|
|
||||||
typedef generic_raii<wchar_t*, [](wchar_t *x) { CoTaskMemFree(x); }> com_wchar_raii;
|
static inline void com_wchar_raii_free(wchar_t *x) { CoTaskMemFree(x); }
|
||||||
typedef generic_raii<void*, [](void* x) { UnmapViewOfFile(x);}> mapping_raii;
|
typedef generic_raii<wchar_t*, com_wchar_raii_free> com_wchar_raii;
|
||||||
|
|
||||||
|
static inline void mapping_raii_free(void *x) { UnmapViewOfFile(x); }
|
||||||
|
typedef generic_raii<void*, mapping_raii_free> mapping_raii;
|
||||||
|
|
||||||
class handle_raii {
|
class handle_raii {
|
||||||
private:
|
private:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user