Skip to content

Commit

Permalink
Add memory utility to_allocated().
Browse files Browse the repository at this point in the history
  • Loading branch information
evoskuil committed Jul 7, 2024
1 parent a86bdcc commit c61c891
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
9 changes: 7 additions & 2 deletions include/bitcoin/system/data/memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ inline std::shared_ptr<const Type> to_shared(const Type& value) NOEXCEPT
}

/// Construct shared pointer to const from moved constructor parameters.
template <typename Type, typename... Args>
template <typename Type, typename ...Args>
inline std::shared_ptr<const Type> to_shared(Args&&... values) NOEXCEPT
{
return std::make_shared<const Type>(Type{ std::forward<Args>(values)... });
Expand All @@ -79,6 +79,11 @@ template <typename Type>
std::shared_ptr<std_vector<std::shared_ptr<const Type>>>
to_shareds(const std_vector<Type>& values) NOEXCEPT;

/// Allocate a shared instance and construct with given arguments.
template <typename Type, typename Allocator, typename ...Args>
std::shared_ptr<const Type> to_allocated(const Allocator& allocator,
Args&&... args) NOEXCEPT;

/// unique_ptr
/// ---------------------------------------------------------------------------

Expand All @@ -97,7 +102,7 @@ inline std::unique_ptr<const Type> to_unique(const Type& value) NOEXCEPT
}

/// Construct unique pointer to const from moved constructor parameters.
template <typename Type, typename... Args>
template <typename Type, typename ...Args>
inline std::unique_ptr<const Type> to_unique(Args&&... values) NOEXCEPT
{
return std::make_unique<const Type>(Type{ std::forward<Args>(values)... });
Expand Down
15 changes: 13 additions & 2 deletions include/bitcoin/system/impl/data/memory.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include <algorithm>
#include <memory>
#include <memory_resource>
#include <utility>
#include <vector>
#include <bitcoin/system/define.hpp>
Expand All @@ -30,7 +31,7 @@ namespace system {

BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)

/// Create shared pointer to vector of const shared ptr from the moved vector.
// Create shared pointer to vector of const shared ptr from the moved vector.
template <typename Type>
std::shared_ptr<std_vector<std::shared_ptr<const Type>>>
to_shareds(std_vector<Type>&& values) NOEXCEPT
Expand All @@ -47,7 +48,7 @@ to_shareds(std_vector<Type>&& values) NOEXCEPT
return out;
}

/// Create shared pointer to vector of const shared ptr from the copied vector.
// Create shared pointer to vector of const shared ptr from the copied vector.
template <typename Type>
std::shared_ptr<std_vector<std::shared_ptr<const Type>>> to_shareds(
const std_vector<Type>& values) NOEXCEPT
Expand All @@ -64,6 +65,16 @@ std::shared_ptr<std_vector<std::shared_ptr<const Type>>> to_shareds(
return out;
}

// Allocate a shared instance and construct with given arguments.
template <typename Type, typename Allocator, typename ...Args>
std::shared_ptr<const Type> to_allocated(const Allocator& allocator,
Args&&... args) NOEXCEPT
{
return std::allocate_shared<const Type,
std::pmr::polymorphic_allocator<const Type>>(allocator,
std::forward<Args>(args)...);
}

BC_POP_WARNING()

} // namespace system
Expand Down

0 comments on commit c61c891

Please sign in to comment.