Main Page

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;
67 class_overload_destroy (klass, class_destroy);
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 }
avr_new
#define avr_new(type, count)
Macro for allocating memory.
Definition: avrmalloc.c:57
class_new
AvrClass * class_new(void)
This function should never be used.
Definition: avrclass.c:46
class_ref
void class_ref(AvrClass *klass)
Increments the reference count for the klass object.
Definition: avrclass.c:106
avr_free
void avr_free(void *ptr)
Free malloc&#39;d memory.
Definition: avrmalloc.c:187
class_destroy
void class_destroy(void *klass)
Releases resources allocated by class&#39;s <klass>_new() function.
Definition: avrclass.c:78
class_unref
void class_unref(AvrClass *klass)
Decrements the reference count for the klass object.
Definition: avrclass.c:120
avr_error
#define avr_error(fmt, args...)
Print an error message to stderr and terminate program.
Definition: avrerror.c:50
class_overload_destroy
void class_overload_destroy(AvrClass *klass, AvrClassFP_Destroy destroy)
Overload the default destroy method.
Definition: avrclass.c:92
class_construct
void class_construct(AvrClass *klass)
Initializes the AvrClass data structure.
Definition: avrclass.c:61

Automatically generated by Doxygen 1.8.12 on Tue Apr 18 2017.