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