Module obj

Support for Refcounted Objects.

These are (currently) allocated with malloc and freed with free, but in addition always carry a refcount (see obj_refcount). obj_ref increments this count, and obj_unref decrements this count; when the count becomes zero, the object is freed. If the object has an associated dispose function specified in obj_new, then this will be called first.

Unless OBJ_REF_ABBREV is defined, these are also defined as ref and unref; there is a bulk dispose for multiple objects.

llib objects may specify a dispose function:

typedef struct {
    int age;
    char *name;
} Person;

static void Person_dispose(Person *p) {
    obj_unref(p->name);
}

Person *person_new(int age, char *name) {
    Person *p = obj_new(Person,Person_dispose);
    p->age = age;
    p->name = str_ref(name);
    return p;
}

In this typical scenario, the dispose function decrements the reference count of the name field when the Person is disposed; this will of course only dispose the string if it hasn’t already been referenced elsewhere, which permits flexible sharing of data. (We use str_ref specifically here because the argument name may not be already a ref-counted string.)

Arrays carry their own size, accessible with array_len; if created with array_new_ref, they will unref their elements when disposed.

Sequences are resizeable arrays. seq_add appends a new value to a sequence. A sequence s is a pointer to an array of T. The underlying array can always be accessed with *s, and the array will be sized-to-fit when seq_array_ref is called.

See test-obj.c and test-seq.c

Macros

FOR standard for-loop.
FOR_ARR for-loop over array.

Functions

obj_cast (T, o) dynamically cast object to type.

RTTI

obj_refcount (p) refcount of an object.
array_len (arr) length of array.
obj_is_array (arr) is this an array?.
obj_ref_array (arr) is this a reference container array?
obj_elem_size (P) size of this object.
obj_is_instance (P, name) whether an object matches a type by name.

New

obj_new_type (T, optional) allocate a new type
obj_new (T, optional) allocate a new refcounted object.
obj_new_from_type (ti) allocate a new object from type.
array_new (type, sz) new array from type.
array_new_ref (type, sz) new array of refcounted objects.
array_new_copy (type, buff, sz) new array from buffer.
str_new (s) create a refcounted string copy.
str_new_size (sz) create a refcounted string of given size
str_cpy (s) make a refcounted string from an arbitrary string.

References

obj_ref (object) increase reference count (ref).
obj_unref (P) decrease reference count (unref).
obj_unref_v (...) decrease ref count for multiple values (dispose).
str_ref (s) increase the refcount of a string.

Utilities

obj_apply_varargs (o, fn, ...) apply a function to all arguments.
obj_apply (dest, setter, src, getter) get from a source and put to a destination.

Array

array_copy (P, i1, i2) get a slice of an array.
array_resize (P, newsz) resize an array.
array_sort (P, kind, ofs) sort an array.
array_sort_struct_ptr (A, type, field) sort an array of structs by integer/pointer field
array_sort_struct_str (A, type, field) sort an array of structs by string field

Sequences

seq_new (T) create a plain sequence of a type
seq_new_ref (T) create a sequence of a refcounted type
seq_new_array (arr) create a sequence from an existing array.
seq_add (s, v) add a value to a sequence.
seq_add_items (s) add arbitrary number of values to a sequence.
seq_remove (s, pos, len) remove values from a sequence.
seq_insert (s, pos, array, sz) insert values into a sequence.
seq_adda (s, array, sz) extend sequence with array of values.
seq_add_ptr (sp, p) add a pointer value to a sequence.
seq_array_ref (sp) get the array from a sequence.


Macros

FOR
standard for-loop.
  • i loop variable.
  • n loop count.
FOR_ARR
for-loop over array.
  • T type base type
  • loop type* pointer
  • array type*

Usage:

    FOR_ARR(int,pi,arr) printf("%d ",*pi);

Functions

obj_cast (T, o)
dynamically cast object to type. Uses obj_is_instance

Parameters:

  • T type name
  • o object

RTTI

obj_refcount (p)
refcount of an object. Will return -1 if it isn’t one of ours!

Parameters:

  • p const void *

Returns:

    int
array_len (arr)
length of array.

Parameters:

  • arr void*

Returns:

    int
obj_is_array (arr)
is this an array?.

Parameters:

  • arr void*

Returns:

    bool
obj_ref_array (arr)
is this a reference container array?

Parameters:

  • arr void*

Returns:

    bool
obj_elem_size (P)
size of this object. For arrays, this is size of base type.

Parameters:

  • P const void *

Returns:

    int
