-
Notifications
You must be signed in to change notification settings - Fork 7
feat(io_interface): Port Reader and Writer interfaces from CLP with minimal compilation fixes. #48
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
feat(io_interface): Port Reader and Writer interfaces from CLP with minimal compilation fixes. #48
Conversation
WalkthroughThis pull request adds a new I/O interface module to the project. The changes update the CMake configuration to include the new Changes
Sequence Diagram(s)sequenceDiagram
participant Client as Client
participant Reader as ReaderInterface
participant IO as I/O Subsystem
Client->>Reader: call read()/try_read_to_delimiter()...
activate Reader
Reader->>IO: issue read operation
IO-->>Reader: return data or error code
alt Successful Read
Reader-->>Client: return data
else Read Error
Reader-->>Client: throw OperationFailed exception / return error
end
deactivate Reader
sequenceDiagram
participant Client as Client
participant Writer as WriterInterface
participant IO as I/O Subsystem
Client->>Writer: call write_char()/write_string()
activate Writer
Writer->>IO: perform write operation
IO-->>Writer: acknowledge write or return error
alt Successful Write
Writer-->>Client: confirm success
else Write Error
Writer-->>Client: throw OperationFailed exception
end
deactivate Writer
Suggested reviewers
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 0
🧹 Nitpick comments (8)
src/ystdlib/io_interface/ErrorCode.hpp (2)
7-29: Consider usingenum classfor modern C++ type safetyWhile the C-style enum declaration works, using
enum class ErrorCodewould provide better type safety by preventing implicit conversions and reducing namespace pollution, which aligns with modern C++ practices.- typedef enum { + enum class ErrorCode { - ErrorCode_Success = 0, + Success = 0, - ErrorCode_BadParam, + BadParam, - ErrorCode_BadParam_DB_URI, + BadParam_DB_URI,This would require updating all references to use the scoped enum values (e.g.,
ErrorCode::Successinstead ofErrorCode_Success).
7-29: Add documentation for error codesConsider adding documentation comments for each error code to describe their purpose and usage scenarios, especially for specific errors like
ErrorCode_BadParam_DB_URIandErrorCode_Failure_DB_Bulk_Write.src/ystdlib/io_interface/WriterInterface.cpp (2)
9-11: Consider documenting null character handling in write_stringThe current implementation uses
c_str()which means that if the string contains null characters, only characters up to the first null character might be written, depending on the underlying implementation ofwrite. Consider documenting this behavior or ensuring it's consistent with expectations.
27-35: LGTM, with a minor suggestion for consistencyThe implementation correctly retrieves position and handles errors. For consistency with lines 14 and 21, consider using
autofor the error code variable:- ErrorCode error_code = try_get_pos(pos); + auto error_code = try_get_pos(pos);src/ystdlib/io_interface/WriterInterface.hpp (2)
14-17: Consider storing the error code in the OperationFailed exception.
Currently, the constructor does not store or utilise theerror_code, and nowhat()override is provided to expose error details.A possible refactor is to store this code in a private member and override
what()to provide diagnostic info.class OperationFailed : public std::exception { public: - OperationFailed(ErrorCode error_code) {} + explicit OperationFailed(ErrorCode error_code) : m_error_code(error_code) {} + const char* what() const noexcept override { + return "OperationFailed in WriterInterface"; + } +private: + ErrorCode m_error_code; };
38-39: Ensure endianness is consistent when writing numeric values.
When writing numeric values in binary form, consider documenting or enforcing a specific endianness for interoperability across different platforms.Also applies to: 71-74
src/ystdlib/io_interface/ReaderInterface.hpp (2)
16-17: Capture and expose the error code in OperationFailed.
Just like in the writer interface, the constructor is empty. Storing the error code and overridingwhat()can improve debugging.
84-94: Recognise potential endianness concerns when reading numeric values.
Similar to the writing interface, reading numeric values in raw binary form can cause portable issues if endianness differs between systems. A documented or enforced endianness may be beneficial.Also applies to: 126-145
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (7)
src/ystdlib/CMakeLists.txt(1 hunks)src/ystdlib/io_interface/CMakeLists.txt(1 hunks)src/ystdlib/io_interface/ErrorCode.hpp(1 hunks)src/ystdlib/io_interface/ReaderInterface.cpp(1 hunks)src/ystdlib/io_interface/ReaderInterface.hpp(1 hunks)src/ystdlib/io_interface/WriterInterface.cpp(1 hunks)src/ystdlib/io_interface/WriterInterface.hpp(1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
`**/*.{cpp,hpp,java,js,jsx,tpp,ts,tsx}`: - Prefer `false == ` rather than `!`.
**/*.{cpp,hpp,java,js,jsx,tpp,ts,tsx}: - Preferfalse == <expression>rather than!<expression>.
src/ystdlib/io_interface/ErrorCode.hppsrc/ystdlib/io_interface/WriterInterface.cppsrc/ystdlib/io_interface/WriterInterface.hppsrc/ystdlib/io_interface/ReaderInterface.cppsrc/ystdlib/io_interface/ReaderInterface.hpp
🧬 Code Definitions (3)
src/ystdlib/io_interface/WriterInterface.cpp (3)
src/ystdlib/io_interface/WriterInterface.hpp (8)
c(45-45)str(50-50)pos(30-30)pos(32-32)pos(56-56)OperationFailed(16-16)offset(31-31)offset(62-62)src/ystdlib/io_interface/ReaderInterface.cpp (4)
seek_from_begin(111-116)seek_from_begin(111-111)get_pos(118-126)get_pos(118-118)src/ystdlib/io_interface/ReaderInterface.hpp (4)
pos(24-24)pos(25-25)pos(117-117)OperationFailed(16-16)
src/ystdlib/io_interface/ReaderInterface.cpp (1)
src/ystdlib/io_interface/ReaderInterface.hpp (12)
delim(38-38)delim(59-59)buf(23-23)buf(48-48)buf(68-68)buf(77-77)OperationFailed(16-16)str_length(102-102)str_length(111-111)pos(24-24)pos(25-25)pos(117-117)
src/ystdlib/io_interface/ReaderInterface.hpp (2)
src/ystdlib/io_interface/WriterInterface.hpp (4)
pos(30-30)pos(32-32)pos(56-56)OperationFailed(16-16)src/ystdlib/io_interface/ReaderInterface.cpp (2)
try_read_exact_length(69-80)try_read_exact_length(69-69)
🔇 Additional comments (9)
src/ystdlib/CMakeLists.txt (1)
3-3: LGTM!The addition of the
io_interfacemodule to the build system is clean and follows the project's convention, correctly placed alongside the existing modules.src/ystdlib/io_interface/CMakeLists.txt (1)
1-14: LGTM!The CMake configuration is properly structured, clearly defining the public headers, private sources, and test sources for the
io_interfacelibrary within theystdlibnamespace.src/ystdlib/io_interface/WriterInterface.cpp (3)
5-7: LGTM!The implementation correctly delegates to the
writemethod with appropriate parameters.
13-18: LGTM!The implementation follows the project's error handling pattern and correctly adheres to coding guidelines by using
ErrorCode_Success != error_codeinstead of the negated form.
20-25: LGTM!The implementation correctly handles errors and follows the coding guidelines.
src/ystdlib/io_interface/ReaderInterface.cpp (4)
13-15: Good usage of the style guideline.
Usingif (false == append)aligns with the coding guideline that prefersfalse == <expression>over!<expression>.
46-55: Consistent error handling approach.
ThrowingOperationFailedfor non-EOF error codes and returningfalsefor EOF is clear and consistent. Nicely done.
69-80: Appropriate handling of truncated reads.
ReturningErrorCode_Truncatedwhen fewer bytes are read helps distinguish partial reads from other errors. This is a good practice for robust error handling.
25-29:✅ Verification successful
Assess partial read handling at EOF.
ReturningErrorCode_Successwhen EOF is reached but some content was read is a design decision. Ensure that the downstream logic properly differentiates between full and partial reads.To confirm correctness, please search for call sites of
try_read_to_delimiterto confirm all partial-read scenarios are handled correctly:
🏁 Script executed:
#!/bin/bash rg -A 10 "try_read_to_delimiter"Length of output: 2478
Partial read handling at EOF verified
The investigation confirms that the only call site for
try_read_to_delimiter(insrc/ystdlib/io_interface/ReaderInterface.cpp) correctly distinguishes between a complete EOF (no data read) and a partial read (some data read). When data is read before EOF, the function convertsErrorCode_EndOfFiletoErrorCode_Success, which the caller then treats as a successful read. No further action is required.
Description
This PR serves as a preliminary step toward standardizing
io_interfaceas part of theystdlib-cpplibrary. This will help us provide a clear view of the changes we are going to make compared to CLP's main branch. Moving forward, we plan to update the API to use the newerror_handlingutilities, such asoutcomeandResult.The list of changed items are:
clptoystdlib::io_interface.OperationFailedby making it inherit fromstd::exception. We have pending changes in feat(error_handling): MigrateTraceableExceptionfrom CLP with changes: #43__FILENAME__and__LINE__TraceableException.hppandDefs.hppNOLINTBEGINandNOLINTENDaround cpp files as we deferclang-tidyprocessing to the refactor stage.test_ReaderInterface.cppandtest_WriterInterface.cppfiles for now. This is necessary becauseTESTS_SOURCESis required when buildingystdliblibraries.Checklist
breaking change.
Validation performed
Appendix
Here's the diff for each ported file.
ReaderInterface.hppReaderInterface.cppWriterInterface.hppWriterInterface.cppErrorCode.hppSummary by CodeRabbit