File: cast.h

package info (click to toggle)
pyosmium 4.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,584 kB
  • sloc: python: 4,400; cpp: 2,504; makefile: 20
file content (115 lines) | stat: -rw-r--r-- 2,932 bytes parent folder | download | duplicates (4)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* SPDX-License-Identifier: BSD-2-Clause
 *
 * This file is part of pyosmium. (https://osmcode.org/pyosmium/)
 *
 * Copyright (C) 2025 Sarah Hoffmann <lonvia@denofr.de> and others.
 * For a full list of authors see the git log.
 */
#ifndef PYOSMIUM_CAST_H
#define PYOSMIUM_CAST_H

#include <datetime.h>

#include <pybind11/pybind11.h>
#include <osmium/osm.hpp>

namespace pybind11 { namespace detail {
    template <> struct type_caster<osmium::Timestamp> {
    public:
        using type = osmium::Timestamp;

        bool load(handle src, bool) {
            // Lazy initialise the PyDateTime import
            if (!PyDateTimeAPI) { PyDateTime_IMPORT; }

            if (!src) {
                return false;
            }

            if (pybind11::isinstance<pybind11::str>(src)) {
                value = osmium::Timestamp(src.cast<std::string>());
                return true;
            }

            if (!PyDateTime_Check(src.ptr())) {
                return false;
            }

            auto ts = src.attr("timestamp")();
            value = (unsigned) ts.cast<double>();

            return true;
        }

        static handle cast(type const &src, return_value_policy, handle)
        {
            // Lazy initialise the PyDateTime import
            if (!PyDateTimeAPI) { PyDateTime_IMPORT; }

            std::time_t tt = src.seconds_since_epoch();

            static auto utc = module::import("datetime").attr("timezone").attr("utc");

            return PyDateTime_FromTimestamp(pybind11::make_tuple(tt, utc).ptr());
        }

        PYBIND11_TYPE_CASTER(type, _("datetime.datetime"));
    };
}} // namespace pybind11::detail

namespace pyosmium {

template <typename T>
T const *try_cast(pybind11::object o) {
    auto const inner = pybind11::getattr(o, "_pyosmium_data", pybind11::none());

    if (pybind11::isinstance<T>(inner)) {
        return inner.cast<T const *>();
    }

    return nullptr;
}


template <typename T>
T const &cast(pybind11::object o) {
    return o.attr("_pyosmium_data").cast<T const &>();
}


template <typename T>
T const *try_cast_list(pybind11::object o) {
    auto const ward = pybind11::getattr(o, "_pyosmium_data", pybind11::none());

    if (ward.is_none()) {
        return nullptr;
    }

    auto const valid_func = pybind11::getattr(ward, "is_valid", pybind11::none());

    if (valid_func.is_none() || !valid_func().cast<bool>()) {
        return nullptr;
    }

    auto const inner = pybind11::getattr(o, "_list", pybind11::none());

    if (pybind11::isinstance<T>(inner)) {
        return inner.cast<T const *>();
    }

    return nullptr;
}


template <typename T>
T const &cast_list(pybind11::object const &o) {
    if (!o.attr("_pyosmium_data").attr("is_valid")().cast<bool>()) {
        throw std::runtime_error{"Illegal access to removed OSM object"};
    }

    return o.attr("_list").cast<T const &>();
}

} // namespace

#endif // PYOSMIUM_CAST_H