offbrand
A collection of generic, reference counted datastructures in C for C
 All Classes Files Functions Variables Typedefs Macros Groups
Macros | Typedefs | Functions
offbrand.h File Reference

offbrand Standard Library More...

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>
#include <time.h>

Go to the source code of this file.

Macros

#define OB_EQUAL_TO   0
 
#define OB_GREATER_THAN   1
 
#define OB_LESS_THAN   -1
 
#define OB_NOT_EQUAL   2
 
#define OB_LEAST_TO_GREATEST   -1
 
#define OB_GREATEST_TO_LEAST   1
 

Typedefs

typedef struct obj_structobj
 
typedef uint32_t ob_ref_count_t
 
typedef void(* ob_dealloc_fptr )(obj *)
 
typedef size_t ob_hash_t
 
typedef ob_hash_t(* ob_hash_fptr )(const obj *)
 
typedef int8_t(* ob_compare_fptr )(const obj *, const obj *)
 
typedef void(* ob_display_fptr )(const obj *)
 

Functions

void ob_init_base (obj *instance, ob_dealloc_fptr dealloc_funct, ob_hash_fptr hash_funct, ob_compare_fptr compare_funct, ob_display_fptr display_funct, const char *classname)
 Initializes instances of all offbrand compatible classes with a reference count of 1, a deallocator, a hash function, and the provided class name. More...
 
objob_release (obj *instance)
 Decrements the instances reference count by 1. If the reference count is reduced to 0 then release automatically calls the instances deallocator. More...
 
objob_retain (obj *instance)
 Increments the instances reference count by 1, indicating that the calling code has "referenced" that instance for later use. More...
 
uint32_t ob_reference_count (obj *instance)
 Returns the current reference count of the given instance. More...
 
uint8_t ob_has_class (const obj *a, const char *classname)
 Checks that the given obj is of the provided class. More...
 
uint8_t ob_has_same_class (const obj *a, const obj *b)
 Checks that two objs are of the same class. More...
 
ob_hash_t ob_hash (const obj *to_hash)
 Computes the hash value of the instance using a class specific hash function if one is available or the defaultHash function if not. More...
 
int8_t ob_compare (const obj *a, const obj *b)
 comparision operator between any two offbrand compatible classes More...
 
void ob_display (const obj *to_print)
 display operation prints information about the instance of any offbrand compatible class to stderr, for debug More...
 

Detailed Description

offbrand Standard Library

The standard library defines all data structures and function calls required for memory management via reference count, class membership tests, hashing, and others.

Author
theck

Macro Definition Documentation

#define OB_EQUAL_TO   0

Comparison Constant, two elements are equal

#define OB_GREATER_THAN   1

Comparison Constant, the first element is greater than the second

#define OB_GREATEST_TO_LEAST   1

Sorting Order Constant, sort elements from greatest to least

#define OB_LEAST_TO_GREATEST   -1

Sorting Order Constant, sort elements from least to greatest

#define OB_LESS_THAN   -1

Comparison Constant, the first element is less than the second

#define OB_NOT_EQUAL   2

Comparison Constant, the first element is not equal to the second, and no other relation ship can be infered

Typedef Documentation

typedef int8_t(* ob_compare_fptr)(const obj *, const obj *)

function pointer to a comparision function that takes pointers to any two offbrand compatible classes and returns one of the comparision constants listed in the constants section.

typedef void(* ob_dealloc_fptr)(obj *)

function pointer to a deallocator for any OffBrand compatible class

typedef void(* ob_display_fptr)(const obj *)

function pointer to a display function for any offbrand compatible class

typedef ob_hash_t(* ob_hash_fptr)(const obj *)

function pointer to a hash function for any offbrand compatible class

typedef size_t ob_hash_t

hash value, used returned from hash functions

typedef uint32_t ob_ref_count_t

reference count, tracks references to instances of offbrand compatible classes

typedef struct obj_struct* obj

basic generic type used to track reference counts, store class specific function pointers, and form the basis for all generic container classes and functions.

Function Documentation

int8_t ob_compare ( const obj a,
const obj b 
)

comparision operator between any two offbrand compatible classes

Parameters
aAn instance of any offbrand compatible class
bAn instance of any offbrand compatible class
Return values
OB_LESS_THANobj a is less than b
OB_GREATER_THANobj a is equivalent to b
OB_EQUAL_TOobj a is greater than b
OB_NOT_EQUALobj a is not equal to b, but no other relationship can be infered
void ob_display ( const obj to_print)

display operation prints information about the instance of any offbrand compatible class to stderr, for debug

Parameters
to_printAn instance of any offbrand compativle class
uint8_t ob_has_class ( const obj a,
const char *  classname 
)

Checks that the given obj is of the provided class.

Parameters
aAn instance of any offbrand compatible class
classnameAn C string describing a class name
Return values
0a is not an instance of classname
non-zeroa is an instance of classname
uint8_t ob_has_same_class ( const obj a,
const obj b 
)

Checks that two objs are of the same class.

Parameters
aAn instance of any offbrand compatible class
bAn instance of any offbrand compatible class
Return values
0a and b are not of the same class
non-zeroa and b are of the same class
ob_hash_t ob_hash ( const obj to_hash)

Computes the hash value of the instance using a class specific hash function if one is available or the defaultHash function if not.

Parameters
to_hashAn instance of any offbrand compatible class
Returns
Hash value
void ob_init_base ( obj instance,
ob_dealloc_fptr  dealloc_funct,
ob_hash_fptr  hash_funct,
ob_compare_fptr  compare_funct,
ob_display_fptr  display_funct,
const char *  classname 
)

Initializes instances of all offbrand compatible classes with a reference count of 1, a deallocator, a hash function, and the provided class name.

Parameters
instanceAn newly allocated instance of any offbrand compatible class
dealloc_functFunction pointer to the deallocator for the instances class
hash_functFunction pointer to the hash function for the instances class
compare_functFunction pointer to the compare function for the instances class
display_functFunction pointer to the display function for the instances class
classnameC string containing instances classname.
uint32_t ob_reference_count ( obj instance)

Returns the current reference count of the given instance.

Parameters
instanceAn instance of any offbrand compatible class
Returns
An unsigned integer reference count
obj* ob_release ( obj instance)

Decrements the instances reference count by 1. If the reference count is reduced to 0 then release automatically calls the instances deallocator.

Parameters
instanceAn instance of any offbrand compatible class
Return values
&instanceReference count was decremented but the instance is still referenced
NULLReference count was decremented to 0 and deallocator was called on instance
obj* ob_retain ( obj instance)

Increments the instances reference count by 1, indicating that the calling code has "referenced" that instance for later use.

Parameters
instanceAn instance of any offbrand compatible class
Returns
A reference to the argument instance.