Boost: Serialization d'un vector d'objet
Bonsoir, j'essaye de serialiser un vecteur d'objet mais voila lorsque j'execute le programme et essaye d'ajouter un utilisateur celui ci segfault sous fedora alors que sous windows il fonctionne.
User.hpp
Code:
1 2 3 4 5 6 7 8 9 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
|
#ifndef _USER_H_
# define _USER_H_
#include <boost/serialization/vector.hpp>
#include <iostream>
#include <vector>
enum Status
{
online,
offline
};
class User
{
protected:
int id;
Status status;
std::string firstname;
std::string lastname;
std::string login;
std::string password;
std::string avatar;
std::string personalMsg;
std::string contacts;
std::string exist;
public:
User();
int getId() const;
void setId(int);
Status getStatus() const;
void setStatus(Status);
std::string getFirstname() const;
void setFirstname(std::string);
std::string getLastname() const;
void setLastname(std::string);
std::string getLogin() const;
void setLogin(std::string);
std::string getPassword() const;
void setPassword(std::string);
std::string getAvatar() const;
void setAvatar(std::string);
std::string getPersonalMsg() const;
void setPersonalMsg(std::string);
bool getExist() const;
void setExist(bool);
std::string getContacts();
void setContacts(std::string, bool);
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive& ar, const unsigned int version) {
ar & boost::serialization::make_nvp("id", id) &
boost::serialization::make_nvp("login", login) &
boost::serialization::make_nvp("firstname", firstname) &
boost::serialization::make_nvp("lastname", lastname) &
boost::serialization::make_nvp("password", password) &
boost::serialization::make_nvp("avatar", avatar) &
boost::serialization::make_nvp("personalMsg", personalMsg) &
boost::serialization::make_nvp("contact-list", contacts) &
boost::serialization::make_nvp("exist", exist);
}
}; |
User.cpp
Code:
1 2 3 4 5 6 7 8 9 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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
#include "User.hpp"
User::User()
{
id = 0;
firstname = "";
lastname = "";
login = "";
avatar = "";
personalMsg = "";
}
int User::getId() const
{
return this->id;
}
void User::setId(int id)
{
this->id = id;
}
Status User::getStatus() const
{
return this->status;
}
void User::setStatus(Status status)
{
this->status = status;
}
std::string User::getFirstname() const
{
return this->firstname;
}
void User::setFirstname(std::string name)
{
this->firstname = name;
}
std::string User::getLastname() const
{
return this->lastname;
}
void User::setLastname(std::string name)
{
this->lastname = name;
}
std::string User::getLogin() const
{
return this->login;
}
void User::setLogin(std::string login)
{
this->login = login;
}
std::string User::getPassword() const
{
return this->password;
}
void User::setPassword(std::string password)
{
this->password = password;
}
std::string User::getAvatar() const
{
return this->avatar;
}
void User::setAvatar(std::string path)
{
this->avatar = path;
}
std::string User::getPersonalMsg() const
{
return this->personalMsg;
}
void User::setPersonalMsg(std::string msg)
{
this->personalMsg = msg;
}
bool User::getExist() const
{
return this->exist;
}
void User::setExist(bool value)
{
this->exist = value;
}
std::string User::getContacts()
{
return this->contacts;
}
void User::setContacts(std::string login, bool empty)
{
if (empty == false)
{
this->contacts += login;
this->contacts += ";";
}
else
{
this->contacts.clear();
this->contacts = login;
}
} |
ManageUsers.hpp
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
#include <boost/serialization/vector.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <vector>
#include <fstream>
#include "User.hpp"
class ManageUsers
{
public:
ManageUsers();
bool checkLogin(User&);
bool login(std::string, std::string);
bool addUsers(User);
bool removeUsers(std::string);
bool modifierInfo(User);
std::vector<User> getUsers() const;
bool addContact(std::string, std::string);
bool removeContact(std::string, std::string);
int posInVector(std::string);
void display(std::vector<User>) const;
private:
std::vector<User> users;
}; |
ManageUsers.cpp
Code:
1 2 3 4 5 6 7 8 9 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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
#include <vector>
#include "User.hpp"
#include "ManageUsers.hpp"
void saveIntoFile(std::vector<User>& d, const char* file)
{
std::ofstream ofile(file, std::ios::out);
assert(ofile.good());
boost::archive::xml_oarchive ar(ofile);
std::cout << "before serialization" << std::endl;
ar << boost::serialization::make_nvp("Users", d);
std::cout << "after serialization" << std::endl;
ofile.close();
}
void getFromFile(std::vector<User>& d, const char* file)
{
std::ifstream ifile(file, std::ios::in);
assert(ifile.good());
boost::archive::xml_iarchive ar(ifile);
ar >> boost::serialization::make_nvp("Users", d);
ifile.close();
}
ManageUsers::ManageUsers()
{
this->users.clear();
}
bool ManageUsers::checkLogin(User& u)
{
int pos = posInVector(u.getLogin());
if (pos == -1)
{
if (this->users.empty())
u.setId(0);
else
u.setId(this->users.back().getId() + 1);
return true;
}
return false;
}
bool ManageUsers::addUsers(User u)
{
if (checkLogin(u) == true)
{
if (u.getLogin().empty() == false && u.getLogin().empty() == false)
{
this->users.push_back(u);
saveIntoFile(this->users, "Users.xml");
return true;
}
return false;
}
return false;
}
bool ManageUsers::login(std::string login, std::string password)
{
int pos = posInVector(login);
if (pos != -1)
{ if (this->users[pos].getPassword() == password)
return true;
}
return false;
}
bool ManageUsers::removeUsers(std::string login)
{
int pos = posInVector(login);
if (pos != -1)
{
this->users[pos].setExist(false);
saveIntoFile(this->users, "Users.xml");
return true;
}
return false;
}
bool ManageUsers::modifierInfo(User u)
{
int pos = posInVector(u.getLogin());
if (u.getFirstname().empty() == false)
this->users[pos].setFirstname(u.getFirstname());
if (u.getLastname().empty() == false)
this->users[pos].setLastname(u.getLastname());
if (u.getPassword().empty() == false)
this->users[pos].setPassword(u.getPassword());
if (u.getAvatar().empty() == false)
this->users[pos].setAvatar(u.getAvatar());
if (u.getPersonalMsg().empty() == false)
this->users[pos].setPersonalMsg(u.getPersonalMsg());
saveIntoFile(this->users, "Users.xml");
return false;
}
std::vector<User> ManageUsers::getUsers() const
{
return this->users;
}
bool ManageUsers::addContact(std::string owner, std::string login)
{
int pos = posInVector(owner);
int posContact = posInVector(login);
if (posContact != -1 && posContact != pos)
{
std::string local = this->users[pos].getContacts();
if (local.find(login) == std::string::npos)
{
this->users[pos].setContacts(login, false);
saveIntoFile(this->users, "Users.xml");
return true;
}
return false;
}
return false;
}
bool ManageUsers::removeContact(std::string owner, std::string login)
{
int pos = posInVector(owner);
int posContact = posInVector(login);
if (posContact != -1 && posContact != pos)
{
std::string local = this->users[pos].getContacts();
if (local.find(login) != std::string::npos)
{
int start = local.find(login);
local.erase(start, login.size() + 1);
this->users[pos].setContacts(local, true);
saveIntoFile(this->users, "Users.xml");
return true;
}
return false;
}
return false;
}
int ManageUsers::posInVector(std::string login)
{
if (!this->users.empty())
{
std::cout << "before deserialization" << std::endl;
getFromFile(this->users, "Users.xml");
std::cout << "after deserialization" << std::endl;
for (int cpt = 0; cpt < this->users.size(); cpt++)
{
if (this->users[cpt].getLogin() == login)
return cpt;
}
return -1;
}
return -1;
}
void ManageUsers::display(std::vector<User> u) const
{
if (!u.empty())
{
for (int cpt = 0; cpt < u.size(); cpt++)
std::cout << "FirstName: "<< u[cpt].getFirstname() << " LastName: " << u[cpt].getLastname() << " Login: " << u[cpt].getLogin() << std::endl;
}
} |
Et pour finir le main de test
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
#include "ManageUsers.hpp"
int main (void)
{
ManageUsers usersRegister;
User users;
User users2;
users.setLogin("BlaBla");
users.setPassword("1234");
users.setFirstname("BlaBla");
users.setLastname("BlaBla");
users.setAvatar("1.jpg");
users.setPersonalMsg("");
users.setExist(true);
usersRegister.addUsers(users);
usersRegister.display(usersRegister.getUsers());
system("pause");
return 0;
} |
Merci d'avance et desole pour la longueur du post.