Skip to content

Enforce value type requirements #83

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

Merged
merged 2 commits into from
Jun 17, 2025
Merged
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
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.15)
project(cpp_channel VERSION 1.3.0)
project(cpp_channel VERSION 1.3.1)

set(CMAKE_CXX_STANDARD 11 CACHE STRING "C++ standard")
set(CMAKE_CXX_STANDARD_REQUIRED ON)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Exceptions:

* Thread-safe push and fetch.
* Use stream operators to push (<<) and fetch (>>) items.
* Value type must be default constructible.
* Value type must be default constructible, move constructible, move assignable, and destructible.
* Blocking (forever waiting to fetch).
* Range-based for loop supported.
* Close to prevent pushing and stop waiting to fetch.
Expand Down
23 changes: 23 additions & 0 deletions include/msd/channel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,27 @@ class closed_channel : public std::runtime_error {
template <typename T>
using default_storage = queue_storage<T>;

/**
* @brief Trait to check if a type is supported by msd::channel.
*
* This trait ensures the type meets all requirements to be safely used
* within the channel:
* - Default constructible: must be able to create a default instance.
* - Move constructible: must be movable to allow efficient element transfer.
* - Move assignable: must support move assignment for storage management.
* - Destructible: must have a valid destructor.
*
* @tparam T The type to check.
*/
template <typename T>
struct is_supported_type {
/**
* @brief Indicates if the type meets all channel requirements.
*/
static constexpr bool value = std::is_default_constructible<T>::value && std::is_move_constructible<T>::value &&
std::is_move_assignable<T>::value && std::is_destructible<T>::value;
};

/**
* @brief Trait to check if a storage type has a static **capacity** member.
*/
Expand All @@ -65,6 +86,8 @@ struct is_static_storage<Storage, decltype((void)Storage::capacity, void())> : s
template <typename T, typename Storage = default_storage<T>>
class channel {
public:
static_assert(is_supported_type<T>::value, "Type T does not meet all requirements.");

/**
* @brief The type of elements stored in the channel.
*/
Expand Down