From 6be63230f1f884e3eec138a9fa020013a1a2ca4b Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 6 Jul 2024 09:25:43 +0800 Subject: [PATCH] update: expose private variables more elegant --- src/core_test/codec/short_code.cc | 8 ++--- src/core_test/ffi/all_cases.cc | 24 ++++++++++++-- src/core_test/utility/exposer.h | 52 ++++++++++++++++++++++--------- 3 files changed, 63 insertions(+), 21 deletions(-) diff --git a/src/core_test/codec/short_code.cc b/src/core_test/codec/short_code.cc index cf2ab22..03e648d 100644 --- a/src/core_test/codec/short_code.cc +++ b/src/core_test/codec/short_code.cc @@ -21,17 +21,17 @@ using klotski::codec::SHORT_CODE_LIMIT; static const auto TEST_THREAD_NUM = 256; /// Forcibly modify private variables to reset state. -FORCIBLY_ACCESS(AllCases, available_, bool) -FORCIBLY_ACCESS(BasicRanges, available_, bool) +//FORCIBLY_ACCESS(AllCases, available_, bool) +//FORCIBLY_ACCESS(BasicRanges, available_, bool) /// Reset basic ranges build state, note it is thread-unsafe. void basic_ranges_reset() { - exposer::BasicRanges_available_(BasicRanges::instance()) = false; +// exposer::BasicRanges_available_(BasicRanges::instance()) = false; } /// Reset all cases build state, note it is thread-unsafe. void all_cases_reset() { - exposer::AllCases_available_(AllCases::instance()) = false; +// exposer::AllCases_available_(AllCases::instance()) = false; } TEST(ShortCode, limit) { diff --git a/src/core_test/ffi/all_cases.cc b/src/core_test/ffi/all_cases.cc index 649234d..81b5a3d 100644 --- a/src/core_test/ffi/all_cases.cc +++ b/src/core_test/ffi/all_cases.cc @@ -16,17 +16,37 @@ using klotski::cases::ALL_CASES_NUM; static constexpr std::string_view ALL_CASES_MD5 = "3888e9fab8d3cbb50908b12b147cfb23"; /// Forcibly modify private variables to reset state. -FORCIBLY_ACCESS(AllCases, available_, bool) -FORCIBLY_ACCESS(BasicRanges, available_, bool) +//FORCIBLY_ACCESS(AllCases, available_, bool) +//FORCIBLY_ACCESS(BasicRanges, available_, bool) + +FORCE_ACCESS_VAR(BasicRanges, available_, bool, Helper_1); +FORCE_ACCESS_VAR(AllCases, available_, bool, Helper_2); + +//namespace exposer { +// +//struct Helper_1 {}; +// +//template struct Exposer; +// +////template<> +//constexpr bool BasicRanges::* fetch(Helper_1); +// +//bool& Demo_val(BasicRanges &c) { +// return c.*fetch(Helper_1{}); +//} +// +//} /// Reset basic ranges build state, note it is thread-unsafe. void basic_ranges_reset() { exposer::BasicRanges_available_(BasicRanges::instance()) = false; +// exposer::BasicRanges_available_(BasicRanges::instance()) = false; } /// Reset all cases build state, note it is thread-unsafe. void all_cases_reset() { exposer::AllCases_available_(AllCases::instance()) = false; +// exposer::AllCases_available_(AllCases::instance()) = false; } TEST(AllCases, all_cases_prebuild) { diff --git a/src/core_test/utility/exposer.h b/src/core_test/utility/exposer.h index 474110f..0923269 100644 --- a/src/core_test/utility/exposer.h +++ b/src/core_test/utility/exposer.h @@ -6,24 +6,36 @@ namespace exposer { -template +// REF: http://bloglitb.blogspot.com/2010/07/access-to-private-members-thats-easy.html + +//template +//struct Exposer { +// static T ptr; +//}; +// +//template +//T Exposer::ptr; +// +//template +//struct ExposerImpl { +// static struct Factory { +// Factory() { Exposer::ptr = Ptr; } +// } factory; +//}; +// +//template +//typename ExposerImpl::Factory ExposerImpl::factory; + +//template +//constexpr T fetch(); + +//template +template struct Exposer { - static T ptr; +// constexpr friend T fetch<>() { return Val; } + constexpr friend T fetch(Unique) { return Val; } }; -template -T Exposer::ptr; - -template -struct ExposerImpl { - static struct Factory { - Factory() { Exposer::ptr = Ptr; } - } factory; -}; - -template -typename ExposerImpl::Factory ExposerImpl::factory; - } // namespace exposer #define FORCIBLY_ACCESS(Class, Member, Type) \ @@ -33,3 +45,13 @@ typename ExposerImpl::Factory ExposerImpl::factory; return T.*exposer::Exposer::ptr; \ } \ } + +#define FORCE_ACCESS_VAR(Class, Member, Type, Unique) \ + namespace exposer { \ + struct Unique {}; \ + template struct Exposer; \ + constexpr Type Class::* fetch(Unique); \ + Type& Class##_##Member(Class &T) { \ + return T.*fetch(Unique{}); \ + } \ + }