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);