Debian Codegraph @

py/src/strength.cpp

raw
1 /*-----------------------------------------------------------------------------
2 | Copyright (c) 2013-2026, Nucleic Development Team.
3 |
4 | Distributed under the terms of the Modified BSD License.
5 |
6 | The full license is in the file LICENSE, distributed with this software.
7 |----------------------------------------------------------------------------*/
8 #include <cppy/cppy.h>
9 #include <kiwi/kiwi.h>
10 #include "util.h"
11
12
13 #ifdef __clang__
14 #pragma clang diagnostic ignored "-Wdeprecated-writable-strings"
15 #endif
16
17 #ifdef __GNUC__
18 #pragma GCC diagnostic ignored "-Wwrite-strings"
19 #endif
20
21
22 namespace kiwisolver
23 {
24
25
26 namespace
27 {
28
29
30 void
31 strength_dealloc( PyObject* self )
32 {
33 Py_TYPE( self )->tp_free( self );
34 }
35
36
37 PyObject*
38 strength_weak( strength* self )
39 {
40 return PyFloat_FromDouble( kiwi::strength::weak );
41 }
42
43
44 PyObject*
45 strength_medium( strength* self )
46 {
47 return PyFloat_FromDouble( kiwi::strength::medium );
48 }
49
50
51 PyObject*
52 strength_strong( strength* self )
53 {
54 return PyFloat_FromDouble( kiwi::strength::strong );
55 }
56
57
58 PyObject*
59 strength_required( strength* self )
60 {
61 return PyFloat_FromDouble( kiwi::strength::required );
62 }
63
64
65 PyObject*
66 strength_create( strength* self, PyObject* args )
67 {
68 PyObject* pya;
69 PyObject* pyb;
70 PyObject* pyc;
71 PyObject* pyw = 0;
72 if( !PyArg_ParseTuple( args, "OOO|O", &pya, &pyb, &pyc, &pyw ) )
73 return 0;
74 double a, b, c;
75 double w = 1.0;
76 if( !convert_to_double( pya, a ) )
77 return 0;
78 if( !convert_to_double( pyb, b ) )
79 return 0;
80 if( !convert_to_double( pyc, c ) )
81 return 0;
82 if( pyw && !convert_to_double( pyw, w ) )
83 return 0;
84 return PyFloat_FromDouble( kiwi::strength::create( a, b, c, w ) );
85 }
86
87
88 static PyGetSetDef
89 strength_getset[] = {
90 { "weak", ( getter )strength_weak, 0,
91 "The predefined weak strength." },
92 { "medium", ( getter )strength_medium, 0,
93 "The predefined medium strength." },
94 { "strong", ( getter )strength_strong, 0,
95 "The predefined strong strength." },
96 { "required", ( getter )strength_required, 0,
97 "The predefined required strength." },
98 { 0 } // sentinel
99 };
100
101
102 static PyMethodDef
103 strength_methods[] = {
104 { "create", ( PyCFunction )strength_create, METH_VARARGS,
105 "Create a strength from constituent values and optional weight." },
106 { 0 } // sentinel
107 };
108
109
110
111 static PyType_Slot strength_Type_slots[] = {
112 { Py_tp_dealloc, void_cast( strength_dealloc ) }, /* tp_dealloc */
113 { Py_tp_getset, void_cast( strength_getset ) }, /* tp_getset */
114 { Py_tp_methods, void_cast( strength_methods ) }, /* tp_methods */
115 { Py_tp_alloc, void_cast( PyType_GenericAlloc ) }, /* tp_alloc */
116 { Py_tp_free, void_cast( PyObject_Del ) }, /* tp_free */
117 { 0, 0 },
118 };
119
120
121 } // namespace
122
123
124 // Initialize static variables (otherwise the compiler eliminates them)
125 PyTypeObject* strength::TypeObject = NULL;
126
127
128 PyType_Spec strength::TypeObject_Spec = {
129 "kiwisolver.Strength", /* tp_name */
130 sizeof( strength ), /* tp_basicsize */
131 0, /* tp_itemsize */
132 Py_TPFLAGS_DEFAULT, /* tp_flags */
133 strength_Type_slots /* slots */
134 };
135
136
137 bool strength::Ready()
138 {
139 // The reference will be handled by the module to which we will add the type
140 TypeObject = pytype_cast( PyType_FromSpec( &TypeObject_Spec ) );
141 if( !TypeObject )
142 {
143 return false;
144 }
145 return true;
146 }
147
148
149 } // namespace