src/lib/object.h
branchnew-lib-errors
changeset 217 7728d6ec3abf
parent 174 c56680e9e021
equal deleted inserted replaced
216:a10ba529ae39 217:7728d6ec3abf
       
     1 #ifndef OBJECT_H
       
     2 #define OBJECT_H
       
     3 
       
     4 /**
       
     5  * @file
       
     6  *
       
     7  * The thing feared by every C-programmer - calling C structs "objects".
       
     8  */
       
     9 #include <stdbool.h>
       
    10 
       
    11 /**
       
    12  * Basic object type info
       
    13  */
       
    14 struct object_type {
       
    15     /** Parent type for nested structs, NULL if the parent is object_type */
       
    16     const struct object_type *parent;
       
    17 };
       
    18 
       
    19 /**
       
    20  * Basic object state
       
    21  */
       
    22 struct object {
       
    23     /** Object's type */
       
    24     const struct object_type *type;
       
    25 };
       
    26 
       
    27 /**
       
    28  * Initialize an object to the given type.
       
    29  */
       
    30 void object_init (struct object *obj, const struct object_type *type);
       
    31 
       
    32 /**
       
    33  * Check that the given object type is compatible with the given type.
       
    34  */
       
    35 bool object_type_check (const struct object_type *obj_type, const struct object_type *type);
       
    36 
       
    37 /**
       
    38  * Check that the given object "implements" the given type.
       
    39  */
       
    40 bool object_check (struct object *obj, const struct object_type *type);
       
    41 
       
    42 /**
       
    43  * Return the given object as a pointer suitable for casting to the struct for the given type.
       
    44  *
       
    45  * It is a bug to call this with an object that's not compatible with the given type.
       
    46  */
       
    47 void* object_cast (struct object *obj, const struct object_type *type);
       
    48 
       
    49 /**
       
    50  * Return a pointer to the object's type that is compatible with the given type.
       
    51  *
       
    52  * It is a bug to call this with a object that's not compatible with the given type.
       
    53  */
       
    54 const void* object_type (struct object *obj, const struct object_type *type);
       
    55 
       
    56 #endif /* OBJECT_H */