使用具有构造函数委托的 SFINAE 通过 class 模板的构造函数一般地应用方法来初始化成员
Applying a method generically to initialize member through a class template's constructor using SFINAE with constructor delegation
我有这个 class 模板,它使用带有构造函数委托的 SFINAE。有 3 种情况可以确定将调用哪个版本的构造函数。
Overall structure of class:
In the first case it is constructing a smaller size from a larger size and can extract a byte, word, or dword from a word, dword or qword by the index value
In the second case it is constructing a larger size from a smaller size and
can set a byte word or dword into word, dword or qword at that index location.
In the third case (default) case it is a 1 to 1 mapping so no calculations nor assertions need to be performed, just save the contents, and the index parameter if passed will have no effect.
Register.h
#pragma once
#include <assert.h>
#include <bitset>
#include <cstdint>
#include <iostream>
#include <iomanip>
#include <limits>
#include <type_traits>
namespace vpc {
using u8 = std::uint8_t;
using u16 = std::uint16_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;
template<typename T>
struct Register {
T data;
T value;
std::bitset<sizeof(T)* CHAR_BIT> bits;
Register() : data{ 0 }, value{ 0 }, bits{ 0 } {}
template<typename P, std::enable_if_t<(sizeof(P) > sizeof(T))>* = nullptr>
Register(const P val, const u8 idx = 0) :
data{ static_cast<T>((val >> std::size(bits) * idx) &
std::numeric_limits<std::make_unsigned_t<T>>::max()) },
value{ data },
bits{ data }
{
constexpr u16 sizeT = sizeof(T);
constexpr u16 sizeP = sizeof(P);
assert((idx >= 0) && (idx <= ((sizeP / sizeT) - 1)) );
}
template<typename P, std::enable_if_t<(sizeof(P) < sizeof(T))>* = nullptr>
Register(const P val, const u8 idx = 0) :
data{ /*static_cast<T>((val >> std::size(bits) * idx) &
std::numeric_limits<std::make_unsigned_t<T>>::max()) },*/
static_cast<T>(val)
},
value{ data },
bits{ data }
{
constexpr u16 sizeT = sizeof(T);
constexpr u16 sizeP = sizeof(P);
assert((idx >= 0) && (idx <= ((sizeT / sizeP) - 1)) );
}
template<typename P, std::enable_if_t<(sizeof(P) == sizeof(T))>* = nullptr>
Register(const P val, const u8 idx = 0) :
// shouldn't need the static cast but I'll leave it here for now
data{ static_cast<T>( val ) }, value{ data }, bits{ data }
{}
template<typename P>
Register(const Register<P>& reg, const u8 idx = 0) : Register(reg.data, idx) {}
};
using Reg8 = Register<u8>;
using Reg16 = Register<u16>;
using Reg32 = Register<u32>;
using Reg64 = Register<u64>;
template<typename T>
std::ostream& operator<<(std::ostream& os, const Register<T>& r) {
return os << "Reg" << std::size(r.bits) << '(' << r.data << ")\nhex: 0x"
<< std::uppercase << std::setfill('0') << std::setw(sizeof(T) * 2) << std::hex
<< r.data << std::dec << "\nbin: "
<< r.bits << "\n\n";
}
template<>
std::ostream& operator<<<u8>(std::ostream& os, const Register<u8>& r) {
return os << "Reg" << std::size(r.bits) << '(' << +r.data << ")\nhex: 0x"
<< std::uppercase << std::setfill('0') << std::setw(sizeof(u8) * 2) << std::hex
<< +r.data << std::dec << "\nbin: "
<< r.bits << "\n\n";
}
} // namespace
如果我们看一下第一种情况 sizeof(P) > sizeof(T)
我们使用 class 的初始化列表来初始化它的成员 data
下面的公式正在对 data
:
data{ static_cast<T>((val >> std::size(bits) * idx) &
std::numeric_limits<std::make_unsigned_t<T>>::max()) }
而在第二种情况下 sizeof(P) < sizeof(T)
它目前被注释掉了。
data{ /*static_cast<T>((val >> std::size(bits) * idx) &
std::numeric_limits<std::make_unsigned_t<T>>::max()) },*/
static_cast<T>(val)
}
我想做的是与上面类似的事情,但我想一般地应用此方法来初始化 data
在这种情况下:
void insertByte(unsigned char a, unsigned int& value, unsigned idx) {
if (idx > 3)
return;
// clear the value at position idx
value &= ~(0xFF << (idx * 8));
unsigned int tmp = a;
tmp = (tmp << (idx * 8));
value |= tmp;
}
上面a
和value
函数中的参数将是模板类型:我的class中的T
和P
。 if 语句将由断言处理。
这也可能有助于理解上面的函数:
unsigned a = (the_int & 0x00ffffff) | (the_byte << 24); // set high-order byte: bits 24-31
unsigned b = (the_int & 0xff00ffff) | (the_byte << 16); // next byte, bits 16-23
unsigned c = (the_int & 0xffff00ff) | (the_byte << 8); // next byte, bits 8-15
unsigned d = (the_int & 0xffffff00) | (the_byte); // low-order byte: bits 0-7
关于如何转换上述函数以适合我的模板以便使用正确的值初始化 data
有什么想法吗?它基本上是第一个 case 构造函数的逆向。
编辑
根据用户的评论:Davis Herring 我将说明我的第二个案例构造函数的概念:
Reg8 r8{ 0xAA };
Reg32 r32a{ r8, 0 };
Reg32 r32b{ r8, 1 };
Reg32 r32c{ r8, 2 };
Reg32 r32d{ r8, 3 };
// Reg32 r32{ r8, 4 }; // assertion failure
// binary output in hex notation:
r8 = 0xAA
r32a = 0x000000AA
r32b = 0x0000AA00
r32c = 0x00AA0000
r32d = 0xAA000000
// Another example
Reg16 r16{ 0xABCD };
Reg32 r32a{ r16, 0 };
Reg32 r32b{ r16, 1 };
// Reg32 r32c{ r16, 2 }; // assertion failure
Reg64 r64a_0{ r32a, 0 };
Reg64 r64a_1{ r32a, 1 };
// Reg64 r64a_2{ r32a, 2 }; // assertion failure
Reg64 r64b_0{ r32b, 0 };
Reg64 r64b_1{ r32b, 1 };
// Reg64 r64b_2{ r32b, 2 }; // assertion failure
Reg64 r64c_0{ r16, 0 };
Reg64 r64c_1{ r16, 1 };
Reg64 r64c_2{ r16, 2 };
Reg64 r64c_3{ r16, 3 };
// Reg64 r64c_4{ r16, 4 }; // assertion failure
// binary output in hex notation:
r16 = 0xABCD
r32a = 0x0000ABCD
r32b = 0xABCD0000
r64a_0 = 0x000000000000ABCD
r64a_1 = 0x0000ABCD00000000
r64b_0 = 0x00000000ABCD0000
r64b_1 = 0xABCD000000000000
r64c_0 = 0x000000000000ABCD
r64c_1 = 0x00000000ABCD0000
r64c_2 = 0x0000ABCD00000000
r64c_3 = 0xABCD000000000000
这是我想要的,从较小的大小构造的任何较大的大小都提供了索引值,如果没有索引,那么它总是设置为从右边开始的最低字节。
编辑
这是我第一次尝试做我打算做的事情,这里我使用的是 lambda 模板。我创建了这个 lambda,它在我的 class 的头文件中,在我的命名空间中使用之后和 class 声明之前。
template<typename P, typename T>
auto wordSize = [](T& t, P& p, const u8 idx) {
p &= ~(0xFF << (idx * 8));
P tmp = static_cast<P>( t );
tmp = (tmp << (idx * 8));
p |= tmp;
return p;
};
现在尝试在我的第二个案例构造函数中使用它:
template<typename P, std::enable_if_t<(sizeof(P) < sizeof(T))>* = nullptr>
explicit Register(P val, const u8 idx = 0) :
data{ static_cast<T>( wordSize<T,P>(val, data, idx ) ) },
value{ data },
bits{ data }
{
constexpr u16 sizeT = sizeof(T);
constexpr u16 sizeP = sizeof(P);
assert((idx >= 0) && (idx <= ((sizeT / sizeP) - 1)) );
}
除此之外,我必须将 <P>
的构造函数参数从 const <P>
更改为 <P>
才能正常工作。现在,当我从较小的类型构造我的 Register 类型时,我将正确的字或字节插入到正确的索引位置,但是其余位没有被 0
初始化。
示例:
Reg8 r8{ 0xAA };
Reg32 r32a{ r8, 0 };
Reg32 r32b{ r8, 1 };
Reg32 r32c{ r8, 2 };
Reg32 r32d{ r8, 3 };
// Expected Binary Output in Hex:
r8 = 0xAA
r32a = 0x000000AA
r32b = 0x0000AA00
r32c = 0x00AA0000
r32d = 0xAA000000
// Actual Outputs:
r8 = 0xAA
r32a = 0xCCCCCCAA
r32b = 0xCCCCAACC
r32c = 0xCCAACCCC
r32d = 0xAACCCCCC
我非常接近实现我的目标,但现在我只需要调整它,以便所有 CC
都是 00
。
要使用任何整数 P val
的移位正确初始化任何整数类型 T
,请使用
data{static_cast<T>(static_cast<T>(val) << sizeof(P)*CHAR_BIT*idx)}
内部转换对于 T
比 int
更宽的移位定义是必要的;外层是抵消 T
窄于 int
的提升所必需的(或者只使用圆括号而不是大括号来允许缩小转换)。
我有这个 class 模板,它使用带有构造函数委托的 SFINAE。有 3 种情况可以确定将调用哪个版本的构造函数。
Overall structure of class:
In the first case it is constructing a smaller size from a larger size and can extract a byte, word, or dword from a word, dword or qword by the index value
In the second case it is constructing a larger size from a smaller size and can set a byte word or dword into word, dword or qword at that index location.
In the third case (default) case it is a 1 to 1 mapping so no calculations nor assertions need to be performed, just save the contents, and the index parameter if passed will have no effect.
Register.h
#pragma once
#include <assert.h>
#include <bitset>
#include <cstdint>
#include <iostream>
#include <iomanip>
#include <limits>
#include <type_traits>
namespace vpc {
using u8 = std::uint8_t;
using u16 = std::uint16_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;
template<typename T>
struct Register {
T data;
T value;
std::bitset<sizeof(T)* CHAR_BIT> bits;
Register() : data{ 0 }, value{ 0 }, bits{ 0 } {}
template<typename P, std::enable_if_t<(sizeof(P) > sizeof(T))>* = nullptr>
Register(const P val, const u8 idx = 0) :
data{ static_cast<T>((val >> std::size(bits) * idx) &
std::numeric_limits<std::make_unsigned_t<T>>::max()) },
value{ data },
bits{ data }
{
constexpr u16 sizeT = sizeof(T);
constexpr u16 sizeP = sizeof(P);
assert((idx >= 0) && (idx <= ((sizeP / sizeT) - 1)) );
}
template<typename P, std::enable_if_t<(sizeof(P) < sizeof(T))>* = nullptr>
Register(const P val, const u8 idx = 0) :
data{ /*static_cast<T>((val >> std::size(bits) * idx) &
std::numeric_limits<std::make_unsigned_t<T>>::max()) },*/
static_cast<T>(val)
},
value{ data },
bits{ data }
{
constexpr u16 sizeT = sizeof(T);
constexpr u16 sizeP = sizeof(P);
assert((idx >= 0) && (idx <= ((sizeT / sizeP) - 1)) );
}
template<typename P, std::enable_if_t<(sizeof(P) == sizeof(T))>* = nullptr>
Register(const P val, const u8 idx = 0) :
// shouldn't need the static cast but I'll leave it here for now
data{ static_cast<T>( val ) }, value{ data }, bits{ data }
{}
template<typename P>
Register(const Register<P>& reg, const u8 idx = 0) : Register(reg.data, idx) {}
};
using Reg8 = Register<u8>;
using Reg16 = Register<u16>;
using Reg32 = Register<u32>;
using Reg64 = Register<u64>;
template<typename T>
std::ostream& operator<<(std::ostream& os, const Register<T>& r) {
return os << "Reg" << std::size(r.bits) << '(' << r.data << ")\nhex: 0x"
<< std::uppercase << std::setfill('0') << std::setw(sizeof(T) * 2) << std::hex
<< r.data << std::dec << "\nbin: "
<< r.bits << "\n\n";
}
template<>
std::ostream& operator<<<u8>(std::ostream& os, const Register<u8>& r) {
return os << "Reg" << std::size(r.bits) << '(' << +r.data << ")\nhex: 0x"
<< std::uppercase << std::setfill('0') << std::setw(sizeof(u8) * 2) << std::hex
<< +r.data << std::dec << "\nbin: "
<< r.bits << "\n\n";
}
} // namespace
如果我们看一下第一种情况 sizeof(P) > sizeof(T)
我们使用 class 的初始化列表来初始化它的成员 data
下面的公式正在对 data
:
data{ static_cast<T>((val >> std::size(bits) * idx) &
std::numeric_limits<std::make_unsigned_t<T>>::max()) }
而在第二种情况下 sizeof(P) < sizeof(T)
它目前被注释掉了。
data{ /*static_cast<T>((val >> std::size(bits) * idx) &
std::numeric_limits<std::make_unsigned_t<T>>::max()) },*/
static_cast<T>(val)
}
我想做的是与上面类似的事情,但我想一般地应用此方法来初始化 data
在这种情况下:
void insertByte(unsigned char a, unsigned int& value, unsigned idx) {
if (idx > 3)
return;
// clear the value at position idx
value &= ~(0xFF << (idx * 8));
unsigned int tmp = a;
tmp = (tmp << (idx * 8));
value |= tmp;
}
上面a
和value
函数中的参数将是模板类型:我的class中的T
和P
。 if 语句将由断言处理。
这也可能有助于理解上面的函数:
unsigned a = (the_int & 0x00ffffff) | (the_byte << 24); // set high-order byte: bits 24-31
unsigned b = (the_int & 0xff00ffff) | (the_byte << 16); // next byte, bits 16-23
unsigned c = (the_int & 0xffff00ff) | (the_byte << 8); // next byte, bits 8-15
unsigned d = (the_int & 0xffffff00) | (the_byte); // low-order byte: bits 0-7
关于如何转换上述函数以适合我的模板以便使用正确的值初始化 data
有什么想法吗?它基本上是第一个 case 构造函数的逆向。
编辑
根据用户的评论:Davis Herring 我将说明我的第二个案例构造函数的概念:
Reg8 r8{ 0xAA };
Reg32 r32a{ r8, 0 };
Reg32 r32b{ r8, 1 };
Reg32 r32c{ r8, 2 };
Reg32 r32d{ r8, 3 };
// Reg32 r32{ r8, 4 }; // assertion failure
// binary output in hex notation:
r8 = 0xAA
r32a = 0x000000AA
r32b = 0x0000AA00
r32c = 0x00AA0000
r32d = 0xAA000000
// Another example
Reg16 r16{ 0xABCD };
Reg32 r32a{ r16, 0 };
Reg32 r32b{ r16, 1 };
// Reg32 r32c{ r16, 2 }; // assertion failure
Reg64 r64a_0{ r32a, 0 };
Reg64 r64a_1{ r32a, 1 };
// Reg64 r64a_2{ r32a, 2 }; // assertion failure
Reg64 r64b_0{ r32b, 0 };
Reg64 r64b_1{ r32b, 1 };
// Reg64 r64b_2{ r32b, 2 }; // assertion failure
Reg64 r64c_0{ r16, 0 };
Reg64 r64c_1{ r16, 1 };
Reg64 r64c_2{ r16, 2 };
Reg64 r64c_3{ r16, 3 };
// Reg64 r64c_4{ r16, 4 }; // assertion failure
// binary output in hex notation:
r16 = 0xABCD
r32a = 0x0000ABCD
r32b = 0xABCD0000
r64a_0 = 0x000000000000ABCD
r64a_1 = 0x0000ABCD00000000
r64b_0 = 0x00000000ABCD0000
r64b_1 = 0xABCD000000000000
r64c_0 = 0x000000000000ABCD
r64c_1 = 0x00000000ABCD0000
r64c_2 = 0x0000ABCD00000000
r64c_3 = 0xABCD000000000000
这是我想要的,从较小的大小构造的任何较大的大小都提供了索引值,如果没有索引,那么它总是设置为从右边开始的最低字节。
编辑
这是我第一次尝试做我打算做的事情,这里我使用的是 lambda 模板。我创建了这个 lambda,它在我的 class 的头文件中,在我的命名空间中使用之后和 class 声明之前。
template<typename P, typename T>
auto wordSize = [](T& t, P& p, const u8 idx) {
p &= ~(0xFF << (idx * 8));
P tmp = static_cast<P>( t );
tmp = (tmp << (idx * 8));
p |= tmp;
return p;
};
现在尝试在我的第二个案例构造函数中使用它:
template<typename P, std::enable_if_t<(sizeof(P) < sizeof(T))>* = nullptr>
explicit Register(P val, const u8 idx = 0) :
data{ static_cast<T>( wordSize<T,P>(val, data, idx ) ) },
value{ data },
bits{ data }
{
constexpr u16 sizeT = sizeof(T);
constexpr u16 sizeP = sizeof(P);
assert((idx >= 0) && (idx <= ((sizeT / sizeP) - 1)) );
}
除此之外,我必须将 <P>
的构造函数参数从 const <P>
更改为 <P>
才能正常工作。现在,当我从较小的类型构造我的 Register 类型时,我将正确的字或字节插入到正确的索引位置,但是其余位没有被 0
初始化。
示例:
Reg8 r8{ 0xAA };
Reg32 r32a{ r8, 0 };
Reg32 r32b{ r8, 1 };
Reg32 r32c{ r8, 2 };
Reg32 r32d{ r8, 3 };
// Expected Binary Output in Hex:
r8 = 0xAA
r32a = 0x000000AA
r32b = 0x0000AA00
r32c = 0x00AA0000
r32d = 0xAA000000
// Actual Outputs:
r8 = 0xAA
r32a = 0xCCCCCCAA
r32b = 0xCCCCAACC
r32c = 0xCCAACCCC
r32d = 0xAACCCCCC
我非常接近实现我的目标,但现在我只需要调整它,以便所有 CC
都是 00
。
要使用任何整数 P val
的移位正确初始化任何整数类型 T
,请使用
data{static_cast<T>(static_cast<T>(val) << sizeof(P)*CHAR_BIT*idx)}
内部转换对于 T
比 int
更宽的移位定义是必要的;外层是抵消 T
窄于 int
的提升所必需的(或者只使用圆括号而不是大括号来允许缩小转换)。