MLib Alpha 1 API Reference

MLib Alpha 1 API Reference

Objects

the MLib provides a simple dynamic object model that applications can use to increase code re-use.

Each MLib object is a class instance. each class is a dynamically allocated structure described by a constant "class type" record


M_Object


  typedef struct M_ObjectRec_*            M_Object;

handle to a M_ObjectRec structure, models an object (i.e. a class instance).



M_ObjectRec


  typedef struct M_ObjectRec_
  {
    M_Class   clazz;
    M_Int     ref_count;

  } M_ObjectRec;

a structure used to describe a given class instance.


fields
clazz

handle to object's class

ref_count

object's reference count


M_Class


  typedef const struct M_ClassRec_*       M_Class;

handle to a constant M_ClassRec structure, used to model a class to the object system. Note that classes are _objects_ too and are allocated by the object sub-system.

classes are described by M_ClassType handles.



M_ClassType


  typedef const struct M_ClassTypeRec_*   M_ClassType;

handle to a constant M_ClassTypeRec structure, used to describe a given class to the object system.



M_ClassTypeRec


  typedef struct M_ClassTypeRec_
  {
    M_ClassType         super;
    M_CString           name;

    M_Size              class_size;
    M_UInt              class_flags;
    M_Class_InitFunc    class_init;
    M_Class_DoneFunc    class_done;

    M_Size              object_size;
    M_Object_InitFunc   object_init;
    M_Object_CopyFunc   object_copy;
    M_Object_IncrFunc   object_incr;
    M_Object_DoneFunc   object_done;

  } M_ClassTypeRec;

a structure used to describe a given class to the object system. class types must be unique, and they're used to allocate and manage M_Class objects.


fields
super

pointer to super-class type

name

class's name (as normal C string)

class_size

size of class in bytes

class_init

class initialiser

class_done

class finalizer

object_size

size of class instance in bytes

object_init

object initializer function

object_copy

object copying function

object_ref

object reference counting function

object_done

object finalizer function


M_Class_InitFunc


  typedef void     (*M_Class_InitFunc)( M_Class    clazz );

initialize a new class object


input
clazz

handle to target class

note

before this function is called, the content of the parent's class is copied into the current one.

throws

any exception


M_Class_DoneFunc


  typedef M_Bool   (*M_Class_DoneFunc)( M_Class   clazz );

finalizer a given class


input
clazz

handle to target class

return

a boolean, true if the parent class finalizer must _not_ be called..

throws

any exception


M_Class_Flags


  enum
  {
    M_CLASS_FLAG_NO_COPY      = 1,
    M_CLASS_FLAG_SINGLETON    = 2,
  };

bit flags used in class type descriptors


values
M_CLASS_FLAG_NO_COPY

this bit flag is set when class instances should not be copiable directly with a call to m_object_copy . this doesn't prevent other copying functions to exist though.. but calling m_object_copy will result in a fatal exception

M_CLASS_FLAG_SINGLETON

this bit flag is set for class instances that are singleton. In this case, calling m_object_copy will call m_object_ref then return a pointer to the target object.


M_Object_InitFunc


  typedef void  (*M_Object_InitFunc)( M_Object   object );

a function used to initialise a given object (i.e. class instance)


input
object

handle to target M_Object

throws

any exception


M_Object_CopyFunc


  typedef void     (*M_Object_CopyFunc)( M_Object   source,
                                         M_Object   copy );

a function used when an object is copied. m_object_copy will start by copying the root object instance, then call this method in order to manage child pointers / references

note that this will never be called for instances of classes that have the ?M_CLASS_FLAG_SINGLETON flag set.


input
source

handle to source object

copy

handle to copy

note

this function should _only_ modify the copy, never the source

throws

any exception


M_Object_IncrFunc


  typedef M_Bool   (*M_Object_IncrFunc)( M_Object  object,
                                         M_Int     increment );

a function used to increment or decrement the reference count of a given object. This is only used for instances of classes that have the ?M_CLASS_FLAG_CUSTOM_REF bit flag set.

