Molecular Dynamics Simulation  1.0
Public Types | Public Member Functions | Private Attributes | List of all members
CSVWriter Class Reference

#include <CSVWriter.h>

Collaboration diagram for CSVWriter:
Collaboration graph

Public Types

using serializable_types = std::variant< size_t, int, double, std::string >
 The types that can be written to the CSV file. More...
 

Public Member Functions

 CSVWriter (std::filesystem::path file_path, const std::vector< std::string > &headers, bool append, std::string separator=";")
 Creates a new CSVWriter instance. More...
 
 CSVWriter (std::filesystem::path file_path, bool append, std::string separator=";")
 Creates a new CSVWriter instance. More...
 
 CSVWriter (const CSVWriter &)=delete
 
CSVWriteroperator= (CSVWriter &)=delete
 
 CSVWriter (CSVWriter &&) noexcept
 
CSVWriteroperator= (CSVWriter &&) noexcept
 
 ~CSVWriter ()
 
void initialize (const std::vector< std::string > &headers)
 Initializes the CSV file and writes the header row. More...
 
void writeRow (const std::vector< serializable_types > &row)
 Writes a row to the CSV file. More...
 

Private Attributes

std::ofstream file
 The file stream to write to. More...
 
std::filesystem::path file_path
 The path to the CSV file to write to. More...
 
bool append
 Whether to append to the CSV file or overwrite it. More...
 
std::vector< std::string > headers
 The headers of the CSV file. More...
 
std::string separator
 The separator to use between values. More...
 

Detailed Description

Definition at line 30 of file CSVWriter.h.

Member Typedef Documentation

◆ serializable_types

using CSVWriter::serializable_types = std::variant<size_t, int, double, std::string>

The types that can be written to the CSV file.

Definition at line 35 of file CSVWriter.h.

Constructor & Destructor Documentation

◆ CSVWriter() [1/4]

CSVWriter::CSVWriter ( std::filesystem::path  file_path,
const std::vector< std::string > &  headers,
bool  append,
std::string  separator = ";" 
)

Creates a new CSVWriter instance.

Parameters
file_pathThe path to the CSV file to write to.
headersThe headers of the CSV file.
appendWhether to append to the CSV file or overwrite it.
separatorThe separator to use between values.

Definition at line 17 of file CSVWriter.cpp.

18  : file_path(std::move(file_path)), append(append), headers(headers), separator(std::move(separator)) {
20 }
std::string separator
The separator to use between values.
Definition: CSVWriter.h:104
void initialize(const std::vector< std::string > &headers)
Initializes the CSV file and writes the header row.
Definition: CSVWriter.cpp:43
bool append
Whether to append to the CSV file or overwrite it.
Definition: CSVWriter.h:94
std::vector< std::string > headers
The headers of the CSV file.
Definition: CSVWriter.h:99
std::filesystem::path file_path
The path to the CSV file to write to.
Definition: CSVWriter.h:89

◆ CSVWriter() [2/4]

CSVWriter::CSVWriter ( std::filesystem::path  file_path,
bool  append,
std::string  separator = ";" 
)
explicit

Creates a new CSVWriter instance.

Parameters
file_pathThe path to the CSV file to write to.
appendWhether to append to the CSV file or overwrite it.
separatorThe separator to use between values.

Definition at line 21 of file CSVWriter.cpp.

22  : file_path(std::move(file_path)), append(append), separator(std::move(separator)) {}

◆ CSVWriter() [3/4]

CSVWriter::CSVWriter ( const CSVWriter )
delete

◆ CSVWriter() [4/4]

CSVWriter::CSVWriter ( CSVWriter &&  rhs)
noexcept

Definition at line 24 of file CSVWriter.cpp.

24  {
25  this->file_path = std::move(rhs.file_path);
26  this->headers = std::move(rhs.headers);
27  this->separator = std::move(rhs.separator);
28  this->file = std::move(rhs.file);
29 }
std::ofstream file
The file stream to write to.
Definition: CSVWriter.h:84

