Atrinik Server 2.5
Functions | Variables
plugins/plugin_python/attr_list.c File Reference
#include <plugin_python.h>

Go to the source code of this file.

Functions

static void * attr_list_len_ptr (Atrinik_AttrList *al)
static unsigned PY_LONG_LONG attr_list_len (Atrinik_AttrList *al)
static PyObject * attr_list_get (Atrinik_AttrList *al, void *idx)
static int attr_list_contains (Atrinik_AttrList *al, PyObject *value)
static int attr_list_set (Atrinik_AttrList *al, void *idx, PyObject *value)
static PyObject * __getitem__ (Atrinik_AttrList *al, PyObject *key)
static PyObject * append (Atrinik_AttrList *al, PyObject *value)
static int InternalCompare (Atrinik_AttrList *left, Atrinik_AttrList *right)
static PyObject * RichCompare (Atrinik_AttrList *left, Atrinik_AttrList *right, int op)
static PyObject * iter (PyObject *seq)
static PyObject * iternext (Atrinik_AttrList *al)
static Py_ssize_t __len__ (Atrinik_AttrList *al)
static int __setitem__ (Atrinik_AttrList *al, PyObject *key, PyObject *value)
static int __contains__ (Atrinik_AttrList *al, PyObject *value)
int Atrinik_AttrList_init (PyObject *module)
PyObject * wrap_attr_list (void *ptr, size_t offset, field_type field)

Variables

static PyMethodDef methods []
static PySequenceMethods SequenceMethods
static PyMappingMethods MappingMethods
PyTypeObject Atrinik_AttrListType

Detailed Description

Implements AttrList Python object; this is a class similar to Python lists, but with more limited functionality (it does not implement many methods normal lists do; however, the AttrList object can be converted to a list).

These objects are used to wrap an array of data, for example, player::known_spells array, which holds the spell IDs the player knows. When Python requests to get player.known_spells, the code generates an AttrList object, which "wraps" the actual array of known spells - it holds a pointer to the structure, the offset in the structure, etc. Thus, it is possible for Python to use __setitem__() and __getitem__() methods on this AttrList object to change/extract the array data in the most efficient way possible; all of the data of the array is never held in memory (unless you implicitly convert it to a list, of course).

The code uses many, many castings and does extensive error checking of passed arguments, as the smallest imperfection could cause a memory corruption, most likely resulting in a crash.

If you are adding more field types to handle your array, you will need to modify the following functions:

Author:
Alex Tokar

Definition in file attr_list.c.


Function Documentation

static int __contains__ ( Atrinik_AttrList al,
PyObject *  value 
) [static]

Wrapper for attr_list_contains(), used by the __contains__() method.

Parameters:
alAttrList pointer __contains__() is being called on.
Returns:
Return value of attr_list_contains().

Definition at line 517 of file attr_list.c.

static PyObject* __getitem__ ( Atrinik_AttrList al,
PyObject *  key 
) [static]

Implements the __getitem__() method.

Parameters:
alThe AttrList object.
keyIndex to try and find.
Returns:
The object from the AttrList at the specified index, NULL on failure.

Definition at line 332 of file attr_list.c.

static Py_ssize_t __len__ ( Atrinik_AttrList al) [static]

Wrapper for attr_list_len(), using Py_ssize_t return value.

Used by the len() method.

Parameters:
alAttrList pointer len() is being called on.
Returns:
Length of the attribute list.

Definition at line 487 of file attr_list.c.

static int __setitem__ ( Atrinik_AttrList al,
PyObject *  key,
PyObject *  value 
) [static]

Wrapper for attr_list_set(), used by the __setitem__() method.

Parameters:
alAttrList pointer __setitem__() is being called on.
Returns:
Return value of attr_list_set().

Definition at line 496 of file attr_list.c.

static PyObject* append ( Atrinik_AttrList al,
PyObject *  value 
) [static]

Implements the append() method; this is equivalent to the following:

