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 AcceptLanguageList::AcceptLanguageList()
83 {
|
84 mike 1.4 AcceptLanguageArray& self = *((AcceptLanguageArray*)this);
85 new (&self) AcceptLanguageArray;
|
86 kumpf 1.1 }
87
|
88 mike 1.4 AcceptLanguageList::AcceptLanguageList(const AcceptLanguageList& x)
|
89 kumpf 1.1 {
|
90 mike 1.4 AcceptLanguageArray& self = *((AcceptLanguageArray*)this);
91 AcceptLanguageArray& other = *((AcceptLanguageArray*)&x);
92 new (&self) AcceptLanguageArray(other);
|
93 kumpf 1.1 }
94
95 AcceptLanguageList::~AcceptLanguageList()
96 {
|
97 mike 1.4 AcceptLanguageArray& self = *((AcceptLanguageArray*)this);
98 self.~AcceptLanguageArray();
|
99 kumpf 1.1 }
100
|
101 mike 1.4 AcceptLanguageList& AcceptLanguageList::operator=(const AcceptLanguageList& x)
|
102 kumpf 1.1 {
|
103 mike 1.4 AcceptLanguageArray& self = *((AcceptLanguageArray*)this);
104 AcceptLanguageArray& other = *((AcceptLanguageArray*)&x);
105
106 if (&self != &other)
107 self = other;
|
108 kumpf 1.1 return *this;
109 }
110
111 Uint32 AcceptLanguageList::size() const
112 {
|
113 mike 1.4 AcceptLanguageArray& self = *((AcceptLanguageArray*)this);
114 return self.size();
|
115 kumpf 1.1 }
116
117 LanguageTag AcceptLanguageList::getLanguageTag(Uint32 index) const
118 {
|
119 mike 1.4 AcceptLanguageArray& self = *((AcceptLanguageArray*)this);
120 return self[index].first;
|
121 kumpf 1.1 }
122
|
123 mike 1.4 Real32 AcceptLanguageList::getQualityValue(Uint32 i) const
|
124 kumpf 1.1 {
|
125 mike 1.4 AcceptLanguageArray& self = *((AcceptLanguageArray*)this);
126 return self[i].second;
|
127 kumpf 1.1 }
128
129 void AcceptLanguageList::insert(
130 const LanguageTag& languageTag,
131 Real32 qualityValue)
132 {
133 LanguageParser::validateQualityValue(qualityValue);
134
|
135 mike 1.4 AcceptLanguageArray& self = *((AcceptLanguageArray*)this);
136 Uint32 i;
137 Uint32 n = self.size();
|
138 kumpf 1.1
|
139 mike 1.4 for (i = 0; i < n; i++)
|
140 kumpf 1.1 {
|
141 mike 1.4 if (self[i].second < qualityValue)
|
142 kumpf 1.1 {
143 // Insert the new element before the element at this index
144 break;
145 }
146 }
147
|
148 mike 1.4 self.insert(i, AcceptLanguagePair(languageTag, qualityValue));
|
149 kumpf 1.1 }
150
|
151 mike 1.4 void AcceptLanguageList::remove(Uint32 i)
|
152 kumpf 1.1 {
|
153 mike 1.4 AcceptLanguageArray& self = *((AcceptLanguageArray*)this);
154 self.remove(i);
|
155 kumpf 1.1 }
156
157 Uint32 AcceptLanguageList::find(const LanguageTag& languageTag) const
158 {
|
159 mike 1.4 AcceptLanguageArray& self = *((AcceptLanguageArray*)this);
160 Uint32 n = self.size();
161
162 for (Uint32 i = 0; i < n; i++)
|
163 kumpf 1.1 {
|
164 mike 1.4 if (languageTag == self[i].first)
|
165 kumpf 1.1 return i;
166 }
|
167 mike 1.4
|
168 kumpf 1.1 return PEG_NOT_FOUND;
169 }
170
171 void AcceptLanguageList::clear()
172 {
|
173 mike 1.4 AcceptLanguageArray& self = *((AcceptLanguageArray*)this);
174 self.clear();
|
175 kumpf 1.1 }
176
|
177 mike 1.4 Boolean AcceptLanguageList::operator==(const AcceptLanguageList& x) const
|
178 kumpf 1.1 {
|
179 mike 1.4 AcceptLanguageArray& self = *((AcceptLanguageArray*)this);
180 AcceptLanguageArray& other = *((AcceptLanguageArray*)&x);
181
182 Uint32 n = self.size();
183
184 if (n != other.size())
|
185 kumpf 1.1 return false;
186
|
187 mike 1.4 for (Uint32 i = 0; i < n; i++)
|
188 kumpf 1.1 {
|
189 mike 1.4 if (self[i].first != other[i].first ||
190 self[i].second != other[i].second)
|
191 kumpf 1.1 {
192 return false;
193 }
194 }
|
195 mike 1.4
|
196 kumpf 1.1 return true;
197 }
198
|
199 mike 1.4 Boolean AcceptLanguageList::operator!=(const AcceptLanguageList& x) const
|
200 kumpf 1.1 {
|
201 mike 1.4 return !operator==(x);
|
202 kumpf 1.1 }
203
204 PEGASUS_NAMESPACE_END
|