6 #ifndef KAGOME_CONTAINERS_OBJECTS_CACHE_HPP 7 #define KAGOME_CONTAINERS_OBJECTS_CACHE_HPP 11 #include <type_traits> 21 template <
typename Type>
23 template <
typename... __args>
25 return new (std::nothrow) Type(std::forward<__args>(args)...);
38 template <
typename T,
typename Alloc = ObjsCacheDefAlloc<T>>
40 static_assert(std::is_array_v<T> ==
false,
41 "Arrays are not allowed in ObjectsCache");
59 for (
auto *s : cache_) {
60 allocator_.deallocate(s);
84 return ObjectPtr(getRawPtr(), [&](
auto *obj)
mutable { setRawPtr(obj); });
96 std::lock_guard guard(cache_blocker_);
97 if (!cache_.empty()) {
101 ptr = allocator_.allocate();
107 if (
nullptr != ptr) {
108 std::lock_guard guard(cache_blocker_);
109 cache_.push_back(ptr);
118 template <
typename T>
121 typename std::remove_pointer<typename std::decay<T>::type>::type;
129 template <
typename T,
typename... ARGS>
133 template <
typename T>
136 #ifndef KAGOME_CACHE_UNIT 137 #define KAGOME_CACHE_UNIT(type) tools::containers::CacheUnit<type> 138 #endif // KAGOME_CACHE_UNIT 144 #ifndef KAGOME_DECLARE_CACHE 145 #define KAGOME_DECLARE_CACHE(prefix, ...) \ 146 using prefix##_cache_type = \ 147 tools::containers::ObjectsCacheManager<__VA_ARGS__>; \ 148 template <typename T> \ 149 using prefix##_UCachedType = std::unique_ptr<T, void (*)(T *const)>; \ 150 extern prefix##_cache_type prefix##_cache; \ 151 template <typename T> \ 152 inline T *prefix##_get_from_cache() { \ 153 return static_cast<tools::containers::ObjectsCache<T> *>(&prefix##_cache) \ 154 ->getCachedObject(); \ 156 template <typename T> \ 157 inline void prefix##_set_to_cache(T *const ptr) { \ 158 static_cast<tools::containers::ObjectsCache<T> *>(&prefix##_cache) \ 159 ->setCachedObject(ptr); \ 161 template <typename T> \ 162 inline std::shared_ptr<T> prefix##_get_shared_from_cache() { \ 163 return static_cast<tools::containers::ObjectsCache<T> *>(&prefix##_cache) \ 164 ->getSharedCachedObject(); \ 166 template <typename T> \ 167 inline prefix##_UCachedType<T> prefix##_get_unique_from_cache() { \ 168 return prefix##_UCachedType<T>(prefix##_get_from_cache<T>(), \ 169 &prefix##_set_to_cache<T>); \ 171 #endif // KAGOME_DECLARE_CACHE 173 #ifndef KAGOME_UNIQUE_TYPE_CACHE 174 #define KAGOME_UNIQUE_TYPE_CACHE(prefix, type) prefix##_UCachedType<type> 175 #endif // KAGOME_UNIQUE_TYPE_CACHE 177 #ifndef KAGOME_DEFINE_CACHE 178 #define KAGOME_DEFINE_CACHE(prefix) prefix##_cache_type prefix##_cache; 179 #endif // KAGOME_DEFINE_CACHE 181 #ifndef KAGOME_EXTRACT_SHARED_CACHE 182 #define KAGOME_EXTRACT_SHARED_CACHE(prefix, type) \ 183 prefix##_get_shared_from_cache<type>() 184 #endif // KAGOME_EXTRACT_SHARED_CACHE 186 #ifndef KAGOME_EXTRACT_UNIQUE_CACHE 187 #define KAGOME_EXTRACT_UNIQUE_CACHE(prefix, type) \ 188 prefix##_get_unique_from_cache<type>() 189 #endif // KAGOME_EXTRACT_UNIQUE_CACHE 191 #ifndef KAGOME_EXTRACT_RAW_CACHE 192 #define KAGOME_EXTRACT_RAW_CACHE(prefix, type) prefix##_get_from_cache<type>() 193 #endif // KAGOME_EXTRACT_RAW_CACHE 195 #ifndef KAGOME_INSERT_RAW_CACHE 196 #define KAGOME_INSERT_RAW_CACHE(prefix, obj) prefix##_set_to_cache(obj) 197 #endif // KAGOME_INSERT_RAW_CACHE 201 #endif // KAGOME_CONTAINERS_OBJECTS_CACHE_HPP