File size: 2,477 Bytes
838f737
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright (c) 2022-2025 The pybind Community.
// All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

#pragma once

#include "detail/common.h"
#include "detail/native_enum_data.h"
#include "detail/type_caster_base.h"
#include "cast.h"

#include <cassert>
#include <limits>
#include <type_traits>
#include <typeindex>

PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)

/// Conversions between Python's native (stdlib) enum types and C++ enums.
template <typename EnumType>
class native_enum : public detail::native_enum_data {
public:
    using Underlying = typename std::underlying_type<EnumType>::type;

    native_enum(const object &parent_scope,
                const char *name,
                const char *native_type_name,
                const char *class_doc = "")
        : detail::native_enum_data(
              parent_scope, name, native_type_name, class_doc, std::type_index(typeid(EnumType))) {
        if (detail::get_local_type_info(typeid(EnumType)) != nullptr
            || detail::get_global_type_info(typeid(EnumType)) != nullptr) {
            pybind11_fail(
                "pybind11::native_enum<...>(\"" + enum_name_encoded
                + "\") is already registered as a `pybind11::enum_` or `pybind11::class_`!");
        }
        if (detail::global_internals_native_enum_type_map_contains(enum_type_index)) {
            pybind11_fail("pybind11::native_enum<...>(\"" + enum_name_encoded
                          + "\") is already registered!");
        }
        arm_finalize_check();
    }

    /// Export enumeration entries into the parent scope
    native_enum &export_values() {
        assert(!export_values_flag); // Catch redundant calls.
        export_values_flag = true;
        return *this;
    }

    /// Add an enumeration entry
    native_enum &value(char const *name, EnumType value, const char *doc = nullptr) {
        // Disarm for the case that the native_enum_data dtor runs during exception unwinding.
        disarm_finalize_check("value after finalize");
        members.append(make_tuple(name, static_cast<Underlying>(value)));
        if (doc) {
            member_docs.append(make_tuple(name, doc));
        }
        arm_finalize_check(); // There was no exception.
        return *this;
    }

    native_enum(const native_enum &) = delete;
    native_enum &operator=(const native_enum &) = delete;
};

PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)