Module interface
Defining and Using Interfaces.
llib has a mechanism for types to implement interfaces. For instance,
list objects implement the Iterable interface, which has a function init
that returns an 'Iterator object.
#include <llib/interface.h"
...
List *ls = list_new_str();
list_add(ls, "ein");
list_add(ls, "zwei");
list_add(ls, "drei");
Iterator *it = interface_get_iterator(ls);
char *s;
while (it->next(it,&s)) {
printf("got '%s'\n",s);
}
unref(it);
There is an optional function nextpair in the Iterator struct, which grabs key/value
pairs:
Map *m = map_new_str_str();
map_put(m,"one","1");
map_put(m,"two","2");
map_put(m,"three","3");
Iterator *it = interface_get_iterator(m);
char *key, *val;
while (it->nextpair(it,&key,&val)) {
printf("'%s': '%s'\n",key,val);
}
Accessible is simpler; the type provides a single lookup function for finding
the value associated with a key; map implements this.
This module also defines default iterators and accessors for arrays. In this way, the JSON generator and the template module may consume arbitrary llib data.
Functions
| interface_add (itype, type, funs) | make a type type support a particular interface itype. |
| interface_get (itype, obj) | find the interface type for this object. |
| interface_typeof (name) | type of an interface |
| interface_get_lookup (P) | get a lookup function for this object. |
| interface_get_iterator (obj) | get an iterator for this object. |
Functions
- interface_add (itype, type, funs)
-
make a type
typesupport a particular interfaceitype.Parameters:
- itype int
- type int
- funs void *
- interface_get (itype, obj)
-
find the interface
typefor this object.Parameters:
- itype int
- obj const void *
Returns:
-
void *
- interface_typeof (name)
-
type of an interface
Parameters:
- name
Returns:
-
int
type index
- interface_get_lookup (P)
-
get a lookup function for this object.
Will look for ‘Accessor’, otherwise returns ‘str_lookup’
Parameters:
- P const void *
Returns:
-
ObjLookup
- interface_get_iterator (obj)
-
get an iterator for this object.
Will look for ‘Iterable’, otherwise returns an array iterator
Parameters:
- obj const void *
Returns:
-
Iterator *