|
Evan@653
|
1 |
/* TODO: Can we just replace this whole thing with a GCache */ |
|
Evan@653
|
2 |
|
|
Evan@653
|
3 |
/** |
|
Evan@653
|
4 |
* @file stringref.h Reference-counted immutable strings |
|
Evan@653
|
5 |
* @ingroup core |
|
Evan@653
|
6 |
*/ |
|
Evan@653
|
7 |
|
|
Evan@653
|
8 |
/* purple |
|
Evan@653
|
9 |
* |
|
Evan@653
|
10 |
* Purple is the legal property of its developers, whose names are too numerous |
|
Evan@653
|
11 |
* to list here. Please refer to the COPYRIGHT file distributed with this |
|
Evan@653
|
12 |
* source distribution. |
|
Evan@653
|
13 |
* |
|
Evan@653
|
14 |
* This program is free software; you can redistribute it and/or modify |
|
Evan@653
|
15 |
* it under the terms of the GNU General Public License as published by |
|
Evan@653
|
16 |
* the Free Software Foundation; either version 2 of the License, or |
|
Evan@653
|
17 |
* (at your option) any later version. |
|
Evan@653
|
18 |
* |
|
Evan@653
|
19 |
* This program is distributed in the hope that it will be useful, |
|
Evan@653
|
20 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
Evan@653
|
21 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
Evan@653
|
22 |
* GNU General Public License for more details. |
|
Evan@653
|
23 |
* |
|
Evan@653
|
24 |
* You should have received a copy of the GNU General Public License |
|
Evan@653
|
25 |
* along with this program; if not, write to the Free Software |
|
Evan@653
|
26 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA |
|
Evan@653
|
27 |
* |
|
Evan@653
|
28 |
*/ |
|
Evan@653
|
29 |
#ifndef _PURPLE_STRINGREF_H_ |
|
Evan@653
|
30 |
#define _PURPLE_STRINGREF_H_ |
|
Evan@653
|
31 |
|
|
Evan@653
|
32 |
#ifdef __cplusplus |
|
Evan@653
|
33 |
extern "C" { |
|
Evan@653
|
34 |
#endif |
|
Evan@653
|
35 |
|
|
Evan@653
|
36 |
typedef struct _PurpleStringref PurpleStringref; |
|
Evan@653
|
37 |
|
|
Evan@653
|
38 |
/** |
|
Evan@653
|
39 |
* Creates an immutable reference-counted string object. The newly |
|
Evan@653
|
40 |
* created object will have a reference count of 1. |
|
Evan@653
|
41 |
* |
|
Evan@653
|
42 |
* @param value This will be the value of the string; it will be |
|
Evan@653
|
43 |
* duplicated. |
|
Evan@653
|
44 |
* |
|
Evan@653
|
45 |
* @return A newly allocated string reference object with a refcount |
|
Evan@653
|
46 |
* of 1. |
|
Evan@653
|
47 |
*/ |
|
Evan@653
|
48 |
PurpleStringref *purple_stringref_new(const char *value); |
|
Evan@653
|
49 |
|
|
Evan@653
|
50 |
/** |
|
Evan@653
|
51 |
* Creates an immutable reference-counted string object. The newly |
|
Evan@653
|
52 |
* created object will have a reference count of zero, and if it is |
|
Evan@653
|
53 |
* not referenced before the next iteration of the mainloop it will |
|
Evan@653
|
54 |
* be freed at that time. |
|
Evan@653
|
55 |
* |
|
Evan@653
|
56 |
* @param value This will be the value of the string; it will be |
|
Evan@653
|
57 |
* duplicated. |
|
Evan@653
|
58 |
* |
|
Evan@653
|
59 |
* @return A newly allocated string reference object with a refcount |
|
Evan@653
|
60 |
* of zero. |
|
Evan@653
|
61 |
*/ |
|
Evan@653
|
62 |
PurpleStringref *purple_stringref_new_noref(const char *value); |
|
Evan@653
|
63 |
|
|
Evan@653
|
64 |
/** |
|
Evan@653
|
65 |
* Creates an immutable reference-counted string object from a printf |
|
Evan@653
|
66 |
* format specification and arguments. The created object will have a |
|
Evan@653
|
67 |
* reference count of 1. |
|
Evan@653
|
68 |
* |
|
Evan@653
|
69 |
* @param format A printf-style format specification. |
|
Evan@653
|
70 |
* |
|
Evan@653
|
71 |
* @return A newly allocated string reference object with a refcount |
|
Evan@653
|
72 |
* of 1. |
|
Evan@653
|
73 |
*/ |
|
Evan@653
|
74 |
PurpleStringref *purple_stringref_printf(const char *format, ...); |
|
Evan@653
|
75 |
|
|
Evan@653
|
76 |
/** |
|
Evan@653
|
77 |
* Increase the reference count of the given stringref. |
|
Evan@653
|
78 |
* |
|
Evan@653
|
79 |
* @param stringref String to be referenced. |
|
Evan@653
|
80 |
* |
|
Evan@653
|
81 |
* @return A pointer to the referenced string. |
|
Evan@653
|
82 |
*/ |
|
Evan@653
|
83 |
PurpleStringref *purple_stringref_ref(PurpleStringref *stringref); |
|
Evan@653
|
84 |
|
|
Evan@653
|
85 |
/** |
|
Evan@653
|
86 |
* Decrease the reference count of the given stringref. If this |
|
Evan@653
|
87 |
* reference count reaches zero, the stringref will be freed; thus |
|
Evan@653
|
88 |
* you MUST NOT use this string after dereferencing it. |
|
Evan@653
|
89 |
* |
|
Evan@653
|
90 |
* @param stringref String to be dereferenced. |
|
Evan@653
|
91 |
*/ |
|
Evan@653
|
92 |
void purple_stringref_unref(PurpleStringref *stringref); |
|
Evan@653
|
93 |
|
|
Evan@653
|
94 |
/** |
|
Evan@653
|
95 |
* Retrieve the value of a stringref. |
|
Evan@653
|
96 |
* |
|
Evan@653
|
97 |
* @note This value should not be cached or stored in a local variable. |
|
Evan@653
|
98 |
* While there is nothing inherently incorrect about doing so, it |
|
Evan@653
|
99 |
* is easy to forget that the cached value is in fact a |
|
Evan@653
|
100 |
* reference-counted object and accidentally use it after |
|
Evan@653
|
101 |
* dereferencing. This is more problematic for a reference- |
|
Evan@653
|
102 |
* counted object than a heap-allocated object, as it may seem to |
|
Evan@653
|
103 |
* be valid or invalid nondeterministically based on how many |
|
Evan@653
|
104 |
* other references to it exist. |
|
Evan@653
|
105 |
* |
|
Evan@653
|
106 |
* @param stringref String reference from which to retrieve the value. |
|
Evan@653
|
107 |
* |
|
Evan@653
|
108 |
* @return The contents of the string reference. |
|
Evan@653
|
109 |
*/ |
|
Evan@653
|
110 |
const char *purple_stringref_value(const PurpleStringref *stringref); |
|
Evan@653
|
111 |
|
|
Evan@653
|
112 |
/** |
|
Evan@653
|
113 |
* Compare two stringrefs for string equality. This returns the same |
|
Evan@653
|
114 |
* value as strcmp would, where <0 indicates that s1 is "less than" s2 |
|
Evan@653
|
115 |
* in the ASCII lexicography, 0 indicates equality, etc. |
|
Evan@653
|
116 |
* |
|
Evan@653
|
117 |
* @param s1 The reference string. |
|
Evan@653
|
118 |
* |
|
Evan@653
|
119 |
* @param s2 The string to compare against the reference. |
|
Evan@653
|
120 |
* |
|
Evan@653
|
121 |
* @return An ordering indication on s1 and s2. |
|
Evan@653
|
122 |
*/ |
|
Evan@653
|
123 |
int purple_stringref_cmp(const PurpleStringref *s1, const PurpleStringref *s2); |
|
Evan@653
|
124 |
|
|
Evan@653
|
125 |
/** |
|
Evan@653
|
126 |
* Find the length of the string inside a stringref. |
|
Evan@653
|
127 |
* |
|
Evan@653
|
128 |
* @param stringref The string in whose length we are interested. |
|
Evan@653
|
129 |
* |
|
Evan@653
|
130 |
* @return The length of the string in stringref |
|
Evan@653
|
131 |
*/ |
|
Evan@653
|
132 |
size_t purple_stringref_len(const PurpleStringref *stringref); |
|
Evan@653
|
133 |
|
|
Evan@653
|
134 |
#ifdef __cplusplus |
|
Evan@653
|
135 |
} |
|
Evan@653
|
136 |
#endif |
|
Evan@653
|
137 |
|
|
Evan@653
|
138 |
#endif /* _PURPLE_STRINGREF_H_ */ |