| 360 | /// destructor, and avoids the need for reinterpret_cast when passing to vectorcall. |
| 361 | template <std::size_t InlineSize> |
| 362 | class ref_small_vector { |
| 363 | public: |
| 364 | ref_small_vector() = default; |
| 365 | |
| 366 | ~ref_small_vector() { |
| 367 | for (std::size_t i = 0; i < m_ptrs.size(); ++i) { |
| 368 | Py_XDECREF(m_ptrs[i]); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | // Disable copy (prevent accidental double-decref) |
| 373 | ref_small_vector(const ref_small_vector &) = delete; |
| 374 | ref_small_vector &operator=(const ref_small_vector &) = delete; |
| 375 | |
| 376 | // Move is allowed |
| 377 | ref_small_vector(ref_small_vector &&other) noexcept : m_ptrs(std::move(other.m_ptrs)) { |
| 378 | // other.m_ptrs is now empty, so its destructor won't decref anything |
| 379 | } |
| 380 | |
| 381 | ref_small_vector &operator=(ref_small_vector &&other) noexcept { |
| 382 | if (this != &other) { |
| 383 | // Decref our current contents |
| 384 | for (std::size_t i = 0; i < m_ptrs.size(); ++i) { |
| 385 | Py_XDECREF(m_ptrs[i]); |
| 386 | } |
| 387 | m_ptrs = std::move(other.m_ptrs); |
| 388 | } |
| 389 | return *this; |
| 390 | } |
| 391 | |
| 392 | /// Add a pointer, taking ownership (no incref, will decref on destruction) |
| 393 | void push_back_steal(PyObject *p) { m_ptrs.push_back(p); } |
| 394 | |
| 395 | /// Add a pointer, borrowing (increfs now, will decref on destruction) |
| 396 | void push_back_borrow(PyObject *p) { |
| 397 | Py_XINCREF(p); |
| 398 | m_ptrs.push_back(p); |
| 399 | } |
| 400 | |
| 401 | /// Add a null pointer (for PY_VECTORCALL_ARGUMENTS_OFFSET slot) |
| 402 | void push_back_null() { m_ptrs.push_back(nullptr); } |
| 403 | |
| 404 | void reserve(std::size_t sz) { m_ptrs.reserve(sz); } |
| 405 | |
| 406 | std::size_t size() const { return m_ptrs.size(); } |
| 407 | |
| 408 | PyObject *operator[](std::size_t idx) const { return m_ptrs[idx]; } |
| 409 | |
| 410 | PyObject *const *data() const { return m_ptrs.data(); } |
| 411 | |
| 412 | private: |
| 413 | small_vector<PyObject *, InlineSize> m_ptrs; |
| 414 | }; |
| 415 | |
| 416 | PYBIND11_NAMESPACE_END(detail) |
| 417 | PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) |