Wrapped everything in namespaces

This commit is contained in:
Andreas Tsouchlos 2021-11-18 22:05:26 +01:00
parent e028e43b6a
commit 83332cc295
4 changed files with 39 additions and 21 deletions

View File

@ -6,8 +6,8 @@
#include <iostream>
#include <tuple>
#include <utility.h>
#include <parsing.h>
#include <utility.h>
/*
@ -17,18 +17,24 @@
*/
namespace detail {
template <typename T>
concept output_policy_c = requires(T t) {
t.write('c');
};
} // namespace detail
template <output_policy_c output_policy_t>
template <detail::output_policy_c output_policy_t>
class Logger {
public:
Logger(output_policy_t output_policy) : m_output_policy(output_policy) {}
template<ConstString msg, typename... args_t>
public:
Logger(output_policy_t output_policy) : m_output_policy(output_policy) {
}
template <detail::ConstString msg, typename... args_t>
void log(args_t...) {
constexpr int len = get_output_len(msg);
static_assert(len > 0, "Syntax error in log string");

View File

@ -1,11 +1,13 @@
//
// Created by andreas on 11/18/21.
//
#ifndef LOGGER_PARSING_H
#define LOGGER_PARSING_H
#include <utility.h>
namespace detail {
// clang-format off
/*
@ -38,12 +40,12 @@
// clang-format on
template<std::size_t N>
template <std::size_t N>
constexpr bool is_digit(ConstString<N> s, unsigned i) {
return (s[i] > 47) && (s[i] < 58);
}
template<std::size_t N>
template <std::size_t N>
constexpr std::pair<unsigned, int> parse_number(ConstString<N> s, unsigned i) {
while ((i < s.size()) && is_digit(s, i)) {
++i;
@ -52,7 +54,7 @@ constexpr std::pair<unsigned, int> parse_number(ConstString<N> s, unsigned i) {
return {i, 0};
}
template<std::size_t N>
template <std::size_t N>
constexpr std::pair<unsigned, int> parse_type(ConstString<N> s, unsigned i) {
if (s[i] == 's') { // string
++i;
@ -113,8 +115,9 @@ constexpr std::pair<unsigned, int> parse_type(ConstString<N> s, unsigned i) {
return {i, -1};
}
template<std::size_t N>
constexpr std::pair<unsigned, int> parse_fmt_string(ConstString<N> s, unsigned i) {
template <std::size_t N>
constexpr std::pair<unsigned, int> parse_fmt_string(ConstString<N> s,
unsigned i) {
int result_extra_len = 0;
if (s[i] == '0')
@ -143,13 +146,12 @@ constexpr std::pair<unsigned, int> parse_fmt_string(ConstString<N> s, unsigned i
return {i, -1};
i = new_i;
result_extra_len += extra_len;
}
return {i, result_extra_len};
}
template<std::size_t N>
template <std::size_t N>
constexpr std::pair<unsigned, int> parse_braces(ConstString<N> s, unsigned i) {
int result_extra_len = 0;
@ -174,7 +176,7 @@ constexpr std::pair<unsigned, int> parse_braces(ConstString<N> s, unsigned i) {
return {i, -1};
}
template<std::size_t N>
template <std::size_t N>
constexpr int get_output_len(ConstString<N> s) {
int result_extra_len = 0;
@ -197,4 +199,7 @@ constexpr int get_output_len(ConstString<N> s) {
}
#endif //LOGGER_PARSING_H
} // namespace detail
#endif // LOGGER_PARSING_H

View File

@ -2,6 +2,9 @@
#define LOGGER_UTILITY_H
namespace detail {
template <std::size_t N>
class ConstString {
public:
@ -15,14 +18,18 @@ public:
}
constexpr std::size_t size() const noexcept {
return N-1;
return N - 1;
}
std::array<char, N> m_content;
};
template <std::size_t N>
ConstString(const char (&)[N]) -> ConstString<N>;
#endif //LOGGER_UTILITY_H
} // namespace detail
#endif // LOGGER_UTILITY_H

View File

@ -16,7 +16,7 @@ int main() {
Uart uart;
Logger logger(uart);
logger.log<"Test format string: {} {}">(1, 2, 3);
logger.log<"Test format string: {:08.4f} {}">(1, 2, 3);
return 0;
}