obj_is_instance (P, name)
whether an object matches a type by name.

Parameters:

  • P const void *
  • name const char *

Returns:

    bool

New

obj_new_type (T, optional)
allocate a new type

Parameters:

  • T type
  • optional DisposeFn destructior

Returns:

    T*
obj_new (T, optional)
allocate a new refcounted object.

Parameters:

  • T type
  • optional DisposeFn destructior

Returns:

    T*
obj_new_from_type (ti)
allocate a new object from type.

Parameters:

  • ti int

Returns:

    void *
array_new (type, sz)
new array from type.

Parameters:

  • type T name of type
  • sz int size of array

Returns:

    T*
array_new_ref (type, sz)
new array of refcounted objects.

Parameters:

  • type T name of type
  • sz int size of array

Returns:

    T*
array_new_copy (type, buff, sz)
new array from buffer.

Parameters:

  • type T name of type
  • buff T*
  • sz int size of array
str_new (s)
create a refcounted string copy.

Parameters:

  • s const char *

Returns:

    char *
str_new_size (sz)
create a refcounted string of given size

Parameters:

  • sz int

Returns:

    char *
str_cpy (s)
make a refcounted string from an arbitrary string.

Parameters:

  • s const char *

Returns:

    char *

References

obj_ref (object)
increase reference count (ref).

Parameters:

  • object T

Returns:

    T
obj_unref (P)
decrease reference count (unref). When this goes to zero, free the object and call the dispose function, if any.

Parameters:

  • P const void *
obj_unref_v (...)
decrease ref count for multiple values (dispose).

Parameters:

  • ... objects
str_ref (s)
increase the refcount of a string. If this is not one of “our strings”, we create a new refcounted string, otherwise proceed as with obj_ref.

Parameters:

  • s const char *

Returns:

    const char *

Utilities

obj_apply_varargs (o, fn, ...)
apply a function to all arguments. If o is not NULL, then call fn(o,arg), otherwise call fn(arg).

Parameters:

  • o void *
  • fn PFun
  • ...
obj_apply (dest, setter, src, getter)
get from a source and put to a destination.

Parameters:

  • dest destination object
  • setter function of dest and the gotten object
  • src source object
  • getter function of src to call repeatedly until it returns NULL

Array

array_copy (P, i1, i2)
get a slice of an array.

Parameters:

  • P void * the array
  • i1 int lower bound
  • i2 int upper bound (can be -ve; -1 is up to end, -2 is one less than end, etc)

Returns:

    void *
array_resize (P, newsz)
resize an array.

Parameters:

  • P void * the array
  • newsz int the new size

Returns:

    void *
array_sort (P, kind, ofs)
sort an array.

Parameters:

  • P T* the array
  • kind int either ARRAY_INT or ARRAY_STR
  • ofs int offset into each item
array_sort_struct_ptr (A, type, field)
sort an array of structs by integer/pointer field

Parameters:

  • A T* the array
  • type T the struct
  • field the name of the struct field
array_sort_struct_str (A, type, field)
sort an array of structs by string field

Parameters:

  • A T* the array
  • type T the struct
  • field the name of the struct field

Sequences

seq_new (T)
create a plain sequence of a type

Parameters:

  • T type
seq_new_ref (T)
create a sequence of a refcounted type

Parameters:

  • T type
seq_new_array (arr)
create a sequence from an existing array.

Parameters:

  • arr void *

Returns:

    void *
seq_add (s, v)
add a value to a sequence.

Parameters:

  • s the sequence
  • v the value
seq_add_items (s)
add arbitrary number of values to a sequence. The values are implicitly terminated with NULL, so this won’t work for arbitrary integers.

Parameters:

  • s the sequence
seq_remove (s, pos, len)
remove values from a sequence.

Parameters:

  • s the sequence
  • pos int position
  • len int number of values
seq_insert (s, pos, array, sz)
insert values into a sequence.

Parameters:

  • s the sequence
  • pos int position
  • array of values
  • sz int number of values (-1 means use array_len)
seq_adda (s, array, sz)
extend sequence with array of values.

Parameters:

  • s the sequence
  • array of values
  • sz int number of values (-1 means use array_len)
seq_add_ptr (sp, p)
add a pointer value to a sequence.

Parameters:

  • sp void *
  • p void *
seq_array_ref (sp)
get the array from a sequence. (This will transfer ownership, shrink to fit, and unref the sequence.)

Parameters:

  • sp void *

Returns:

    void *
generated by LDoc 1.4.3 Last updated 2015-04-27 12:35:55