C
christian2.schmidt
Hi,
I'm trying to wrap an IList-instance in a native template class with a
"vector"-like interface. cliext::vector seems not suited, as it copies
the list - correct me if I'm wrong.
template <typename T>
class ClrVector {
gcroot<IList<T>^> list;
public:
ClrVector(IList<T>^ _list) : list (_list) { }
const T% operator[] (size_t i) const { return list->default; }
T% operator[] (size_t i) { return list->default; }
// ...
};
My problem is that the non-const operator[] returns a reference to a
temporary copy of the list's item. So assignments vec=x do not
work. For that I would have to call the setter of the list's default
property.
I tried a T%-wrapper for a property, by using getter and setter
delegates - but without success:
template <typename T>
ref class ReferenceWrapper
{
delegate T Getter();
delegate void Setter(T);
Getter^ getter;
Setter^ setter;
public:
ReferenceWrapper(Getter^ _getter, Setter^ _setter)
: getter (_getter), setter (_setter) { }
operator T() { return getter(); }
ReferenceWrapper<T> operator=(const T& other) {
setter(other);
return *this;
}
};
// class ClrVector<T>.operator[]:
ReferenceWrapper<T> operator[] (size_t i) {
// error:
}
I tried a native class, too, but delegates can only be declared in ref
classes.
Does anyone have an idea how to continue?
Thanks,
Christian
I'm trying to wrap an IList-instance in a native template class with a
"vector"-like interface. cliext::vector seems not suited, as it copies
the list - correct me if I'm wrong.
template <typename T>
class ClrVector {
gcroot<IList<T>^> list;
public:
ClrVector(IList<T>^ _list) : list (_list) { }
const T% operator[] (size_t i) const { return list->default; }
T% operator[] (size_t i) { return list->default; }
// ...
};
My problem is that the non-const operator[] returns a reference to a
temporary copy of the list's item. So assignments vec=x do not
work. For that I would have to call the setter of the list's default
property.
I tried a T%-wrapper for a property, by using getter and setter
delegates - but without success:
template <typename T>
ref class ReferenceWrapper
{
delegate T Getter();
delegate void Setter(T);
Getter^ getter;
Setter^ setter;
public:
ReferenceWrapper(Getter^ _getter, Setter^ _setter)
: getter (_getter), setter (_setter) { }
operator T() { return getter(); }
ReferenceWrapper<T> operator=(const T& other) {
setter(other);
return *this;
}
};
// class ClrVector<T>.operator[]:
ReferenceWrapper<T> operator[] (size_t i) {
// error:
return ReferenceWrapper said:default::set);
}
I tried a native class, too, but delegates can only be declared in ref
classes.
Does anyone have an idea how to continue?
Thanks,
Christian