Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support construction with allocators for allocator-aware types #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/cs_cow_guarded.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ class cow_guarded
template <typename... Us>
cow_guarded(Us &&... data);

/**
Construct a cow_guarded guarded object which uses an allocator. This
constructor will accept any number of parameters, all of which
are forwarded to the constructor of T.
*/
template <typename Alloc, typename... Us>
cow_guarded(std::allocator_arg_t, Alloc alloc, Us &&... data);

/**
Acquire a handle to the protected object. As a side effect, the
protected object will be locked from access by any other
Expand Down Expand Up @@ -220,6 +228,16 @@ cow_guarded<T, M>::cow_guarded(Us &&... data)
{
}

template <typename T, typename M>
template <typename Alloc, typename... Us>
cow_guarded<T, M>::cow_guarded(
std::allocator_arg_t,
Alloc alloc,
Us &&... data)
: m_data(std::allocate_shared<T>(alloc, std::forward<Us>(data)...))
{
}

template <typename T, typename M>
auto cow_guarded<T, M>::lock() -> handle
{
Expand Down Expand Up @@ -338,4 +356,10 @@ auto cow_guarded<T, M>::try_lock_shared_until(const TimePoint &timepoint) const

} // namespace libguarded

template<typename T, typename M, typename Alloc>
struct std::uses_allocator<libguarded::cow_guarded<T, M>, Alloc>
: std::uses_allocator<T, Alloc>::type
{
};

#endif
19 changes: 19 additions & 0 deletions src/cs_deferred_guarded.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ class deferred_guarded
template <typename... Us>
deferred_guarded(Us &&... data);

template <typename Alloc, typename... Us>
deferred_guarded(std::allocator_arg_t, Alloc alloc, Us &&... data);

template <typename Func>
void modify_detach(Func && func);

Expand Down Expand Up @@ -112,6 +115,16 @@ deferred_guarded<T, M>::deferred_guarded(Us &&... data)
{
}

template <typename T, typename M>
template <typename Alloc, typename... Us>
deferred_guarded<T, M>::deferred_guarded(
std::allocator_arg_t,
Alloc alloc,
Us &&... data)
: m_obj(std::forward<Us>(data)..., alloc), m_pendingWrites(false)
{
}

template <typename T, typename M>
template <typename Func>
void deferred_guarded<T, M>::modify_detach(Func && func)
Expand Down Expand Up @@ -296,4 +309,10 @@ auto deferred_guarded<T, M>::try_lock_shared_until(const TimePoint & tp) const -

} // namespace libguarded

template<typename T, typename M, typename Alloc>
struct std::uses_allocator<libguarded::deferred_guarded<T, M>, Alloc>
: std::uses_allocator<T, Alloc>::type
{
};

#endif
25 changes: 25 additions & 0 deletions src/cs_lr_guarded.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ class lr_guarded
template <typename... Us>
lr_guarded(Us &&... data);

/**
Construct a lr_guarded guarded object which uses an allocator. This
constructor will accept any number of parameters, all of which
are forwarded to the constructor of T.
*/
template <typename Alloc, typename... Us>
lr_guarded(std::allocator_arg_t, Alloc alloc, Us &&... data);

/**
Modify the data by passing a functor. The functor must take
exactly one argument of type T&. The functor will be called
Expand Down Expand Up @@ -154,6 +162,17 @@ lr_guarded<T, M>::lr_guarded(Us &&... data)
{
}

template <typename T, typename M>
template <typename Alloc, typename... Us>
lr_guarded<T, M>::lr_guarded(
std::allocator_arg_t,
Alloc alloc,
Us &&... data)
: m_left(std::forward<Us>(data)..., alloc), m_right(m_left), m_readingLeft(true), m_countingLeft(true),
m_leftReadCount(0), m_rightReadCount(0)
{
}

template <typename T, typename M>
template <typename Func>
void lr_guarded<T, M>::modify(Func && func)
Expand Down Expand Up @@ -263,4 +282,10 @@ auto lr_guarded<T, M>::try_lock_shared_until(const TimePoint &) const -> shared_

} // namespace libguarded

template<typename T, typename M, typename Alloc>
struct std::uses_allocator<libguarded::lr_guarded<T, M>, Alloc>
: std::uses_allocator<T, Alloc>::type
{
};

#endif
24 changes: 24 additions & 0 deletions src/cs_ordered_guarded.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ class ordered_guarded
template <typename... Us>
ordered_guarded(Us &&... data);

/**
Construct a guarded object which uses an allocator. This
constructor will accept any number of parameters, all of which
are forwarded to the constructor of T.
*/
template <typename Alloc, typename... Us>
ordered_guarded(std::allocator_arg_t, Alloc alloc, Us &&... data);

