wvdbusserver: implement NameHasOwner request.
[wvstreams.git] / include / wvstring.h
blob1926efc645111552b4f862a799d2c5c903330e9f
1 /* -*- Mode: C++ -*-
2 * Worldvisions Weaver Software:
3 * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
5 * Implementation of a simple and efficient printable-string class.
6 *
7 * It leaves out many of the notational conveniences provided by other
8 * string classes, because they waste too much CPU time and space.
9 * It does the one thing really missing from char* strings, that is,
10 * dynamic buffer management.
12 * The 'str' member is the actual (char*) string. You should never
13 * need to access it directly.
15 #ifndef __WVSTRING_H
16 #define __WVSTRING_H
18 #include <string.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string> // no code is actually used from here
24 /* 1 byte for terminating NUL */
25 #define WVSTRING_EXTRA 1
28 #define __WVS_F(n) WvStringParm __wvs_##n
29 #define __WVS_FORM(n) WvStringParm __wvs_##n = WvFastString::null
30 #define WVSTRING_FORMAT_DECL WvStringParm __wvs_format, \
31 WvStringParm __wvs_a0, \
32 __WVS_FORM( a1), __WVS_FORM( a2), __WVS_FORM( a3), \
33 __WVS_FORM( a4), __WVS_FORM( a5), __WVS_FORM( a6), \
34 __WVS_FORM( a7), __WVS_FORM( a8), __WVS_FORM( a9), \
35 __WVS_FORM(a10), __WVS_FORM(a11), __WVS_FORM(a12), \
36 __WVS_FORM(a13), __WVS_FORM(a14), __WVS_FORM(a15), \
37 __WVS_FORM(a16), __WVS_FORM(a17), __WVS_FORM(a18), \
38 __WVS_FORM(a19)
39 #define WVSTRING_FORMAT_DEFN WvStringParm __wvs_format, \
40 WvStringParm __wvs_a0, \
41 __WVS_F( a1), __WVS_F( a2), __WVS_F( a3), \
42 __WVS_F( a4), __WVS_F( a5), __WVS_F( a6), \
43 __WVS_F( a7), __WVS_F( a8), __WVS_F( a9), \
44 __WVS_F(a10), __WVS_F(a11), __WVS_F(a12), \
45 __WVS_F(a13), __WVS_F(a14), __WVS_F(a15), \
46 __WVS_F(a16), __WVS_F(a17), __WVS_F(a18), \
47 __WVS_F(a19)
48 #define WVSTRING_FORMAT_CALL __wvs_format, __wvs_a0, \
49 __wvs_a1, __wvs_a2, __wvs_a3, __wvs_a4, __wvs_a5, \
50 __wvs_a6, __wvs_a7, __wvs_a8, __wvs_a9, __wvs_a10, \
51 __wvs_a11, __wvs_a12, __wvs_a13, __wvs_a14, __wvs_a15, \
52 __wvs_a16, __wvs_a17, __wvs_a18, __wvs_a19
54 struct WvStringBuf;
55 class WvFastString;
56 class WvString;
57 class QString; // for operator QString()
58 class QCString;
60 // all WvFastString objects are const - they should _only_ be created
61 // automatically by automatic typecasting in parameter passing. So let's
62 // create a handy alias.
63 typedef const WvFastString & WvStringParm;
67 struct WvStringBuf
69 size_t size; // string length - if zero, use strlen!!
70 unsigned links; // number of WvStrings using this buf.
71 char data[1]; // optional room for extra string data
75 // the _actual_ space taken by a WvStringBuf, without the data[] array
76 // (which is variable-sized, not really 1 byte)
77 #define WVSTRINGBUF_SIZE(s) (s->data - (char *)s)
79 /**
80 * A WvFastString acts exactly like a WvString, but can take (const char *)
81 * strings without needing to allocate any memory, thus making it faster.
83 * When we copy to a normal WvString object, _then_ we allocate the memory.
84 * If that never happens, we never need to allocate.
86 * DON'T CREATE INSTANCES OF THIS! It's mostly useful for parameter passing,
87 * and for that you should use WvStringParm. You can get yourself into _big_
88 * trouble if you have an instance of a WvFastString created from a (char *)
89 * object and then you modify the original (char *).
91 * For almost all purposes, use WvString instead. At worst, it's a bit slower.
93 class WvFastString
95 friend class WvString; // so WvString can access members of _other_ objects
97 protected:
98 WvStringBuf *buf;
99 char *str;
101 // WvStringBuf used for char* strings that have not been cloned.
102 static WvStringBuf nullbuf;
104 public:
105 // a null string, converted to char* as "(nil)"
106 static const WvFastString null;
109 * Create an empty, NULL string. In the past, these were dangerous
110 * and could only be filled with operator= or setsize(); nowadays, NULL
111 * strings are explicitly allowed, since it's useful to express the
112 * difference between a zero-length string and a NULL result.
114 WvFastString();
115 void setsize(size_t i);
118 * Returns a copy of string pointed i bytes into this. Will not make it point
119 * past the trailing null byte.
121 WvFastString offset(size_t i) const;
124 * Copy constructor. We can safely copy from a normal WvString like this
125 * too, since no special behaviour is required in this direction. (Note
126 * that copying from a WvFastString to a WvString _does_ require special
127 * care!)
129 WvFastString(const WvFastString &s);
130 WvFastString(const WvString &s);
133 * Create a string out of a (char *)-style string _without_ copying any
134 * memory. It's fast, but we have to trust that the _str won't change
135 * for the lifetime of our WvFastString. That's usually safe, if you
136 * didn't use WvFastString where you should have used a WvString.
138 WvFastString(const char *_str);
141 * Create a string out of a Qt library QString. If you use this,
142 * you need to link with libwvqt.so.
144 WvFastString(const QString &s);
145 WvFastString(const QCString &s);
148 * Create a string out of a stdc++ string. To use this, #include
149 * wvstdstring.h.
151 inline WvFastString(const std::string &s);
154 * NOTE: make sure that 32 bytes is big enough for your longest
155 * int. This is true up to at least 64 bits.
157 WvFastString(short i);
158 WvFastString(unsigned short i);
159 WvFastString(int i);
160 WvFastString(unsigned int i);
161 WvFastString(long i);
162 WvFastString(unsigned long i);
163 WvFastString(long long i);
164 WvFastString(unsigned long long i);
165 WvFastString(double i);
167 /** when this is called, we assume output.str == NULL; it will be filled. */
168 static void do_format(WvFastString &output, const char *format,
169 const WvFastString * const *a);
173 * Now, you're probably thinking to yourself: Boy, does this ever
174 * look ridiculous. And indeed it does. However, it is
175 * completely type-safe and when functions are enabled, it
176 * reduces automatically to its minimum possible implementation.
177 * (ie. all extra comparisons with wv_null go away if the
178 * parameter really _is_ wv_null, and that is the default!)
180 * I failed to find a way to optimize out the comparisons for
181 * parameters that _are_ provided, however.
183 * There is a small problem, which is that only up to 20 (numbers
184 * 0 to 19) additional arguments are allowed. Luckily, no one has
185 * ever used that many on one "printf"-type line in the history of
186 * the world.
188 WvFastString(WVSTRING_FORMAT_DECL)
190 const WvFastString *x[20];
192 x[ 0] = (&__wvs_a0 != &null)? &__wvs_a0 : 0;
193 x[ 1] = (&__wvs_a1 != &null)? &__wvs_a1 : 0;
194 x[ 2] = (&__wvs_a2 != &null)? &__wvs_a2 : 0;
195 x[ 3] = (&__wvs_a3 != &null)? &__wvs_a3 : 0;
196 x[ 4] = (&__wvs_a4 != &null)? &__wvs_a4 : 0;
197 x[ 5] = (&__wvs_a5 != &null)? &__wvs_a5 : 0;
198 x[ 6] = (&__wvs_a6 != &null)? &__wvs_a6 : 0;
199 x[ 7] = (&__wvs_a7 != &null)? &__wvs_a7 : 0;
200 x[ 8] = (&__wvs_a8 != &null)? &__wvs_a8 : 0;
201 x[ 9] = (&__wvs_a9 != &null)? &__wvs_a9 : 0;
202 x[10] = (&__wvs_a10 != &null)? &__wvs_a10 : 0;
203 x[11] = (&__wvs_a11 != &null)? &__wvs_a11 : 0;
204 x[12] = (&__wvs_a12 != &null)? &__wvs_a12 : 0;
205 x[13] = (&__wvs_a13 != &null)? &__wvs_a13 : 0;
206 x[14] = (&__wvs_a14 != &null)? &__wvs_a14 : 0;
207 x[15] = (&__wvs_a15 != &null)? &__wvs_a15 : 0;
208 x[16] = (&__wvs_a16 != &null)? &__wvs_a16 : 0;
209 x[17] = (&__wvs_a17 != &null)? &__wvs_a17 : 0;
210 x[18] = (&__wvs_a18 != &null)? &__wvs_a18 : 0;
211 x[19] = (&__wvs_a19 != &null)? &__wvs_a19 : 0;
213 link(&nullbuf, NULL);
214 do_format(*this, __wvs_format.str, x);
217 ~WvFastString();
220 * Figure out the length of this string. ==0 if NULL or empty.
222 size_t len() const;
224 protected:
225 void construct(const char *_str);
227 // this doesn't exist - it's just here to keep it from being auto-created
228 // by stupid C++.
229 WvFastString &operator= (const WvFastString &s2);
231 // connect/disconnect ourselves from a WvStringBuf.
232 void link(WvStringBuf *_buf, const char *_str);
233 void unlink();
235 // allocate new space for buffers - needed only by the (int i) constructor,
236 // for now.
237 WvStringBuf *alloc(size_t size);
238 void newbuf(size_t size);
240 public:
241 // string comparison
242 bool operator== (WvStringParm s2) const;
243 bool operator!= (WvStringParm s2) const;
244 bool operator< (WvStringParm s2) const;
245 bool operator== (const char *s2) const;
246 bool operator!= (const char *s2) const;
247 bool operator< (const char *s2) const;
249 /** the not operator is 'true' if string is empty */
250 bool operator! () const;
252 // pointer arithmetic
253 const char *operator+ (int i) const
254 { return str + i; }
255 const char *operator- (int i) const
256 { return str - i; }
258 /** auto-convert WvString to (const char *), when needed. */
259 operator const char*() const
260 { return str; }
263 * return a (const char *) for this string. The typecast operator does
264 * this automatically when needed, but sometimes (especially with varargs
265 * like in printf()) that isn't convenient enough.
267 const char *cstr() const
268 { return str; }
271 * return a Qt library QString containing the contents of this string.
272 * You need to link to libwvqt.so if you use this.
274 operator QString() const;
277 * Return a stdc++ string with the contents of this string. To use
278 * this, #include wvstdstring.h.
280 //inline operator std::string() const;
283 * used to convert WvString to int, when needed.
284 * we no longer provide a typecast, because it causes annoyance.
286 int num() const
287 { return str ? atoi(str) : 0; }
289 /** returns true if this string is null */
290 bool isnull() const
291 { return str == NULL; }
293 /** returns either this string, or, if isnull(), the given string. */
294 const WvFastString &ifnull(WvStringParm defval) const
295 { return isnull() ? defval : *this; }
300 * WvString is an implementation of a simple and efficient
301 * printable-string class. It leaves out many of the notational
302 * conveniences provided by other string classes, because they waste
303 * too much CPU time and space.
305 * It does the one thing really missing from char* strings, that is,
306 * dynamic buffer management.
308 * When you copy one WvString to another, it does _not_ duplicate the
309 * buffer; it just creates another pointer to it. To really duplicate
310 * the buffer, call the unique() member function.
312 * To change the contents of a WvString, you need to run its edit()
313 * member function, which executes unique() and then returns a char*
314 * pointer to the WvString contents.
316 * The most annoying side-effect of this implementation is that if you
317 * construct a WvString from a char* buffer or static string, WvString
318 * won't duplicate it. Usually this is okay and much faster (for
319 * example, if you just want to print a static string). However, if
320 * you construct a WvString from a dynamic variable, changing the
321 * dynamic variable will change the WvString unless you run unique()
322 * or edit(). Worse still, deleting the dynamic variable will make
323 * WvString act unpredictably.
325 * But it does cut out extra dynamic memory allocation for the most
326 * common cases, and it almost always avoids manual 'new' and 'delete'
327 * of string objects.
329 class WvString : public WvFastString
331 public:
332 // an empty string, converted to char* as ""
333 static const WvString empty;
335 WvString() {} // nothing special needed
336 WvString(short i) : WvFastString(i) { } // nothing special
337 WvString(unsigned short i) : WvFastString(i) { } // nothing special
338 WvString(int i) : WvFastString(i) { } // nothing special
339 WvString(unsigned int i) : WvFastString(i) { } // nothing special
340 WvString(long i) : WvFastString(i) { } // nothing special
341 WvString(unsigned long i) : WvFastString(i) { } // nothing special
342 WvString(long long i) : WvFastString(i) { } // nothing special
343 WvString(unsigned long long i) : WvFastString(i) { } // nothing special
344 WvString(double i) : WvFastString(i) { } // nothing special
347 * Magic copy constructor for "fast" char* strings. When we copy from
348 * a "fast" string to a real WvString, we might need to allocate memory
349 * (equivalent to unique()) so the original char* can be safely changed
350 * or destroyed.
352 WvString(const WvString &s)
353 { copy_constructor(s); }
354 WvString(const WvFastString &s)
355 { copy_constructor(s); }
358 * Create a WvString out of a char* string. We always allocate memory
359 * and make a copy here. To avoid memory copies, you can (carefully)
360 * use a WvFastString. To just have quick parameter passing, use a
361 * WvStringParm instead.
363 WvString(const char *_str);
366 * Create a WvString out of a Qt library QString. You have to link with
367 * libwvqt.so if you want to use this.
369 WvString(const QString &);
370 WvString(const QCString &);
373 * Create a string out of a stdc++ string. To use this, #include
374 * wvstdstring.h.
376 inline WvString(const std::string &s);
378 WvString(WVSTRING_FORMAT_DECL) : WvFastString(WVSTRING_FORMAT_CALL)
381 WvString &append(WvStringParm s);
382 WvString &append(WVSTRING_FORMAT_DECL)
383 { return append(WvString(WVSTRING_FORMAT_CALL)); }
385 WvString &operator= (int i);
386 WvString &operator= (const WvFastString &s2);
387 WvString &operator= (const char *s2)
388 { return *this = WvFastString(s2); }
390 /** make the buf and str pointers owned only by this WvString. */
391 WvString &unique();
393 /** returns true if this string is already unique() */
394 bool is_unique() const;
396 /** make the string editable, and return a non-const (char*) */
397 char *edit()
398 { return unique().str; }
400 protected:
401 void copy_constructor(const WvFastString &s);
402 inline void construct(const char *_str)
404 link(&nullbuf, _str);
406 // apenwarr (2002/04/24): from now on, all WvString objects are created
407 // with unique(), so you should _never_ have to call it explicitly. We
408 // still can (and should!) use fast parameter passing via WvFastString.
409 unique();
415 * A ridiculous class needed because UniConf::operator->() needs to return
416 * a pointer, even though that pointer is going to be dereferenced
417 * immediately anyway. We can instantiate a temporary WvStringStar, which
418 * can then return its 'this' pointer.
420 class WvStringStar : public WvFastString
422 public:
423 WvStringStar(WvStringParm s) : WvFastString(s)
425 WvFastString *operator -> ()
426 { return this; }
430 inline bool operator== (const char *s1, WvStringParm s2)
432 return s2 == s1;
436 inline bool operator!= (const char *s1, WvStringParm s2)
438 return s2 != s1;
441 #endif // __WVSTRING_H