Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / basegfx / source / tools / systemdependentdata.cxx
blob223b607ffae0e6c87f952eefb14e8a340743e6db
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/.
8 */
10 #include <basegfx/utils/systemdependentdata.hxx>
11 #include <math.h>
13 namespace basegfx
15 SystemDependentDataManager::SystemDependentDataManager()
19 SystemDependentDataManager::~SystemDependentDataManager()
22 } // namespace basegfx
24 namespace basegfx
26 MinimalSystemDependentDataManager::MinimalSystemDependentDataManager()
27 : SystemDependentDataManager(),
28 maSystemDependentDataReferences()
32 MinimalSystemDependentDataManager::~MinimalSystemDependentDataManager()
36 void MinimalSystemDependentDataManager::startUsage(basegfx::SystemDependentData_SharedPtr& rData)
38 if(rData)
40 maSystemDependentDataReferences.insert(rData);
44 void MinimalSystemDependentDataManager::endUsage(basegfx::SystemDependentData_SharedPtr& rData)
46 if(rData)
48 maSystemDependentDataReferences.erase(rData);
52 void MinimalSystemDependentDataManager::touchUsage(basegfx::SystemDependentData_SharedPtr& /* rData */)
56 void MinimalSystemDependentDataManager::flushAll()
58 maSystemDependentDataReferences.clear();
60 } // namespace basegfx
62 namespace basegfx
64 SystemDependentData::SystemDependentData(
65 SystemDependentDataManager& rSystemDependentDataManager)
66 : mrSystemDependentDataManager(rSystemDependentDataManager),
67 mnCalculatedCycles(0)
71 SystemDependentData::~SystemDependentData()
75 sal_uInt32 SystemDependentData::calculateCombinedHoldCyclesInSeconds() const
77 if(0 == mnCalculatedCycles)
79 const sal_Int64 nBytes(estimateUsageInBytes());
80 const sal_uInt32 nSeconds = 60; // HoldCyclesInSeconds
82 // default is Seconds (minimal is one)
83 sal_uInt32 nResult(0 == nSeconds ? 1 : nSeconds);
85 if(0 != nBytes)
87 // use sqrt to get some curved shape. With a default of 60s we get
88 // a single second at 3600 byte. To get close to 10mb, multiply by
89 // a corresponding scaling factor
90 const double fScaleToMB(3600.0 / (1024.0 * 1024.0 * 10.0));
92 // also use a multiplier to move the start point higher
93 const double fMultiplierSeconds(10.0);
95 // calculate
96 nResult = static_cast<sal_uInt32>((fMultiplierSeconds * nSeconds) / sqrt(nBytes * fScaleToMB));
98 // minimal value is 1
99 if(nResult < 1)
101 nResult = 1;
104 // maximal value is nSeconds
105 if(nResult > nSeconds)
107 nResult = nSeconds;
111 // set locally (once, on-demand created, non-zero)
112 const_cast<SystemDependentData*>(this)->mnCalculatedCycles = nResult;
115 return mnCalculatedCycles;
118 sal_Int64 SystemDependentData::estimateUsageInBytes() const
120 // default implementation has no idea
121 return 0;
123 } // namespace basegfx
125 namespace basegfx
127 SystemDependentDataHolder::SystemDependentDataHolder()
128 : maSystemDependentReferences()
132 SystemDependentDataHolder::~SystemDependentDataHolder()
134 for(const auto& candidate : maSystemDependentReferences)
136 basegfx::SystemDependentData_SharedPtr aData(candidate.second.lock());
138 if(aData)
140 aData->getSystemDependentDataManager().endUsage(aData);
145 void SystemDependentDataHolder::addOrReplaceSystemDependentData(basegfx::SystemDependentData_SharedPtr& rData)
147 const size_t hash_code(typeid(*rData).hash_code());
148 auto result(maSystemDependentReferences.find(hash_code));
150 if(result != maSystemDependentReferences.end())
152 basegfx::SystemDependentData_SharedPtr aData(result->second.lock());
154 if(aData)
156 aData->getSystemDependentDataManager().endUsage(aData);
159 maSystemDependentReferences.erase(result);
160 result = maSystemDependentReferences.end();
163 maSystemDependentReferences[hash_code] = rData;
164 rData->getSystemDependentDataManager().startUsage(rData);
167 SystemDependentData_SharedPtr SystemDependentDataHolder::getSystemDependentData(size_t hash_code) const
169 basegfx::SystemDependentData_SharedPtr aRetval;
170 auto result(maSystemDependentReferences.find(hash_code));
172 if(result != maSystemDependentReferences.end())
174 aRetval = result->second.lock();
176 if(aRetval)
178 aRetval->getSystemDependentDataManager().touchUsage(aRetval);
180 else
182 const_cast< SystemDependentDataHolder* >(this)->maSystemDependentReferences.erase(result);
186 return aRetval;
188 } // namespace basegfx
190 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */