- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有这个类模板,它使用带构造函数委托(delegate)的 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)
我们使用类的初始化列表来初始化它的成员 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
将是模板类型: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,它位于我的类的头文件中,在我的命名空间中使用之后和类声明之前。
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)}
对于比 int
更宽的 T
,要很好地定义移位,内部转换是必要的;外部的是抵消 T
比 int
更窄的提升所必需的(或者只使用圆括号而不是大括号来允许缩小转换)。
关于c++ - 使用带构造函数委托(delegate)的 SFINAE 通过类模板的构造函数一般地应用方法来初始化成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56195409/
我有一个加载 View 的 View ,需要将 View 推送到主导航 Controller 。 我已经为每个 View 设置了一个委托(delegate),并且基本上使我的调用沿着“链”返回到主导航
我通过 NSURLConnction 从服务器获取数据,并希望从获取的数组中填充表格 View 。数据出现在 NSURLConnection 委托(delegate)方法的日志中,但我意识到 numb
我已经将我用作标题 View 的 View 子类化,它里面有一些按钮委托(delegate),它工作得很好。 但是,我在 viewController 上方展示了一个 modalViewControl
我希望这听起来像是一个显而易见的问题,但是委托(delegate)返回类型是否也必须与其委托(delegate)的方法的返回类型相匹配? EG,像这样: public static void Save
我想使用 Kotlin 委托(delegate),但我不想在委托(delegate)人之外创建委托(delegate)。委托(delegate)的所有示例都如下所示: interface Worker
我有一个问题: - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInf
伙计们。我正在寻求帮助。这似乎是一个非常简单的任务,但我可以解决一整天。我正在尝试使用容器 View 创建侧面菜单。当用户按下“更多”按钮(barButtonItem)时,整个 View 向右滑动并出
我正在努力将 UIWebView 迁移到 WKWebView。我已经更改了所有委托(delegate)方法。我需要 WKWebView 委托(delegate)方法等于下面的 UIWebView 委托
我正在学习 VB.NET 中的委托(delegate),但对委托(delegate)类型感到困惑。在阅读有关委托(delegate)的内容时,我了解到委托(delegate)是一种数据类型,可以引用具
我有一个用 Objetive-C 构建的框架。该框架用于连接蓝牙设备并与之交互。 在演示代码中,Objetive-C 委托(delegate)函数如下所示。演示代码由框架创建者提供。 -(void)b
//NewCharts.h #import @interface NewCharts : UIViewController @property(nonatomic,retain)IBOutlet U
我正在努力了解 async/await 并认为我确实了解有关用法的一些事情。但仍然不太清楚在下面的场景中实际好处是什么。 查看 Task.Run 用法。第一种方法使用普通委托(delegate)并使用
我已经尝试了在我的网站上使用 openID 委托(delegate)的所有可能选项,但没有一个方法对我有用。 我的 HTML 文件的 head 部分有“link rel”标签。 我在 HTML 文件的
如何快速创建一个委托(delegate),即 NSUserNotificationCenterDelegate? 最佳答案 这里有一些关于两个 View Controller 之间委托(delegat
我有一个带有数据源的 NSComboBox,当您单击三角形并通过单击它选择其中一个项目时,它可以完美运行。但是,我还希望它允许用户在框中键入以使用自动完成来选择名称。目前,当用户键入时,我希望选择的项
我在代码中定义了以下类和委托(delegate)(为简洁起见,剪掉了许多其他行)。 Public Delegate Sub TimerCallback(canceled As Boolean) Pub
我使用 ansible 2.1 并且我想使用 delegate_to 向一组主机运行命令。我使用 localhost 作为主机参数,并且我想将“touch”命令委托(delegate)给两个 cls
我想通过重载为我的类实现几个构造函数。据我了解,遵循 DRY 原则的惯用方式是使用一种称为委托(delegate)构造函数的功能。我也看到了关于在任何地方使用引用参数并不惜一切代价避免使用指针的想法,
代表们会导致内存泄漏吗? 我的意思是,例如如果一个类A包含一个ADelegate,并且后者指向BMethod(属于B类),这可以防止GC收集A类或B类吗? 如果是这样,我们如何“释放”代表(设置ADe
委托(delegate)命令和路由命令有什么区别? 我读了一些文章说在 MVVM 上使用委托(delegate)命令而不是路由命令。 那么当我们使用 MVVM 时,Delegate Command 相
我是一名优秀的程序员,十分优秀!