kmediad 0.5.0a
A cross-platform Web-based audio player
Functions | Variables
klib_object.c File Reference

Implementation of the klib_Object class. More...

#include <string.h>
#include <malloc.h>
#include "klib_log.h"
#include "klib_defines.h"
#include "klib_object.h"

Functions

struct _klib_Stringklib_string_new_empty ()
void klib_string_printf (struct _klib_String *,...)
struct _klib_String_klib_object_tostring (const klib_Object *self)
struct _klib_Stringklib_object_tostring (const klib_Object *self)
 Get a string representation of the object.
BOOL klib_object_istype (klib_Object *self, const char *class_name)
 Returns true if the object has a particular type name.
void klib_object_dispose (klib_Object *self)
 Reclaim memory.
void klib_object_construct (klib_Object *self, const char *class_name)
 Initialize the object's memory and ref count.
void klib_object_unref (klib_Object *self)
 Decrement the reference count of the object.
void klib_object_add_ref (klib_Object *self)
 Increment the reference count of the object.

Variables

int klib_object_trace_flags = 0
 Can be set non-zero to enable very fine tracing.

Detailed Description

Implementation of the klib_Object class.


Function Documentation

struct _klib_String* _klib_object_tostring ( const klib_Object self) [read]
  {
  struct _klib_String *s = klib_string_new_empty();
  klib_string_printf (s, "Object %X of class %s",
      (long)self, self->class_name);
  return s;
  }
void klib_object_add_ref ( klib_Object self)

Increment the reference count of the object.

  {
  self->ref_count++;
  if (klib_object_trace_flags & KLIB_OBJECT_TRACE_REFS)
    {
    klib_log (KLIB_LOG_TRACE, "add_ref() called on object %X of class %s\n",
      (long)self, self->class_name);
    klib_log (KLIB_LOG_TRACE, "New ref count is %d\n", self->ref_count);
    }
  }
void klib_object_construct ( klib_Object self,
const char *  class_name 
)

Initialize the object's memory and ref count.

  {
  if (klib_object_trace_flags & KLIB_OBJECT_TRACE_CONSTRUCTION)
    klib_log (KLIB_LOG_TRACE, "Called constructor for Object\n");

  memset (self, 0, sizeof (klib_Object));
  self->ref_count = 0;
  self->dispose_func = klib_object_dispose;
  self->tostring_func = _klib_object_tostring;
  strncpy (self->class_name, class_name, sizeof (self->class_name) - 1);
  }
void klib_object_dispose ( klib_Object self)

Reclaim memory.

  {
  if (klib_object_trace_flags & KLIB_OBJECT_TRACE_CONSTRUCTION)
    klib_log (KLIB_LOG_TRACE, "Called destructor for Object\n");
  free (self);
  }
BOOL klib_object_istype ( klib_Object self,
const char *  class_name 
)

Returns true if the object has a particular type name.

  {
  if (!self) return FALSE;
  if (strcmp (class_name, self->class_name) == 0) return TRUE;
  return FALSE;
  }
struct _klib_String* klib_object_tostring ( const klib_Object self) [read]

Get a string representation of the object.

Caller must free this String

  {
  if (self == NULL) return klib_string_new ("(null)");
  return self->tostring_func (self);
  }
void klib_object_unref ( klib_Object self)

Decrement the reference count of the object.

  {
  if (klib_object_trace_flags & KLIB_OBJECT_TRACE_REFS)
    {
    klib_log (KLIB_LOG_TRACE, "unref() called on object %X of class %s\n",
      (long)self, self->class_name);
    }
  if (self->ref_count > 0)
    {
    if (klib_object_trace_flags & KLIB_OBJECT_TRACE_CONSTRUCTION)
      klib_log (KLIB_LOG_TRACE, "ref_count is %d -- will decrement\n",
    self->ref_count);
    self->ref_count--;
    }
  else
    {
    if (klib_object_trace_flags & KLIB_OBJECT_TRACE_CONSTRUCTION)
      klib_log (KLIB_LOG_TRACE, "ref_count is zero -- will dispose\n");
    self->dispose_func (self);
    }
  }
struct _klib_String* klib_string_new_empty ( ) [read]
  {
  return klib_string_new ("");
  }
void klib_string_printf ( struct _klib_String ,
  ... 
)

Variable Documentation

Can be set non-zero to enable very fine tracing.