strength.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*-----------------------------------------------------------------------------
  2. | Copyright (c) 2013-2017, Nucleic Development Team.
  3. |
  4. | Distributed under the terms of the Modified BSD License.
  5. |
  6. | The full license is in the file COPYING.txt, distributed with this software.
  7. |----------------------------------------------------------------------------*/
  8. #include <Python.h>
  9. #include <kiwi/kiwi.h>
  10. #include "pythonhelpers.h"
  11. #include "util.h"
  12. using namespace PythonHelpers;
  13. struct strength
  14. {
  15. PyObject_HEAD;
  16. };
  17. static void
  18. strength_dealloc( PyObject* self )
  19. {
  20. Py_TYPE( self )->tp_free( self );
  21. }
  22. static PyObject*
  23. strength_weak( strength* self )
  24. {
  25. return PyFloat_FromDouble( kiwi::strength::weak );
  26. }
  27. static PyObject*
  28. strength_medium( strength* self )
  29. {
  30. return PyFloat_FromDouble( kiwi::strength::medium );
  31. }
  32. static PyObject*
  33. strength_strong( strength* self )
  34. {
  35. return PyFloat_FromDouble( kiwi::strength::strong );
  36. }
  37. static PyObject*
  38. strength_required( strength* self )
  39. {
  40. return PyFloat_FromDouble( kiwi::strength::required );
  41. }
  42. static PyObject*
  43. strength_create( strength* self, PyObject* args )
  44. {
  45. PyObject* pya;
  46. PyObject* pyb;
  47. PyObject* pyc;
  48. PyObject* pyw = 0;
  49. if( !PyArg_ParseTuple( args, "OOO|O", &pya, &pyb, &pyc, &pyw ) )
  50. return 0;
  51. double a, b, c;
  52. double w = 1.0;
  53. if( !convert_to_double( pya, a ) )
  54. return 0;
  55. if( !convert_to_double( pyb, b ) )
  56. return 0;
  57. if( !convert_to_double( pyc, c ) )
  58. return 0;
  59. if( pyw && !convert_to_double( pyw, w ) )
  60. return 0;
  61. return PyFloat_FromDouble( kiwi::strength::create( a, b, c, w ) );
  62. }
  63. static PyGetSetDef
  64. strength_getset[] = {
  65. { "weak", ( getter )strength_weak, 0,
  66. "The predefined weak strength." },
  67. { "medium", ( getter )strength_medium, 0,
  68. "The predefined medium strength." },
  69. { "strong", ( getter )strength_strong, 0,
  70. "The predefined strong strength." },
  71. { "required", ( getter )strength_required, 0,
  72. "The predefined required strength." },
  73. { 0 } // sentinel
  74. };
  75. static PyMethodDef
  76. strength_methods[] = {
  77. { "create", ( PyCFunction )strength_create, METH_VARARGS,
  78. "Create a strength from constituent values and optional weight." },
  79. { 0 } // sentinel
  80. };
  81. PyTypeObject strength_Type = {
  82. PyVarObject_HEAD_INIT( &PyType_Type, 0 )
  83. "kiwisolver.strength", /* tp_name */
  84. sizeof( strength ), /* tp_basicsize */
  85. 0, /* tp_itemsize */
  86. (destructor)strength_dealloc, /* tp_dealloc */
  87. (printfunc)0, /* tp_print */
  88. (getattrfunc)0, /* tp_getattr */
  89. (setattrfunc)0, /* tp_setattr */
  90. #if PY_VERSION_HEX >= 0x03050000
  91. ( PyAsyncMethods* )0, /* tp_as_async */
  92. #elif PY_VERSION_HEX >= 0x03000000
  93. ( void* ) 0, /* tp_reserved */
  94. #else
  95. ( cmpfunc )0, /* tp_compare */
  96. #endif
  97. (reprfunc)0, /* tp_repr */
  98. (PyNumberMethods*)0, /* tp_as_number */
  99. (PySequenceMethods*)0, /* tp_as_sequence */
  100. (PyMappingMethods*)0, /* tp_as_mapping */
  101. (hashfunc)0, /* tp_hash */
  102. (ternaryfunc)0, /* tp_call */
  103. (reprfunc)0, /* tp_str */
  104. (getattrofunc)0, /* tp_getattro */
  105. (setattrofunc)0, /* tp_setattro */
  106. (PyBufferProcs*)0, /* tp_as_buffer */
  107. Py_TPFLAGS_DEFAULT, /* tp_flags */
  108. 0, /* Documentation string */
  109. (traverseproc)0, /* tp_traverse */
  110. (inquiry)0, /* tp_clear */
  111. (richcmpfunc)0, /* tp_richcompare */
  112. 0, /* tp_weaklistoffset */
  113. (getiterfunc)0, /* tp_iter */
  114. (iternextfunc)0, /* tp_iternext */
  115. (struct PyMethodDef*)strength_methods, /* tp_methods */
  116. (struct PyMemberDef*)0, /* tp_members */
  117. strength_getset, /* tp_getset */
  118. 0, /* tp_base */
  119. 0, /* tp_dict */
  120. (descrgetfunc)0, /* tp_descr_get */
  121. (descrsetfunc)0, /* tp_descr_set */
  122. 0, /* tp_dictoffset */
  123. (initproc)0, /* tp_init */
  124. (allocfunc)PyType_GenericAlloc, /* tp_alloc */
  125. (newfunc)0, /* tp_new */
  126. (freefunc)PyObject_Del, /* tp_free */
  127. (inquiry)0, /* tp_is_gc */
  128. 0, /* tp_bases */
  129. 0, /* tp_mro */
  130. 0, /* tp_cache */
  131. 0, /* tp_subclasses */
  132. 0, /* tp_weaklist */
  133. (destructor)0 /* tp_del */
  134. };
  135. int import_strength()
  136. {
  137. return PyType_Ready( &strength_Type );
  138. }