-
Notifications
You must be signed in to change notification settings - Fork 38
Add batch write #85
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
Add batch write #85
Changes from 1 commit
922e66b
8eea5dc
bcd6481
2730039
667ba32
3c5f998
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// Copyright (C) 2020-2025 Andrei Avram | ||
|
||
#ifndef MSD_CHANNEL_RESULT_HPP_ | ||
#define MSD_CHANNEL_RESULT_HPP_ | ||
|
||
/** @file */ | ||
|
||
namespace msd { | ||
|
||
/** | ||
* @brief A result that contains either a value of type T or an error of type E. | ||
* | ||
* @tparam T The type of the value on success. | ||
* @tparam E The type of the error on failure. | ||
*/ | ||
template <typename T, typename E> | ||
class result { | ||
public: | ||
/** | ||
* @brief Constructs an empty result (not valid). | ||
*/ | ||
explicit result() = default; | ||
|
||
/** | ||
* @brief Constructs a successful result with a value. | ||
* | ||
* @param value The value to store into the result. | ||
*/ | ||
explicit result(T value) : value_{value} {} | ||
|
||
/** | ||
* @brief Constructs an error result. | ||
* | ||
* @param error The error value to store. | ||
*/ | ||
explicit result(E error) : has_error_{true}, error_{error} {} | ||
|
||
/** | ||
* @brief Checks whether the result is a success. | ||
* | ||
* @return true if the result holds a value, false if it holds an error. | ||
*/ | ||
explicit operator bool() const { return !has_error_; } | ||
|
||
/** | ||
* @brief Gets the stored value. | ||
* | ||
* @return const T& Reference to the stored value. | ||
* @warning Behavior is undefined if the result holds an error. | ||
*/ | ||
const T& value() const { return value_; } | ||
|
||
/** | ||
* @brief Gets the stored error. | ||
* | ||
* @return const E& Reference to the stored error. | ||
* @warning Behavior is undefined if the result holds a value. | ||
*/ | ||
const E& error() const { return error_; } | ||
Comment on lines
+45
to
+59
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In include/msd/result.hpp, the value() and error() methods don't check the state of the result before returning values. This could lead to undefined behavior when accessing the wrong union member. Consider adding assertions or other safety mechanisms to prevent misuse. |
||
|
||
private: | ||
union { | ||
T value_; | ||
E error_; | ||
}; | ||
bool has_error_{}; | ||
Comment on lines
+61
to
+66
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In include/msd/result.hpp, using a union with non-trivial types without proper lifetime management could lead to undefined behavior. Consider implementing proper construction/destruction handling for the union members or using std::variant instead.
Comment on lines
+62
to
+66
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In include/msd/result.hpp, when E is a non-trivial type, the union could lead to undefined behavior without proper destruction. Consider adding a destructor that checks has_error_ and properly calls the destructor of the active member. |
||
}; | ||
|
||
/** | ||
* @brief Specialization of result for void success type. | ||
* | ||
* @tparam E The type of the error on failure. | ||
*/ | ||
template <typename E> | ||
class result<void, E> { | ||
public: | ||
/** | ||
* @brief Constructs a successful void result. | ||
*/ | ||
result() = default; | ||
|
||
/** | ||
* @brief Constructs an error result. | ||
* | ||
* @param error The error value to store. | ||
*/ | ||
explicit result(E error) : has_value_{false}, error_{error} {} | ||
|
||
/** | ||
* @brief Checks whether the result is a success. | ||
* | ||
* @return true if the result is successful, false otherwise. | ||
*/ | ||
explicit operator bool() const { return has_value_; } | ||
|
||
/** | ||
* @brief Gets the stored error. | ||
* | ||
* @return Const reference to the stored error. | ||
* @note Only valid if the result holds an error. | ||
*/ | ||
const E& error() const { return error_; } | ||
|
||
private: | ||
bool has_value_{true}; | ||
E error_; | ||
}; | ||
|
||
} // namespace msd | ||
|
||
#endif // MSD_CHANNEL_RESULT_HPP_ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In include/msd/channel.hpp, the wait_before_write method doesn't check if is_closed_ is true before waiting, which could lead to a thread waiting indefinitely when the channel is closed but full. Consider adding a check for is_closed_ in the wait condition.