this can be used to implement locking/unlocking during the reference count update for certain classes.


input
object

handle to target object

increment

count increment. can only be 1 or -1

return

boolean. if true, indicates that the object must be destroyed.

note

the return value is only tested during a m_object_unref call, which means when "increment" == -1. If the object's reference count is not 0 at this time, the program will panic !!

throws

any exception


M_Object_DoneFunc


  typedef M_Bool  (*M_Object_DoneFunc)( M_Object   object );

a function used to finalize a given object (i.e. class instance)


input
object

handle to target M_Object

result

boolean. true when the parent object finalizer must _not_ be called


M_ClassRec


  typedef struct M_ClassRec_
  {
    M_Memory            memory;
    M_Core              mcore;
    M_ClassPool         class_pool;
    M_ULong             num_objs;
    M_UInt32            magic;
    M_ClassType         type;
    M_Class             super;
    M_ClassInfo         info;
    M_Pointer           data;

    M_Object_InitFunc   object_init;
    M_Object_DoneFunc   object_done;
    M_Object_CopyFunc   object_copy;
    M_Object_RefFunc    object_ref;

  } M_ClassRec;

a structure used to describe a given class. Note that classes are themselves objects..


fields
memory

memory manager handle

mcore

MLib core handle

class_pool

class pool handle

num_objs

number of class instances created

magic

magic number used to check classes at runtime

type

handle to class type

super

handle to super class

info

handle to internal data for this class (managed by the class pool, unavailable to applications and classes)

data

class-specific data

object_init

object initializer..

object_done

object finalizer for class's instances

object_copy

object copier for class's instances

object_ref

object reference counter for class's instances, if any

note

normally, the "object_done" and "object_copy" fields are initialised by the "class_type" content. However, the class initialiser is capable of changing these functions if it wants to..


M_OBJECT


#define  M_OBJECT(x)    ((M_Object)(x))

a convenient macro used to convert a pointer to a M_Object .



M_OBJECT_CHK


#define  M_OBJECT_CHK(o)    M_OBJECT(M_OBJECT_CHECK_TYPE(o,NULL))

a convenient macro used to convert a pointer to a M_Object . on debug builds, it will panic if the parameter is not an object handle..



m_object_new


  MLIB_API(M_Pointer)
  m_object_new( M_Class   clazz );

create a new object (i.e. class instance), from a given class


input
class

handle to object's class

return

handle to new object. cannot be NULL

throws

any exception (e.g. m_err_memory_alloc )

note

the new object's reference count is set to 1

you can use m_class_from_type to retrieve a class handle from a given class type..


m_object_new_from_type


  MLIB_API(M_Pointer)
  m_object_new_from_type( M_Core       mcore,
                          M_ClassType  class_type,
                          M_Pointer    class_data );

create a new object (i.e. class instance), given its class type


input
mcore

MLib core handle

class_type

class type handle

class_data

used when creating the

throws

any exception (e.g. m_err_memory_alloc )

note

the new object's reference count is set to 1

this function is slightly slower that m_object_new , but doesn't require a pre-determined class handle.


m_object_ref


  MLIB_API(void)
  m_object_ref( M_Pointer object );

increment a given object's reference count


input
object

handle to target M_Object


m_object_unref


  MLIB_API(void)
  m_object_unref( M_Pointer object );

decrement a given object's reference count. it it reaches 0, the object is destroyed automatically..


input
object

handle to target M_Object


m_object_copy


  MLIB_API(M_Pointer)
  m_object_copy( M_Pointer object );

copy a given object.


input
object

handle to target M_Object

return

object copy

throws

any exception (e.g. m_err_memory_alloc )

note

the copy's reference count is set to 1


m_object_push


  MLIB_API(void)
  m_object_push( M_Pointer  object );

push an object on top of the cleanup stack


input
object

handle to target M_Object

throws

m_err_memory_alloc


m_object_pop


  MLIB_API(void)
  m_object_pop( M_Pointer  object );

pops an object from the cleanup stack. do not unref/destroy it


input
object

handle to target M_Object


