LCOV - code coverage report
Current view: top level - python - dtype.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 6 6 100.0 %
Date: 2024-11-17 01:47:58 Functions: 9 9 100.0 %

          Line data    Source code
       1             : // SPDX-License-Identifier: BSD-3-Clause
       2             : // Copyright (c) 2023 Scipp contributors (https://github.com/scipp)
       3             : /// @file
       4             : #pragma once
       5             : 
       6             : #include <string_view>
       7             : 
       8             : #include <scipp/core/dtype.h>
       9             : #include <scipp/units/unit.h>
      10             : 
      11             : #include "pybind11.h"
      12             : #include "unit.h"
      13             : 
      14             : namespace pybind11 {
      15             : class dtype;
      16             : }
      17             : 
      18             : scipp::core::DType dtype_of(const pybind11::object &x);
      19             : 
      20             : scipp::core::DType scipp_dtype(const pybind11::object &type);
      21             : 
      22             : std::tuple<scipp::core::DType, std::optional<scipp::units::Unit>>
      23             : cast_dtype_and_unit(const pybind11::object &dtype, const ProtoUnit &unit);
      24             : 
      25             : void ensure_conversion_possible(scipp::core::DType from, scipp::core::DType to,
      26             :                                 const std::string &data_name);
      27             : 
      28             : template <class T, class = void> struct converting_cast {
      29       16048 :   static decltype(auto) cast(const pybind11::object &obj) {
      30       16048 :     return obj.cast<T>();
      31             :   }
      32             : };
      33             : 
      34             : template <class T>
      35             : struct converting_cast<T, std::enable_if_t<std::is_integral_v<T>>> {
      36        1977 :   static decltype(auto) cast(const pybind11::object &obj) {
      37        1977 :     if (dtype_of(obj) == scipp::dtype<double>) {
      38             :       // This conversion is not implemented in pybind11 v2.6.2
      39           4 :       return obj.cast<pybind11::int_>().cast<T>();
      40             :     } else {
      41             :       // All other conversions are either supported by pybind11 or not
      42             :       // desired anyway.
      43        1973 :       return obj.cast<T>();
      44             :     }
      45             :   }
      46             : };
      47             : 
      48             : scipp::core::DType
      49             : common_dtype(const pybind11::object &values, const pybind11::object &variances,
      50             :              scipp::core::DType dtype,
      51             :              scipp::core::DType default_dtype = scipp::core::dtype<double>);
      52             : 
      53             : bool has_datetime_dtype(const pybind11::object &obj);
      54             : 
      55             : [[nodiscard]] scipp::units::Unit
      56             : parse_datetime_dtype(const std::string &dtype_name);
      57             : [[nodiscard]] scipp::units::Unit
      58             : parse_datetime_dtype(const pybind11::object &dtype);

Generated by: LCOV version 1.14