fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / sc / source / core / data / dpitemdata.cxx
blob6bd1f736cc1490b57568efc0ef624f981e8732f2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "dpitemdata.hxx"
22 #include "document.hxx"
23 #include "dpobject.hxx"
24 #include "formulacell.hxx"
25 #include "globstr.hrc"
26 #include "dptabdat.hxx"
27 #include <rtl/math.hxx>
29 const sal_Int32 ScDPItemData::DateFirst = -1;
30 const sal_Int32 ScDPItemData::DateLast = 10000;
32 size_t ScDPItemData::Hash::operator() (const ScDPItemData& rVal) const
34 switch (rVal.GetType())
36 case GroupValue:
37 case Value:
38 case RangeStart:
39 return (size_t)(rVal.mfValue);
40 case String:
41 case Error:
43 if (!rVal.mpString)
44 return 0;
46 if (rVal.mbStringInterned)
47 return reinterpret_cast<size_t>(rVal.mpString);
49 OUStringHash aStrHasher;
50 return aStrHasher(*rVal.mpString);
52 default:
56 return 0;
59 sal_Int32 ScDPItemData::Compare(const ScDPItemData& rA, const ScDPItemData& rB)
61 if (rA.meType != rB.meType)
63 // group value, value and string in this order. Ensure that the empty
64 // type comes last.
65 return rA.meType < rB.meType ? -1 : 1;
68 switch (rA.meType)
70 case GroupValue:
72 if (rA.maGroupValue.mnGroupType == rB.maGroupValue.mnGroupType)
74 if (rA.maGroupValue.mnValue == rB.maGroupValue.mnValue)
75 return 0;
77 return rA.maGroupValue.mnValue < rB.maGroupValue.mnValue ? -1 : 1;
80 return rA.maGroupValue.mnGroupType < rB.maGroupValue.mnGroupType ? -1 : 1;
82 case Value:
83 case RangeStart:
85 if (rA.mfValue == rB.mfValue)
86 return 0;
88 return rA.mfValue < rB.mfValue ? -1 : 1;
90 case String:
91 case Error:
92 if (rA.mpString == rB.mpString)
93 // strings may be interned.
94 return 0;
96 return ScGlobal::GetCollator()->compareString(rA.GetString(), rB.GetString());
97 default:
100 return 0;
103 ScDPItemData::ScDPItemData() :
104 mfValue(0.0), meType(Empty), mbStringInterned(false) {}
106 ScDPItemData::ScDPItemData(const ScDPItemData& r) :
107 meType(r.meType), mbStringInterned(r.mbStringInterned)
109 switch (r.meType)
111 case String:
112 case Error:
113 mpString = mbStringInterned ? r.mpString : new OUString(*r.mpString);
114 break;
115 case Value:
116 case RangeStart:
117 mfValue = r.mfValue;
118 break;
119 case GroupValue:
120 maGroupValue.mnGroupType = r.maGroupValue.mnGroupType;
121 maGroupValue.mnValue = r.maGroupValue.mnValue;
122 break;
123 case Empty:
124 default:
125 mfValue = 0.0;
129 void ScDPItemData::DisposeString()
131 if (!mbStringInterned)
133 if (meType == String || meType == Error)
134 delete mpString;
137 mbStringInterned = false;
140 ScDPItemData::ScDPItemData(const OUString& rStr) :
141 mpString(new OUString(rStr)), meType(String), mbStringInterned(false) {}
143 ScDPItemData::ScDPItemData(sal_Int32 nGroupType, sal_Int32 nValue) :
144 meType(GroupValue), mbStringInterned(false)
146 maGroupValue.mnGroupType = nGroupType;
147 maGroupValue.mnValue = nValue;
150 ScDPItemData::~ScDPItemData()
152 DisposeString();
155 void ScDPItemData::SetEmpty()
157 DisposeString();
158 meType = Empty;
161 void ScDPItemData::SetString(const OUString& rS)
163 DisposeString();
164 mpString = new OUString(rS);
165 meType = String;
168 void ScDPItemData::SetString(const OUString* pS)
170 DisposeString();
171 mpString = pS;
172 meType = String;
173 mbStringInterned = true;
176 void ScDPItemData::SetValue(double fVal)
178 DisposeString();
179 mfValue = fVal;
180 meType = Value;
183 void ScDPItemData::SetRangeStart(double fVal)
185 DisposeString();
186 mfValue = fVal;
187 meType = RangeStart;
190 void ScDPItemData::SetRangeFirst()
192 DisposeString();
193 rtl::math::setInf(&mfValue, true);
194 meType = RangeStart;
197 void ScDPItemData::SetRangeLast()
199 DisposeString();
200 rtl::math::setInf(&mfValue, false);
201 meType = RangeStart;
204 void ScDPItemData::SetErrorString(const OUString* pS)
206 SetString(pS);
207 meType = Error;
210 bool ScDPItemData::IsCaseInsEqual(const ScDPItemData& r) const
212 if (meType != r.meType)
213 return false;
215 switch (meType)
217 case Value:
218 case RangeStart:
219 return rtl::math::approxEqual(mfValue, r.mfValue);
220 case GroupValue:
221 return maGroupValue.mnGroupType == r.maGroupValue.mnGroupType &&
222 maGroupValue.mnValue == r.maGroupValue.mnValue;
223 default:
227 if (mpString == r.mpString)
228 // Fast equality check for interned strings.
229 return true;
231 return ScGlobal::GetpTransliteration()->isEqual(GetString(), r.GetString());
234 bool ScDPItemData::operator== (const ScDPItemData& r) const
236 if (meType != r.meType)
237 return false;
239 switch (meType)
241 case Value:
242 case RangeStart:
243 return rtl::math::approxEqual(mfValue, r.mfValue);
244 case GroupValue:
245 return maGroupValue.mnGroupType == r.maGroupValue.mnGroupType &&
246 maGroupValue.mnValue == r.maGroupValue.mnValue;
247 default:
251 // need exact equality until we have a safe case insensitive string hash
252 return GetString() == r.GetString();
255 bool ScDPItemData::operator!= (const ScDPItemData& r) const
257 return !operator== (r);
260 bool ScDPItemData::operator< (const ScDPItemData& r) const
262 return Compare(*this, r) == -1;
265 ScDPItemData& ScDPItemData::operator= (const ScDPItemData& r)
267 DisposeString();
268 meType = r.meType;
269 mbStringInterned = false;
270 switch (r.meType)
272 case String:
273 case Error:
274 mpString = r.mbStringInterned ? r.mpString : new OUString(*r.mpString);
275 mbStringInterned = r.mbStringInterned;
276 break;
277 case Value:
278 case RangeStart:
279 mfValue = r.mfValue;
280 break;
281 case GroupValue:
282 maGroupValue.mnGroupType = r.maGroupValue.mnGroupType;
283 maGroupValue.mnValue = r.maGroupValue.mnValue;
284 break;
285 case Empty:
286 default:
287 mfValue = 0.0;
289 return *this;
292 ScDPValue::Type ScDPItemData::GetCellType() const
294 switch (meType)
296 case Error:
297 return ScDPValue::Error;
298 case Empty:
299 return ScDPValue::Empty;
300 case Value:
301 return ScDPValue::Value;
302 default:
306 return ScDPValue::String;
309 #if DEBUG_PIVOT_TABLE
311 void ScDPItemData::Dump(const char* msg) const
313 printf("--- (%s)\n", msg);
314 switch (meType)
316 case Empty:
317 printf("empty\n");
318 break;
319 case Error:
320 printf("error: %s\n",
321 OUStringToOString(*mpString, RTL_TEXTENCODING_UTF8).getStr());
322 break;
323 case GroupValue:
324 printf("group value: group type = %d value = %d\n",
325 maGroupValue.mnGroupType, maGroupValue.mnValue);
326 break;
327 case String:
328 printf("string: %s\n",
329 OUStringToOString(*mpString, RTL_TEXTENCODING_UTF8).getStr());
330 break;
331 case Value:
332 printf("value: %g\n", mfValue);
333 break;
334 case RangeStart:
335 printf("range start: %g\n", mfValue);
336 break;
337 default:
338 printf("unknown type\n");
340 printf("---\n");
342 #endif
344 bool ScDPItemData::IsEmpty() const
346 return meType == Empty;
349 bool ScDPItemData::IsValue() const
351 return meType == Value;
354 OUString ScDPItemData::GetString() const
356 switch (meType)
358 case String:
359 case Error:
360 return *mpString;
361 case Value:
362 case RangeStart:
363 return OUString::number(mfValue);
364 case GroupValue:
365 return OUString::number(maGroupValue.mnValue);
366 case Empty:
367 default:
371 return OUString();
374 double ScDPItemData::GetValue() const
376 if (meType == Value || meType == RangeStart)
377 return mfValue;
379 return 0.0;
382 ScDPItemData::GroupValueAttr ScDPItemData::GetGroupValue() const
384 if (meType == GroupValue)
385 return maGroupValue;
387 GroupValueAttr aGV;
388 aGV.mnGroupType = -1;
389 aGV.mnValue = -1;
390 return aGV;
393 bool ScDPItemData::HasStringData() const
395 return meType == String || meType == Error;
398 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */