Put everything into a new 'const_fmt' namespace; Renamed namespace detail to const_fmt_detail

This commit is contained in:
Andreas Tsouchlos 2022-02-13 17:48:24 +01:00
parent 70fd273a70
commit 2ecc001a64
8 changed files with 86 additions and 73 deletions

View File

@ -4,12 +4,12 @@
#include <cstring>
#include "format_impl.h"
#include "parse.h"
#include "utility.h"
#include "format_impl.h"
namespace detail {
namespace const_fmt { namespace const_fmt_detail {
/*
@ -42,16 +42,16 @@ constexpr inline void check_fmt_params() {
template <fmt_data_t fmt_data, std::integral arg_t>
constexpr inline void format_arg(char* dest, arg_t arg) {
detail::format_int(dest, arg, fmt_data);
const_fmt_detail::format_int(dest, arg, fmt_data);
};
template <fmt_data_t fmt_data, std::floating_point arg_t>
constexpr inline void format_arg(char* dest, arg_t arg) {
//detail::format_float(dest, arg, fmt_data);
constexpr inline void format_arg(char* dest, arg_t arg){
// const_fmt_detail::format_float(dest, arg, fmt_data);
};
// TODO: Error handling
template<fmt_data_t fmt_data>
template <fmt_data_t fmt_data>
constexpr inline void format_arg(char* dest, const char* arg) {
const std::size_t len = const_strlen(arg);
if (len > fmt_data.length) return;
@ -74,14 +74,16 @@ constexpr inline void format_args(char*) {
}
template <auto fmt_data_array, typename first_arg_t, typename... args_t>
constexpr inline void format_args(char* dest, first_arg_t first_arg, args_t... args) {
constexpr inline void format_args(char* dest, first_arg_t first_arg,
args_t... args) {
format_arg<fmt_data_array[0]>(dest + fmt_data_array[0].position, first_arg);
format_args<drop_first(fmt_data_array)>(dest, args...);
}
template <auto ast>
consteval inline std::array<char, get_ast_output_len<ast>()> get_preproc_string() {
consteval inline std::array<char, get_ast_output_len<ast>()>
get_preproc_string() {
auto result = get_init_array<get_ast_output_len<ast>()>('f');
int i = 0;
@ -97,7 +99,7 @@ consteval inline std::array<char, get_ast_output_len<ast>()> get_preproc_string(
}
} // namespace detail
}} // namespace const_fmt::const_fmt_detail
/*
@ -107,32 +109,38 @@ consteval inline std::array<char, get_ast_output_len<ast>()> get_preproc_string(
*/
template <detail::ConstString s, typename... args_t>
namespace const_fmt {
template <const_fmt_detail::ConstString s, typename... args_t>
constexpr inline auto format(args_t... args) {
constexpr auto ast = detail::parse_string<s>().value;
constexpr auto fmt_data = detail::get_fmt_data<ast>();
constexpr auto ast = const_fmt_detail::parse_string<s>().value;
constexpr auto fmt_data = const_fmt_detail::get_fmt_data<ast>();
auto result = detail::get_preproc_string<ast>();
auto result = const_fmt_detail::get_preproc_string<ast>();
detail::format_args<fmt_data>(result.begin(), args...);
const_fmt_detail::format_args<fmt_data>(result.begin(), args...);
return result;
}
template<detail::ConstString t_s>
template <const_fmt_detail::ConstString t_s>
class fmt_literal_obj_t {
public:
template<typename... args_t>
constexpr auto operator()(args_t... args) {
return format<t_s>(args...);
}
public:
template <typename... args_t>
constexpr auto operator()(args_t... args) {
return format<t_s>(args...);
}
};
template <detail::ConstString t_s>
template <const_fmt_detail::ConstString t_s>
constexpr auto operator""_fmt() {
return fmt_literal_obj_t<t_s>{};
}
} // namespace const_fmt
#endif // LOGGER_FORMAT_H

View File

@ -16,7 +16,7 @@
#include "utility.h"
namespace detail {
namespace const_fmt { namespace const_fmt_detail {
/*
@ -148,7 +148,7 @@ constexpr inline void format_float(char* out, float_t value,
}
} // namespace detail
}} // namespace const_fmt::const_fmt_detail
#endif // LOGGER_FORMAT_IMPL_H

View File

@ -37,7 +37,7 @@
// clang-format on
namespace detail {
namespace const_fmt { namespace const_fmt_detail {
/*
@ -119,42 +119,42 @@ constexpr inline parse_result_t<unsigned> parse_number(unsigned i) {
template <ConstString s>
constexpr inline parse_result_t<FormatType> parse_type(unsigned i) {
switch (s[i]) {
case 's':
return {true, ++i, FormatType::s};
case 'c':
return {true, ++i, FormatType::c};
case 'b':
return {true, ++i, FormatType::b};
case 'B':
return {true, ++i, FormatType::B};
case 'd':
return {true, ++i, FormatType::d};
case 'o':
return {true, ++i, FormatType::o};
case 'x':
return {true, ++i, FormatType::x};
case 'X':
return {true, ++i, FormatType::X};
case 'a':
return {true, ++i, FormatType::a};
case 'A':
return {true, ++i, FormatType::A};
case 'e':
return {true, ++i, FormatType::e};
case 'E':
return {true, ++i, FormatType::E};
case 'f':
return {true, ++i, FormatType::f};
case 'F':
return {true, ++i, FormatType::F};
case 'g':
return {true, ++i, FormatType::g};
case 'G':
return {true, ++i, FormatType::G};
case 'p':
return {true, ++i, FormatType::p};
default:
return {false, i, FormatType::s};
case 's':
return {true, ++i, FormatType::s};
case 'c':
return {true, ++i, FormatType::c};
case 'b':
return {true, ++i, FormatType::b};
case 'B':
return {true, ++i, FormatType::B};
case 'd':
return {true, ++i, FormatType::d};
case 'o':
return {true, ++i, FormatType::o};
case 'x':
return {true, ++i, FormatType::x};
case 'X':
return {true, ++i, FormatType::X};
case 'a':
return {true, ++i, FormatType::a};
case 'A':
return {true, ++i, FormatType::A};
case 'e':
return {true, ++i, FormatType::e};
case 'E':
return {true, ++i, FormatType::E};
case 'f':
return {true, ++i, FormatType::f};
case 'F':
return {true, ++i, FormatType::F};
case 'g':
return {true, ++i, FormatType::g};
case 'G':
return {true, ++i, FormatType::G};
case 'p':
return {true, ++i, FormatType::p};
default:
return {false, i, FormatType::s};
}
}
@ -214,7 +214,8 @@ constexpr inline parse_result_t<fmt_node_t> parse_braces(unsigned i) {
}
template <ConstString s>
constexpr inline parse_result_t<string_result_t<get_ast_len<s>()>> parse_string() {
constexpr inline parse_result_t<string_result_t<get_ast_len<s>()>>
parse_string() {
parse_result_t<string_result_t<get_ast_len<s>()>> result;
result.is_valid = true;
@ -242,7 +243,7 @@ constexpr inline parse_result_t<string_result_t<get_ast_len<s>()>> parse_string(
}
} // namespace detail
}} // namespace const_fmt::const_fmt_detail

View File

@ -5,7 +5,7 @@
#include <array>
namespace detail {
namespace const_fmt { namespace const_fmt_detail {
/*
@ -136,7 +136,7 @@ struct fmt_data_t {
};
} // namespace detail
}} // namespace const_fmt::const_fmt_detail
#endif // LOGGER_TYPES_H

View File

@ -7,7 +7,7 @@
#include "types.h"
namespace detail {
namespace const_fmt { namespace const_fmt_detail {
constexpr inline std::size_t const_pow(std::size_t base, std::size_t pow) {
@ -41,7 +41,8 @@ consteval inline std::size_t count_ast_format_nodes() {
template <auto ast>
consteval inline std::array<fmt_data_t, count_ast_format_nodes<ast>()> get_fmt_data() {
consteval inline std::array<fmt_data_t, count_ast_format_nodes<ast>()>
get_fmt_data() {
std::array<fmt_data_t, count_ast_format_nodes<ast>()> result = {};
std::size_t position = 0;
@ -96,7 +97,7 @@ consteval inline int get_ast_output_len() {
return result;
}
constexpr inline std::size_t const_strlen(const char* arg) {
constexpr inline std::size_t const_strlen(const char* arg) {
if (std::is_constant_evaluated()) {
return *arg ? 1 + const_strlen(arg + 1) : 0;
} else {
@ -105,7 +106,7 @@ constexpr inline std::size_t const_strlen(const char* arg) {
}
} // namespace detail
}} // namespace const_fmt::const_fmt_detail
#endif // LOGGER_UTILITY_H

View File

@ -2,7 +2,8 @@
#include <gtest/gtest.h>
using namespace detail;
using namespace const_fmt;
using namespace const_fmt::const_fmt_detail;
TEST(Format, positive_int) {

View File

@ -2,7 +2,7 @@
#include <const_fmt/parse.h>
using namespace detail;
using namespace const_fmt::const_fmt_detail;
TEST(Parse, parse_number) {

View File

@ -1,9 +1,11 @@
#include <const_fmt/utility.h>
#include <gtest/gtest.h>
using namespace const_fmt;
TEST(Utility, const_pow) {
EXPECT_EQ(detail::const_pow(10, 0), 1);
EXPECT_EQ(detail::const_pow(0, 1), 0);
EXPECT_EQ(detail::const_pow(2, 8), 0b1'0000'0000);
EXPECT_EQ(const_fmt_detail::const_pow(10, 0), 1);
EXPECT_EQ(const_fmt_detail::const_pow(0, 1), 0);
EXPECT_EQ(const_fmt_detail::const_pow(2, 8), 0b1'0000'0000);
}