-
Notifications
You must be signed in to change notification settings - Fork 70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
how about relation data ,for example a pointer of object #45
Comments
Does this cover the issue? Pointer (and If this dies not cover your situation then please provide an example. |
for example static int ID = 0;
struct Object
{
int id;
Object()
{
this->id = ID++;
};
};
struct Student :public Object
{
Student():Object(){ }
};
struct ClassRoom :public Object
{
ClassRoom() :Object()
{
}
void addStudent(Student& pStudent)
{
this->students.push_back(pStudent.id);
}
std::vector<int> students;
};
void main()
{
Student obj;
obj.age = 20;
obj.height = 180;
obj.name = "xia";
ClassRoom room;
room.addStudent(obj);
} and the obj and room 's id is diffrent when recreate, how to do it when i use ThorsSerializer |
#include "ThorSerialize/JsonThor.h"
#include "ThorSerialize/SerUtil.h"
#include "ThorSerialize/Traits.h"
// Your class definitions from above.
// You should fix Student it is missing some fields.
ThorsAnvil_MakeTrait(Object, id);
ThorsAnvil_ExpandTrait(Student, Object, age, height, name);
ThorsAnvil_ExpandTrait(ClassRoom, Object, students);
int main()
{
Student obj;
obj.age = 20;
obj.height = 180;
obj.name = "xia";
ClassRoom room;
room.addStudent(obj);
// Save state of program
using ThorsAnvil::Serialize::jsonExport;
std::cout << jsonExport(room) << "\n";
std::cout << jsonExport(obj) << "\n";
std::cout << jsonExport(ID) << "\n";
// Load State of program
using ThorsAnvil::Serialize::jsonImport;
std::cin >> jsonImport(room);
std::cin >> jsonImport(obj);
std::cin >> jsonImport(ID);
} |
the point of the problem is "the relation between obj and room",
|
I solved that problem in the example above. If you serialize the ID as part of the object it will be restored correctly and thus the reference from room to object will be maintained. The side affect of this is that you need to serialize the global Are you asking to run arbitrary user code to re-calculate |
Sure. But it is only useful if you can use the In this case you would serialize the container containing all the objects and when you load them back in the correct id will still be correct. #include "ThorSerialize/JsonThor.h"
#include "ThorSerialize/SerUtil.h"
#include "ThorSerialize/Traits.h"
struct Object;
static std::vector<Object*> store;
struct Object
{
int id;
virtual ~Object()
{
store[id] = nullptr;
}
Object()
{
this->id = store.size();
store.push_back(this);
}
};
ThorsAnvil_MakeTrait(Object, id);
int main()
{
// Load previous state
std::cin >> jsonImport(store);
// Save State on exit
std::cout << jsonExport(store);
} |
Are you asking to run arbitrary user code to re-calculate id? |
You need to make the #include "ThorSerialize/Traits.h"
#include "ThorSerialize/JsonThor.h"
#include <iostream>
int ID = 0;
struct IdObj
{
IdObj(int id)
: id(id)
{}
int id;
friend std::ostream& operator<<(std::ostream& str, IdObj const& data) {
// When streaming to output you must make sure that␣
// this is valid JSON.
return str << data.id;
}
friend std::istream& operator>>(std::istream& str, IdObj& data) {
// When reading the value you must make sure you read everything that
// you wrote out during streaming.
str >> data.id;
// Do the action you want to update the ID to something unique to you
return str;
}
};
struct Object
{
IdObj id;
Object()
: id(ID++)
{}
};
ThorsAnvil_MakeTraitCustom(IdObj);
ThorsAnvil_MakeTrait(Object, id);
using ThorsAnvil::Serialize::jsonImport;
using ThorsAnvil::Serialize::jsonExport;
int main()
{
Object id;
std::cout << jsonExport(id) << "\n";
std::cin >> jsonImport(id);
} Since you have defined the serialization functions you can execute any arbitory code when you read the values in. |
in this case,i find that ,it is hard to find a common way to serialization when i need add logic code when deserialization . |
Not sure what you are saying. Unless you have anything specific you are trying to solve please close the issue as I don't see anything generic that a serialization library should be solving for you here. |
No description provided.
The text was updated successfully, but these errors were encountered: