复合属性对象,当所有子对象都更改时,它禁止多个信号发射
Composite property object that inhibits multiple signal emissions when all subobjects are changed
我有一个简单的property<T>
类,带有一个value_changed<T>
,您可以在调用value_changed<T>::emit(T)
时connect
/disconnect
并接收或禁止事件。想想C++11类固醇上的Qt信号/插槽。
我的下一个挑战是提供一个由子属性组成的类似属性的对象。例如,考虑一个位置或大小,它们都由多个值组成。我希望能够将子对象视为property
,并在同时更改多个值时获得组合信号。例如,做
struct
{
property<int> x;
property<int> y;
}
position2d pos{0,0};
// ...
pos = {1,1}; // this should fire x.value_changed, y.value_changed, and pos.value_changed (once!)
最后这句话是问题的核心。我正在努力编写一个可以使用子对象名称自定义的可重用composite_property
(位置会得到x
,y
,但大小会得到width
/height
)。
请注意,property<struct { int x; int y; }>
是不够的:更改x
不会发出复合value_changed
信号。
我能想到的最好的方法是在分配给超对象时用一堆样板代码来连接/断开子对象,这很乏味并且违背了 DRY 原则。
我对狂野的模板魔术持开放态度,尽管我知道变量的自由命名(x
/y
和width
/height
)至少需要一些样板代码。
编辑为了完整起见,这是我现在拥有的property
模板:
template<typename T>
class property
{
public:
using value_type = T;
using reference = std::add_lvalue_reference_t<T>;
using const_reference = std::add_lvalue_reference_t<std::add_const_t<T>>;
using rvalue_reference = std::add_rvalue_reference_t<T>;
property(const_reference value_ = {}) : value(value_) {}
operator const_reference() const { return value; }
property& operator=(const_reference& other)
{
const bool changed = value != other;
value = other;
if(changed)
value_changed.emit(value);
return *this;
}
bool operator==(const_reference other) const { return value == other; }
bool operator!=(const_reference other) const { return value != other; }
bool operator< (const_reference other) const { return value < other; }
bool operator<=(const_reference other) const { return value <= other; }
bool operator> (const_reference other) const { return value > other; }
bool operator>=(const_reference other) const { return value >= other; }
signal<value_type> value_changed;
private:
value_type value;
};
signal
涉及更多,可在此处获得。基本上,connect
像Qt,除了它返回一个像Boost.Signal这样的connection_type
对象,可以用来disconnect
该连接。
请注意,我对绕过信号的后门"静默修改属性"函数持开放态度,但它只实现了我需要的一半。
由于问题被标记为 c++1z,这里有一个简单的解决方案,它使用了一些闪亮的新 C++17 功能(按照上面评论中讨论的思路):
template<class T, auto... PMs> struct composite_property : property<T>
{
using const_reference = typename property<T>::const_reference;
composite_property(const_reference v = {}) : property<T>(v)
{
(... , (this->value.*PMs).value_changed.connect([this](auto&&)
{
if(listen_to_members) this->value_changed.emit(this->value);
}));
}
composite_property& operator=(const_reference& other)
{
listen_to_members = false;
property<T>::operator=(other);
listen_to_members = true; // not exception-safe, should use RAII to reset
return *this;
}
private:
bool listen_to_members = true;
};
出于纯粹的懒惰,我对你的property<T>
做了一个改变:我已经公开value
。当然,有几种方法可以避免这种情况,但它们与手头的问题无关,所以我选择保持简单。
我们可以用这个玩具的例子来测试解决方案:
struct position2d
{
property<int> x;
property<int> y;
position2d& operator=(const position2d& other)
{
x = other.x.value;
y = other.y.value;
return *this;
}
};
bool operator!=(const position2d& lhs, const position2d& rhs) { return lhs.x.value != rhs.x.value || lhs.y.value != rhs.y.value; }
int main()
{
composite_property<position2d, &position2d::x, &position2d::y> pos = position2d{0, 0};
pos.value.x.value_changed.connect([](int x) { std::cout << " x value changed to " << x << 'n'; });
pos.value.y.value_changed.connect([](int y) { std::cout << " y value changed to " << y << 'n'; });
pos.value_changed.connect([](auto&& p) { std::cout << " pos value changed to {" << p.x << ", " << p.y << "}n"; });
std::cout << "changing xn";
pos.value.x = 7;
std::cout << "changing yn";
pos.value.y = 3;
std::cout << "changing posn";
pos = {3, 7};
}
这是一个包含所有必要定义的实时示例(我的代码在底部)。
虽然必须将成员显式列为composite_property
模板的参数可能会很烦人,但它也提供了相当多的灵活性。例如,我们可以有一个具有三个成员属性的类,并在不同的成员属性对上定义不同的复合属性。包含类不受任何此类影响,也可以独立于任何复合属性工作,成员用作独立属性。
请注意,用户提供的position2d
复制赋值运算符是有原因的:如果我们将其保留为默认值,它将复制成员属性本身,这不会发出信号,而是复制源属性。
该代码在 C++1z 模式下的 Clang 中继上运行。它还会导致 GCC 中继上的 ICE;我们应该尝试减少示例以获得可以在错误报告中提交的内容。
这里工作的关键 C++17 功能是auto
非类型模板参数。在以前的语言版本中,有一些更丑陋的替代方案,例如,将指向成员的指针包装在类似ptr_to_mem<decltype(&position2d::x), &position2d::x>
中,可能使用宏来避免重复。
在composite_property
构造函数的实现中也有一个 over,
的折叠表达式,但这也可以通过初始化虚拟数组来完成(以稍微冗长的方式)。
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- CMake-按正确顺序将项目与C运行时对象文件链接
- 空基优化子对象的地址
- 将对象数组的引用传递给函数
- 你能重载对象变量名本身返回的内容吗
- C++使用整数的压缩数组初始化对象
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 将对象移动到std::shared_ptr
- 代理对象的常量正确性
- 提升 ASIO 无法识别计时器对象
- 将Ref对象作为类成员
- 常量引用函数参数:是否可以禁止临时对象?
- 复合属性对象,当所有子对象都更改时,它禁止多个信号发射
- 如何在将对象传递给 C/C++时禁止 R 垃圾回收
- 禁止用户创建对象,但允许某些类
- 如何禁止获取对象的地址
- 如何禁止从其他类型向临时对象赋值和隐式强制转换
- ISO C++禁止在指向函数的指针和指向对象的指针之间进行转换
- 如何禁止在c++ 2011中调用右值对象的const成员函数?