Wed, 12 Nov 2008 05:14:03 +0000
merge of '77693555855fe9cd3215414f79964dba346cc5fa'
and '19a87e98e5857ad0289f2c760d460f7f1dbbb42d'
| 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 | #ifndef _GAIM_STRINGREF_H_ | |
| 27 | #define _GAIM_STRINGREF_H_ | |
| 28 | ||
|
14988
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
29 | #ifdef __cplusplus |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
30 | extern "C" { |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
31 | #endif |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
32 | |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
33 | typedef struct _GaimStringref GaimStringref; |
| 7763 | 34 | |
| 35 | /** | |
| 36 | * Creates an immutable reference-counted string object. The newly | |
| 37 | * created object will have a reference count of 1. | |
| 38 | * | |
| 39 | * @param value This will be the value of the string; it will be | |
| 40 | * duplicated. | |
| 41 | * | |
| 7767 | 42 | * @return A newly allocated string reference object with a refcount |
| 43 | * of 1. | |
| 7763 | 44 | */ |
| 45 | GaimStringref *gaim_stringref_new(const char *value); | |
| 46 | ||
| 47 | /** | |
| 7786 | 48 | * Creates an immutable reference-counted string object. The newly |
| 49 | * created object will have a reference count of zero, and if it is | |
| 50 | * not referenced before the next iteration of the mainloop it will | |
| 51 | * be freed at that time. | |
| 52 | * | |
| 53 | * @param value This will be the value of the string; it will be | |
| 54 | * duplicated. | |
| 55 | * | |
| 56 | * @return A newly allocated string reference object with a refcount | |
| 57 | * of zero. | |
| 58 | */ | |
| 59 | GaimStringref *gaim_stringref_new_noref(const char *value); | |
| 60 | ||
| 61 | /** | |
| 7767 | 62 | * Creates an immutable reference-counted string object from a printf |
| 63 | * format specification and arguments. The created object will have a | |
| 64 | * reference count of 1. | |
| 65 | * | |
| 66 | * @param format A printf-style format specification. | |
| 67 | * | |
| 68 | * @return A newly allocated string reference object with a refcount | |
| 69 | * of 1. | |
| 70 | */ | |
| 71 | GaimStringref *gaim_stringref_printf(const char *format, ...); | |
| 72 | ||
| 73 | /** | |
| 7763 | 74 | * Increase the reference count of the given stringref. |
| 75 | * | |
| 76 | * @param stringref String to be referenced. | |
| 77 | * | |
| 78 | * @return A pointer to the referenced string. | |
| 79 | */ | |
| 80 | GaimStringref *gaim_stringref_ref(GaimStringref *stringref); | |
| 81 | ||
| 82 | /** | |
| 83 | * Decrease the reference count of the given stringref. If this | |
| 84 | * reference count reaches zero, the stringref will be freed; thus | |
| 85 | * you MUST NOT use this string after dereferencing it. | |
| 86 | * | |
| 87 | * @param stringref String to be dereferenced. | |
| 88 | */ | |
| 89 | void gaim_stringref_unref(GaimStringref *stringref); | |
| 90 | ||
| 91 | /** | |
| 92 | * Retrieve the value of a stringref. | |
| 93 | * | |
| 94 | * @note This value should not be cached or stored in a local variable. | |
| 95 | * While there is nothing inherently incorrect about doing so, it | |
| 96 | * is easy to forget that the cached value is in fact a | |
| 97 | * reference-counted object and accidentally use it after | |
| 98 | * dereferencing. This is more problematic for a reference- | |
| 99 | * counted object than a heap-allocated object, as it may seem to | |
| 100 | * be valid or invalid nondeterministically based on how many | |
| 101 | * other references to it exist. | |
| 102 | * | |
| 103 | * @param stringref String reference from which to retrieve the value. | |
| 104 | * | |
| 105 | * @return The contents of the string reference. | |
| 106 | */ | |
| 7768 | 107 | const char *gaim_stringref_value(const GaimStringref *stringref); |
| 7763 | 108 | |
| 7786 | 109 | /** |
| 110 | * Compare two stringrefs for string equality. This returns the same | |
| 111 | * value as strcmp would, where <0 indicates that s1 is "less than" s2 | |
| 112 | * in the ASCII lexicography, 0 indicates equality, etc. | |
| 113 | * | |
| 114 | * @param s1 The reference string. | |
| 115 | * | |
| 116 | * @param s2 The string to compare against the reference. | |
| 117 | * | |
| 118 | * @return An ordering indication on s1 and s2. | |
| 119 | */ | |
| 120 | int gaim_stringref_cmp(const GaimStringref *s1, const GaimStringref *s2); | |
| 121 | ||
| 122 | /** | |
| 123 | * Find the length of the string inside a stringref. | |
| 124 | * | |
| 125 | * @param stringref The string in whose length we are interested. | |
| 126 | * | |
| 127 | * @return The length of the string in stringref | |
| 128 | */ | |
| 129 | size_t gaim_stringref_len(const GaimStringref *stringref); | |
| 130 | ||
|
14988
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
131 | #ifdef __cplusplus |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
132 | } |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
133 | #endif |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
134 | |
| 7763 | 135 | #endif /* _GAIM_STRINGREF_H_ */ |