1 mike 1.2 //%2005////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
4 // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
5 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
6 // IBM Corp.; EMC Corporation, The Open Group.
7 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
8 // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
9 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
10 // EMC Corporation; VERITAS Software Corporation; The Open Group.
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining a copy
13 // of this software and associated documentation files (the "Software"), to
14 // deal in the Software without restriction, including without limitation the
15 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16 // sell copies of the Software, and to permit persons to whom the Software is
17 // furnished to do so, subject to the following conditions:
18 //
19 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
20 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
21 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
22 mike 1.2 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
23 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
24 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 //==============================================================================
29 //
30 //%/////////////////////////////////////////////////////////////////////////////
31
32 #ifndef _Pegasus_StringRep_h
33 #define _Pegasus_StringRep_h
34
|
37 mike 1.2 #include <new>
38
39 PEGASUS_NAMESPACE_BEGIN
40
41 struct StringRep
42 {
43 StringRep();
44
45 ~StringRep();
46
47 static StringRep* alloc(size_t cap);
48
49 static void free(StringRep* rep);
50
51 static StringRep* create(const Uint16* data, size_t size);
52
53 static StringRep* create(const char* data, size_t size);
54
55 static StringRep* copyOnWrite(StringRep* rep);
56
57 static Uint32 length(const Uint16* str);
58 mike 1.2
59 static void ref(const StringRep* rep);
60
61 static void unref(const StringRep* rep);
62
63 static StringRep _emptyRep;
64
65 // Number of characters in this string, excluding the null terminator.
66 size_t size;
67
68 // Number of characters this representation has room for. This is
69 // greater or equal to size.
70 size_t cap;
71
72 // Number of string refering to this StringRep (1, 2, etc).
73 AtomicInt refs;
74
75 // The first character in the string. Extra space is allocated off the
76 // end of this structure for additional characters.
77 Uint16 data[1];
78 };
79 mike 1.2
80 inline void StringRep::free(StringRep* rep)
81 {
82 rep->refs.~AtomicInt();
83 ::operator delete(rep);
84 }
85
86 inline StringRep::StringRep() : size(0), cap(0), refs(2)
87 {
88 // Only called on _emptyRep. We set the reference count to two to
89 // keep a String from modifying it (if the reference count were one,
90 // a string would think it was the sole owner of the StringRep object).
91 data[0] = 0;
92 }
93
94 inline StringRep::~StringRep()
95 {
96 // Only called on _emptyRep.
97 refs.~AtomicInt();
98 }
99
100 mike 1.2 inline void StringRep::ref(const StringRep* rep)
101 {
102 if (rep != &StringRep::_emptyRep)
103 ((StringRep*)rep)->refs++;
104 }
105
106 inline void StringRep::unref(const StringRep* rep)
107 {
108 if (rep != &StringRep::_emptyRep &&
|
110 mike 1.2 StringRep::free((StringRep*)rep);
111 }
112
113 PEGASUS_COMMON_LINKAGE void StringThrowOutOfBounds();
114
115 PEGASUS_COMMON_LINKAGE void StringAppendCharAux(StringRep*& _rep);
116
117 PEGASUS_COMMON_LINKAGE Boolean StringEqualNoCase(
118 const String& s1, const String& s2);
119
120 PEGASUS_COMMON_LINKAGE Uint32 StringFindAux(
121 const StringRep* _rep, const Char16* s, Uint32 n);
122
123 inline void _checkBounds(size_t index, size_t size)
124 {
125 #ifndef PEGASUS_STRING_NO_THROW
126 if (index > size)
127 StringThrowOutOfBounds();
128 #endif
129 }
130
131 mike 1.2 PEGASUS_NAMESPACE_END
132
133 #endif /* _Pegasus_StringRep_h */
|