attr_list[len(attr_list)] = xyz
Parameters:
alThe AttrList object.
valueValue to append.
Returns:
None.

Definition at line 390 of file attr_list.c.

int Atrinik_AttrList_init ( PyObject *  module)

Initializes the AttrList module.

Parameters:
moduleThe main Atrinik module.
Returns:
1 on success, 0 on failure.

Definition at line 589 of file attr_list.c.

static int attr_list_contains ( Atrinik_AttrList al,
PyObject *  value 
) [static]

Check if the provided AttrList contains 'value'.

Parameters:
alThe AttrList object.
valueThe value to try and find.
Returns:
1 if the value was found, 0 otherwise.

Definition at line 158 of file attr_list.c.

static PyObject* attr_list_get ( Atrinik_AttrList al,
void *  idx 
) [static]

Get value from an AttrList object.

Parameters:
alThe AttrList object.
idxPointer to the index value/key.
Returns:
0 on success, -1 on failure.

Definition at line 112 of file attr_list.c.

static unsigned PY_LONG_LONG attr_list_len ( Atrinik_AttrList al) [static]

Similar to attr_list_len_ptr(), but does not return a pointer.

Parameters:
alAttrList being used.
Returns:
The length of the provided AttrList.

Definition at line 93 of file attr_list.c.

static void* attr_list_len_ptr ( Atrinik_AttrList al) [static]

Get a pointer to integer that holds the maximum length depending on the AttrList's field type.

Parameters:
alThe AttrList pointer.
Returns:
Pointer to the integer holding the maximum length. Must be cast to the correct type before usage.

Definition at line 70 of file attr_list.c.

static int attr_list_set ( Atrinik_AttrList al,
void *  idx,
PyObject *  value 
) [static]

Set new value for an array member that AttrList object is wrapping.

Parameters:
alThe AttrList object.
idxPointer to the index value/key.
valueNew value to set.
Returns:
0 on success, -1 on failure.

Definition at line 196 of file attr_list.c.

static PyObject* iter ( PyObject *  seq) [static]

Start iterating.

Parameters:
seqWhat to iterate through.
Returns:
New iteration object.

Definition at line 435 of file attr_list.c.

static PyObject* iternext ( Atrinik_AttrList al) [static]

Keep iterating over an iteration object.

Parameters:
alThe iteration object.
Returns:
Next object from attribute list, NULL if the end was reached.

Definition at line 452 of file attr_list.c.

PyObject* wrap_attr_list ( void *  ptr,
size_t  offset,
field_type  field 
)

Creates a new AttrList wrapping an array.

Parameters:
ptrPointer to the structure the array is in.
offsetWhere the array is in the structure.
fieldType of the array being handled; for example, FIELDTYPE_KNOWN_SPELLS.
Returns:
The new wrapper object.

Definition at line 611 of file attr_list.c.


Variable Documentation

PyTypeObject Atrinik_AttrListType

AttrListType definition.

Definition at line 542 of file attr_list.c.

PyMappingMethods MappingMethods [static]
Initial value:
{
    (lenfunc) __len__,
    (binaryfunc) __getitem__,
    (objobjargproc) __setitem__,
}

Defines what to map some common methods (len(), __setitem__() and __getitem__()) to.

Definition at line 534 of file attr_list.c.

PyMethodDef methods[] [static]
Initial value:
{
    {"__getitem__", (PyCFunction) __getitem__, METH_O | METH_COEXIST, 0},
    {"append", (PyCFunction) append, METH_O, 0},
    {NULL, NULL, 0, 0}
}

Available Python methods for the AtrinikPlayer type.

Definition at line 408 of file attr_list.c.

PySequenceMethods SequenceMethods [static]
Initial value:
{
    (lenfunc) __len__,
    NULL, NULL, NULL, NULL, NULL, NULL,
    (objobjproc) __contains__,
    NULL, NULL
}

Common sequence methods.

Definition at line 523 of file attr_list.c.