File: avrclass.c

package info (click to toggle)
simulavr 0.1.2.2-6.1
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 2,756 kB
  • ctags: 3,179
  • sloc: ansic: 19,987; sh: 3,623; python: 3,528; makefile: 406; asm: 308; yacc: 145; lex: 48
file content (128 lines) | stat: -rw-r--r-- 3,580 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * $Id: avrclass.c,v 1.8 2003/12/01 09:10:13 troth Exp $
 *
 ****************************************************************************
 *
 * simulavr - A simulator for the Atmel AVR family of microcontrollers.
 * Copyright (C) 2001, 2002, 2003  Theodore A. Roth
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 ****************************************************************************
 */

/**
 * \file avrclass.c
 * \brief Methods to provide user interfaces to the AvrClass structure.
 *
 * This module provides the basis for simulavr's object mechanism. For a
 * detailed discussion on using simulavr's class mechanism, see the simulavr
 * users manual. FIXME: [TRoth 2002/03/19] move the discussion here. */

#include <stdlib.h>

#include "avrerror.h"
#include "avrmalloc.h"
#include "avrclass.h"

/** \brief This function should never be used. 
 *
 *  The only potential use for it as a template for derived classes. 
 *  Do Not Use This Function! */

AvrClass *
class_new (void)
{
    AvrClass *klass = avr_new (AvrClass, 1);
    class_construct (klass);
    return klass;
}

/** \brief Initializes the AvrClass data structure. 
 *
 *  A derived class should call this function from their own 
 *  \<klass\>_construct() function. All classes should
 *  have their constructor function call their parent's constructor
 *  function. */

void
class_construct (AvrClass *klass)
{
    if (klass == NULL)
        avr_error ("passed null ptr");

    klass->ref_count = 1;
    class_overload_destroy (klass, class_destroy);
}

/** \brief Releases resources allocated by class's \<klass\>_new() function. 
 *
 * This function should never be called except as the last statement 
 * of a directly derived class's destroy method. 
 * All classes should have their destroy method call their parent's 
 * destroy method. */

void
class_destroy (void *klass)
{
    if (klass == NULL)
        return;

    avr_free (klass);
}

/** \brief Overload the default destroy method.
 *
 * Derived classes will call this to replace class_destroy() with their own
 * destroy method. */

void
class_overload_destroy (AvrClass *klass, AvrClassFP_Destroy destroy)
{
    if (klass == NULL)
        avr_error ("passed null ptr");

    klass->destroy = destroy;
}

/** \brief Increments the reference count for the klass object. 
 *
 * The programmer must call this whenever a reference to an object 
 * is stored in more than one place. */

void
class_ref (AvrClass *klass)
{
    if (klass == NULL)
        avr_error ("passed null ptr");

    klass->ref_count++;
}

/** \brief Decrements the reference count for the klass object. 
 *
 * When the reference count reaches zero, the class's destroy method 
 * is called on the object. */

void
class_unref (AvrClass *klass)
{
    if (klass == NULL)
        avr_error ("passed null ptr");

    klass->ref_count--;
    if (klass->ref_count == 0)
        klass->destroy (klass);
}