m_object_pop_unref


  MLIB_API(void)
  m_object_pop_unref( M_Pointer  object );

pops an object from the cleanup stack, then unref it


input
object

handle to target M_Object


m_object_get_memory


  MLIB_API(M_Memory)
  m_object_get_memory( M_Pointer object );

retrieve the current memory manager handle from an object


input
object

handle to target M_Object

return

memory manager handle


m_object_get_core


  MLIB_API(M_Core)
  m_object_get_core( M_Pointer object );

retrieve the current MLib core handle from an object


input
object

handle to target M_Object

return

MLib core handle


m_object_is_a


  MLIB_API(M_Bool)
  m_object_is_a( M_Pointer    object,
                 M_ClassType  class_type );

checks wether an object is an instance of a given class type


input
object

handle to target M_Object

class_type

class type to compare to

return

boolean. true if the object's class is the one described by "class_info", or one of its children..


m_object_is_instance_of


  MLIB_API(M_Bool)
  m_object_is_instance_of( M_Pointer  object,
                           M_Class    clazz );

checks wether an object is an instance of a given class


input
object

handle to target M_Object

class_type

class type to compare to

return

boolean. true if the object's class is the one described by "class_info", or one of its children..


m_assert_is_object


  MLIB_API(M_Bool)
  m_object_check( M_Pointer  object )

checks at runtime that a pointer is really an object handle on debug builds, this will abort the program in case of error


input
object

handle to target M_Object


m_object_get_class


#define  m_object_get_class(o)    M_OBJECT(o)->clazz

retrieve's an object's class


input
object

handle to target M_Object

return

the object's class record


M_ClassPool


  typedef struct M_ClassPoolRec_*  M_ClassPool;

an opaque handle to the pool of classes in a given MLib core.



M_ClassInfo


  typedef struct M_ClassInfoRec_*         M_ClassInfo;

opque handle to a M_ClassInfo structure. used internally by the object sub-system to store internal typermation about the class



M_CLASS


#define  M_CLASS(x)  ((M_Class)(x))

a convenient macro to convert a pointer to a M_Class.



M_CLASS_CHK


#define  M_CLASS_CHK(x)  M_CLASS( M_CLASS_CHECK_TYPE(x,NULL) )

a convenient macro to convert a pointer to a M_Class. On debug builds this also perform runtime info checks (which will abort the program if the pointer is not a valid class handle)



m_assert_is_class


#define  m_assert_is_class(x)  M_CLASS_CHECK_TYPE(x,NULL)

checks at runtime that a given pointer is a class handle



m_class_from_type


  MLIB_API(M_Class)
  m_class_from_type( M_Core       mcore,
                     M_ClassType  class_type,
                     M_Pointer    class_data );

retrieves the class handle corresponding to a given class type


input
mcore

MLib code handle

class_type

class type handle

return

class handle, cannot be NULL

throws

any (e.g. m_err_memory_alloc ..)

note

it's usually a good idea to store the result to use it with m_object_new


m_class_flush


  MLIB_API(void)
  m_class_flush( M_Class  clazz );

this function is used to remove a given class from the class pool, and should seldom be used.


input
clazz

target M_Class handle

note

If one or more instances of the class exist, this function will _panic_ and abort the program..

this function should only be used when the class's implementation isn't available anymore, for example when unloading a dynamic module that implements a given class, m_class_flush must be called before removing its code from memory


m_class_is_a


  MLIB_API(M_Bool)
  m_class_is_a( M_Class      clazz,
                M_ClassType  class_type );

checks that a given class corresponds to a given class type


input
class

handle to class

type

handle to class type used in the comparison

return

boolean, true if the class's info, or one of its super infos, correspond to the class type.


m_class_is_sub_of


  MLIB_API(M_Bool)
  m_class_is_sub_of( M_Class  clazz,
                     M_Class  parent );

checks that a given class corresponds to a given class type. if not, aborts the program..


input
class

handle to class

parent

handle to parent class used in the comparison

return

boolean. true if "class" is "parent" or any of its childs in the class hierarchy tree


generated on Tue Oct 09 23:59:46 2001