callback.c

00001 /*
00002  * $Id: callback.c,v 1.5 2003/12/01 09:10:14 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 #include <config.h>
00027 
00028 #include <stdio.h>
00029 #include <stdlib.h>
00030 
00031 #include "avrerror.h"
00032 #include "avrmalloc.h"
00033 #include "avrclass.h"
00034 #include "utils.h"
00035 #include "callback.h"
00036 
00037 /****************************************************************************\
00038  *
00039  * Clock Call Back methods
00040  *
00041 \****************************************************************************/
00042 
00043 #ifndef DOXYGEN                 /* don't expose to doxygen */
00044 
00045 struct _CallBack
00046 {
00047     AvrClass parent;
00048     CallBack_FP func;           /* the callback function */
00049     AvrClass *data;             /* user data to be passed to callback
00050                                    function */
00051 };
00052 
00053 #endif /* DOXYGEN */
00054 
00055 CallBack *
00056 callback_new (CallBack_FP func, AvrClass *data)
00057 {
00058     CallBack *cb;
00059 
00060     cb = avr_new (CallBack, 1);
00061     callback_construct (cb, func, data);
00062     class_overload_destroy ((AvrClass *)cb, callback_destroy);
00063 
00064     return cb;
00065 }
00066 
00067 void
00068 callback_construct (CallBack *cb, CallBack_FP func, AvrClass *data)
00069 {
00070     if (cb == NULL)
00071         avr_error ("passed null ptr");
00072 
00073     class_construct ((AvrClass *)cb);
00074 
00075     cb->func = func;
00076 
00077     cb->data = data;
00078 
00079     if (data)
00080         class_ref (data);
00081 }
00082 
00083 void
00084 callback_destroy (void *cb)
00085 {
00086     CallBack *_cb = (CallBack *)cb;
00087 
00088     if (cb == NULL)
00089         return;
00090 
00091     if (_cb->data)
00092         class_unref (_cb->data);
00093 
00094     class_destroy (cb);
00095 }
00096 
00097 DList *
00098 callback_list_add (DList *head, CallBack *cb)
00099 {
00100     return dlist_add (head, (AvrClass *)cb, NULL);
00101 }
00102 
00103 static int
00104 callback_execute (AvrClass *data, void *user_data)
00105 {
00106     CallBack *cb = (CallBack *)data;
00107     uint64_t time = *(uint64_t *) user_data;
00108 
00109     return cb->func (time, cb->data);
00110 }
00111 
00112 DList *
00113 callback_list_execute_all (DList *head, uint64_t time)
00114 {
00115     return dlist_iterator (head, callback_execute, &time);
00116 }

Automatically generated by Doxygen 1.5.2 on 14 Feb 2008.