\ingroup annotations Annotation for arguments with values
| 1990 | /// \ingroup annotations |
| 1991 | /// Annotation for arguments with values |
| 1992 | struct arg_v : arg { |
| 1993 | private: |
| 1994 | template <typename T> |
| 1995 | arg_v(arg &&base, T &&x, const char *descr = nullptr) |
| 1996 | : arg(base), value(reinterpret_steal<object>(detail::make_caster<T>::cast( |
| 1997 | std::forward<T>(x), return_value_policy::automatic, {}))), |
| 1998 | descr(descr) |
| 1999 | #if defined(PYBIND11_DETAILED_ERROR_MESSAGES) |
| 2000 | , |
| 2001 | type(type_id<T>()) |
| 2002 | #endif |
| 2003 | { |
| 2004 | // Workaround! See: |
| 2005 | // https://github.com/pybind/pybind11/issues/2336 |
| 2006 | // https://github.com/pybind/pybind11/pull/2685#issuecomment-731286700 |
| 2007 | if (PyErr_Occurred()) { |
| 2008 | PyErr_Clear(); |
| 2009 | } |
| 2010 | } |
| 2011 | |
| 2012 | public: |
| 2013 | /// Direct construction with name, default, and description |
| 2014 | template <typename T> |
| 2015 | arg_v(const char *name, T &&x, const char *descr = nullptr) |
| 2016 | : arg_v(arg(name), std::forward<T>(x), descr) {} |
| 2017 | |
| 2018 | /// Called internally when invoking `py::arg("a") = value` |
| 2019 | template <typename T> |
| 2020 | arg_v(const arg &base, T &&x, const char *descr = nullptr) |
| 2021 | : arg_v(arg(base), std::forward<T>(x), descr) {} |
| 2022 | |
| 2023 | /// Same as `arg::noconvert()`, but returns *this as arg_v&, not arg& |
| 2024 | arg_v &noconvert(bool flag = true) { |
| 2025 | arg::noconvert(flag); |
| 2026 | return *this; |
| 2027 | } |
| 2028 | |
| 2029 | /// Same as `arg::nonone()`, but returns *this as arg_v&, not arg& |
| 2030 | arg_v &none(bool flag = true) { |
| 2031 | arg::none(flag); |
| 2032 | return *this; |
| 2033 | } |
| 2034 | |
| 2035 | /// The default value |
| 2036 | object value; |
| 2037 | /// The (optional) description of the default value |
| 2038 | const char *descr; |
| 2039 | #if defined(PYBIND11_DETAILED_ERROR_MESSAGES) |
| 2040 | /// The C++ type name of the default value (only available when compiled in debug mode) |
| 2041 | std::string type; |
| 2042 | #endif |
| 2043 | }; |
| 2044 | |
| 2045 | /// \ingroup annotations |
| 2046 | /// Annotation indicating that all following arguments are keyword-only; the is the equivalent of |