Sun, 11 Jan 2004 06:10:22 +0000
[gaim-migrate @ 8770]
the 0-byte-string trick doesn't work on non-gcc compilers.
| 7763 | 1 | /** |
| 2 | * @file stringref.h Reference-counted immutable strings | |
| 3 | * @ingroup core | |
| 4 | * | |
| 5 | * gaim | |
| 6 | * | |
| 8046 | 7 | * Gaim is the legal property of its developers, whose names are too numerous |
| 8 | * to list here. Please refer to the COPYRIGHT file distributed with this | |
| 9 | * source distribution. | |
| 7763 | 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify | |
| 12 | * it under the terms of the GNU General Public License as published by | |
| 13 | * the Free Software Foundation; either version 2 of the License, or | |
| 14 | * (at your option) any later version. | |
| 15 | * | |
| 16 | * This program is distributed in the hope that it will be useful, | |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 19 | * GNU General Public License for more details. | |
| 20 | * | |
| 21 | * You should have received a copy of the GNU General Public License | |
| 22 | * along with this program; if not, write to the Free Software | |
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 24 | * | |
| 25 | */ | |
| 26 | ||
| 27 | #ifndef _GAIM_STRINGREF_H_ | |
| 28 | #define _GAIM_STRINGREF_H_ | |
| 29 | ||
| 30 | /** | |
| 7786 | 31 | * The internal representation of a stringref. |
| 32 | * | |
| 7763 | 33 | * @note For this structure to be useful, the string contained within |
| 34 | * it must be immutable -- for this reason, do _not_ access it | |
| 35 | * directly! | |
| 36 | */ | |
| 37 | typedef struct _GaimStringref { | |
| 7791 | 38 | guint32 ref; /**< The reference count of this string. |
| 39 | * Note that reference counts are only | |
| 40 | * 31 bits, and the high-order bit | |
| 41 | * indicates whether this string is up | |
| 42 | * for GC at the next idle handler... | |
| 43 | * But you aren't going to touch this | |
| 44 | * anyway, right? */ | |
| 8072 | 45 | char value[1]; /**< The string contained in this ref. |
| 7791 | 46 | * Notice that it is simply "hanging |
| 47 | * off the end" of the ref ... this | |
| 48 | * is to save an allocation. */ | |
| 7763 | 49 | } GaimStringref; |
| 50 | ||
| 51 | /** | |
| 52 | * Creates an immutable reference-counted string object. The newly | |
| 53 | * created object will have a reference count of 1. | |
| 54 | * | |
| 55 | * @param value This will be the value of the string; it will be | |
| 56 | * duplicated. | |
| 57 | * | |
| 7767 | 58 | * @return A newly allocated string reference object with a refcount |
| 59 | * of 1. | |
| 7763 | 60 | */ |
| 61 | GaimStringref *gaim_stringref_new(const char *value); | |
| 62 | ||
| 63 | /** | |
| 7786 | 64 | * Creates an immutable reference-counted string object. The newly |
| 65 | * created object will have a reference count of zero, and if it is | |
| 66 | * not referenced before the next iteration of the mainloop it will | |
| 67 | * be freed at that time. | |
| 68 | * | |
| 69 | * @param value This will be the value of the string; it will be | |
| 70 | * duplicated. | |
| 71 | * | |
| 72 | * @return A newly allocated string reference object with a refcount | |
| 73 | * of zero. | |
| 74 | */ | |
| 75 | GaimStringref *gaim_stringref_new_noref(const char *value); | |
| 76 | ||
| 77 | /** | |
| 7767 | 78 | * Creates an immutable reference-counted string object from a printf |
| 79 | * format specification and arguments. The created object will have a | |
| 80 | * reference count of 1. | |
| 81 | * | |
| 82 | * @param format A printf-style format specification. | |
| 83 | * | |
| 84 | * @return A newly allocated string reference object with a refcount | |
| 85 | * of 1. | |
| 86 | */ | |
| 87 | GaimStringref *gaim_stringref_printf(const char *format, ...); | |
| 88 | ||
| 89 | /** | |
| 7763 | 90 | * Increase the reference count of the given stringref. |
| 91 | * | |
| 92 | * @param stringref String to be referenced. | |
| 93 | * | |
| 94 | * @return A pointer to the referenced string. | |
| 95 | */ | |
| 96 | GaimStringref *gaim_stringref_ref(GaimStringref *stringref); | |
| 97 | ||
| 98 | /** | |
| 99 | * Decrease the reference count of the given stringref. If this | |
| 100 | * reference count reaches zero, the stringref will be freed; thus | |
| 101 | * you MUST NOT use this string after dereferencing it. | |
| 102 | * | |
| 103 | * @param stringref String to be dereferenced. | |
| 104 | */ | |
| 105 | void gaim_stringref_unref(GaimStringref *stringref); | |
| 106 | ||
| 107 | /** | |
| 108 | * Retrieve the value of a stringref. | |
| 109 | * | |
| 110 | * @note This value should not be cached or stored in a local variable. | |
| 111 | * While there is nothing inherently incorrect about doing so, it | |
| 112 | * is easy to forget that the cached value is in fact a | |
| 113 | * reference-counted object and accidentally use it after | |
| 114 | * dereferencing. This is more problematic for a reference- | |
| 115 | * counted object than a heap-allocated object, as it may seem to | |
| 116 | * be valid or invalid nondeterministically based on how many | |
| 117 | * other references to it exist. | |
| 118 | * | |
| 119 | * @param stringref String reference from which to retrieve the value. | |
| 120 | * | |
| 121 | * @return The contents of the string reference. | |
| 122 | */ | |
| 7768 | 123 | const char *gaim_stringref_value(const GaimStringref *stringref); |
| 7763 | 124 | |
| 7786 | 125 | /** |
| 126 | * Compare two stringrefs for string equality. This returns the same | |
| 127 | * value as strcmp would, where <0 indicates that s1 is "less than" s2 | |
| 128 | * in the ASCII lexicography, 0 indicates equality, etc. | |
| 129 | * | |
| 130 | * @param s1 The reference string. | |
| 131 | * | |
| 132 | * @param s2 The string to compare against the reference. | |
| 133 | * | |
| 134 | * @return An ordering indication on s1 and s2. | |
| 135 | */ | |
| 136 | int gaim_stringref_cmp(const GaimStringref *s1, const GaimStringref *s2); | |
| 137 | ||
| 138 | /** | |
| 139 | * Find the length of the string inside a stringref. | |
| 140 | * | |
| 141 | * @param stringref The string in whose length we are interested. | |
| 142 | * | |
| 143 | * @return The length of the string in stringref | |
| 144 | */ | |
| 145 | size_t gaim_stringref_len(const GaimStringref *stringref); | |
| 146 | ||
| 7763 | 147 | #endif /* _GAIM_STRINGREF_H_ */ |