Cachemere
Modular Caching Library for C++
measurement.hpp
1 #include <memory>
2 
3 namespace cachemere::measurement {
4 
5 namespace detail {
6 
7 // Utility functions to allow passing both object and pointers to objects
8 // to the measurement functors.
9 template<typename T> inline const T& deref_maybe(const T& object)
10 {
11  return object;
12 }
13 template<typename T> inline const T& deref_maybe(T* object)
14 {
15  return *object;
16 }
17 template<typename T> inline const T& deref_maybe(const std::shared_ptr<T>& object)
18 {
19  return *object;
20 }
21 template<typename T> inline const T& deref_maybe(const std::unique_ptr<T>& object)
22 {
23  return *object;
24 }
25 
26 } // namespace detail
27 
28 template<typename T> template<typename V> size_t Size<T>::operator()(const V& object) const
29 {
30  return detail::deref_maybe(object).size();
31 }
32 
33 template<typename T> template<typename V> size_t SizeOf<T>::operator()(const V& /* object */) const
34 {
35  return sizeof(T);
36 }
37 
38 template<typename T> template<typename V> size_t CapacityDynamicallyAllocated<T>::operator()(const V& object) const
39 {
40  size_t capacity = detail::deref_maybe(object).capacity();
41  if (capacity < 1024) {
42  capacity = std::max(static_cast<size_t>(16), round_up(capacity));
43  }
44 
45  return capacity;
46 }
47 
48 template<typename T> size_t CapacityDynamicallyAllocated<T>::round_up(const size_t value) const
49 {
50  return ((value + sizeof(void*) - 1) / sizeof(void*)) * sizeof(void*);
51 }
52 
53 } // namespace cachemere::measurement
cachemere::measurement
Utilities for measuring cached items.
Definition: measurement.h:7