avrclass.c
Go to the documentation of this file.
1 /*
2  * $Id: avrclass.c,v 1.8 2003/12/01 09:10:13 troth Exp $
3  *
4  ****************************************************************************
5  *
6  * simulavr - A simulator for the Atmel AVR family of microcontrollers.
7  * Copyright (C) 2001, 2002, 2003 Theodore A. Roth
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  *
23  ****************************************************************************
24  */
25 
26 /**
27  * \file avrclass.c
28  * \brief Methods to provide user interfaces to the AvrClass structure.
29  *
30  * This module provides the basis for simulavr's object mechanism. For a
31  * detailed discussion on using simulavr's class mechanism, see the simulavr
32  * users manual. FIXME: [TRoth 2002/03/19] move the discussion here. */
33 
34 #include <stdlib.h>
35 
36 #include "avrerror.h"
37 #include "avrmalloc.h"
38 #include "avrclass.h"
39 
40 /** \brief This function should never be used.
41  *
42  * The only potential use for it as a template for derived classes.
43  * Do Not Use This Function! */
44 
45 AvrClass *
46 class_new (void)
47 {
48  AvrClass *klass = avr_new (AvrClass, 1);
49  class_construct (klass);
50  return klass;
51 }
52 
53 /** \brief Initializes the AvrClass data structure.
54  *
55  * A derived class should call this function from their own
56  * <klass>_construct() function. All classes should
57  * have their constructor function call their parent's constructor
58  * function. */
59 
60 void
61 class_construct (AvrClass *klass)
62 {
63  if (klass == NULL)
64  avr_error ("passed null ptr");
65 
66  klass->ref_count = 1;
68 }
69 
70 /** \brief Releases resources allocated by class's <klass>_new() function.
71  *
72  * This function should never be called except as the last statement
73  * of a directly derived class's destroy method.
74  * All classes should have their destroy method call their parent's
75  * destroy method. */
76 
77 void
78 class_destroy (void *klass)
79 {
80  if (klass == NULL)
81  return;
82 
83  avr_free (klass);
84 }
85 
86 /** \brief Overload the default destroy method.
87  *
88  * Derived classes will call this to replace class_destroy() with their own
89  * destroy method. */
90 
91 void
92 class_overload_destroy (AvrClass *klass, AvrClassFP_Destroy destroy)
93 {
94  if (klass == NULL)
95  avr_error ("passed null ptr");
96 
97  klass->destroy = destroy;
98 }
99 
100 /** \brief Increments the reference count for the klass object.
101  *
102  * The programmer must call this whenever a reference to an object
103  * is stored in more than one place. */
104 
105 void
106 class_ref (AvrClass *klass)
107 {
108  if (klass == NULL)
109  avr_error ("passed null ptr");
110 
111  klass->ref_count++;
112 }
113 
114 /** \brief Decrements the reference count for the klass object.
115  *
116  * When the reference count reaches zero, the class's destroy method
117  * is called on the object. */
118 
119 void
120 class_unref (AvrClass *klass)
121 {
122  if (klass == NULL)
123  avr_error ("passed null ptr");
124 
125  klass->ref_count--;
126  if (klass->ref_count == 0)
127  klass->destroy (klass);
128 }

Automatically generated by Doxygen 1.8.2 on Tue Feb 26 2013.