【发布时间】:2016-04-23 19:33:54
【问题描述】:
我在this article 中找到了一些不错的属性模板。
我希望他们支持完美转发,但我不喜欢我目前的解决方案:
// a read-write property which invokes user-defined functions
template <class Type, class Object, const Type&(Object::*real_getter)() const, const Type&(Object::*real_setter)(const Type&), const Type&(Object::*real_mover)(Type&&) = nullptr>
class UnrestrictedProperty {
Object* object;
public:
UnrestrictedProperty(Object* owner_object = nullptr) : object(owner_object) {}
// initializer to specify the owner
void operator()(Object* owner_object) {
this->object = owner_object;
}
// function call syntax
const Type& operator()() const {
return (object->*real_getter)();
}
const Type& operator()(const Type& value) {
return (object->*real_setter)(value);
}
const Type& operator()(Type&& value) {
if (real_mover) return (object->*real_mover)(std::forward<Type>(value));
return (object->*real_setter)(value);
}
// get/set syntax
const Type& get() const {
return (object->*real_getter)();
}
const Type& set(const Type& value) {
return (object->*real_setter)(value);
}
const Type& set(Type&& value) {
if (real_mover) return (object->*real_mover)(std::forward<Type>(value));
return (object->*real_setter)(value);
}
// access with '=' sign
operator const Type&() const {
return (object->*real_getter)();
}
const Type& operator=(const Type& value) {
return (object->*real_setter)(value);
}
const Type& operator=(Type&& value) {
if (real_mover) return (object->*real_mover)(std::forward<Type>(value));
return (object->*real_setter)(value);
}
// comparators
bool operator==(const Type& value) const {
return get() == value;
}
bool operator!=(const Type& value) const {
return not operator==(value);
}
// might be useful for template deductions
typedef Type value_type;
};
// << operator for UnrestrictedProperty
template <class Type, class Object, const Type&(Object::*real_getter)() const, const Type&(Object::*real_setter)(const Type&), const Type&(Object::*real_mover)(Type&&)>
std::ostream& operator<<(std::ostream& output, const UnrestrictedProperty<Type, Object, real_getter, real_setter, real_mover> unrestricted_property) {
return (output << unrestricted_property.get());
}
用法:
// test type that logs constructor, destructor and assignment operator calls
struct TestType {
TestType() { trace("[", this, "] constructor"); }
~TestType() { trace("[", this, "] destructor"); }
TestType(const TestType& other) {
trace("[", this, "] copy constructor copying ", other);
}
TestType& operator=(const TestType& other) {
debug("[", this, "] copy assignment operator");
TestType temporary(other);
trace("[", this, "] swap data with ", temporary);
return *this;
}
TestType& operator=(TestType&& other) {
debug("[", this, "] move assignment operator");
trace("[", this, "] swap data with ", other);
return *this;
}
};
// << operator for TestType
std::ostream& operator<<(std::ostream& output, const TestType& value) {
return (output << "[" << &value << "]");
}
// test object containing an UnrestrictedProperty with custom getter and setter methods
class TestObject {
TestType internal_value;
const TestType& get_value() const {
return internal_value;
}
const TestType& set_value(const TestType& value) {
return (internal_value = value);
}
const TestType& set_value(TestType&& value) {
return (internal_value = std::move(value));
}
public:
// create an UnrestrictedProperty for a TestType value-type in the TestObject class
UnrestrictedProperty<TestType, TestObject, &TestObject::get_value, &TestObject::set_value/*, &TestObject::set_value*/> value;
// (the thrid (commented out) function pointer is the rvalue version of the setter)
TestObject() {
// tell the value property on which object instance it should call the getter and setter methods
value(this);
}
};
void test() {
print("property test starts");
{
print("creating object");
TestObject object;
print("assigning object.value with rvalue");
{
object.value = TestType();
}
print("assigning object.value with lvalue");
{
TestType local = TestType();
object.value = local;
}
print("leaving objects scope");
}
print("property test ends");
}
没有指定 rvalue-setter 的输出(注释掉):
[ ] 属性测试开始 [] 创建对象 [T] [0026F22C] 构造函数 [ ] 用右值分配 object.value [T] [0026F157] 构造函数 [D] [0026F22C] 复制赋值运算符 [T] [0026EF43] 复制构造函数复制 [0026F157] [T] [0026F22C] 与 [0026EF43] 交换数据 [T] [0026EF43] 析构函数 [T] [0026F157] 析构函数 [ ] 用左值分配 object.value [T] [0026F223] 构造函数 [D] [0026F22C] 复制赋值运算符 [T] [0026EF43] 复制构造函数复制 [0026F223] [T] [0026F22C] 与 [0026EF43] 交换数据 [T] [0026EF43] 析构函数 [T] [0026F223] 析构函数 [ ] 离开对象范围 [T] [0026F22C] 析构函数 [ ] 属性测试结束具有指定 rvalue-setter 的输出:
[ ] 属性测试开始 [] 创建对象 [T] [0015F7EC] 构造函数 [ ] 用右值分配 object.value [T] [0015F717] 构造函数 [D] [0015F7EC] 移动赋值运算符 [T] [0015F7EC] 与 [0015F717] 交换数据 [T] [0015F717] 析构函数 [ ] 用左值分配 object.value [T] [0015F7E3] 构造函数 [D] [0015F7EC] 复制赋值运算符 [T] [0015F503] 复制构造函数复制 [0015F7E3] [T] [0015F7EC] 与 [0015F503] 交换数据 [T] [0015F503] 析构函数 [T] [0015F7E3] 析构函数 [ ] 离开对象范围 [T] [0015F7EC] 析构函数 [ ] 属性测试结束所以……
它按预期工作,但我必须单独传递右值设置器的指针,并且每次传递右值时都必须检查它是否为空。
有什么建议吗?
【问题讨论】:
标签: c++ templates function-pointers perfect-forwarding