Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change BitfieldExtract to use a pointer to the bitfield member
- Loading branch information
Showing
8 changed files
with
164 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright 2021 Dolphin Emulator Project | ||
// Licensed under GPLv2+ | ||
// Refer to the license.txt file included. | ||
|
||
#pragma once | ||
|
||
#include <type_traits> | ||
|
||
namespace Common | ||
{ | ||
template <typename> | ||
struct MemberPointerInfo; | ||
// Helper to get information about a pointer to a data member. | ||
// See https://en.cppreference.com/w/cpp/language/pointer#Pointers_to_members | ||
// This template takes the type for a member pointer. | ||
template <typename M, typename O> | ||
struct MemberPointerInfo<M O::*> | ||
{ | ||
using MemberType = M; | ||
using ObjectType = O; | ||
}; | ||
|
||
// This template takes a specific member pointer. | ||
template <auto member_pointer> | ||
using MemberType = typename MemberPointerInfo<decltype(member_pointer)>::MemberType; | ||
|
||
// This template takes a specific member pointer. | ||
template <auto member_pointer> | ||
using ObjectType = typename MemberPointerInfo<decltype(member_pointer)>::ObjectType; | ||
|
||
namespace detail | ||
{ | ||
template <int x> | ||
struct Data | ||
{ | ||
static constexpr int GetX() { return x; } | ||
}; | ||
struct Foo | ||
{ | ||
Data<1> a; | ||
Data<2> b; | ||
int c; | ||
}; | ||
struct Bar : Foo | ||
{ | ||
int d; | ||
}; | ||
|
||
static_assert(std::is_same_v<MemberType<&Foo::a>, Data<1>>); | ||
static_assert(MemberType<&Foo::a>::GetX() == 1); | ||
static_assert(std::is_same_v<MemberType<&Foo::b>, Data<2>>); | ||
static_assert(MemberType<&Foo::b>::GetX() == 2); | ||
static_assert(std::is_same_v<MemberType<&Foo::c>, int>); | ||
|
||
static_assert(std::is_same_v<ObjectType<&Foo::a>, Foo>); | ||
static_assert(std::is_same_v<ObjectType<&Foo::b>, Foo>); | ||
static_assert(std::is_same_v<ObjectType<&Foo::c>, Foo>); | ||
|
||
static_assert(std::is_same_v<MemberType<&Foo::c>, MemberPointerInfo<int Foo::*>::MemberType>); | ||
static_assert(std::is_same_v<ObjectType<&Foo::c>, MemberPointerInfo<int Foo::*>::ObjectType>); | ||
|
||
static_assert(std::is_same_v<MemberType<&Bar::c>, int>); | ||
static_assert(std::is_same_v<MemberType<&Bar::d>, int>); | ||
|
||
static_assert(std::is_same_v<ObjectType<&Bar::d>, Bar>); | ||
// Somewhat unexpected behavior: | ||
static_assert(std::is_same_v<ObjectType<&Bar::c>, Foo>); | ||
static_assert(!std::is_same_v<ObjectType<&Bar::c>, Bar>); | ||
} // namespace detail | ||
} // namespace Common |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.