avrclass.c

Go to the documentation of this file.
00001 /*
00002  * $Id: avrclass.c,v 1.8 2003/12/01 09:10:13 troth Exp $
00003  *
00004  ****************************************************************************
00005  *
00006  * simulavr - A simulator for the Atmel AVR family of microcontrollers.
00007  * Copyright (C) 2001, 2002, 2003  Theodore A. Roth
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022  *
00023  ****************************************************************************
00024  */
00025 
00026 /**
00027  * \file avrclass.c
00028  * \brief Methods to provide user interfaces to the AvrClass structure.
00029  *
00030  * This module provides the basis for simulavr's object mechanism. For a
00031  * detailed discussion on using simulavr's class mechanism, see the simulavr
00032  * users manual. FIXME: [TRoth 2002/03/19] move the discussion here. */
00033 
00034 #include <stdlib.h>
00035 
00036 #include "avrerror.h"
00037 #include "avrmalloc.h"
00038 #include "avrclass.h"
00039 
00040 /** \brief This function should never be used. 
00041  *
00042  *  The only potential use for it as a template for derived classes. 
00043  *  Do Not Use This Function! */
00044 
00045 AvrClass *
00046 class_new (void)
00047 {
00048     AvrClass *klass = avr_new (AvrClass, 1);
00049     class_construct (klass);
00050     return klass;
00051 }
00052 
00053 /** \brief Initializes the AvrClass data structure. 
00054  *
00055  *  A derived class should call this function from their own 
00056  *  <klass>_construct() function. All classes should
00057  *  have their constructor function call their parent's constructor
00058  *  function. */
00059 
00060 void
00061 class_construct (AvrClass *klass)
00062 {
00063     if (klass == NULL)
00064         avr_error ("passed null ptr");
00065 
00066     klass->ref_count = 1;
00067     class_overload_destroy (klass, class_destroy);
00068 }
00069 
00070 /** \brief Releases resources allocated by class's <klass>_new() function. 
00071  *
00072  * This function should never be called except as the last statement 
00073  * of a directly derived class's destroy method. 
00074  * All classes should have their destroy method call their parent's 
00075  * destroy method. */
00076 
00077 void
00078 class_destroy (void *klass)
00079 {
00080     if (klass == NULL)
00081         return;
00082 
00083     avr_free (klass);
00084 }
00085 
00086 /** \brief Overload the default destroy method.
00087  *
00088  * Derived classes will call this to replace class_destroy() with their own
00089  * destroy method. */
00090 
00091 void
00092 class_overload_destroy (AvrClass *klass, AvrClassFP_Destroy destroy)
00093 {
00094     if (klass == NULL)
00095         avr_error ("passed null ptr");
00096 
00097     klass->destroy = destroy;
00098 }
00099 
00100 /** \brief Increments the reference count for the klass object. 
00101  *
00102  * The programmer must call this whenever a reference to an object 
00103  * is stored in more than one place. */
00104 
00105 void
00106 class_ref (AvrClass *klass)
00107 {
00108     if (klass == NULL)
00109         avr_error ("passed null ptr");
00110 
00111     klass->ref_count++;
00112 }
00113 
00114 /** \brief Decrements the reference count for the klass object. 
00115  *
00116  * When the reference count reaches zero, the class's destroy method 
00117  * is called on the object. */
00118 
00119 void
00120 class_unref (AvrClass *klass)
00121 {
00122     if (klass == NULL)
00123         avr_error ("passed null ptr");
00124 
00125     klass->ref_count--;
00126     if (klass->ref_count == 0)
00127         klass->destroy (klass);
00128 }

Automatically generated by Doxygen 1.4.7 on 25 Oct 2006.