Rework SAVEDptr to be a struct with accessors instead of an union.
It's not possible to put non-POD elements in a union, and a struct with accessors is a more elegant solution than a union with POD elements and explicit casts for the rest.pull/4/head
parent
3af4e1c5b0
commit
b7409b8ad6
62
src/file.cpp
62
src/file.cpp
|
@ -199,14 +199,14 @@ const SolveSpaceUI::SaveTable SolveSpaceUI::SAVED[] = {
|
||||||
{ 0, NULL, 0, NULL }
|
{ 0, NULL, 0, NULL }
|
||||||
};
|
};
|
||||||
|
|
||||||
union SAVEDptr {
|
struct SAVEDptr {
|
||||||
IdList<EntityMap,EntityId> M;
|
IdList<EntityMap,EntityId> &M() { return *((IdList<EntityMap,EntityId> *)this); }
|
||||||
/* std::string S; */
|
std::string &S() { return *((std::string *)this); }
|
||||||
bool b;
|
bool &b() { return *((bool *)this); }
|
||||||
RgbaColor c;
|
RgbaColor &c() { return *((RgbaColor *)this); }
|
||||||
int d;
|
int &d() { return *((int *)this); }
|
||||||
double f;
|
double &f() { return *((double *)this); }
|
||||||
uint32_t x;
|
uint32_t &x() { return *((uint32_t *)this); }
|
||||||
};
|
};
|
||||||
|
|
||||||
void SolveSpaceUI::SaveUsingTable(int type) {
|
void SolveSpaceUI::SaveUsingTable(int type) {
|
||||||
|
@ -215,27 +215,27 @@ void SolveSpaceUI::SaveUsingTable(int type) {
|
||||||
if(SAVED[i].type != type) continue;
|
if(SAVED[i].type != type) continue;
|
||||||
|
|
||||||
int fmt = SAVED[i].fmt;
|
int fmt = SAVED[i].fmt;
|
||||||
union SAVEDptr *p = (union SAVEDptr *)SAVED[i].ptr;
|
SAVEDptr *p = (SAVEDptr *)SAVED[i].ptr;
|
||||||
// Any items that aren't specified are assumed to be zero
|
// Any items that aren't specified are assumed to be zero
|
||||||
if(fmt == 'S' && ((std::string*)p)->empty()) continue;
|
if(fmt == 'S' && p->S().empty()) continue;
|
||||||
if(fmt == 'd' && p->d == 0) continue;
|
if(fmt == 'd' && p->d() == 0) continue;
|
||||||
if(fmt == 'f' && EXACT(p->f == 0.0)) continue;
|
if(fmt == 'f' && EXACT(p->f() == 0.0)) continue;
|
||||||
if(fmt == 'x' && p->x == 0) continue;
|
if(fmt == 'x' && p->x() == 0) continue;
|
||||||
|
|
||||||
fprintf(fh, "%s=", SAVED[i].desc);
|
fprintf(fh, "%s=", SAVED[i].desc);
|
||||||
switch(fmt) {
|
switch(fmt) {
|
||||||
case 'S': fprintf(fh, "%s", ((std::string*)p)->c_str()); break;
|
case 'S': fprintf(fh, "%s", p->S().c_str()); break;
|
||||||
case 'b': fprintf(fh, "%d", p->b ? 1 : 0); break;
|
case 'b': fprintf(fh, "%d", p->b() ? 1 : 0); break;
|
||||||
case 'c': fprintf(fh, "%08x", p->c.ToPackedInt()); break;
|
case 'c': fprintf(fh, "%08x", p->c().ToPackedInt()); break;
|
||||||
case 'd': fprintf(fh, "%d", p->d); break;
|
case 'd': fprintf(fh, "%d", p->d()); break;
|
||||||
case 'f': fprintf(fh, "%.20f", p->f); break;
|
case 'f': fprintf(fh, "%.20f", p->f()); break;
|
||||||
case 'x': fprintf(fh, "%08x", p->x); break;
|
case 'x': fprintf(fh, "%08x", p->x()); break;
|
||||||
|
|
||||||
case 'M': {
|
case 'M': {
|
||||||
int j;
|
int j;
|
||||||
fprintf(fh, "{\n");
|
fprintf(fh, "{\n");
|
||||||
for(j = 0; j < p->M.n; j++) {
|
for(j = 0; j < p->M().n; j++) {
|
||||||
EntityMap *em = &(p->M.elem[j]);
|
EntityMap *em = &(p->M().elem[j]);
|
||||||
fprintf(fh, " %d %08x %d\n",
|
fprintf(fh, " %d %08x %d\n",
|
||||||
em->h.v, em->input.v, em->copyNumber);
|
em->h.v, em->input.v, em->copyNumber);
|
||||||
}
|
}
|
||||||
|
@ -369,22 +369,22 @@ void SolveSpaceUI::LoadUsingTable(char *key, char *val) {
|
||||||
int i;
|
int i;
|
||||||
for(i = 0; SAVED[i].type != 0; i++) {
|
for(i = 0; SAVED[i].type != 0; i++) {
|
||||||
if(strcmp(SAVED[i].desc, key)==0) {
|
if(strcmp(SAVED[i].desc, key)==0) {
|
||||||
union SAVEDptr *p = (union SAVEDptr *)SAVED[i].ptr;
|
SAVEDptr *p = (SAVEDptr *)SAVED[i].ptr;
|
||||||
unsigned int u = 0;
|
unsigned int u = 0;
|
||||||
switch(SAVED[i].fmt) {
|
switch(SAVED[i].fmt) {
|
||||||
case 'S': (*(std::string*)p) = val; break;
|
case 'S': p->S() = val; break;
|
||||||
case 'b': p->b = (atoi(val) != 0); break;
|
case 'b': p->b() = (atoi(val) != 0); break;
|
||||||
case 'd': p->d = atoi(val); break;
|
case 'd': p->d() = atoi(val); break;
|
||||||
case 'f': p->f = atof(val); break;
|
case 'f': p->f() = atof(val); break;
|
||||||
case 'x': sscanf(val, "%x", &u); p->x = u; break;
|
case 'x': sscanf(val, "%x", &u); p->x()= u; break;
|
||||||
|
|
||||||
case 'c':
|
case 'c':
|
||||||
sscanf(val, "%x", &u);
|
sscanf(val, "%x", &u);
|
||||||
p->c = RgbaColor::FromPackedInt(u);
|
p->c() = RgbaColor::FromPackedInt(u);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'P':
|
case 'P':
|
||||||
*((std::string*)p) = val;
|
p->S() = val;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'M': {
|
case 'M': {
|
||||||
|
@ -392,7 +392,7 @@ void SolveSpaceUI::LoadUsingTable(char *key, char *val) {
|
||||||
// makes a shallow copy, so that would result in us
|
// makes a shallow copy, so that would result in us
|
||||||
// freeing memory that we want to keep around. Just
|
// freeing memory that we want to keep around. Just
|
||||||
// zero it out so that new memory is allocated.
|
// zero it out so that new memory is allocated.
|
||||||
p->M = {};
|
p->M() = {};
|
||||||
for(;;) {
|
for(;;) {
|
||||||
EntityMap em;
|
EntityMap em;
|
||||||
char line2[1024];
|
char line2[1024];
|
||||||
|
@ -401,7 +401,7 @@ void SolveSpaceUI::LoadUsingTable(char *key, char *val) {
|
||||||
if(sscanf(line2, "%d %x %d", &(em.h.v), &(em.input.v),
|
if(sscanf(line2, "%d %x %d", &(em.h.v), &(em.input.v),
|
||||||
&(em.copyNumber)) == 3)
|
&(em.copyNumber)) == 3)
|
||||||
{
|
{
|
||||||
p->M.Add(&em);
|
p->M().Add(&em);
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue