1 karl 1.2 //%2006////////////////////////////////////////////////////////////////////////
|
2 kumpf 1.1 //
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 karl 1.2 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
12 // EMC Corporation; Symantec Corporation; The Open Group.
|
13 kumpf 1.1 //
14 // Permission is hereby granted, free of charge, to any person obtaining a copy
15 // of this software and associated documentation files (the "Software"), to
16 // deal in the Software without restriction, including without limitation the
17 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
18 // sell copies of the Software, and to permit persons to whom the Software is
19 // furnished to do so, subject to the following conditions:
20 //
21 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
22 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
23 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
24 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
25 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
26 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 //==============================================================================
31 //
32 //%/////////////////////////////////////////////////////////////////////////////
33
34 kumpf 1.1 #include <Pegasus/Common/AcceptLanguageList.h>
35 #include <Pegasus/Common/LanguageParser.h>
36 #include <Pegasus/Common/InternalException.h>
37 #include <Pegasus/Common/AutoPtr.h>
38 #include <Pegasus/Common/ArrayInternal.h>
|
39 mike 1.4 #include <Pegasus/Common/Pair.h>
|
40 kumpf 1.1
41 PEGASUS_NAMESPACE_BEGIN
42
|
43 mike 1.4 typedef Pair<LanguageTag, Real32> AcceptLanguagePair;
44 typedef Array<AcceptLanguagePair> AcceptLanguageArray;
45
46 #define PEGASUS_ARRAY_T AcceptLanguagePair
47 # include <Pegasus/Common/ArrayInter.h>
48 # include "ArrayImpl.h"
49 #undef PEGASUS_ARRAY_T
50
51 //
52 // Implementation Notes:
53 // =====================
54 //
55 // The internal representation member (_rep) is a pointer to an
56 // AcceptLanguageListRep object. We could define a class with this name
57 // as follows:
58 //
59 // class AcceptLanguageListRep
60 // {
61 // AcceptLanguageArray array;
62 // };
63 //
64 mike 1.4 // But this requires separate heap object to hold the array. Instead we use
65 // the following fact to eliminate the extra heap object:
66 //
67 // sizeof(AcceptLanguageArray) == sizeof(AcceptLanguageListRep*)
|
68 kumpf 1.1 //
|
69 mike 1.4 // We know this since all arrays contain a single pointer to a representation
70 // object. Take for example the following structure:
|
71 kumpf 1.1 //
|
72 mike 1.4 // class MyClass
73 // {
74 // void* rep;
75 // };
|
76 kumpf 1.1 //
|
77 mike 1.4 // Clearly, sizeof(MyClass) == sizeof(void*). We eliminate the extra heap object
78 // by overlaying the AcceptLanguageList::_rep pointer with the array base. So
79 // AcceptLanguageList::_rep in fact refers to the Array<T>::_rep.
|
80 kumpf 1.1 //
81
|
82 kumpf 1.5 static inline AcceptLanguageArray& GetAcceptLanguageArray(
83 AcceptLanguageList* list)
84 {
85 return *reinterpret_cast<AcceptLanguageArray*>(list);
86 }
87
88 static inline const AcceptLanguageArray& GetAcceptLanguageArray(
89 const AcceptLanguageList* list)
90 {
91 return *reinterpret_cast<const AcceptLanguageArray*>(list);
92 }
93
|
94 kumpf 1.1 AcceptLanguageList::AcceptLanguageList()
95 {
|
96 kumpf 1.5 AcceptLanguageArray& self = GetAcceptLanguageArray(this);
|
97 mike 1.4 new (&self) AcceptLanguageArray;
|
98 kumpf 1.1 }
99
|
100 mike 1.4 AcceptLanguageList::AcceptLanguageList(const AcceptLanguageList& x)
|
101 kumpf 1.1 {
|
102 kumpf 1.5 AcceptLanguageArray& self = GetAcceptLanguageArray(this);
103 const AcceptLanguageArray& other = GetAcceptLanguageArray(&x);
|
104 mike 1.4 new (&self) AcceptLanguageArray(other);
|
105 kumpf 1.1 }
106
107 AcceptLanguageList::~AcceptLanguageList()
108 {
|
109 kumpf 1.5 AcceptLanguageArray& self = GetAcceptLanguageArray(this);
|
110 mike 1.4 self.~AcceptLanguageArray();
|
111 kumpf 1.1 }
112
|
113 mike 1.4 AcceptLanguageList& AcceptLanguageList::operator=(const AcceptLanguageList& x)
|
114 kumpf 1.1 {
|
115 kumpf 1.5 AcceptLanguageArray& self = GetAcceptLanguageArray(this);
116 const AcceptLanguageArray& other = GetAcceptLanguageArray(&x);
|
117 mike 1.4
118 if (&self != &other)
119 self = other;
|
120 kumpf 1.1 return *this;
121 }
122
123 Uint32 AcceptLanguageList::size() const
124 {
|
125 kumpf 1.5 const AcceptLanguageArray& self = GetAcceptLanguageArray(this);
|
126 mike 1.4 return self.size();
|
127 kumpf 1.1 }
128
129 LanguageTag AcceptLanguageList::getLanguageTag(Uint32 index) const
130 {
|
131 kumpf 1.5 const AcceptLanguageArray& self = GetAcceptLanguageArray(this);
|
132 mike 1.4 return self[index].first;
|
133 kumpf 1.1 }
134
|
135 mike 1.4 Real32 AcceptLanguageList::getQualityValue(Uint32 i) const
|
136 kumpf 1.1 {
|
137 kumpf 1.5 const AcceptLanguageArray& self = GetAcceptLanguageArray(this);
|
138 mike 1.4 return self[i].second;
|
139 kumpf 1.1 }
140
141 void AcceptLanguageList::insert(
142 const LanguageTag& languageTag,
143 Real32 qualityValue)
144 {
145 LanguageParser::validateQualityValue(qualityValue);
146
|
147 kumpf 1.5 AcceptLanguageArray& self = GetAcceptLanguageArray(this);
|
148 mike 1.4 Uint32 i;
149 Uint32 n = self.size();
|
150 kumpf 1.1
|
151 mike 1.4 for (i = 0; i < n; i++)
|
152 kumpf 1.1 {
|
153 mike 1.4 if (self[i].second < qualityValue)
|
154 kumpf 1.1 {
155 // Insert the new element before the element at this index
156 break;
157 }
158 }
159
|
160 mike 1.4 self.insert(i, AcceptLanguagePair(languageTag, qualityValue));
|
161 kumpf 1.1 }
162
|
163 mike 1.4 void AcceptLanguageList::remove(Uint32 i)
|
164 kumpf 1.1 {
|
165 kumpf 1.5 AcceptLanguageArray& self = GetAcceptLanguageArray(this);
|
166 mike 1.4 self.remove(i);
|
167 kumpf 1.1 }
168
169 Uint32 AcceptLanguageList::find(const LanguageTag& languageTag) const
170 {
|
171 kumpf 1.5 const AcceptLanguageArray& self = GetAcceptLanguageArray(this);
|
172 mike 1.4 Uint32 n = self.size();
173
174 for (Uint32 i = 0; i < n; i++)
|
175 kumpf 1.1 {
|
176 mike 1.4 if (languageTag == self[i].first)
|
177 kumpf 1.1 return i;
178 }
|
179 mike 1.4
|
180 kumpf 1.1 return PEG_NOT_FOUND;
181 }
182
183 void AcceptLanguageList::clear()
184 {
|
185 kumpf 1.5 AcceptLanguageArray& self = GetAcceptLanguageArray(this);
|
186 mike 1.4 self.clear();
|
187 kumpf 1.1 }
188
|
189 mike 1.4 Boolean AcceptLanguageList::operator==(const AcceptLanguageList& x) const
|
190 kumpf 1.1 {
|
191 kumpf 1.5 const AcceptLanguageArray& self = GetAcceptLanguageArray(this);
192 const AcceptLanguageArray& other = GetAcceptLanguageArray(&x);
|
193 mike 1.4
194 Uint32 n = self.size();
195
196 if (n != other.size())
|
197 kumpf 1.1 return false;
198
|
199 mike 1.4 for (Uint32 i = 0; i < n; i++)
|
200 kumpf 1.1 {
|
201 mike 1.4 if (self[i].first != other[i].first ||
202 self[i].second != other[i].second)
|
203 kumpf 1.1 {
204 return false;
205 }
206 }
|
207 mike 1.4
|
208 kumpf 1.1 return true;
209 }
210
|
211 mike 1.4 Boolean AcceptLanguageList::operator!=(const AcceptLanguageList& x) const
|
212 kumpf 1.1 {
|
213 mike 1.4 return !operator==(x);
|
214 kumpf 1.1 }
215
216 PEGASUS_NAMESPACE_END
|