tom_usenet said:
You don't need it to return bool, void is fine.
That name is illegal because of the underscores - user code shouldn't
use double underscores at all, to avoid clashing with library and
compiler stuff and with the name mangling algorithm.
namespace
{
bool dummy = (func(), true); //use of comma operator
}
Do you call the same function 3 times for a good reason? Why not just
call it once?
The final point to notice is that global variables are not necessarily
initialized before main is entered, but only before the first function
defined in the same translation unit as the global is called. In
practice this is only really a problem for static libraries (which
strip out unreferenced global variables if you're not careful) and
with DLLs where initialization will be deferred until the DLL is
loaded (and Dllmain is called on Windows).
Tom
you have all valid points. but that is not my application.
I am writing a portable serialization library, you can see it below. I
havent implemented the ability to serialize pointers yet thats what the
TypeMap is for. It is initialised when the program starts and is filled with
the names and helper constructors of all the declared classes. The name of
the class is then written to a file preceding the classes data being
serialized so that when deserializing it can lookup the proper constructor
for the class.
I will post the complete code when i can get past this damn error that the
std::map causes when using it with the SDL multithreading librarys.
the library is used like this:
---foo.h--------
class foo
{
int var1, var2, var3, var4;
DECLARE_CLASS(foo)
void Serialize(FILE* file);
};
---foo.cpp----
#include "Serialize.h"
#include "foo.h"
IMPLEMENT_CLASS(foo)
void foo::Serialize(FILE* file)
{
file, var1, var2, var3, var4;
}
--------------Serialize.h---------------
#pragma once
#include <stdio.h>
#include <map>
#include <string>
//Global members
****************************************************************************
******
extern bool IsLoading;
extern map<string, void*> TypeMap;
bool __addclass(const char* name, void* ptr);
#define DECLARE_CLASS(class_name) \
static void* CreateInstance(void); \
friend FILE& operator,(FILE& file, class_name& Ob); \
friend FILE& operator,(FILE& file, class_name*& Ob);
#define IMPLEMENT_CLASS(class_name) \
static bool __dummyvar__ = __addclass(#class_name,
class_name::CreateInstance); \
void* class_name::CreateInstance(void) { return new class_name; } \
FILE& operator,(FILE& file, class_name& Ob) { Ob.Serialize(file); return
file; }; \
FILE& operator,(FILE& file, class_name*& Ob) { if (IsLoading) Ob = new
class_name; Ob->Serialize(file); return file; };
FILE& operator,(FILE& file, char var);
FILE& operator,(FILE& file, bool var);
FILE& operator,(FILE& file, short var);
FILE& operator,(FILE& file, int var);
FILE& operator,(FILE& file, long var);
FILE& operator,(FILE& file, float var);
FILE& operator,(FILE& file, double var);
FILE& operator,(FILE& file, long double var);
FILE& operator,(FILE& file, unsigned long var);
FILE& operator,(FILE& file, char*& var);
FILE& operator,(FILE& file, bool*& var);
FILE& operator,(FILE& file, short*& var);
FILE& operator,(FILE& file, int*& var);
FILE& operator,(FILE& file, long*& var);
FILE& operator,(FILE& file, float*& var);
FILE& operator,(FILE& file, double*& var);
FILE& operator,(FILE& file, long double*& var);
FILE& operator,(FILE& file, unsigned long*& var);
void SerializeCharArray(FILE& file, char* array, unsigned long length);
char* DeSerializeCharArray(FILE& file, unsigned long& length);
--------------Serialize.cpp---------------
#include "stdafx.h"
//Global members
****************************************************************************
******
bool IsLoading = NULL;
map<string, void*> TypeMap;
bool __addclass(const char* name, void* ptr) {
TypeMap.insert(make_pair(name, ptr)); //the error occurs here if anyone
knows how to resolve it
return 0;
};
FILE& operator,(FILE& file, char var) { if (IsLoading) fread(&var,
sizeof(char), 1, &file);
else fwrite(&var, sizeof(char), 1, &file);
return file; };
FILE& operator,(FILE& file, bool var) { if (IsLoading) fread(&var,
sizeof(bool), 1, &file);
else fwrite(&var, sizeof(bool), 1, &file);
return file; };
FILE& operator,(FILE& file, short var) { if (IsLoading) fread(&var,
sizeof(short), 1, &file);
else fwrite(&var, sizeof(short), 1, &file);
return file; };
FILE& operator,(FILE& file, int var) { if (IsLoading) fread(&var,
sizeof(int), 1, &file);
else fwrite(&var, sizeof(int), 1, &file);
return file; };
FILE& operator,(FILE& file, long var) { if (IsLoading) fread(&var,
sizeof(long), 1, &file);
else fwrite(&var, sizeof(long), 1, &file);
return file; };
FILE& operator,(FILE& file, float var) { if (IsLoading) fread(&var,
sizeof(float), 1, &file);
else fwrite(&var, sizeof(float), 1, &file);
return file; };
FILE& operator,(FILE& file, double var) { if (IsLoading) fread(&var,
sizeof(double), 1, &file);
else fwrite(&var, sizeof(double), 1, &file);
return file; };
FILE& operator,(FILE& file, long double var) { if (IsLoading) fwrite(&var,
sizeof(long double), 1, &file);
else fwrite(&var, sizeof(long double), 1, &file);
return file; };
FILE& operator,(FILE& file, unsigned long var) { if (IsLoading) fread(&var,
sizeof(unsigned long), 1, &file);
else fwrite(&var, sizeof(unsigned long), 1, &file);
return file; };
FILE& operator,(FILE& file, char*& var) { if (IsLoading) {var = new char;
fread(var, sizeof(char), 1, &file); }
else fwrite(var, sizeof(char), 1, &file);
return file; };
FILE& operator,(FILE& file, bool*& var) { if (IsLoading) {var = new bool;
fread(var, sizeof(bool), 1, &file); }
else fwrite(var, sizeof(bool), 1, &file);
return file; };
FILE& operator,(FILE& file, short*& var) { if (IsLoading) {var = new short;
fread(var, sizeof(short), 1, &file); }
else fwrite(var, sizeof(short), 1, &file);
return file; };
FILE& operator,(FILE& file, int*& var) { if (IsLoading) {var = new int;
fread(var, sizeof(int), 1, &file); }
else fwrite(var, sizeof(int), 1, &file);
return file; };
FILE& operator,(FILE& file, long*& var) { if (IsLoading) {var = new long;
fread(var, sizeof(long), 1, &file); }
else fwrite(var, sizeof(long), 1, &file);
return file; };
FILE& operator,(FILE& file, float*& var) { if (IsLoading) {var = new float;
fread(var, sizeof(float), 1, &file); }
else fwrite(var, sizeof(float), 1, &file);
return file; };
FILE& operator,(FILE& file, double*& var) { if (IsLoading) {var = new
double; fread(var, sizeof(double), 1, &file); }
else fwrite(var, sizeof(double), 1, &file);
return file; };
FILE& operator,(FILE& file, long double*& var) { if (IsLoading) {var = new
long double; fwrite(var, sizeof(long double), 1, &file); }
else fwrite(var, sizeof(long double), 1, &file);
return file; };
FILE& operator,(FILE& file, unsigned long*& var) { if (IsLoading) {var = new
unsigned long; fread(var, sizeof(unsigned long), 1, &file); }
else fwrite(var, sizeof(unsigned long), 1, &file);
return file; };
void SerializeCharArray(FILE& file, char* array, unsigned long length)
{
fwrite(&length, sizeof(unsigned long), 1, &file);
fwrite(array, sizeof(char), length+1, &file);
};
char* DeSerializeCharArray(FILE& file, unsigned long& length)
{
fread(&length, sizeof(unsigned long), 1, &file);
char* array = new char[length+1];
fread(array, sizeof(char), length+1, &file);
return array;
};