Skip to content

Commit

Permalink
Additions to stdutils
Browse files Browse the repository at this point in the history
  • Loading branch information
pierre-dejoue committed Mar 1, 2024
1 parent 99aa7c9 commit 6a2e39b
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 7 deletions.
9 changes: 9 additions & 0 deletions src/stdutils/include/stdutils/chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@
namespace stdutils {
namespace chrono {

// For example to measure a duration in milliseconds:
//
// std::chrono::duration<float, std::milli> duration;
// {
// stdutils::chrono::DurationMeas meas(duration);
// // Do something
// }
// float duration_ms = time.count();
//
template<typename Rep, typename Period>
class DurationMeas
{
Expand Down
41 changes: 39 additions & 2 deletions src/stdutils/include/stdutils/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ struct Severity
static constexpr SeverityCode EXCPT = -1;
static constexpr SeverityCode ERR = 1;
static constexpr SeverityCode WARN = 2;
static constexpr SeverityCode INFO = 3;
};

std::string_view str_severity_code(SeverityCode code);
Expand All @@ -48,6 +49,15 @@ using StreamParser = std::function<Ret(std::basic_istream<CharT, std::char_trait
template <typename Ret, typename CharT>
Ret open_and_parse_file(const std::filesystem::path& filepath, const StreamParser<Ret, CharT>& stream_parser, const stdutils::io::ErrorHandler& err_handler) noexcept;

/**
* Save a file with a writer to std::basic_ostream
*/
template <typename Obj, typename CharT>
using StreamWriter = std::function<void(std::basic_ostream<CharT, std::char_traits<CharT>>&, const Obj&, const stdutils::io::ErrorHandler&)>;

template <typename Obj, typename CharT>
void save_file(const std::filesystem::path& filepath, const StreamWriter<Obj, CharT>& stream_writer, const Obj& obj, const stdutils::io::ErrorHandler& err_handler) noexcept;

/**
* LineStream: A wrapper around std::getline to count line nb
*/
Expand Down Expand Up @@ -114,12 +124,14 @@ using SkipLineStream = Basic_SkipLineStream<char>;
template <typename CharT>
std::size_t countlines(std::basic_istream<CharT, std::char_traits<CharT>>& istream);


//
//
// IMPLEMENTATION
// Implementation
//
//


template <typename Ret, typename CharT>
Ret open_and_parse_file(const std::filesystem::path& filepath, const StreamParser<Ret, CharT>& stream_parser, const stdutils::io::ErrorHandler& err_handler) noexcept
{
Expand All @@ -141,12 +153,37 @@ Ret open_and_parse_file(const std::filesystem::path& filepath, const StreamParse
catch(const std::exception& e)
{
std::stringstream oss;
oss << "Exception: " << e.what();
oss << "stdutils::io::open_and_parse_file(): " << e.what();
err_handler(stdutils::io::Severity::EXCPT, oss.str());
}
return Ret();
}

template <typename Obj, typename CharT>
void save_file(const std::filesystem::path& filepath, const StreamWriter<Obj, CharT>& stream_writer, const Obj& obj, const stdutils::io::ErrorHandler& err_handler) noexcept
{
try
{
std::basic_ofstream<CharT> outputstream(filepath);
if (outputstream.is_open())
{
stream_writer(outputstream, obj, err_handler);
}
else
{
std::stringstream oss;
oss << "Cannot open file " << filepath;
err_handler(stdutils::io::Severity::FATAL, oss.str());
}
}
catch(const std::exception& e)
{
std::stringstream oss;
oss << "stdutils::io::save_file(): " << e.what();
err_handler(stdutils::io::Severity::EXCPT, oss.str());
}
}

template <typename CharT>
Basic_LineStream<CharT>::Basic_LineStream(stream_type& source)
: m_stream(source)
Expand Down
13 changes: 13 additions & 0 deletions src/stdutils/include/stdutils/span.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ class span {};
template <typename T>
class span<T, dyn_extent, void>
{
public:
using element_type = T;
using value_type = std::remove_cv_t<T>;
using pointer = T*;
using const_pointer = const T*;

public:
span(T* ptr, std::size_t size) noexcept : m_ptr(ptr), m_size(size) { assert(m_ptr); }
span(const span<T>&) noexcept = default;
Expand Down Expand Up @@ -51,6 +57,13 @@ template <typename T, std::size_t Sz>
class span<T, Sz, std::enable_if_t<Sz != dyn_extent>>
{
using this_type = span<T, Sz, std::enable_if_t<Sz != dyn_extent>>;

public:
using element_type = T;
using value_type = std::remove_cv_t<T>;
using pointer = T*;
using const_pointer = const T*;

public:
explicit span(T* ptr) noexcept : m_ptr(ptr) { assert(m_ptr); }
span(const this_type&) noexcept = default;
Expand Down
9 changes: 6 additions & 3 deletions src/stdutils/include/stdutils/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,22 @@ std::string capitalize(const std::string& in);
* out << indent(2); // Output 2 indentations
*/
template <typename CharT>
class BasicIndent : private std::basic_string<CharT>
class BasicIndent
{
public:
class Multi;

BasicIndent(std::size_t count, CharT ch = ' ') : std::basic_string<CharT>(count, ch) {}
BasicIndent(std::size_t count, CharT ch = ' ') : m_str(count, ch) {}

Multi operator()(std::size_t factor) const { return Multi(*this, factor); }

friend std::basic_ostream<CharT>& operator<<(std::basic_ostream<CharT>& out, const BasicIndent<CharT>& indent)
{
return out << static_cast<const std::basic_string<CharT>&>(indent);
return out << indent.m_str;
}

private:
std::basic_string<CharT> m_str;
};

template <typename CharT>
Expand Down
8 changes: 6 additions & 2 deletions src/stdutils/src/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ std::string_view str_severity_code(SeverityCode code)
{
return "EXCPT";
}
else if (code == Severity::ERR)
{
return "ERROR";
}
else if (code == Severity::WARN)
{
return "WARNING";
}
else if (code == Severity::ERR)
else if (code == Severity::INFO)
{
return "ERROR";
return "INFO";
}
else
{
Expand Down
2 changes: 2 additions & 0 deletions src/tests/stdutils/src/test_io.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Copyright (c) 2023 Pierre DEJOUE
// This code is distributed under the terms of the MIT License
#include <catch_amalgamated.hpp>

#include <stdutils/io.h>
Expand Down
2 changes: 2 additions & 0 deletions src/tests/stdutils/src/test_span.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Copyright (c) 2023 Pierre DEJOUE
// This code is distributed under the terms of the MIT License
#include <catch_amalgamated.hpp>

#include <stdutils/span.h>
Expand Down

0 comments on commit 6a2e39b

Please sign in to comment.