Class Inventory
source code
Ordered item inventory, indexed by both name and position.
Inventory
is a uniform container that offers both
hash table and array (sorted or unsorted) interfaces. Both types of read
access are fast, operations that modify it may be slower. Inventory can
also maintain a notion of default item.
Inventory
can be used both as an actual container
for some data, or just wrap a static array with a the same interface so
the actual storage is opaque to inventory user. The former kind of
inventories can be created with Inventory.new
()
or Inventory.new_filled
(); constant inventory
is created with Inventory.new_from_array
().
Contantess of an inventory can be tested with Inventory.is_const
().
Possible operations with data items stored in an inventory are
specified upon inventory creation with
InventoryItemType
structure. Not all fields are
mandatory, with items allowing more operations the inventory is more
capable too. For example, if items offer a method to make copies, Inventory.new_item() can be used to directly create new
items in the inventory (this capability can be tested with Inventory.can_make_copies()).
Item can have `traits', that is data that can be obtained generically.
They are similar to GObject
properties. Actually, if
items are objects, they should simply map object properties to traits.
But it is possible to define traits for simple structures too.
|
|
|
|
|
|
|
|
|
foreach(function,
user_data)
Calls a function on each item of an inventory, in order. |
source code
|
|
|
find(predicate,
user_data)
Finds an inventory item using user-specified predicate function. |
source code
|
|
|
|
|
|
|
|
|
|
|
restore_order()
Assures an inventory is sorted. |
source code
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
new_item(name,
newname)
Creates a new item as a copy of existing one and inserts it to
inventory. |
source code
|
|
Creates a new inventory.
- Parameters:
itype - Type of items the inventory will contain.
(const-InventoryItemType*)
- Returns:
- The newly created inventory. (Inventory)
|
Returns the number of items in an inventory.
- Returns:
- The number of items. (int)
|
Returns whether an inventory can create new items itself.
The prerequistie is that item type is a serializable object. It
enables functions like Inventory.new_item().
- Returns:
True if inventory can create new items
itself. (bool)
|
Finds position of an item in an inventory.
- Parameters:
name - Item name. (string)
- Returns:
- Item position, or (guint)-1 if there is no such item.
(int)
|
Calls a function on each item of an inventory, in order.
function 's first argument is item position
(transformed with GUINT_TO_POINTER()), second is item pointer, and the
last is user_data .
- Parameters:
function - A function to call on each item. It must not modify
inventory . (GHFunc)
user_data - Data passed to function . (gpointer)
|
Finds an inventory item using user-specified predicate function.
predicate is called for each item in
inventory (in order) until it returns
True . Its arguments are the same as in Inventory.foreach().
- Parameters:
predicate - A function testing some item property. It must not modify
inventory . (GHRFunc)
user_data - Data passed to predicate . (gpointer)
- Returns:
- The item for which
predicate returned
True . If there is no such item in the
inventory, None is returned.
(gpointer)
|
Sets the default of an inventory.
Item name must already exist in the inventory.
- Parameters:
name - Item name, pass None to unset default item.
(string)
|
Returns the name of the default item of an inventory.
- Returns:
- The default item name,
None if no default
name is set. Item of this name may or may not exist in the
inventory. (string)
|
Notifies inventory an item was updated.
This function makes sense primarily for non-object items, as object
items can notify inventory via signals.
- Parameters:
name - Item name. (string)
|
Notifies inventory item on given position was updated.
This function makes sense primarily for non-object items, as object
items can provide watchable_signal .
- Parameters:
|
Forces an inventory to be unsorted.
Item positions don't change, but future Inventory.insert_item() won't try to insert items in
order.
|
Deletes an item from an inventory.
- Parameters:
name - Name of item to delete. (string)
- Returns:
True if item was deleted. (bool)
|
Deletes an item on given position from an inventory.
- Parameters:
n - Position of item to delete. (int)
- Returns:
True if item was deleted. (bool)
|
Creates a new GtkTreeModel wrapper around a
Inventory .
- Returns:
- The newly created inventory store. (InventoryStore)
|
Looks up an item in an inventory.
- Parameters:
name - Item name. (string)
- Returns:
- Item called
name , or None
if there is no such item. (gobject.GObject )
|
Looks up an item in an inventory, eventually falling back to
default.
The lookup order is: item of requested name, default item (if set),
any inventory item, None (can happen only when
inventory is empty).
- Parameters:
name - Item name. (string)
- Returns:
- Item called
name , or default item. (gobject.GObject )
|
Returns item on given position in an inventory.
- Parameters:
n - Item position. It must be between zero and the number of items
in inventory, inclusive. If it is equal to the number of items,
None is returned. In other words, inventory
behaves like a None -terminated array, you can
simply iterate over it until Inventory.get_nth_item() returns
None . (int)
- Returns:
- Item at given position. (
gobject.GObject )
|
Returns the default item of an inventory.
- Returns:
- The default item. If there is no default item,
None is returned. (gobject.GObject )
|
Inserts an item into an inventory.
Item of the same name must not exist yet.
If the inventory is sorted, item is inserted to keep order. If the
inventory is unsorted, item is simply added to the end.
- Parameters:
object - (gobject.GObject )
|
Inserts an item to an explicit position in an inventory.
Item of the same name must not exist yet.
- Parameters:
object - (gobject.GObject )
n - Position to insert item to. (int)
|
Renames an inventory item.
If an item of name newname is already present in
inventory , the rename will fail.
- Parameters:
name - Name of item to rename. (string)
newname - New name of item. (string)
|
Creates a new item as a copy of existing one and inserts it to
inventory.
The newly created item can be called differently than
newname if that already exists.
- Parameters:
name - Name of item to duplicate, may be None to use
default item (the same happens when name does
not exist). (string)
newname - Name of new item, it must not exist yet. It may be
None , the new name is based on
name then. (string)
- Returns:
- The newly added item. (
gobject.GObject )
|