strength.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*-----------------------------------------------------------------------------
  2. | Copyright (c) 2013-2019, 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. #ifdef __clang__
  12. #pragma clang diagnostic ignored "-Wdeprecated-writable-strings"
  13. #endif
  14. #ifdef __GNUC__
  15. #pragma GCC diagnostic ignored "-Wwrite-strings"
  16. #endif
  17. namespace kiwisolver
  18. {
  19. namespace
  20. {
  21. void
  22. strength_dealloc( PyObject* self )
  23. {
  24. Py_TYPE( self )->tp_free( self );
  25. }
  26. PyObject*
  27. strength_weak( strength* self )
  28. {
  29. return PyFloat_FromDouble( kiwi::strength::weak );
  30. }
  31. PyObject*
  32. strength_medium( strength* self )
  33. {
  34. return PyFloat_FromDouble( kiwi::strength::medium );
  35. }
  36. PyObject*
  37. strength_strong( strength* self )
  38. {
  39. return PyFloat_FromDouble( kiwi::strength::strong );
  40. }
  41. PyObject*
  42. strength_required( strength* self )
  43. {
  44. return PyFloat_FromDouble( kiwi::strength::required );
  45. }
  46. PyObject*
  47. strength_create( strength* self, PyObject* args )
  48. {
  49. PyObject* pya;
  50. PyObject* pyb;
  51. PyObject* pyc;
  52. PyObject* pyw = 0;
  53. if( !PyArg_ParseTuple( args, "OOO|O", &pya, &pyb, &pyc, &pyw ) )
  54. return 0;
  55. double a, b, c;
  56. double w = 1.0;
  57. if( !convert_to_double( pya, a ) )
  58. return 0;
  59. if( !convert_to_double( pyb, b ) )
  60. return 0;
  61. if( !convert_to_double( pyc, c ) )
  62. return 0;
  63. if( pyw && !convert_to_double( pyw, w ) )
  64. return 0;
  65. return PyFloat_FromDouble( kiwi::strength::create( a, b, c, w ) );
  66. }
  67. static PyGetSetDef
  68. strength_getset[] = {
  69. { "weak", ( getter )strength_weak, 0,
  70. "The predefined weak strength." },
  71. { "medium", ( getter )strength_medium, 0,
  72. "The predefined medium strength." },
  73. { "strong", ( getter )strength_strong, 0,
  74. "The predefined strong strength." },
  75. { "required", ( getter )strength_required, 0,
  76. "The predefined required strength." },
  77. { 0 } // sentinel
  78. };
  79. static PyMethodDef
  80. strength_methods[] = {
  81. { "create", ( PyCFunction )strength_create, METH_VARARGS,
  82. "Create a strength from constituent values and optional weight." },
  83. { 0 } // sentinel
  84. };
  85. static PyType_Slot strength_Type_slots[] = {
  86. { Py_tp_dealloc, void_cast( strength_dealloc ) }, /* tp_dealloc */
  87. { Py_tp_getset, void_cast( strength_getset ) }, /* tp_getset */
  88. { Py_tp_methods, void_cast( strength_methods ) }, /* tp_methods */
  89. { Py_tp_alloc, void_cast( PyType_GenericAlloc ) }, /* tp_alloc */
  90. { Py_tp_free, void_cast( PyObject_Del ) }, /* tp_free */
  91. { 0, 0 },
  92. };
  93. } // namespace
  94. // Initialize static variables (otherwise the compiler eliminates them)
  95. PyTypeObject* strength::TypeObject = NULL;
  96. PyType_Spec strength::TypeObject_Spec = {
  97. "kiwisolver.strength", /* tp_name */
  98. sizeof( strength ), /* tp_basicsize */
  99. 0, /* tp_itemsize */
  100. Py_TPFLAGS_DEFAULT, /* tp_flags */
  101. strength_Type_slots /* slots */
  102. };
  103. bool strength::Ready()
  104. {
  105. // The reference will be handled by the module to which we will add the type
  106. TypeObject = pytype_cast( PyType_FromSpec( &TypeObject_Spec ) );
  107. if( !TypeObject )
  108. {
  109. return false;
  110. }
  111. return true;
  112. }
  113. } // namespace