Imported Upstream version 1.2.0
[platform/upstream/dbus-python.git] / _dbus_bindings / float.c
1 /* Simple D-Bus types: Double and (with appropriate #defines) Float
2  *
3  * Copyright (C) 2006 Collabora Ltd. <http://www.collabora.co.uk/>
4  *
5  * Permission is hereby granted, free of charge, to any person
6  * obtaining a copy of this software and associated documentation
7  * files (the "Software"), to deal in the Software without
8  * restriction, including without limitation the rights to use, copy,
9  * modify, merge, publish, distribute, sublicense, and/or sell copies
10  * of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  */
25
26 #include <Python.h>
27 #include <structmember.h>
28
29 #include "dbus_bindings-internal.h"
30 #include "types-internal.h"
31
32 PyDoc_STRVAR(Double_tp_doc,
33 "A double-precision floating point number (a subtype of float).");
34
35 #ifdef WITH_DBUS_FLOAT32
36 PyDoc_STRVAR(Float_tp_doc,
37 "A single-precision floating point number (a subtype of float).");
38 #endif
39
40 PyTypeObject DBusPyDouble_Type = {
41     PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
42     "dbus.Double",
43     0,
44     0,
45     0,                                      /* tp_dealloc */
46     0,                                      /* tp_print */
47     0,                                      /* tp_getattr */
48     0,                                      /* tp_setattr */
49     0,                                      /* tp_compare */
50     0,                                      /* tp_repr */
51     0,                                      /* tp_as_number */
52     0,                                      /* tp_as_sequence */
53     0,                                      /* tp_as_mapping */
54     0,                                      /* tp_hash */
55     0,                                      /* tp_call */
56     0,                                      /* tp_str */
57     0,                                      /* tp_getattro */
58     0,                                      /* tp_setattro */
59     0,                                      /* tp_as_buffer */
60     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
61     Double_tp_doc,                          /* tp_doc */
62     0,                                      /* tp_traverse */
63     0,                                      /* tp_clear */
64     0,                                      /* tp_richcompare */
65     0,                                      /* tp_weaklistoffset */
66     0,                                      /* tp_iter */
67     0,                                      /* tp_iternext */
68     0,                                      /* tp_methods */
69     0,                                      /* tp_members */
70     0,                                      /* tp_getset */
71     DEFERRED_ADDRESS(&DBusPythonFloatType), /* tp_base */
72     0,                                      /* tp_dict */
73     0,                                      /* tp_descr_get */
74     0,                                      /* tp_descr_set */
75     0,                                      /* tp_dictoffset */
76     0,                                      /* tp_init */
77     0,                                      /* tp_alloc */
78     0,                                      /* tp_new */
79 };
80
81 #ifdef WITH_DBUS_FLOAT32
82
83 PyTypeObject DBusPyFloat_Type = {
84     PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
85     "dbus.Float",
86     0,
87     0,
88     0,                                      /* tp_dealloc */
89     0,                                      /* tp_print */
90     0,                                      /* tp_getattr */
91     0,                                      /* tp_setattr */
92     0,                                      /* tp_compare */
93     0,                                      /* tp_repr */
94     0,                                      /* tp_as_number */
95     0,                                      /* tp_as_sequence */
96     0,                                      /* tp_as_mapping */
97     0,                                      /* tp_hash */
98     0,                                      /* tp_call */
99     0,                                      /* tp_str */
100     0,                                      /* tp_getattro */
101     0,                                      /* tp_setattro */
102     0,                                      /* tp_as_buffer */
103     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
104     Float_tp_doc,                           /* tp_doc */
105     0,                                      /* tp_traverse */
106     0,                                      /* tp_clear */
107     0,                                      /* tp_richcompare */
108     0,                                      /* tp_weaklistoffset */
109     0,                                      /* tp_iter */
110     0,                                      /* tp_iternext */
111     0,                                      /* tp_methods */
112     0,                                      /* tp_members */
113     0,                                      /* tp_getset */
114     DEFERRED_ADDRESS(&DBusPythonFloatType), /* tp_base */
115     0,                                      /* tp_dict */
116     0,                                      /* tp_descr_get */
117     0,                                      /* tp_descr_set */
118     0,                                      /* tp_dictoffset */
119     0,                                      /* tp_init */
120     0,                                      /* tp_alloc */
121     0,                                      /* tp_new */
122 };
123 #endif /* defined(WITH_DBUS_FLOAT32) */
124
125 dbus_bool_t
126 dbus_py_init_float_types(void)
127 {
128     DBusPyDouble_Type.tp_base = &DBusPyFloatBase_Type;
129     if (PyType_Ready(&DBusPyDouble_Type) < 0) return 0;
130     DBusPyDouble_Type.tp_print = NULL;
131
132 #ifdef WITH_DBUS_FLOAT32
133     DBusPyFloat_Type.tp_base = &DBusPyFloatBase_Type;
134     if (PyType_Ready(&DBusPyFloat_Type) < 0) return 0;
135     DBusPyFloat_Type.tp_print = NULL;
136 #endif
137
138     return 1;
139 }
140
141 dbus_bool_t
142 dbus_py_insert_float_types(PyObject *this_module)
143 {
144     /* PyModule_AddObject steals a ref */
145     Py_INCREF(&DBusPyDouble_Type);
146     if (PyModule_AddObject(this_module, "Double",
147                            (PyObject *)&DBusPyDouble_Type) < 0) return 0;
148 #ifdef WITH_DBUS_FLOAT32
149     Py_INCREF(&DBusPyFloat_Type);
150     if (PyModule_AddObject(this_module, "Float",
151                            (PyObject *)&DBusPyFloat_Type) < 0) return 0;
152 #endif
153
154     return 1;
155 }