C# Properties in C++
Posted: Wed Jul 25, 2007 6:49 am
Hi all,
I have been busy with C# programming at work and found out that i sort of like Properties but standard C++ is lacking this feature. So i set out to do some coding and achieved the following scalar property template don't complain about formatting because that easy to change
how is this helpfull one might wonder, but think about queues and such. Lets use a fixed size fifo. albeit a very simple one .
now we can use the fifo as a property, so lets consider the following program:
it stores the values 0 - 9 in the queue and gets them from the queue again. so the outcome of this program is: 0 1 2 3 4 5 6 7 8 9. nice huh no more adding and removing functions just using the elements of queue.
creating a LIFO like :
and using the same code snippet except with using the LIFO instead of the FIFO;
yields the result: 9 8 7 6 5 4 3 2 1 0. the metod of using the LIFO or FIFO is quite the same. You might wonder how efficient the code might be so here is the object dump of the FIFO example and it shows that is very ptiomised:
hope you find this usefull and am looking forward to your remark etc.
I have been busy with C# programming at work and found out that i sort of like Properties but standard C++ is lacking this feature. So i set out to do some coding and achieved the following scalar property template don't complain about formatting because that easy to change
Code: Select all
template <typename T>
class Property {
public:
explicit Property<T>() : _value() {}
explicit Property<T>(const T & data) : _value(data) {set(data);}
Property<T>(const Property<T> & rhs) {set(rhs._value);}
const T & operator = (const T & data) {set(data);}
const Property<T> & operator = (const Property<T> & rhs) { set(rhs._value); return(*this); }
operator T () { return(get()); }
template <typename X>
void operator = (const X & rhs) {
_value = rhs;
}
template <typename X>
operator X () {
return((X)_value);
}
protected:
virtual const T & get(void){return(_value);}
virtual void set(const T & data){_value = data;}
private:
T _value;
};
Code: Select all
template <typename T, int N>
class FIFO {
T _buffer[N];
int _indexSet, _indexGet;
public:
FIFO() : _indexGet(0), _indexSet(0){}
void operator = (const T & data) {
_buffer[_indexSet++] = data;
}
operator T () {
return(_buffer[_indexGet++]);
}
};
Code: Select all
Property<FIFO<int, 10> > fifo;
for(int i =0; i < 10; i++) fifo = i;
for(int i =0; i < 10; i++) printf("%d ", (int)fifo);
creating a LIFO like :
Code: Select all
template <typename T, int N>
class LIFO {
T _buffer[N];
int _index;
public:
LIFO() : _index(0) {}
void operator = (const T & data) {
_buffer[_index++] = data;
}
operator T () {
return(_buffer[_index--]);
}
};
Code: Select all
Property<LIFO<int, 10> > lifo;
for(int i =0; i < 10; i++) lifo = i;
for(int i =0; i < 10; i++) printf("%d ", (int)lifo);
Code: Select all
Property<FIFO<int, 10> > fifo;
for(int i =0; i < 10; i++) fifo = i;
401264: 31 d2 xor %edx,%edx
401266: 89 8d 58 ff ff ff mov %ecx,0xffffff58(%ebp)
40126c: c7 45 88 00 00 00 00 movl $0x0,0xffffff88(%ebp)
401273: 8d b6 00 00 00 00 lea 0x0(%esi),%esi
401279: 8d bc 27 00 00 00 00 lea 0x0(%edi),%edi
401280: 8b 5d 84 mov 0xffffff84(%ebp),%ebx
401283: 8d 4a 01 lea 0x1(%edx),%ecx
401286: 89 14 9e mov %edx,(%esi,%ebx,4)
401289: 43 inc %ebx
40128a: 83 f9 09 cmp $0x9,%ecx
40128d: 89 5d 84 mov %ebx,0xffffff84(%ebp)
401290: 89 ca mov %ecx,%edx
401292: 7e ec jle 401280 <_main+0x230>
401294: bb 09 00 00 00 mov $0x9,%ebx
401299: 8d b4 26 00 00 00 00 lea 0x0(%esi),%esi
4012a0: 8b 45 88 mov 0xffffff88(%ebp),%eax
4012a3: 8b 14 86 mov (%esi,%eax,4),%edx
4012a6: 8d 48 01 lea 0x1(%eax),%ecx
4012a9: 89 4d 88 mov %ecx,0xffffff88(%ebp)
4012ac: c7 04 24 31 30 40 00 movl $0x403031,(%esp)
4012b3: 89 54 24 04 mov %edx,0x4(%esp)
4012b7: e8 94 13 00 00 call 402650 <_printf>
4012bc: 4b dec %ebx
4012bd: 79 e1 jns 4012a0 <_main+0x250>
for(int i =0; i < 10; i++) printf("%d ", (int)fifo);