Skip to content

Commit 76f7419

Browse files
Improve typing of the drop_frame parameter on the RationalTime.to_timecode method (#1443)
* Improve typing of the drop_frame parameter on the RationalTime.to_timecode method Signed-off-by: Jean-Christophe Morin <[email protected]>
1 parent f24c776 commit 76f7419

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <pybind11/pybind11.h>
2+
#include <pybind11/stl.h>
3+
#include "nonstd/optional.hpp"
4+
5+
using nonstd::optional;
6+
using nonstd::nullopt_t;
7+
8+
namespace pybind11 { namespace detail {
9+
// Caster for converting to/from nonstd::optional.
10+
// Pybind11 supports optional types when C++17 is used.
11+
// So for now we have to create the casters manually.
12+
// See https://pybind11.readthedocs.io/en/stable/advanced/cast/stl.html#c-17-library-containers
13+
template<typename T> struct type_caster<optional<T>>
14+
: public optional_caster<optional<T>> {};
15+
16+
template<> struct type_caster<nullopt_t>
17+
: public void_caster<nullopt_t> {};
18+
}}

src/py-opentimelineio/opentime-bindings/opentime_rationalTime.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
#include "opentime/rationalTime.h"
88
#include "opentimelineio/stringUtils.h"
9+
#include "opentimelineio/optional.h"
10+
#include "py-opentimelineio/bindings-common/casters.h"
911

1012
namespace py = pybind11;
1113
using namespace pybind11::literals;
@@ -28,11 +30,10 @@ struct ErrorStatusConverter {
2830
ErrorStatus error_status;
2931
};
3032

31-
32-
IsDropFrameRate df_enum_converter(py::object& df) {
33-
if (df.is(py::none())) {
33+
IsDropFrameRate df_enum_converter(optional<bool>& df) {
34+
if (!df.has_value()) {
3435
return IsDropFrameRate::InferFromRate;
35-
} else if (py::cast<bool>(py::bool_(df))) {
36+
} else if (df.value()) {
3637
return IsDropFrameRate::ForceYes;
3738
} else {
3839
return IsDropFrameRate::ForceNo;
@@ -107,7 +108,7 @@ For example, the duration of a clip from frame 10 to frame 15 is 6 frames. Resul
107108
.def("to_frames", (int (RationalTime::*)() const) &RationalTime::to_frames, "Returns the frame number based on the current rate.")
108109
.def("to_frames", (int (RationalTime::*)(double) const) &RationalTime::to_frames, "rate"_a, "Returns the frame number based on the given rate.")
109110
.def("to_seconds", &RationalTime::to_seconds)
110-
.def("to_timecode", [](RationalTime rt, double rate, py::object drop_frame) {
111+
.def("to_timecode", [](RationalTime rt, double rate, optional<bool> drop_frame) {
111112
return rt.to_timecode(
112113
rate,
113114
df_enum_converter(drop_frame),

src/py-opentimelineio/opentimelineio-bindings/otio_utils.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
#include <pybind11/pybind11.h>
77
#include <pybind11/stl.h>
88
#include <string>
9+
#include "py-opentimelineio/bindings-common/casters.h"
910
#include "opentimelineio/any.h"
10-
#include "opentimelineio/optional.h"
1111
#include "opentimelineio/stringUtils.h"
1212
#include "opentimelineio/serializableObject.h"
1313
#include "opentimelineio/vectorIndexing.h"
@@ -17,13 +17,6 @@ using namespace opentimelineio::OPENTIMELINEIO_VERSION;
1717

1818
void install_external_keepalive_monitor(SerializableObject* so, bool apply_now);
1919

20-
namespace pybind11 { namespace detail {
21-
template<typename T> struct type_caster<optional<T>>
22-
: public optional_caster<optional<T>> {};
23-
24-
template<> struct type_caster<nullopt_t>
25-
: public void_caster<nullopt_t> {};
26-
}}
2720

2821
template <typename T>
2922
struct managing_ptr {

0 commit comments

Comments
 (0)