-
Notifications
You must be signed in to change notification settings - Fork 0
Initial Implementation of the buffer template class #37
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
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
a84e784
Initial Implementation of the buffer template class
aexzhou a866e42
read buffer works about to start implementation and adding to buffer
henovw cab3720
[Buffer, General] Initial integration of Buffer class into main code
aexzhou a9b4066
Merge branch 'buffer_base_class' of https://github.com/UBC-OpenRoboti…
aexzhou 07f11ff
filling buffer data
henovw e024ecc
Added Debug function
aexzhou 9e58122
buffer changes - extract next and pop items - max buffer size
henovw 37b3672
beginning implementation of new buffer struct
henovw ec2fdfd
Merge branch 'main' into buffer_base_class
aexzhou 1f9d0ba
Fixed merge issues from merging main branch in
aexzhou 96c0e5f
changes to struct implemented partly
henovw 67b7101
Fixed uninitialized databuffer pointer
aexzhou 9676dd7
changed toString() in DataBuffer.cpp
annhypen 2f7ea7d
Merge remote-tracking branch 'refs/remotes/origin/buffer_base_class' …
annhypen 5687518
Fix databuffer comparison logic error
aexzhou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| // #pragma once | ||
| #include <list> | ||
| #include <mutex> | ||
| #include <cstddef> | ||
|
|
||
| /** | ||
| * @brief Base class for buffers with templated data type | ||
| * @tparam T The type of data stored in the buffer | ||
| * | ||
| * This template class provides thread-safe buffer operations | ||
| * (e.g., writes, reads ...) | ||
| * that can be customized by child classes | ||
| * to work with any data type. | ||
| */ | ||
| template <typename T> | ||
| class BufferBase { | ||
| public: | ||
| BufferBase() = default; | ||
| virtual ~BufferBase() = default; | ||
|
|
||
| /** | ||
| * @brief Write data to the buffer | ||
| * @param data The data to append to the buffer | ||
| */ | ||
| virtual void write(T data) { | ||
| std::lock_guard<std::mutex> lock(mutex_); | ||
| buffer_.push_back(std::move(data)); | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * @brief Replace the entire buffer with new data | ||
| * @param data The data to set as the new buffer content | ||
| */ | ||
| virtual void setData(T data) { | ||
| std::lock_guard<std::mutex> lock(mutex_); | ||
| buffer_.clear(); | ||
| buffer_.push_back(std::move(data)); | ||
| } | ||
|
|
||
| /** | ||
| * @brief Read all data from the buffer without clearing it | ||
| * @return A copy of all data in the buffer | ||
| */ | ||
| virtual std::list<T> readAll() const { | ||
| std::lock_guard<std::mutex> lock(mutex_); | ||
| return buffer_; | ||
| } | ||
|
|
||
| /** | ||
| * @brief Read and clear all data from the buffer | ||
| * @return All data that was in the buffer | ||
| */ | ||
| virtual std::list<T> consume() { | ||
| std::lock_guard<std::mutex> lock(mutex_); | ||
| std::list<T> result = std::move(buffer_); | ||
| buffer_.clear(); | ||
| return result; | ||
| } | ||
|
|
||
| // Returns the first element of the buffer and removes it from the buffer | ||
| virtual T extractNextBuffer() { | ||
| buffer_data_t ret = buffer_.front(); | ||
| buffer_.pop_front(); | ||
| return ret; | ||
| } | ||
|
|
||
| virtual void popFront() { | ||
| buffer_.pop_front(); | ||
| } | ||
|
|
||
| /** | ||
| * @brief Get the number of elements in the buffer | ||
| * @return The number of elements | ||
| */ | ||
| virtual size_t size() const { | ||
| std::lock_guard<std::mutex> lock(mutex_); | ||
| return buffer_.size(); | ||
| } | ||
|
|
||
| /** | ||
| * @brief Check if the buffer is empty | ||
| * @return true if the buffer is empty, false otherwise | ||
| */ | ||
| virtual bool empty() const { | ||
| std::lock_guard<std::mutex> lock(mutex_); | ||
| return buffer_.empty(); | ||
| } | ||
|
|
||
| /** | ||
| * @brief Clear all data from the buffer | ||
| */ | ||
| virtual void clear() { | ||
| std::lock_guard<std::mutex> lock(mutex_); | ||
| buffer_.clear(); | ||
| } | ||
|
|
||
| protected: | ||
| std::list<T> buffer_; | ||
| mutable std::mutex mutex_; | ||
| int MAX_BUFFER_SIZE = 5; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,10 @@ | ||
| #pragma once | ||
| #include <ctime> | ||
|
|
||
| typedef struct { | ||
| char a; | ||
| float a_data; | ||
| char b; | ||
| float b_data; | ||
| float data; // actual value | ||
| std::time_t timestamp; // date recorded | ||
| const char * dataunit; // e.g. "kPa", "mL" | ||
| const char * datatype; // e.g. "temperature", "sound" | ||
|
|
||
| } buffer_data_t; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
Info: the
virtualkeyword here allows for class methods for thisbuffer_baseclass to be inherited to children classesLearn more about
virtual:https://www.geeksforgeeks.org/cpp/virtual-function-cpp/