|
| 1 | +// Copyright (c) 2016-2024 The Pybind Development Team. |
| 2 | +// All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +#pragma once |
| 6 | + |
| 7 | +#include "common.h" |
| 8 | + |
| 9 | +#include <cstddef> |
| 10 | +#include <typeinfo> |
| 11 | + |
| 12 | +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) |
| 13 | +PYBIND11_NAMESPACE_BEGIN(detail) |
| 14 | + |
| 15 | +struct value_and_holder { |
| 16 | + instance *inst = nullptr; |
| 17 | + size_t index = 0u; |
| 18 | + const detail::type_info *type = nullptr; |
| 19 | + void **vh = nullptr; |
| 20 | + |
| 21 | + // Main constructor for a found value/holder: |
| 22 | + value_and_holder(instance *i, const detail::type_info *type, size_t vpos, size_t index) |
| 23 | + : inst{i}, index{index}, type{type}, |
| 24 | + vh{inst->simple_layout ? inst->simple_value_holder |
| 25 | + : &inst->nonsimple.values_and_holders[vpos]} {} |
| 26 | + |
| 27 | + // Default constructor (used to signal a value-and-holder not found by get_value_and_holder()) |
| 28 | + value_and_holder() = default; |
| 29 | + |
| 30 | + // Used for past-the-end iterator |
| 31 | + explicit value_and_holder(size_t index) : index{index} {} |
| 32 | + |
| 33 | + template <typename V = void> |
| 34 | + V *&value_ptr() const { |
| 35 | + return reinterpret_cast<V *&>(vh[0]); |
| 36 | + } |
| 37 | + // True if this `value_and_holder` has a non-null value pointer |
| 38 | + explicit operator bool() const { return value_ptr() != nullptr; } |
| 39 | + |
| 40 | + template <typename H> |
| 41 | + H &holder() const { |
| 42 | + return reinterpret_cast<H &>(vh[1]); |
| 43 | + } |
| 44 | + bool holder_constructed() const { |
| 45 | + return inst->simple_layout |
| 46 | + ? inst->simple_holder_constructed |
| 47 | + : (inst->nonsimple.status[index] & instance::status_holder_constructed) != 0u; |
| 48 | + } |
| 49 | + // NOLINTNEXTLINE(readability-make-member-function-const) |
| 50 | + void set_holder_constructed(bool v = true) { |
| 51 | + if (inst->simple_layout) { |
| 52 | + inst->simple_holder_constructed = v; |
| 53 | + } else if (v) { |
| 54 | + inst->nonsimple.status[index] |= instance::status_holder_constructed; |
| 55 | + } else { |
| 56 | + inst->nonsimple.status[index] &= (std::uint8_t) ~instance::status_holder_constructed; |
| 57 | + } |
| 58 | + } |
| 59 | + bool instance_registered() const { |
| 60 | + return inst->simple_layout |
| 61 | + ? inst->simple_instance_registered |
| 62 | + : ((inst->nonsimple.status[index] & instance::status_instance_registered) != 0); |
| 63 | + } |
| 64 | + // NOLINTNEXTLINE(readability-make-member-function-const) |
| 65 | + void set_instance_registered(bool v = true) { |
| 66 | + if (inst->simple_layout) { |
| 67 | + inst->simple_instance_registered = v; |
| 68 | + } else if (v) { |
| 69 | + inst->nonsimple.status[index] |= instance::status_instance_registered; |
| 70 | + } else { |
| 71 | + inst->nonsimple.status[index] &= (std::uint8_t) ~instance::status_instance_registered; |
| 72 | + } |
| 73 | + } |
| 74 | +}; |
| 75 | + |
| 76 | +PYBIND11_NAMESPACE_END(detail) |
| 77 | +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) |
0 commit comments