From 62dc114c11a906d76fa05f62b1c06efdcbb1c1bd Mon Sep 17 00:00:00 2001 From: AWE Henry Date: Fri, 4 Oct 2024 20:35:15 +0800 Subject: [PATCH] Custom format arguments --- include/papilio/core.hpp | 2 +- test/test_core/test_core.cpp | 62 ++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/include/papilio/core.hpp b/include/papilio/core.hpp index 96f93c5..7e9fdbb 100755 --- a/include/papilio/core.hpp +++ b/include/papilio/core.hpp @@ -1805,7 +1805,7 @@ class static_format_args final : public format_args_base }; PAPILIO_EXPORT template -class basic_dynamic_format_args final : public format_args_base +class basic_dynamic_format_args : public format_args_base { using my_base = format_args_base; diff --git a/test/test_core/test_core.cpp b/test/test_core/test_core.cpp index 78b084b..393f4f6 100644 --- a/test/test_core/test_core.cpp +++ b/test/test_core/test_core.cpp @@ -65,6 +65,68 @@ TEST(format_args, static) }(make_format_args(182375, 182376, "name"_a = "scene")); } +namespace test_core +{ +template +class custom_format_args final : public papilio::basic_dynamic_format_args +{ + using my_base = papilio::basic_dynamic_format_args; + +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 args(1, "three"_a = 3, 2); + + EXPECT_EQ(get(args[0]), 1); + EXPECT_EQ(get(args[1]), 2); + EXPECT_EQ(get(args["three"]), 3); + + EXPECT_TRUE(args.contains("argc")); + EXPECT_EQ(get(args["argc"]), 3); + + format_args_ref args_ref(args); + EXPECT_TRUE(args_ref.contains("argc")); + EXPECT_EQ(get(args_ref["argc"]), 3); +} + TEST(format_args, ref) { using namespace papilio;