Skip to content

Commit

Permalink
Expand RingBufferImplementation beyond shared_ptr and unique_ptr
Browse files Browse the repository at this point in the history
Signed-off-by: Jeffery Hsu <jefferyyjhsu@gmail.com>
  • Loading branch information
jefferyyjhsu committed Nov 14, 2023
1 parent 5900906 commit adeb5f2
Showing 1 changed file with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,15 @@ class RingBufferImplementation : public BufferImplementationBase<BufferT>
/**
* This member function is thread-safe.
* Two versions for the implementation of the function.
* One for buffer containing unique_ptr and the other for other typess
* One for buffer containing unique_ptr and the other for other types
*
* \return a vector containing all the elements from the ring buffer
*/
template<typename T = BufferT, std::enable_if_t<is_std_unique_ptr<T>::value, void> * = nullptr>
template<typename T = BufferT, std::enable_if_t<is_std_unique_ptr<T>::value &&
std::is_copy_constructible<
typename is_std_unique_ptr<T>::Ptr_type
>::value,
void> * = nullptr>
std::vector<BufferT> get_all_data_impl()
{
std::lock_guard<std::mutex> lock(mutex_);
Expand All @@ -258,7 +262,8 @@ class RingBufferImplementation : public BufferImplementationBase<BufferT>
return result_vtr;
}

template<typename T = BufferT, std::enable_if_t<!is_std_unique_ptr<T>::value, void> * = nullptr>
template<typename T = BufferT, std::enable_if_t<
std::is_copy_constructible<T>::value, void> * = nullptr>
std::vector<BufferT> get_all_data_impl()
{
std::lock_guard<std::mutex> lock(mutex_);
Expand All @@ -270,6 +275,23 @@ class RingBufferImplementation : public BufferImplementationBase<BufferT>
return result_vtr;
}

template<typename T = BufferT, std::enable_if_t<!is_std_unique_ptr<T>::value &&
!std::is_copy_constructible<T>::value, void> * = nullptr>
std::vector<BufferT> get_all_data_impl()
{
throw std::logic_error("Underlined type results in invalid get_all_data_impl()");
return {};
}

template<typename T = BufferT, std::enable_if_t<is_std_unique_ptr<T>::value &&
!std::is_copy_constructible<typename is_std_unique_ptr<T>::Ptr_type>::value,
void> * = nullptr>
std::vector<BufferT> get_all_data_impl()
{
throw std::logic_error("Underlined type in unique_ptr results in invalid get_all_data_impl()");
return {};
}

size_t capacity_;

std::vector<BufferT> ring_buffer_;
Expand Down

0 comments on commit adeb5f2

Please sign in to comment.