◆ ~CSVWriter()

CSVWriter::~CSVWriter ( )

Definition at line 38 of file CSVWriter.cpp.

38  {
39  Logger::logger->debug("Closing CSVWriter for file {}...", file_path.string());
40  file.close();
41 }
static std::shared_ptr< spdlog::logger > logger
Publically accessible shared pointer to the logger.
Definition: Logger.h:35

Member Function Documentation

◆ initialize()

void CSVWriter::initialize ( const std::vector< std::string > &  headers)

Initializes the CSV file and writes the header row.

Parameters
headersThe headers of the CSV file.

Definition at line 43 of file CSVWriter.cpp.

43  {
44  Logger::logger->debug("Creating CSVWriter for file {}...", file_path.string());
45 
46  this->headers = headers;
47 
48  if (!std::filesystem::exists(file_path)) {
49  std::filesystem::create_directories(file_path.parent_path());
50  }
51 
52  if (append) {
53  Logger::logger->warn("Appending to file {}...", file_path.string());
54  file.open(file_path, std::ios_base::app);
55  } else {
56  Logger::logger->warn("Overwriting file {}...", file_path.string());
57  file.open(file_path, std::ios_base::out);
58  }
59 
60  if (!file.is_open()) {
61  Logger::logger->error("Could not open file {} for writing!", file_path.string());
62  throw std::runtime_error("Could not open file for writing!");
63  }
64 
65  if (!append) {
66  writeRow({headers.begin(), headers.end()});
67  }
68 }
void writeRow(const std::vector< serializable_types > &row)
Writes a row to the CSV file.
Definition: CSVWriter.cpp:70

◆ operator=() [1/2]

CSVWriter & CSVWriter::operator= ( CSVWriter &&  rhs)
noexcept

Definition at line 30 of file CSVWriter.cpp.

30  {
31  this->file_path = std::move(rhs.file_path);
32  this->headers = std::move(rhs.headers);
33  this->separator = std::move(rhs.separator);
34  this->file = std::move(rhs.file);
35  return *this;
36 }

◆ operator=() [2/2]

CSVWriter& CSVWriter::operator= ( CSVWriter )
delete

◆ writeRow()

void CSVWriter::writeRow ( const std::vector< serializable_types > &  row)

Writes a row to the CSV file.

Parameters
rowThe row to write.

Definition at line 70 of file CSVWriter.cpp.

70  {
71  if (row.size() != headers.size()) {
72  Logger::logger->error("Row size ({}) does not match cols size ({})!", row.size(), headers.size());
73  throw std::runtime_error("Row size does not match cols size!");
74  }
75 
76  for (size_t i = 0; i < row.size(); i++) {
77  if (i > 0) {
78  file << separator;
79  }
80  std::visit([&](auto&& arg) { write_csv_element(file, arg); }, row[i]);
81  }
82  file << std::endl;
83 }
void write_csv_element(std::ofstream &file, const T &value)
Writes a single element to the CSV file.
Definition: CSVWriter.cpp:8

Member Data Documentation

◆ append

bool CSVWriter::append
private

Whether to append to the CSV file or overwrite it.

Definition at line 94 of file CSVWriter.h.

◆ file

std::ofstream CSVWriter::file
private

The file stream to write to.

Definition at line 84 of file CSVWriter.h.

◆ file_path

std::filesystem::path CSVWriter::file_path
private

The path to the CSV file to write to.

Definition at line 89 of file CSVWriter.h.

◆ headers

std::vector<std::string> CSVWriter::headers
private

The headers of the CSV file.

Definition at line 99 of file CSVWriter.h.

◆ separator

std::string CSVWriter::separator
private

The separator to use between values.

Definition at line 104 of file CSVWriter.h.


The documentation for this class was generated from the following files: