Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:17:30

0001 // Copyright (c) 2016-2024 The Pybind Development Team.
0002 // All rights reserved. Use of this source code is governed by a
0003 // BSD-style license that can be found in the LICENSE file.
0004 
0005 #pragma once
0006 
0007 #include "common.h"
0008 
0009 #include <cstddef>
0010 #include <typeinfo>
0011 
0012 PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
0013 PYBIND11_NAMESPACE_BEGIN(detail)
0014 
0015 struct value_and_holder {
0016     instance *inst = nullptr;
0017     size_t index = 0u;
0018     const detail::type_info *type = nullptr;
0019     void **vh = nullptr;
0020 
0021     // Main constructor for a found value/holder:
0022     value_and_holder(instance *i, const detail::type_info *type, size_t vpos, size_t index)
0023         : inst{i}, index{index}, type{type},
0024           vh{inst->simple_layout ? inst->simple_value_holder
0025                                  : &inst->nonsimple.values_and_holders[vpos]} {}
0026 
0027     // Default constructor (used to signal a value-and-holder not found by get_value_and_holder())
0028     value_and_holder() = default;
0029 
0030     // Used for past-the-end iterator
0031     explicit value_and_holder(size_t index) : index{index} {}
0032 
0033     template <typename V = void>
0034     V *&value_ptr() const {
0035         return reinterpret_cast<V *&>(vh[0]);
0036     }
0037     // True if this `value_and_holder` has a non-null value pointer
0038     explicit operator bool() const { return value_ptr() != nullptr; }
0039 
0040     template <typename H>
0041     H &holder() const {
0042         return reinterpret_cast<H &>(vh[1]);
0043     }
0044     bool holder_constructed() const {
0045         return inst->simple_layout
0046                    ? inst->simple_holder_constructed
0047                    : (inst->nonsimple.status[index] & instance::status_holder_constructed) != 0u;
0048     }
0049     // NOLINTNEXTLINE(readability-make-member-function-const)
0050     void set_holder_constructed(bool v = true) {
0051         if (inst->simple_layout) {
0052             inst->simple_holder_constructed = v;
0053         } else if (v) {
0054             inst->nonsimple.status[index] |= instance::status_holder_constructed;
0055         } else {
0056             inst->nonsimple.status[index] &= (std::uint8_t) ~instance::status_holder_constructed;
0057         }
0058     }
0059     bool instance_registered() const {
0060         return inst->simple_layout
0061                    ? inst->simple_instance_registered
0062                    : ((inst->nonsimple.status[index] & instance::status_instance_registered) != 0);
0063     }
0064     // NOLINTNEXTLINE(readability-make-member-function-const)
0065     void set_instance_registered(bool v = true) {
0066         if (inst->simple_layout) {
0067             inst->simple_instance_registered = v;
0068         } else if (v) {
0069             inst->nonsimple.status[index] |= instance::status_instance_registered;
0070         } else {
0071             inst->nonsimple.status[index] &= (std::uint8_t) ~instance::status_instance_registered;
0072         }
0073     }
0074 };
0075 
0076 PYBIND11_NAMESPACE_END(detail)
0077 PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)