From 32a5612d4c10c5affd1223c7581fb9254b3770cd Mon Sep 17 00:00:00 2001 From: Chris Shoemaker Date: Wed, 11 Jan 2006 04:27:02 +0000 Subject: [PATCH] Add CACHE_REPLACE(dst, src) macro for common case in string setters. git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@12319 57a11ea4-9604-0410-9ed3-97b8803252fd --- lib/libqof/qof/gnc-engine-util.h | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/libqof/qof/gnc-engine-util.h b/lib/libqof/qof/gnc-engine-util.h index c258f53f31..da764d79b5 100644 --- a/lib/libqof/qof/gnc-engine-util.h +++ b/lib/libqof/qof/gnc-engine-util.h @@ -305,8 +305,22 @@ void gnc_string_cache_remove(gconstpointer key); */ gpointer gnc_string_cache_insert(gconstpointer key); -#define CACHE_INSERT(str) gnc_string_cache_insert((gconstpointer)(str)); -#define CACHE_REMOVE(str) gnc_string_cache_remove((str)); +#define CACHE_INSERT(str) gnc_string_cache_insert((gconstpointer)(str)) +#define CACHE_REMOVE(str) gnc_string_cache_remove((str)) + +/* Replace cached string currently in 'dst' with string in 'src'. + * Typical usage: + * void foo_set_name(Foo *f, const char *str) { + * CACHE_REPLACE(f->name, str); + * } + * It avoids unnecessary ejection by doing INSERT before REMOVE. +*/ +#define CACHE_REPLACE(dst, src) do { \ + gpointer tmp = CACHE_INSERT((src)); \ + CACHE_REMOVE((dst)); \ + (dst) = tmp; \ + } while (0) + #endif /* QOF_UTIL_H */ /** @} */