• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PredatorCZ / PreCore / 460

pending completion
460

push

github-actions-ci

PredatorCZ
try fix coverage

3204 of 6095 relevant lines covered (52.57%)

354.19 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.11
/include/python/uni/enum.hpp
1
/*  Python binding class for enumerations
2
    part of uni module
3
    Copyright 2020-2022 Lukas Cone
4

5
    Licensed under the Apache License, Version 2.0 (the "License");
6
    you may not use this file except in compliance with the License.
7
    You may obtain a copy of the License at
8

9
        http://www.apache.org/licenses/LICENSE-2.0
10

11
    Unless required by applicable law or agreed to in writing, software
12
    distributed under the License is distributed on an "AS IS" BASIS,
13
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
    See the License for the specific language governing permissions and
15
    limitations under the License.
16
*/
17

18
#pragma once
19
#include <Python.h>
20
#include <algorithm>
21
#include <string_view>
22
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
23

24
namespace UniPy {
25
template <class Info> struct Enum {
26
  PyObject_HEAD;
27

28
  static PyTypeObject *GetType() {
29
    static PyMappingMethods mappingMethods = {
30
        (lenfunc)Info::Len,
31
        Subscript,
32
        0,
33
    };
34

35
    static PyTypeObject typeType{
36
        .tp_name = Info::GetName(),
37
        .tp_basicsize = sizeof(Enum),
38
        .tp_getattr = GetAttribute,
39
        .tp_as_mapping = &mappingMethods,
40
        .tp_flags = Py_TPFLAGS_DEFAULT,
41
        .tp_doc = Info::GetDoc(),
42
        .tp_new = New,
43
    };
44

45
    return &typeType;
46
  }
47

48
  static PyObject *New(PyTypeObject *type, PyObject *, PyObject *) {
3✔
49
    return type->tp_alloc(type, 0);
3✔
50
  }
51

1✔
52
  static PyObject *Subscript(PyObject *, PyObject *index) {
1✔
53
    return SubscriptRaw(PyLong_AsSize_t(index));
54
  }
1✔
55

1✔
56
  static PyObject *SubscriptRaw(size_t index) {
57
    if (index >= Info::Len(nullptr)) {
1✔
58
      PyErr_SetString(PyExc_IndexError, "index out of range");
1✔
59
      return nullptr;
60
    }
61

13✔
62
    const auto cName = std::next(Info::begin(), index)->name;
13✔
63

64
    return PyUnicode_FromStringAndSize(cName.data(), cName.size());
3✔
65
  }
3✔
66

67
  static PyObject *GetAttribute(PyObject *, char *attrName) {
4✔
68
    std::string_view attrNameRef(attrName);
4✔
69
    const auto foundEnum = std::find_if(Info::begin(), Info::end(),
70
                                        [&](typename Info::value_type &typ) {
6✔
71
                                          return typ.name == attrNameRef;
6✔
72
                                        });
73

74
    if (foundEnum == Info::end()) {
13✔
75
      return nullptr;
13✔
76
    }
1✔
77

1✔
78
    return PyLong_FromSize_t(foundEnum->id);
79
  }
80
};
24✔
81
} // namespace UniPy
82

12✔
83
#pragma GCC diagnostic pop
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc