@brief Wrapper around a Python object pointer. * * Manages the reference count for a @c PyObject pointer and is * implicitly convertible to the pointer. This is especially * convenient for interacting with Python C API functions that @a * borrow references and return @a new references (this is the most * common kind). * * This class is @a not thread-safe. However, it's best practice
| 114 | * for an explanation of reference counts. |
| 115 | */ |
| 116 | class object |
| 117 | { |
| 118 | public: |
| 119 | /** @brief Take ownership of a Python object pointer. |
| 120 | * @details @a Steals the reference. |
| 121 | */ |
| 122 | object(PyObject* ptr); |
| 123 | |
| 124 | /** @brief Create a Python string. */ |
| 125 | object(const std::string& val); |
| 126 | /** @brief Create a Python integer. */ |
| 127 | object(long val); |
| 128 | /** @brief Create a Python floating point number. */ |
| 129 | object(double val); |
| 130 | |
| 131 | object() {} |
| 132 | /** @details @a Borrows the reference. */ |
| 133 | object(const object& other); |
| 134 | /** @details @a Borrows the reference. */ |
| 135 | object& operator=(const object& other); |
| 136 | /** @details @a Steals the reference. */ |
| 137 | object(object&& other) noexcept; |
| 138 | /** @details @a Steals the reference. */ |
| 139 | object& operator=(object&& other); |
| 140 | ~object(); |
| 141 | |
| 142 | /** @returns @a Borrowed reference. */ |
| 143 | inline PyObject* get() noexcept { return m_ptr; } |
| 144 | /** @returns @a Borrowed reference. */ |
| 145 | inline const PyObject* get() const noexcept { return m_ptr; } |
| 146 | /** @returns @a Borrowed reference. */ |
| 147 | inline operator PyObject*() noexcept { return get(); } |
| 148 | /** @returns @a Borrowed reference. */ |
| 149 | inline operator const PyObject*() const noexcept { return get(); } |
| 150 | |
| 151 | /** @brief Release ownership of Python object pointer. |
| 152 | * @returns @a New reference. |
| 153 | */ |
| 154 | PyObject* release() noexcept; |
| 155 | |
| 156 | /** Convert Python @c str to C++ @c std::string. */ |
| 157 | operator std::string(); |
| 158 | /** Convert Python @c int to C++ @c long. */ |
| 159 | operator long(); |
| 160 | /** Convert Python @c float to C++ @c double. */ |
| 161 | operator double(); |
| 162 | |
| 163 | private: |
| 164 | /** Python object pointer. */ |
| 165 | PyObject* m_ptr = nullptr; |
| 166 | }; |
| 167 | |
| 168 | } // namespace python |
| 169 | } // namespace lbann |
no test coverage detected