- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
#define TPLM Tuple(TPLMOVE) +
#define TPLC Tuple(TPLCOPY) +
#define TPL Tuple() +
//#define GTPL Tuple() +
#define TPLERROR(t) { memcpy(0, 0, 1); }
#define TPLMOVE 1 // copy to tuple, memset 0 original. = destruct dest, copy to dest.
#define TPLCOPY 2 // copy constructor = destruct dest, copy to dest
#define TPLGET 3 // destruct all values :)
#include <memory.h>
#include <math.h>
#include <stdio.h>
#include <typeinfo>
#include <string.h>
#define max(a, b) (((a) > (b)) ? (a) : (b))
#define min(a, b) (((a) < (b)) ? (a) : (b))
unsigned int mcrc(char* name, unsigned int sz){
unsigned int ret=0; if(!sz) sz=(unsigned int)strlen(name);
char*t=name+sz; int p=0;
while(name<t){
*(((char*)&ret)+p)+=*name; p++;
if(p>=4) p=0; name++;
}
return ret;
}
struct Tupled{ unsigned int sz, type; void *v; char data[0]; };
class Tuple{
int count, set, dusz, dasz;
unsigned char *data;
int type; int adel;
public:
Tuple(){ count=0; set=0; type=0; dusz=0; dasz=0; data=0; adel=0; }
Tuple(int t){ type=t; }
~Tuple(){ if(count!=set) TPLERROR("~Tuple"); delete data; }
Tuple(Tuple &t){
count=t.count; set=t.set; dusz=t.dusz; dasz=t.dasz; data=t.data;
t.count=0; t.set=0; t.data=0; adel=1;
}
template <class T>
Tuple& operator+(T &t){ if(!adel) Add(&t, sizeof(t), t); else Del(&t, sizeof(t), t); return *this; }
template <class T>
Tuple& operator-(T &t){ if(!adel) Add(&t, sizeof(t), t); else Del(&t, sizeof(t), t); return *this; }
/* Операторы '*', '/', ',', '>', '<' код идентичен */
template <class T>
void Add(void *v, int s, T &t){
if(dasz-dusz<s+4+(int)sizeof(void*)){
unsigned char *ldata=data;
data=new unsigned char[dasz+max(128, s+4)];
memcpy(data, ldata, dasz);
dasz+=max(128, s);
delete [] ldata;
}
Tupled &d=*(Tupled*)(data+dusz);
d.sz=s;
memcpy(&d.v, v, sizeof(void*));
if(type==TPLCOPY){ *(T*)d.data=t; } else memcpy(d.data, v, s);
if(type==TPLMOVE) t.~T();
d.type=mcrc((char*)typeid(t).name(), 0);
dusz+=sizeof(Tupled)+s; count++;
}
template <class T>
void Del(void *v, int s, T &t){
if(set>=count){ TPLERROR("Tuple::Set"); return ; }
unsigned char *p=GetData(set);
if(!p){ TPLERROR("Tuple::NoData"); return ; }
Tupled &d=*(Tupled*)p;
unsigned int tp=mcrc((char*)typeid(t).name(), 0);
if(tp!=d.type){ TPLERROR("Tuple::TYPE"); return ;}
t.~T();
if(d.sz!=s){ TPLERROR("Tuple::SIZE"); return ;}
memcpy(v, d.data, d.sz);
set++;
}
unsigned char* GetData(int c){
if(c>=count) return 0;
unsigned char *p=data;
for(int i=0; i<c; i++){
p+=sizeof(Tupled)+*(int*)p;
}
return p;
}
};
Очередной самобытный велосипед от микеля.
http://rsdn.ru/forum/src/4218954.aspx