1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
10 #include <basegfx/utils/systemdependentdata.hxx>
15 SystemDependentDataManager::SystemDependentDataManager()
19 SystemDependentDataManager::~SystemDependentDataManager()
22 } // namespace basegfx
26 MinimalSystemDependentDataManager::MinimalSystemDependentDataManager()
27 : SystemDependentDataManager(),
28 maSystemDependentDataReferences()
32 MinimalSystemDependentDataManager::~MinimalSystemDependentDataManager()
36 void MinimalSystemDependentDataManager::startUsage(basegfx::SystemDependentData_SharedPtr
& rData
)
40 maSystemDependentDataReferences
.insert(rData
);
44 void MinimalSystemDependentDataManager::endUsage(basegfx::SystemDependentData_SharedPtr
& rData
)
48 maSystemDependentDataReferences
.erase(rData
);
52 void MinimalSystemDependentDataManager::touchUsage(basegfx::SystemDependentData_SharedPtr
& /* rData */)
56 void MinimalSystemDependentDataManager::flushAll()
58 maSystemDependentDataReferences
.clear();
60 } // namespace basegfx
64 SystemDependentData::SystemDependentData(
65 SystemDependentDataManager
& rSystemDependentDataManager
)
66 : mrSystemDependentDataManager(rSystemDependentDataManager
),
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
);
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);
96 nResult
= static_cast<sal_uInt32
>((fMultiplierSeconds
* nSeconds
) / sqrt(nBytes
* fScaleToMB
));
104 // maximal value is nSeconds
105 if(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
123 } // namespace basegfx
127 SystemDependentDataHolder::SystemDependentDataHolder()
128 : maSystemDependentReferences()
132 SystemDependentDataHolder::~SystemDependentDataHolder()
134 for(const auto& candidate
: maSystemDependentReferences
)
136 basegfx::SystemDependentData_SharedPtr
aData(candidate
.second
.lock());
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());
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();
178 aRetval
->getSystemDependentDataManager().touchUsage(aRetval
);
182 const_cast< SystemDependentDataHolder
* >(this)->maSystemDependentReferences
.erase(result
);
188 } // namespace basegfx
190 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */