Atrinik Server  4.0
atrinik_archetype.c
Go to the documentation of this file.
1 /*************************************************************************
2  * Atrinik, a Multiplayer Online Role Playing Game *
3  * *
4  * Copyright (C) 2009-2014 Alex Tokar and Atrinik Development Team *
5  * *
6  * Fork from Crossfire (Multiplayer game for X-windows). *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the Free Software *
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
21  * *
22  * The author can be reached at admin@atrinik.org *
23  ************************************************************************/
24 
32 #include <plugin_python.h>
33 
37 static fields_struct fields[] = {
38  {"name", FIELDTYPE_SHSTR, offsetof(archetype_t, name), 0, 0,
39  "Name of the archetype.; str (readonly)"},
40  {"head", FIELDTYPE_ARCH, offsetof(archetype_t, head), 0, 0,
41  "The main part of a linked object.; Atrinik.Object.Object or None "
42  "(readonly)"},
43  {"more", FIELDTYPE_ARCH, offsetof(archetype_t, more), 0, 0,
44  "Next part of a linked object.; Atrinik.Object.Object or None "
45  "(readonly)"},
46  {"clone", FIELDTYPE_OBJECT2, offsetof(archetype_t, clone), 0, 0,
47  "Archetype's default object from which new objects are created "
48  "using functions such as :func:`Atrinik.CreateObject`.; "
49  "Atrinik.Object.Object (readonly)"}
50 };
51 
61 static PyObject *get_attribute(Atrinik_Archetype *at, void *context)
62 {
63  return generic_field_getter(context, at->at);
64 }
65 
77 static PyObject *Atrinik_Archetype_new(PyTypeObject *type, PyObject *args,
78  PyObject *kwds)
79 {
80  Atrinik_Archetype *at = (Atrinik_Archetype *) type->tp_alloc(type, 0);
81  if (at != NULL) {
82  at->at = NULL;
83  }
84 
85  return (PyObject *) at;
86 }
87 
94 {
95  at->at = NULL;
96 #ifndef IS_PY_LEGACY
97  Py_TYPE(at)->tp_free((PyObject *) at);
98 #else
99  at->ob_type->tp_free((PyObject *) at);
100 #endif
101 }
102 
111 {
112  return Py_BuildValue("s", at->at->name);
113 }
114 
115 static int Atrinik_Archetype_InternalCompare(Atrinik_Archetype *left,
116  Atrinik_Archetype *right)
117 {
118  return (left->at < right->at ? -1 : (left->at == right->at ? 0 : 1));
119 }
120 
121 static PyObject *Atrinik_Archetype_RichCompare(Atrinik_Archetype *left,
122  Atrinik_Archetype *right, int op)
123 {
124  if (left == NULL || right == NULL ||
125  !PyObject_TypeCheck((PyObject *) left, &Atrinik_ArchetypeType) ||
126  !PyObject_TypeCheck((PyObject *) right, &Atrinik_ArchetypeType)) {
127  Py_INCREF(Py_NotImplemented);
128  return Py_NotImplemented;
129  }
130 
131  return generic_rich_compare(op,
132  Atrinik_Archetype_InternalCompare(left, right));
133 }
134 
138 static PyGetSetDef getseters[NUM_FIELDS + 1];
139 
141 PyTypeObject Atrinik_ArchetypeType = {
142 #ifdef IS_PY3K
143  PyVarObject_HEAD_INIT(NULL, 0)
144 #else
145  PyObject_HEAD_INIT(NULL)
146  0,
147 #endif
148  "Atrinik.Archetype",
149  sizeof(Atrinik_Archetype),
150  0,
151  (destructor) Atrinik_Archetype_dealloc,
152  NULL, NULL, NULL,
153 #ifdef IS_PY3K
154  NULL,
155 #else
156  (cmpfunc) Atrinik_Archetype_InternalCompare,
157 #endif
158  0, 0, 0, 0, 0, 0,
159  (reprfunc) Atrinik_Archetype_str,
160  0, 0, 0,
161  Py_TPFLAGS_DEFAULT,
162  "Atrinik archetypes",
163  NULL, NULL,
164  (richcmpfunc) Atrinik_Archetype_RichCompare,
165  0, 0, 0,
166  NULL,
167  0,
168  getseters,
169  0, 0, 0, 0, 0, 0, 0,
171  0, 0, 0, 0, 0, 0, 0, 0
172 #ifndef IS_PY_LEGACY
173  , 0
174 #endif
175 #ifdef Py_TPFLAGS_HAVE_FINALIZE
176  , NULL
177 #endif
178 };
179 
187 int Atrinik_Archetype_init(PyObject *module)
188 {
189  size_t i;
190 
191  /* Field getters */
192  for (i = 0; i < NUM_FIELDS; i++) {
193  PyGetSetDef *def = &getseters[i];
194 
195  def->name = fields[i].name;
196  def->get = (getter) get_attribute;
197  def->set = NULL;
198  def->doc = fields[i].doc;
199  def->closure = &fields[i];
200  }
201 
202  getseters[i].name = NULL;
203 
204  Atrinik_ArchetypeType.tp_new = PyType_GenericNew;
205 
206  if (PyType_Ready(&Atrinik_ArchetypeType) < 0) {
207  return 0;
208  }
209 
210  Py_INCREF(&Atrinik_ArchetypeType);
211  PyModule_AddObject(module, "Archetype",
212  (PyObject *) &Atrinik_ArchetypeType);
213 
214  return 1;
215 }
216 
225 {
226  /* Return None if no archetype was to be wrapped. */
227  if (at == NULL) {
228  Py_INCREF(Py_None);
229  return Py_None;
230  }
231 
232  Atrinik_Archetype *wrapper = PyObject_NEW(Atrinik_Archetype,
233  &Atrinik_ArchetypeType);
234  if (wrapper != NULL) {
235  wrapper->at = at;
236  }
237 
238  return (PyObject *) wrapper;
239 }
240 
uint8_t type
One of operation types.
Definition: sound_ambient.c:45
PyObject * generic_rich_compare(int op, int result)
PyObject_HEAD archetype_t * at
PyTypeObject Atrinik_ArchetypeType
#define IS_PY_LEGACY
Definition: plugin_python.h:55
int Atrinik_Archetype_init(PyObject *module)
Definition: arch.h:40
#define NUM_FIELDS
PyObject * wrap_archetype(archetype_t *at)
static PyObject * Atrinik_Archetype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
static fields_struct fields[]
static void Atrinik_Archetype_dealloc(Atrinik_Archetype *at)
PyObject * generic_field_getter(fields_struct *field, void *ptr)
shstr * name
More definite name, like "kobold".
Definition: arch.h:46
static PyGetSetDef getseters[NUM_FIELDS+1]
static PyObject * get_attribute(Atrinik_Archetype *at, void *context)
static PyObject * Atrinik_Archetype_str(Atrinik_Archetype *at)