feat: Create datawriter class#22
Conversation
MichalSzandar
left a comment
There was a problem hiding this comment.
- Create IDataFormatStrategy virtual class
- add unique_ptr< IDataFormatStrategy> in DataWriter
| EEGData eegData; | ||
| while (eegQueue->try_dequeue(eegData)) { | ||
| writeEEGData(eegData); | ||
| } | ||
| } | ||
|
|
||
| if (markerQueue) { | ||
| Marker marker; | ||
| while (markerQueue->try_dequeue(marker)) { | ||
| writeMarker(marker); | ||
| } | ||
| } |
There was a problem hiding this comment.
Don't repeat yourself
| void DataWriter::writeEEGData(const EEGData& data) { | ||
| outputFile << "eeg," << data.timestamp << ",\""; | ||
| for (std::size_t index = 0; index < data.channels.size(); ++index) { | ||
| if (index > 0) { | ||
| outputFile << ','; | ||
| } | ||
| outputFile << data.channels[index]; | ||
| } | ||
| outputFile << '"' << '\n'; | ||
| } | ||
|
|
||
| void DataWriter::writeMarker(const Marker& marker) { | ||
| outputFile << "marker," << marker.timestamp << ",\"" << marker.eventName << '"' << '\n'; | ||
| } |
There was a problem hiding this comment.
Move those to CSVFormatStrategy
| return wroteData; | ||
| } | ||
|
|
||
| DataWriter::DataWriter() : formatStrategy(std::make_unique<CSVFormatStrategy>()) {} |
There was a problem hiding this comment.
pass IDataFormatStrategy as argument for DataWriter constructor, delete default constructor.
| virtual void writeHeader(std::ofstream& outputFile) const = 0; | ||
| virtual void writeEEGData(std::ofstream& outputFile, const EEGData& data) const = 0; | ||
| virtual void writeMarker(std::ofstream& outputFile, const Marker& marker) const = 0; | ||
| }; |
There was a problem hiding this comment.
The issue is that you force every strategy to use std::ofstream as a way to save data which is not good, because in other strategies like FifFormatStrategy we're (most likely) going to use Fiffstream to save our data. I think it would be better to add open(std::string filepath) function as I suggested in the UML diagram.
| #ifndef EEGDATA_HPP | ||
| #define EEGDATA_HPP | ||
|
|
||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| struct EEGData { | ||
| std::vector<float> channels; | ||
| double timestamp = 0.0; | ||
|
|
||
| EEGData() = default; | ||
|
|
||
| EEGData(const std::vector<float>& channelValues, double sampleTimestamp) | ||
| : channels(channelValues), timestamp(sampleTimestamp) {} | ||
|
|
||
| EEGData(std::vector<float>&& channelValues, double sampleTimestamp) | ||
| : channels(std::move(channelValues)), timestamp(sampleTimestamp) {} | ||
| }; | ||
|
|
||
| #endif // EEGDATA_HPP No newline at end of file |
There was a problem hiding this comment.
I would consider moving EEGData and Marker classes to seperate directory like include/data_structures or something like that
Create DataWriter class and Marker, EEGData struct stubs, along with 2 basic unit tests for the DataWriter.
resolves #7