如何使用BOOST_HANA_ADAPT_ADT定义获取/设置对
How to define get/set pair with BOOST_HANA_ADAPT_ADT?
我想反省第三方 ADT,它定义了访问类"属性"的 getter/setter 对。例如:
struct Echo {
float mix; // read-only, don't ask why.
void set_mix(float mix);
};
我想写:
BOOST_HANA_ADAPT_ADT(Echo,
(mix,
[] (const auto& self) { return self.mix; },
[] (auto& self, float x) { self.set_mix(x); })
);
这可能吗?
我不确定您要做什么,但是您能否像这样使用虚拟类型做点什么:
#include "boost/hana.hpp"
struct Echo {
float mix; // read-only, don't ask why.
void set_mix(float mix);
};
//Getter and setter functionality moved into here
struct FakeType
{
Echo* const inner;
FakeType(Echo* inner) : inner(inner){}
operator float() { return inner->mix; }
FakeType& operator=(const float& value) {
inner->set_mix(value);
return *this;
}
};
BOOST_HANA_ADAPT_ADT(Echo,
//Now returns a "FakeType" instead of the float directly
(mix, [](Echo& p) { return FakeType(&p); })
);
FakeType
类处理所有"getter"和"setter"类型的东西.....
相关文章:
- 如何获取进程以设置其亲和力?
- 如何获取控制面板设置值,如字体大小
- 获取和设置函数 c++
- 适用于 macOS 的 Xcode 应用程序。这就是我设置从USB麦克风输入获取音频的方式。一年前工作,现在没有了。为什么
- 在 C++ 中将函数获取和设置为虚拟函数
- Qt 原子 int 获取并立即设置
- 如何使用C++获取/设置OBS中的垂直滚动过滤器属性?
- 如何使用BOOST_HANA_ADAPT_ADT定义获取/设置对
- 如何直接从 FILETIME 结构获取/设置和获取秒数
- 如何获取设置位中的相对位置
- 获取设置故障位的流位置 / std::ios::抛出故障
- 在运行时获取/设置模板结构的静态成员
- 为什么获取/设置Auto_Ptr会导致此编译器错误
- 构造函数在哪里获取/设置默认分配器
- 在WinCE和Linux上获取/设置SystemTimeAdjustment
- 获取/设置功能以更改文本和颜色
- 运算符[] C++获取/设置
- Qt:从应用程序/可执行目录获取设置ini文件的问题
- 如何获取/设置字符数组
- 如何获取设置输入文件路径运行时