MeshLib C++ Docs
Loading...
Searching...
No Matches
MRHeapBytes.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRMeshFwd.h"
4#include "MRMacros.h"
5#include <vector>
6#include <memory>
7
8namespace MR
9{
10
13
15template<typename T>
16[[nodiscard]] inline size_t heapBytes( const std::vector<T> & vec )
17{
18 constexpr bool hasHeapBytes = requires( const T& t ) { t.heapBytes(); };
19 if constexpr ( hasHeapBytes )
20 {
21 size_t res = vec.capacity() * sizeof( T );
22 for ( const T & t : vec )
23 res += t.heapBytes();
24 return res;
25 }
26 else
27 {
28 return vec.capacity() * sizeof( T );
29 }
30}
31
32template<typename T, typename U>
33[[nodiscard]] inline size_t heapBytes( const Vector<T, U>& vec )
34{
35 constexpr bool hasHeapBytes = requires( const T & t ) { t.heapBytes(); };
36 if constexpr ( hasHeapBytes )
37 {
38 size_t res = vec.size() * sizeof( T );
39 for ( const T & t : vec )
40 res += t.heapBytes();
41 return res;
42 }
43 else
44 {
45 return vec.size() * sizeof( T );
46 }
47}
48
50template<typename T>
51[[nodiscard]] inline size_t heapBytes( const std::unique_ptr<T> & ptr )
52{
53 if ( !ptr )
54 return 0;
55 return sizeof( T ) + ptr->heapBytes();
56}
57
59template<typename T>
60[[nodiscard]] inline size_t heapBytes( const std::shared_ptr<T> & ptr )
61{
62 if ( !ptr )
63 return 0;
64 return sizeof( T ) + ptr->heapBytes();
65}
66
69template<typename T> MR_REQUIRES_IF_SUPPORTED( std::is_function_v<T> )
70[[nodiscard]] inline size_t heapBytes( const std::function<T> & )
71{
72 return 0;
73}
74
76template<typename ...Ts>
77[[nodiscard]] inline size_t heapBytes( const phmap::flat_hash_map<Ts...>& hashMap )
78{
84 const auto cap = hashMap.capacity();
85 constexpr size_t kWidth = 16;
86 return cap + kWidth + cap * sizeof( typename phmap::flat_hash_map<Ts...>::slot_type );
87}
88
90
91}
#define MR_REQUIRES_IF_SUPPORTED(...)
Definition MRMacros.h:34
std::vector<T>-like container that requires specific indexing type,
Definition MRVector.h:23
size_t heapBytes(const BitSet &bs)
returns the amount of memory given BitSet occupies on heap
Definition MRBitSet.h:313
std::size_t size() const
Definition MRVector.h:55
only for bindings generation
Definition MRCameraOrientationPlugin.h:8