Repository navigation
Expand file tree
/
Copy pathpy_operation_callbacks.hpp
More file actions
77 lines (62 loc) · 2.49 KB
/
Copy pathpy_operation_callbacks.hpp
File metadata and controls
77 lines (62 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* SPDX-FileCopyrightText: 2026 LichtFeld Studio Authors
*
* SPDX-License-Identifier: GPL-3.0-or-later */
#pragma once
#include "core/logger.hpp"
#include "io/project_operations.hpp"
#include <nanobind/nanobind.h>
#include <exception>
#include <memory>
#include <string>
#include <utility>
namespace nb = nanobind;
namespace lfs::python {
// Progress and cancel callbacks for native project operations, which
// copy them while the GIL is released. Share the Python handle so those
// copies only change C++ reference counts.
struct PyProgressCallback {
std::shared_ptr<nb::object> callback;
bool present;
explicit PyProgressCallback(nb::object value)
: callback(std::make_shared<nb::object>(std::move(value))),
present(*callback && !callback->is_none()) {}
// The native callback, or none when Python passed None.
[[nodiscard]] lfs::io::project::ProjectOperationProgress operation() const {
return present ? lfs::io::project::ProjectOperationProgress(*this)
: lfs::io::project::ProjectOperationProgress{};
}
void operator()(float progress, const std::string& message) const {
nb::gil_scoped_acquire gil;
if (!present)
return;
try {
(*callback)(progress, message);
} catch (const std::exception& e) {
LOG_ERROR("Python progress callback error: {}", e.what());
}
}
};
struct PyCancelCallback {
std::shared_ptr<nb::object> callback;
bool present;
explicit PyCancelCallback(nb::object value)
: callback(std::make_shared<nb::object>(std::move(value))),
present(*callback && !callback->is_none()) {}
// The native callback, or none when Python passed None.
[[nodiscard]] lfs::io::project::ProjectOperationCancel operation() const {
return present ? lfs::io::project::ProjectOperationCancel(*this)
: lfs::io::project::ProjectOperationCancel{};
}
bool operator()() const {
nb::gil_scoped_acquire gil;
if (!present)
return false;
try {
return nb::cast<bool>((*callback)());
} catch (const std::exception& e) {
LOG_ERROR("Python cancellation callback error: {}", e.what());
return true;
}
}
};
} // namespace lfs::python