Skip to content

Commit

Permalink
Custom format arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
HenryAWE committed Oct 4, 2024
1 parent cb52b76 commit 62dc114
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
2 changes: 1 addition & 1 deletion include/papilio/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1805,7 +1805,7 @@ class static_format_args final : public format_args_base<Context, CharT>
};

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

Expand Down
62 changes: 62 additions & 0 deletions test/test_core/test_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,68 @@ TEST(format_args, static)
}(make_format_args(182375, 182376, "name"_a = "scene"));
}

namespace test_core
{
template <typename Context>
class custom_format_args final : public papilio::basic_dynamic_format_args<Context>
{
using my_base = papilio::basic_dynamic_format_args<Context>;

public:
using char_type = typename Context::char_type;
using string_type = typename my_base::string_type;
using string_view_type = typename my_base::string_view_type;
using format_arg_type = typename my_base::format_arg_type;

using my_base::my_base;

bool contains(string_view_type key) const noexcept override
{
if(key == PAPILIO_TSTRING_VIEW(char_type, "argc"))
return true;

return my_base::contains(key);
}

const format_arg_type& get(string_view_type key) const override
{
if(key == PAPILIO_TSTRING_VIEW(char_type, "argc"))
{
update_argc();
return m_argc;
}

return my_base::get(key);
}

private:
mutable format_arg_type m_argc;

void update_argc() const
{
m_argc = format_arg_type(this->indexed_size() + this->named_size());
}
};
} // namespace test_core

TEST(format_args, custom)
{
using namespace papilio;

test_core::custom_format_args<format_context> args(1, "three"_a = 3, 2);

EXPECT_EQ(get<int>(args[0]), 1);
EXPECT_EQ(get<int>(args[1]), 2);
EXPECT_EQ(get<int>(args["three"]), 3);

EXPECT_TRUE(args.contains("argc"));
EXPECT_EQ(get<std::size_t>(args["argc"]), 3);

format_args_ref args_ref(args);
EXPECT_TRUE(args_ref.contains("argc"));
EXPECT_EQ(get<std::size_t>(args_ref["argc"]), 3);
}

TEST(format_args, ref)
{
using namespace papilio;
Expand Down

0 comments on commit 62dc114

Please sign in to comment.