Browse Source

perf: enhance exposed functions

legacy
Dnomd343 6 months ago
parent
commit
53092d3c9b
  1. 4
      src/core_test/cases/cases_helper.h
  2. 8
      src/core_test/codec/short_code.cc
  3. 24
      src/core_test/ffi/all_cases.cc
  4. 62
      src/core_test/utility/exposer.h

4
src/core_test/cases/cases_helper.h

@ -31,8 +31,8 @@ protected:
}; };
/// Forcibly modify private variables to reset state. /// Forcibly modify private variables to reset state.
FORCIBLY_ACCESS(AllCases, available_, bool) FORCE_ACCESS_VAR(AllCases, bool, available_)
FORCIBLY_ACCESS(BasicRanges, available_, bool) FORCE_ACCESS_VAR(BasicRanges, bool, available_)
/// Test fixture macro with custom test suite name. /// Test fixture macro with custom test suite name.
#define TEST_FF(test_suite_name, test_name) \ #define TEST_FF(test_suite_name, test_name) \

8
src/core_test/codec/short_code.cc

@ -21,17 +21,17 @@ using klotski::codec::SHORT_CODE_LIMIT;
static const auto TEST_THREAD_NUM = 256; static const auto TEST_THREAD_NUM = 256;
/// Forcibly modify private variables to reset state. /// Forcibly modify private variables to reset state.
//FORCIBLY_ACCESS(AllCases, available_, bool) FORCE_ACCESS_VAR(AllCases, bool, available_)
//FORCIBLY_ACCESS(BasicRanges, available_, bool) FORCE_ACCESS_VAR(BasicRanges, bool, available_)
/// Reset basic ranges build state, note it is thread-unsafe. /// Reset basic ranges build state, note it is thread-unsafe.
void basic_ranges_reset() { 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. /// Reset all cases build state, note it is thread-unsafe.
void all_cases_reset() { void all_cases_reset() {
// exposer::AllCases_available_(AllCases::instance()) = false; exposer::AllCases_available_(AllCases::instance()) = false;
} }
TEST(ShortCode, limit) { TEST(ShortCode, limit) {

24
src/core_test/ffi/all_cases.cc

@ -16,37 +16,17 @@ using klotski::cases::ALL_CASES_NUM;
static constexpr std::string_view ALL_CASES_MD5 = "3888e9fab8d3cbb50908b12b147cfb23"; static constexpr std::string_view ALL_CASES_MD5 = "3888e9fab8d3cbb50908b12b147cfb23";
/// Forcibly modify private variables to reset state. /// Forcibly modify private variables to reset state.
//FORCIBLY_ACCESS(AllCases, available_, bool) FORCE_ACCESS_VAR(AllCases, bool, available_)
//FORCIBLY_ACCESS(BasicRanges, available_, bool) FORCE_ACCESS_VAR(BasicRanges, bool, available_)
FORCE_ACCESS_VAR(BasicRanges, available_, bool, Helper_1);
FORCE_ACCESS_VAR(AllCases, available_, bool, Helper_2);
//namespace exposer {
//
//struct Helper_1 {};
//
//template struct Exposer<bool (BasicRanges::*), &BasicRanges::available_, Helper_1>;
//
////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. /// Reset basic ranges build state, note it is thread-unsafe.
void basic_ranges_reset() { void basic_ranges_reset() {
exposer::BasicRanges_available_(BasicRanges::instance()) = false; exposer::BasicRanges_available_(BasicRanges::instance()) = false;
// exposer::BasicRanges_available_(BasicRanges::instance()) = false;
} }
/// Reset all cases build state, note it is thread-unsafe. /// Reset all cases build state, note it is thread-unsafe.
void all_cases_reset() { void all_cases_reset() {
exposer::AllCases_available_(AllCases::instance()) = false; exposer::AllCases_available_(AllCases::instance()) = false;
// exposer::AllCases_available_(AllCases::instance()) = false;
} }
TEST(AllCases, all_cases_prebuild) { TEST(AllCases, all_cases_prebuild) {

62
src/core_test/utility/exposer.h

@ -6,52 +6,34 @@
namespace exposer { namespace exposer {
// REF: http://bloglitb.blogspot.com/2010/07/access-to-private-members-thats-easy.html template <typename T, T Val, typename Tag>
//template <typename T>
//struct Exposer {
// static T ptr;
//};
//
//template <typename T>
//T Exposer<T>::ptr;
//
//template <typename T, T Ptr>
//struct ExposerImpl {
// static struct Factory {
// Factory() { Exposer<T>::ptr = Ptr; }
// } factory;
//};
//
//template <typename T, T Ptr>
//typename ExposerImpl<T, Ptr>::Factory ExposerImpl<T, Ptr>::factory;
//template <typename T>
//constexpr T fetch();
//template <typename T, T Val, int Flag>
template <typename T, T Val, typename Unique>
struct Exposer { struct Exposer {
// constexpr friend T fetch<>() { return Val; } constexpr friend T fetch(Tag) { return Val; }
constexpr friend T fetch(Unique) { return Val; }
}; };
} // namespace exposer } // namespace exposer
#define FORCIBLY_ACCESS(Class, Member, Type) \ #define COMBINE_IMPL(x, y) x##y
namespace exposer { \
template struct ExposerImpl<decltype(&Class::Member), &Class::Member>; \ #define COMBINE(x, y) COMBINE_IMPL(x, y)
inline auto& Class##_##Member(Class &T) { \
return T.*exposer::Exposer<Type Class::*>::ptr; \ #define UNIQUE_TAG COMBINE(Tag, __COUNTER__)
} \
}
#define FORCE_ACCESS_VAR(Class, Member, Type, Unique) \ #define FORCE_ACCESS_VAR_IMPL(Class, Type, Member, Tag) \
namespace exposer { \ namespace exposer { \
struct Unique {}; \ struct Tag {}; \
template struct Exposer<Type (Class::*), &Class::Member, Unique>; \ template struct Exposer<Type(Class::*), &Class::Member, Tag>; \
constexpr Type Class::* fetch(Unique); \ constexpr Type Class::* fetch(Tag); \
Type& Class##_##Member(Class &T) { \ constexpr Type& Class##_##Member(Class &c) { \
return T.*fetch(Unique{}); \ return c.*fetch(Tag{}); \
} \
constexpr const Type& Class##_##Member(const Class &c) { \
return c.*fetch(Tag{}); \
} \
constexpr Type Class##_##Member(Class &&c) { \
return c.*fetch(Tag{}); \
} \ } \
} }
#define FORCE_ACCESS_VAR(Class, Type, Member) \
FORCE_ACCESS_VAR_IMPL(Class, Type, Member, UNIQUE_TAG)

Loading…
Cancel
Save