Skip to content

Commit

Permalink
Rename & update
Browse files Browse the repository at this point in the history
  • Loading branch information
HenryAWE committed Jan 29, 2024
1 parent 9ccd8a6 commit 3ebfeae
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 90 deletions.
2 changes: 1 addition & 1 deletion binding/c_papilio/src/c_papilio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
struct papilio_context_t
{
std::string str;
papilio::mutable_format_args args;
papilio::dynamic_format_args args;
};

papilio_context* papilio_create_context(void)
Expand Down
2 changes: 1 addition & 1 deletion example/playground/ipapilio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ipapilio
std::map<std::string, command_data, std::less<>> m_cmds;

std::string m_fmt;
papilio::mutable_format_args m_args;
papilio::dynamic_format_args m_args;

void build_cmd();

Expand Down
110 changes: 48 additions & 62 deletions include/papilio/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,7 @@ class basic_format_arg
: m_val(std::in_place_type<handle>, std::type_index(info)) {}

basic_format_arg& operator=(const basic_format_arg&) = default;

basic_format_arg& operator=(basic_format_arg&& rhs) noexcept
{
basic_format_arg(std::move(rhs)).swap(*this);
Expand Down Expand Up @@ -971,7 +972,7 @@ class static_format_args final : public detail::format_args_base<Context, CharT>
};

PAPILIO_EXPORT template <typename Context, typename CharT = typename Context::char_type>
class basic_mutable_format_args final : public detail::format_args_base<Context, CharT>
class basic_dynamic_format_args final : public detail::format_args_base<Context, CharT>
{
using my_base = detail::format_args_base<Context, CharT>;

Expand All @@ -991,12 +992,12 @@ class basic_mutable_format_args final : public detail::format_args_base<Context,
format_arg_type,
std::less<>>;

basic_mutable_format_args() = default;
basic_mutable_format_args(const basic_mutable_format_args&) = delete;
basic_mutable_format_args(basic_mutable_format_args&&) = default;
basic_dynamic_format_args() = default;
basic_dynamic_format_args(const basic_dynamic_format_args&) = delete;
basic_dynamic_format_args(basic_dynamic_format_args&&) = default;

template <typename... Args>
basic_mutable_format_args(Args&&... args)
basic_dynamic_format_args(Args&&... args)
{
push_tuple(std::forward<Args>(args)...);
}
Expand Down Expand Up @@ -1096,7 +1097,7 @@ class basic_mutable_format_args final : public detail::format_args_base<Context,

// Type-erased format arguments.
PAPILIO_EXPORT template <typename Context, typename CharT = typename Context::char_type>
class basic_dynamic_format_args final : public detail::format_args_base<Context, CharT>
class basic_format_args_ref final : public detail::format_args_base<Context, CharT>
{
using my_base = detail::format_args_base<Context, CharT>;

Expand All @@ -1106,11 +1107,11 @@ class basic_dynamic_format_args final : public detail::format_args_base<Context,
using size_type = std::size_t;
using format_arg_type = basic_format_arg<Context>;

basic_dynamic_format_args() = delete;
constexpr basic_dynamic_format_args(const basic_dynamic_format_args&) noexcept = default;
basic_format_args_ref() = delete;
constexpr basic_format_args_ref(const basic_format_args_ref&) noexcept = default;

template <std::derived_from<my_base> T>
constexpr basic_dynamic_format_args(const T& args) noexcept
constexpr basic_format_args_ref(const T& args) noexcept
: m_ptr(&args)
{
PAPILIO_ASSERT(m_ptr != this); // avoid circular reference
Expand Down Expand Up @@ -1163,8 +1164,8 @@ class basic_dynamic_format_args final : public detail::format_args_base<Context,
const my_base* m_ptr;
};

PAPILIO_EXPORT using dynamic_format_args = basic_dynamic_format_args<format_context, char>;
PAPILIO_EXPORT using wdynamic_format_args = basic_dynamic_format_args<wformat_context, wchar_t>;
PAPILIO_EXPORT using format_args_ref = basic_format_args_ref<format_context, char>;
PAPILIO_EXPORT using wformat_args_ref = basic_format_args_ref<wformat_context, wchar_t>;

PAPILIO_EXPORT template <typename Context = format_context, typename... Args>
auto make_format_args(Args&&... args)
Expand Down Expand Up @@ -1210,42 +1211,45 @@ namespace detail
};

// clang-format on
} // namespace detail

