aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/kiwisolver/py3/py/variable.cpp
blob: 1a9c69f8f65f14b55cdd22962375371c9eb88df2 (plain) (blame)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*-----------------------------------------------------------------------------
| Copyright (c) 2013-2019, Nucleic Development Team.
|
| Distributed under the terms of the Modified BSD License.
|
| The full license is in the file LICENSE, distributed with this software.
|----------------------------------------------------------------------------*/
#include <cppy/cppy.h>
#include <kiwi/kiwi.h>
#include "symbolics.h"
#include "types.h"
#include "util.h"


namespace kiwisolver
{


namespace
{


PyObject*
Variable_new( PyTypeObject* type, PyObject* args, PyObject* kwargs )
{
	static const char *kwlist[] = { "name", "context", 0 };
	PyObject* context = 0;
	PyObject* name = 0;

	if( !PyArg_ParseTupleAndKeywords(
		args, kwargs, "|OO:__new__", const_cast<char**>( kwlist ),
		&name, &context ) )
		return 0;

	cppy::ptr pyvar( PyType_GenericNew( type, args, kwargs ) );
	if( !pyvar )
		return 0;

	Variable* self = reinterpret_cast<Variable*>( pyvar.get() );
	self->context = cppy::xincref( context );

	if( name != 0 )
	{
		if( !PyUnicode_Check( name ) )
			return cppy::type_error( name, "str" );
		std::string c_name;
		if( !convert_pystr_to_str(name, c_name) )
			return 0;  // LCOV_EXCL_LINE
		new( &self->variable ) kiwi::Variable( c_name );
	}
	else
	{
		new( &self->variable ) kiwi::Variable();
	}

	return pyvar.release();
}


void
Variable_clear( Variable* self )
{
	Py_CLEAR( self->context );
}


int
Variable_traverse( Variable* self, visitproc visit, void* arg )
{
	Py_VISIT( self->context );
	return 0;
}


void
Variable_dealloc( Variable* self )
{
	PyObject_GC_UnTrack( self );
	Variable_clear( self );
	self->variable.~Variable();
	Py_TYPE( self )->tp_free( pyobject_cast( self ) );
}


PyObject*
Variable_repr( Variable* self )
{
	return PyUnicode_FromString( self->variable.name().c_str() );
}


PyObject*
Variable_name( Variable* self )
{
	return PyUnicode_FromString( self->variable.name().c_str() );
}


PyObject*
Variable_setName( Variable* self, PyObject* pystr )
{
	if( !PyUnicode_Check( pystr ) )
		return cppy::type_error( pystr, "str" );
   std::string str;
   if( !convert_pystr_to_str( pystr, str ) )
       return 0;
   self->variable.setName( str );
	Py_RETURN_NONE;
}


PyObject*
Variable_context( Variable* self )
{
	if( self->context )
		return cppy::incref( self->context );
	Py_RETURN_NONE;
}


PyObject*
Variable_setContext( Variable* self, PyObject* value )
{
	if( value != self->context )
	{
		PyObject* temp = self->context;
		self->context = cppy::incref( value );
		Py_XDECREF( temp );
	}
	Py_RETURN_NONE;
}


PyObject*
Variable_value( Variable* self )
{
	return PyFloat_FromDouble( self->variable.value() );
}


PyObject*
Variable_add( PyObject* first, PyObject* second )
{
	return BinaryInvoke<BinaryAdd, Variable>()( first, second );
}


PyObject*
Variable_sub( PyObject* first, PyObject* second )
{
	return BinaryInvoke<BinarySub, Variable>()( first, second );
}


PyObject*
Variable_mul( PyObject* first, PyObject* second )
{
	return BinaryInvoke<BinaryMul, Variable>()( first, second );
}


PyObject*
Variable_div( PyObject* first, PyObject* second )
{
	return BinaryInvoke<BinaryDiv, Variable>()( first, second );
}


PyObject*
Variable_neg( PyObject* value )
{
	return UnaryInvoke<UnaryNeg, Variable>()( value );
}


PyObject*
Variable_richcmp( PyObject* first, PyObject* second, int op )
{
	switch( op )
	{
		case Py_EQ:
			return BinaryInvoke<CmpEQ, Variable>()( first, second );
		case Py_LE:
			return BinaryInvoke<CmpLE, Variable>()( first, second );
		case Py_GE:
			return BinaryInvoke<CmpGE, Variable>()( first, second );
		default:
			break;
	}
	PyErr_Format(
		PyExc_TypeError,
		"unsupported operand type(s) for %s: "
		"'%.100s' and '%.100s'",
		pyop_str( op ),
		first->ob_type->tp_name,
		second->ob_type->tp_name
	);
	return 0;
}


static PyMethodDef
Variable_methods[] = {
	{ "name", ( PyCFunction )Variable_name, METH_NOARGS,
	  "Get the name of the variable." },
	{ "setName", ( PyCFunction )Variable_setName, METH_O,
	  "Set the name of the variable." },
	{ "context", ( PyCFunction )Variable_context, METH_NOARGS,
	  "Get the context object associated with the variable." },
	{ "setContext", ( PyCFunction )Variable_setContext, METH_O,
	  "Set the context object associated with the variable." },
	{ "value", ( PyCFunction )Variable_value, METH_NOARGS,
	  "Get the current value of the variable." },
	{ 0 } // sentinel
};


static PyType_Slot Variable_Type_slots[] = {
    { Py_tp_dealloc, void_cast( Variable_dealloc ) },      /* tp_dealloc */
    { Py_tp_traverse, void_cast( Variable_traverse ) },    /* tp_traverse */
    { Py_tp_clear, void_cast( Variable_clear ) },          /* tp_clear */
    { Py_tp_repr, void_cast( Variable_repr ) },            /* tp_repr */
    { Py_tp_richcompare, void_cast( Variable_richcmp ) },  /* tp_richcompare */
    { Py_tp_methods, void_cast( Variable_methods ) },      /* tp_methods */
    { Py_tp_new, void_cast( Variable_new ) },              /* tp_new */
    { Py_tp_alloc, void_cast( PyType_GenericAlloc ) },     /* tp_alloc */
    { Py_tp_free, void_cast( PyObject_GC_Del ) },          /* tp_free */
    { Py_nb_add, void_cast( Variable_add ) },              /* nb_add */
    { Py_nb_subtract, void_cast( Variable_sub ) },         /* nb_subtract */
    { Py_nb_multiply, void_cast( Variable_mul ) },         /* nb_multiply */
    { Py_nb_negative, void_cast( Variable_neg ) },         /* nb_negative */
    { Py_nb_true_divide, void_cast( Variable_div ) },      /* nb_true_divide */
    { 0, 0 },
};


} // namespace


// Initialize static variables (otherwise the compiler eliminates them)
PyTypeObject* Variable::TypeObject = NULL;


PyType_Spec Variable::TypeObject_Spec = {
	"kiwisolver.Variable",             /* tp_name */
	sizeof( Variable ),                /* tp_basicsize */
	0,                                 /* tp_itemsize */
	Py_TPFLAGS_DEFAULT|
    Py_TPFLAGS_HAVE_GC|
    Py_TPFLAGS_BASETYPE,               /* tp_flags */
    Variable_Type_slots                /* slots */
};


bool Variable::Ready()
{
    // The reference will be handled by the module to which we will add the type
	TypeObject = pytype_cast( PyType_FromSpec( &TypeObject_Spec ) );
    if( !TypeObject )
    {
        return false;
    }
    return true;
}

}  // namespace kiwisolver