template <typename Func>
decltype(auto) modify(Func &&func);

Expand Down Expand Up @@ -105,6 +113,16 @@ ordered_guarded<T, M>::ordered_guarded(Us &&... data)
{
}

template <typename T, typename M>
template <typename Alloc, typename... Us>
ordered_guarded<T, M>::ordered_guarded(
std::allocator_arg_t,
Alloc alloc,
Us &&... data)
: m_obj(std::forward<Us>(data)..., alloc)
{
}

template <typename T, typename M>
template <typename Func>
decltype(auto) ordered_guarded<T, M>::modify(Func &&func)
Expand Down Expand Up @@ -164,4 +182,10 @@ auto ordered_guarded<T, M>::try_lock_shared_until(const TimePoint &timepoint) co

} // namespace libguarded

template<typename T, typename M, typename Alloc>
struct std::uses_allocator<libguarded::ordered_guarded<T, M>, Alloc>
: std::uses_allocator<T, Alloc>::type
{
};

#endif
24 changes: 24 additions & 0 deletions src/cs_plain_guarded.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ class plain_guarded
template <typename... Us>
plain_guarded(Us &&... data);

/**
Construct a guarded object which uses an allocator. This
constructor will accept any number of parameters, all of which
are forwarded to the constructor of T.
*/
template <typename Alloc, typename... Us>
plain_guarded(std::allocator_arg_t, Alloc alloc, Us &&... data);

/**
Acquire a handle to the protected object. As a side effect, the
protected object will be locked from access by any other
Expand Down Expand Up @@ -183,6 +191,16 @@ plain_guarded<T, M>::plain_guarded(Us &&... data)
{
}

template <typename T, typename M>
template <typename Alloc, typename... Us>
plain_guarded<T, M>::plain_guarded(
std::allocator_arg_t,
Alloc alloc,
Us &&... data)
: m_obj(std::forward<Us>(data)..., alloc)
{
}

template <typename T, typename M>
auto plain_guarded<T, M>::lock() -> handle
{
Expand Down Expand Up @@ -278,4 +296,10 @@ using guarded [[deprecated("renamed to plain_guarded")]] = plain_guarded<T, M>;

} // namespace libguarded

template<typename T, typename M, typename Alloc>
struct std::uses_allocator<libguarded::plain_guarded<T, M>, Alloc>
: std::uses_allocator<T, Alloc>::type
{
};

#endif
19 changes: 19 additions & 0 deletions src/cs_shared_guarded.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ class shared_guarded
template <typename... Us>
shared_guarded(Us &&... data);

template <typename Alloc, typename... Us>
shared_guarded(std::allocator_arg_t, Alloc alloc, Us &&... data);

// exclusive access
[[nodiscard]] handle lock();
[[nodiscard]] handle try_lock();
Expand Down Expand Up @@ -142,6 +145,16 @@ shared_guarded<T, M, L>::shared_guarded(Us &&... data)
{
}

template <typename T, typename M, typename L>
template <typename Alloc, typename... Us>
shared_guarded<T, M, L>::shared_guarded(
std::allocator_arg_t,
Alloc alloc,
Us &&... data)
: m_obj(std::forward<Us>(data)..., alloc)
{
}

template <typename T, typename M, typename L>
auto shared_guarded<T, M, L>::lock() -> handle
{
Expand Down Expand Up @@ -234,4 +247,10 @@ auto shared_guarded<T, M, L>::try_lock_shared_until(const TimePoint &tp) const -

} // namespace libguarded

template<typename T, typename M, typename L, typename Alloc>
struct std::uses_allocator<libguarded::shared_guarded<T, M, L>, Alloc>
: std::uses_allocator<T, Alloc>::type
{
};

#endif