template <typename T, typename CharT>
requires streamable<T, CharT>
class streamable_formatter
PAPILIO_EXPORT template <typename T, typename CharT>
requires detail::streamable<T, CharT>
class streamable_formatter
{
public:
template <typename ParseContext>
auto parse(ParseContext& ctx)
{
public:
template <typename ParseContext>
auto parse(ParseContext& ctx)
auto it = ctx.begin();
if(*it == U'L')
{
auto it = ctx.begin();
if(*it == U'L')
{
m_use_locale = true;
++it;
}

return it;
m_use_locale = true;
++it;
}

template <typename Context>
auto format(const T& val, Context& ctx) const
{
basic_oiterstream<CharT, typename Context::iterator> os(ctx.out());
return it;
}

if(m_use_locale)
os.imbue(ctx.getloc());
template <typename Context>
auto format(const T& val, Context& ctx) const
{
basic_oiterstream<CharT, typename Context::iterator> os(ctx.out());

os << val;
if(m_use_locale)
os.imbue(ctx.getloc());

return os.get();
}
os << val;

private:
bool m_use_locale = false;
};
return os.get();
}

private:
bool m_use_locale = false;
};

namespace detail
{
template <typename T, typename CharT>
struct select_formatter
{
Expand All @@ -1269,7 +1273,7 @@ class basic_format_context
public:
using char_type = CharT;
using iterator = OutputIt;
using format_args_type = basic_dynamic_format_args<basic_format_context, char_type>;
using format_args_type = basic_format_args_ref<basic_format_context, char_type>;

template <typename T>
using formatter_type = detail::select_formatter_t<T, char_type>;
Expand Down Expand Up @@ -1398,7 +1402,7 @@ class basic_format_parse_context
using iterator = const_iterator;
using size_type = std::size_t;
using format_context_type = FormatContext;
using format_args_type = basic_dynamic_format_args<FormatContext, char_type>;
using format_args_type = basic_format_args_ref<FormatContext, char_type>;

basic_format_parse_context() = delete;
basic_format_parse_context(const basic_format_parse_context&) = delete;
Expand Down Expand Up @@ -1492,8 +1496,8 @@ class basic_format_parse_context
PAPILIO_EXPORT using format_parse_context = basic_format_parse_context<format_context>;
PAPILIO_EXPORT using wformat_parse_context = basic_format_parse_context<wformat_context>;

PAPILIO_EXPORT using mutable_format_args = basic_mutable_format_args<format_context>;
PAPILIO_EXPORT using wmutable_format_args = basic_mutable_format_args<wformat_context>;
PAPILIO_EXPORT using dynamic_format_args = basic_dynamic_format_args<format_context>;
PAPILIO_EXPORT using wdynamic_format_args = basic_dynamic_format_args<wformat_context>;

namespace detail
{
Expand Down Expand Up @@ -1527,28 +1531,10 @@ concept formattable = formattable_with<
std::remove_const_t<T>,
basic_format_context<detail::fmt_iter_for<CharT>, CharT>>;

PAPILIO_EXPORT template <typename T>
const void* ptr(T* p) noexcept
{
return p;
}

PAPILIO_EXPORT template <typename T>
const void* ptr(const std::shared_ptr<T>& p) noexcept
{
return p.get();
}

PAPILIO_EXPORT template <typename T, typename D>
const void* ptr(const std::unique_ptr<T, D>& p) noexcept
{
return p.get();
}

PAPILIO_EXPORT template <typename T, typename D>
const void* ptr(const optional_unique_ptr<T, D>& p) noexcept
PAPILIO_EXPORT template <pointer_like T>
const void* ptr(const T& p) noexcept
{
return p.get();
return std::to_address(p);
}
} // namespace papilio

Expand Down
12 changes: 6 additions & 6 deletions include/papilio/format.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace detail
OutputIt out,
locale_ref loc,
std::basic_string_view<CharT> fmt,
const basic_dynamic_format_args<Context>& args
const basic_format_args_ref<Context>& args
)
{
static_assert(std::is_same_v<OutputIt, typename Context::iterator>);
Expand All @@ -59,7 +59,7 @@ namespace detail
}

template <typename OutputIt, typename CharT>
using vfmt_ctx_for = basic_dynamic_format_args<basic_format_context<OutputIt, CharT>>;
using vfmt_ctx_for = basic_format_args_ref<basic_format_context<OutputIt, CharT>>;
} // namespace detail

PAPILIO_EXPORT template <typename OutputIt>
Expand Down Expand Up @@ -246,19 +246,19 @@ namespace detail

PAPILIO_EXPORT
[[nodiscard]]
std::string vformat(std::string_view fmt, const dynamic_format_args& args);
std::string vformat(std::string_view fmt, const format_args_ref& args);

PAPILIO_EXPORT
[[nodiscard]]
std::string vformat(const std::locale& loc, std::string_view fmt, const dynamic_format_args& args);
std::string vformat(const std::locale& loc, std::string_view fmt, const format_args_ref& args);

PAPILIO_EXPORT
[[nodiscard]]
std::wstring vformat(std::wstring_view fmt, const wdynamic_format_args& args);
std::wstring vformat(std::wstring_view fmt, const wformat_args_ref& args);

