libgeda
|
00001 /* gEDA - GPL Electronic Design Automation 00002 * libgeda - gEDA's library 00003 * Copyright (C) 2010 gEDA Contributors (see ChangeLog for details) 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License as published by 00007 * the Free Software Foundation; either version 2 of the License, or 00008 * (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License 00016 * along with this program; if not, write to the Free Software 00017 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA 00018 */ 00019 #include <config.h> 00020 00021 #include "libgeda_priv.h" 00022 00031 struct WeakRef 00032 { 00033 void (*notify_func)(void *, void *); 00034 void *user_data; 00035 }; 00036 00047 void 00048 s_weakref_notify (void *dead_ptr, GList *weak_refs) 00049 { 00050 GList *iter; 00051 struct WeakRef *entry; 00052 for (iter = weak_refs; iter != NULL; iter = g_list_next (iter)) { 00053 entry = (struct WeakRef *) iter->data; 00054 if (entry != NULL && entry->notify_func != NULL) { 00055 entry->notify_func (dead_ptr, entry->user_data); 00056 } 00057 g_free (entry); 00058 } 00059 g_list_free (weak_refs); 00060 } 00061 00075 GList * 00076 s_weakref_add (GList *weak_refs, void (*notify_func)(void *, void *), 00077 void *user_data) 00078 { 00079 struct WeakRef *entry = g_malloc0 (sizeof (struct WeakRef)); 00080 entry->notify_func = notify_func; 00081 entry->user_data = user_data; 00082 return g_list_prepend (weak_refs, entry); 00083 } 00084 00096 GList * 00097 s_weakref_remove (GList *weak_refs, void (*notify_func)(void *, void *), 00098 void *user_data) 00099 { 00100 GList *iter; 00101 struct WeakRef *entry; 00102 for (iter = weak_refs; iter != NULL; iter = g_list_next (iter)) { 00103 entry = iter->data; 00104 if ((entry != NULL) && 00105 (entry->notify_func == notify_func) && 00106 (entry->user_data == user_data)) { 00107 g_free (entry); 00108 iter->data = NULL; 00109 } 00110 } 00111 return g_list_remove_all (weak_refs, NULL); 00112 } 00113 00114 static void 00115 weak_ptr_notify_func (void *dead_ptr, void *user_data) { 00116 void **weak_pointer_loc = (void **) user_data; 00117 if (weak_pointer_loc != NULL) { 00118 *weak_pointer_loc = NULL; 00119 } 00120 } 00121 00132 GList * 00133 s_weakref_add_ptr (GList *weak_refs, void **weak_pointer_loc) 00134 { 00135 return s_weakref_add (weak_refs, weak_ptr_notify_func, weak_pointer_loc); 00136 } 00137 00149 GList * 00150 s_weakref_remove_ptr (GList *weak_refs, void **weak_pointer_loc) 00151 { 00152 return s_weakref_remove (weak_refs, weak_ptr_notify_func, weak_pointer_loc); 00153 }