Is this possible? if so, how to code it? (c++)

Started by
7 comments, last by Antheus 14 years, 2 months ago
Let's say i have a Class that handles saving/loading and encrypting/decrypting that i can use for all my projects by just including it in the project. If i have a simple struct such as: struct StudentInfo { int Age; string Name; int Mark; bool Pass; } Now since i need to use this class for all my projects and not just this one, i can't pass the structure like this: void Func(StudentInfo &Object) And i also can't access the members like this: (Inside Func) Object.Age Object.Name Object.Mark Object.Pass What can i do?
"Spending your life waiting for the messiah to come save the world is like waiting around for the straight piece to come in Tetris...even if it comes, by that time you've accumulated a mountain of shit so high that you're fucked no matter what you do. "
Advertisement
Quote:Original post by Waaayoff
Now since i need to use this class for all my projects and not just this one, i can't pass the structure like this:

void Func(StudentInfo &Object)


Uh, why not? What does needing to use it in multiple projects have to do with passing the object to a function?

Quote:Original post by Waaayoff
And i also can't access the members like this:


Uh, why not? Those are members of the struct, aren't they? So what's the problem?
there is a way to do it but i dont think people recommend it (not quite sure why). Unfortunatly ive forgotten how to do it, i think it has something to do with casting the instance of the struct to char* when using the member functions read or write... errr.. example:

std::ofstream out("somefile.txt");/* --- */out.write(reinterpret_cast<char*>(&studentInfoInstance), sizeof(StudentInfo));


something like that..
Quote:Original post by RDragon1
Quote:Original post by Waaayoff
Now since i need to use this class for all my projects and not just this one, i can't pass the structure like this:

void Func(StudentInfo &Object)


Uh, why not? What does needing to use it in multiple projects have to do with passing the object to a function?

Quote:Original post by Waaayoff
And i also can't access the members like this:


Uh, why not? Those are members of the struct, aren't they? So what's the problem?




Well, since the class is going to be the same for all projects, the func declaration will always be:

void Func(StudentInfo &Object)

But what if the struct name was PlayerInfo? We can't pass an Object of PlayerInfo to Func since it's expecting an Object of StudentInfo.
"Spending your life waiting for the messiah to come save the world is like waiting around for the straight piece to come in Tetris...even if it comes, by that time you've accumulated a mountain of shit so high that you're fucked no matter what you do. "
The OP probably meant to have an API for that special file management that is unaware of the concrete structures; it should be able to load/save anything that is passed.

However, such a persistence system based on de-/serialization has its own caveats. E.g.: What is if a structure evolves over time although some older versions are already stored somewhere? What is with references/pointers to other structures? Endianess differences of the platform where a file was stored and the platform where it has to be loaded? And, similarly, the differences in data types for 32 vs. 64 bit machines? Not to forget the problem that e.g. C++ classes may have hidden features that are not found in C structures.

Simply casting a memory region to char[] for saving may work, but it will cause all kinds of trouble during loading. You may look at the principles of blender's SDNA or google's Protocol Buffer to get some insight how others do it (although AFAIK both these examples deal with C structures only, so that C++ would need some extra work, e.g. a kind of a class registry for mapping class names to object factories).
You can actually achieve what you want by using template functions.

Declare a template function in your saving class like so:

template<class T> void Save(T &DataToSave){    int nNrOfBytes = sizeof(T);    char *pBytes = reinterpret_cast<char*>(&DataToSave);    // TODO:    // Do your decryption here (updating pBytes and nNrOfBytes if necessary)    // Save the data as proposed by CodeCriminal    // (out is an open file stream)    out.write(pBytes, nNrOfBytes);}


Your application would call the function as so
StudentInfo studentInfoInstance;PlayerInfo playerInfoInstance;// Initialize your struct instances here// Call the save template methodCSaveAndDecryptClass.Save<StudentInfo>(studentInfoInstance);CSaveAndDecryptClass.Save<PlayerInfo>(playerInfoInstance);
Thanks ;)
"Spending your life waiting for the messiah to come save the world is like waiting around for the straight piece to come in Tetris...even if it comes, by that time you've accumulated a mountain of shit so high that you're fucked no matter what you do. "
The examples posted above will only work for POD types, which std::string is not. Most useful C++ classes are not POD, so you will need a separate layer that will turn them into a byte stream which can them be fed to raw decrypt/write functions.
Quote:Original post by rip-off
The examples posted above will only work for POD types, which std::string is not.


Worse. Under MVC it will work for strings less than 16 characters due to inlined buffer, simply as a side-effect of how Visual Studio implements std::string.

This topic is closed to new replies.

Advertisement