PAPILIO_EXPORT
[[nodiscard]]
std::wstring vformat(const std::locale& loc, std::wstring_view fmt, const wdynamic_format_args& args);
std::wstring vformat(const std::locale& loc, std::wstring_view fmt, const wformat_args_ref& args);

PAPILIO_EXPORT template <typename OutputIt, typename... Args>
OutputIt format_to(OutputIt out, format_string<Args...> fmt, Args&&... args)
Expand Down
8 changes: 4 additions & 4 deletions src/format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace papilio
{
std::string vformat(
std::string_view fmt, const dynamic_format_args& args
std::string_view fmt, const format_args_ref& args
)
{
std::string result;
Expand All @@ -13,7 +13,7 @@ std::string vformat(
}

std::string vformat(
const std::locale& loc, std::string_view fmt, const dynamic_format_args& args
const std::locale& loc, std::string_view fmt, const format_args_ref& args
)
{
std::string result;
Expand All @@ -23,7 +23,7 @@ std::string vformat(
}

std::wstring vformat(
std::wstring_view fmt, const wdynamic_format_args& args
std::wstring_view fmt, const wformat_args_ref& args
)
{
std::wstring result;
Expand All @@ -33,7 +33,7 @@ std::wstring vformat(
}

std::wstring vformat(
const std::locale& loc, std::wstring_view fmt, const wdynamic_format_args& args
const std::locale& loc, std::wstring_view fmt, const wformat_args_ref& args
)
{
std::wstring result;
Expand Down
28 changes: 14 additions & 14 deletions test/test_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,18 +224,18 @@ TEST(format_arg, access)
}
}

TEST(format_args, mutable)
TEST(format_args, dynamic)
{
using namespace papilio;

{
mutable_format_args args;
dynamic_format_args args;
EXPECT_EQ(args.indexed_size(), 0);
EXPECT_EQ(args.named_size(), 0);
}

{
mutable_format_args args(1, "three"_a = 3, 2);
dynamic_format_args args(1, "three"_a = 3, 2);

EXPECT_EQ(args.indexed_size(), 2);
EXPECT_EQ(args.named_size(), 1);
Expand Down Expand Up @@ -283,24 +283,24 @@ TEST(format_args, static)
}
}

TEST(format_args, dynamic)
TEST(format_args, ref)
{
using namespace papilio;

{
mutable_format_args underlying_fmt_args;
dynamic_format_args dyn_fmt_args(underlying_fmt_args);
dynamic_format_args underlying_fmt_args;
format_args_ref dyn_fmt_args(underlying_fmt_args);

EXPECT_EQ(&dyn_fmt_args.cast_to<mutable_format_args>(), &underlying_fmt_args);
EXPECT_EQ(&dyn_fmt_args.cast_to<dynamic_format_args>(), &underlying_fmt_args);

dynamic_format_args new_dyn_fmt_args(dyn_fmt_args);
format_args_ref new_dyn_fmt_args(dyn_fmt_args);

EXPECT_EQ(&new_dyn_fmt_args.cast_to<mutable_format_args>(), &underlying_fmt_args);
EXPECT_EQ(&new_dyn_fmt_args.cast_to<dynamic_format_args>(), &underlying_fmt_args);
}

{
auto underlying_fmt_args = make_format_args(182375, 182376);
dynamic_format_args dyn_fmt_args(underlying_fmt_args);
format_args_ref dyn_fmt_args(underlying_fmt_args);

EXPECT_EQ(&dyn_fmt_args.cast_to<decltype(underlying_fmt_args)>(), &underlying_fmt_args);
}
Expand All @@ -313,7 +313,7 @@ TEST(format_context, char)
using context_type = basic_format_context<
std::back_insert_iterator<std::string>,
char>;
using args_type = basic_mutable_format_args<context_type>;
using args_type = basic_dynamic_format_args<context_type>;

std::string result;
args_type args;
Expand Down Expand Up @@ -343,7 +343,7 @@ TEST(format_context, wchar_t)
using context_type = basic_format_context<
std::back_insert_iterator<std::wstring>,
wchar_t>;
using args_type = basic_mutable_format_args<context_type>;
using args_type = basic_dynamic_format_args<context_type>;

std::wstring result;
args_type args;
Expand Down Expand Up @@ -371,7 +371,7 @@ TEST(format_parse_context, char)
using namespace papilio;

{
mutable_format_args args;
dynamic_format_args args;
args.push_tuple(0, 1, 2);
args.push("value"_a = 0);

Expand All @@ -396,7 +396,7 @@ TEST(format_parse_context, char)
}

{
mutable_format_args args;
dynamic_format_args args;
args.push_tuple(0, 1, 2);
args.push("value"_a = 0);

Expand Down
Loading

0 comments on commit 3ebfeae

Please sign in to comment.