38 #ifndef LIBPMEMOBJ_CPP_DEFRAG_HPP
39 #define LIBPMEMOBJ_CPP_DEFRAG_HPP
41 #include <type_traits>
47 #include <libpmemobj/atomic_base.h>
48 #include <libpmemobj/base.h>
55 using for_each_ptr_function = void (*)(obj::persistent_ptr_base &ptr);
58 using t_has_for_each_ptr =
typename std::enable_if<
59 std::is_same<decltype(std::declval<T>().for_each_ptr(
60 std::declval<for_each_ptr_function>())),
64 using t_is_defragmentable = supports<T, t_has_for_each_ptr>;
78 static constexpr
typename std::enable_if<detail::t_is_defragmentable<T>::value,
80 is_defragmentable() noexcept
92 static constexpr
typename std::enable_if<!detail::t_is_defragmentable<T>::value,
94 is_defragmentable() noexcept
139 template <
typename T>
140 typename std::enable_if<is_defragmentable<T>(),
void>::type
143 if (pmemobj_pool_by_ptr(&t) != pop.
handle())
144 throw std::runtime_error(
145 "object is not from the chosen pool");
148 this->container.push_back(&ptr);
167 template <
typename T,
typename = T>
168 typename std::enable_if<!is_defragmentable<T>(),
void>::type
171 if (pmemobj_pool_by_ptr(&t) != pop.
handle())
172 throw std::runtime_error(
173 "object is not from the chosen pool");
189 template <
typename T>
193 if (pmemobj_pool_by_oid(ptr.raw()) != pop.
handle())
194 throw std::runtime_error(
195 "persistent_ptr does not point to an object from the chosen pool");
197 this->container.push_back(&ptr);
219 pobj_defrag_result result = this->pop.
defrag(
220 this->container.data(), this->container.size());
226 std::vector<persistent_ptr_base *> container;