[英]Deserialize an object using c++ boost library
我正在嘗試使用 boost 庫反序列化我的Command
object。 我的目標顯然是獲得序列化的 object 並將其傳遞給反序列化器。 我的class:
class Command {
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive& arch, const unsigned int version)
{
arch& letter;
arch& x;
arch& y;
arch& button;
}
char letter;
int x;
int y;
std::string button;
public:
Command(char _letter, int _x, int _y, std::string _button) {
letter = _letter;
x = _x;
y = _y;
button = _button;
}
Command() {}
~Command() {}
char getLetter() { return letter; }
int getX() { return x; }
int getY() { return y; }
std::string getButton() { return button; }
void printCommand() {
std::cout << "letter: " << letter << std::endl;
std::cout << "x : " << x << std::endl;
std::cout << "y : " << y << std::endl;
std::cout << "button: " << button << std::endl;
std::cout << "================" << std::endl;
}
};
我使用 boost 庫使用以下代碼序列化 object:
template <class T>
std::string serialize(T obj) {
std::ofstream ofs("output");
{
boost::archive::text_oarchive oa(ofs);
oa << obj;
}
return "output";
}
由於我通過 TCP 服務器發送這些序列化對象,因此我需要對它們進行序列化,但我使用的反序列化代碼不起作用:
template <class T>
T deSerialize(std::string s) {
T t = T();
std::ifstream ifs(s);
boost::archive::text_iarchive ia(ifs);
ia >> t;
return t;
}
我認為我對這個過程缺乏了解,所以我似乎無法理解為什么它不起作用。
我真的不知道。 也許是因為您的示例不是獨立的。 那么讓我來幫助你:
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/serialization/access.hpp>
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/vector.hpp>
#include <fstream>
#include <iostream>
class Command {
private:
friend class boost::serialization::access;
template <class Archive> void serialize(Archive& arch, unsigned) {
arch& letter& x& y& button;
}
char letter;
int x;
int y;
std::string button;
public:
Command(char _letter = '\0', int _x = 0, int _y = 0,
std::string_view _button = {})
: letter(_letter)
, x(_x)
, y(_y)
, button(_button)
{ }
char getLetter() const { return letter; }
int getX() const { return x; }
int getY() const { return y; }
std::string getButton() const { return button; }
friend std::ostream& operator<<(std::ostream& os, Command const& cmd){
return os << "letter: " << cmd.letter << "\n" //
<< "x : " << cmd.x << "\n" //
<< "y : " << cmd.y << "\n" //
<< "button: " << cmd.button << "\n";
}
};
template <class T> void my_save(T const& obj, std::string fileName)
{
std::ofstream ofs(fileName);
boost::archive::text_oarchive oa(ofs);
oa << obj;
}
template <class T> T my_load(std::string fileName)
{
std::ifstream ifs(fileName);
boost::archive::text_iarchive ia(ifs);
T result;
ia >> result;
return result;
}
int main() {
using Commands = std::vector<Command>;
my_save(Commands {
{'s', 10, 20, "Save"},
{'q', 21, 31, "Quit"},
{'l', 32, 42, "Load"},
{'x', 43, 53, "Exit"},
}, "output.txt");
for (auto&& cmd : my_load<Commands>("output.txt"))
std::cout << "-----\n" << cmd;
}
印刷
-----
letter: s
x : 10
y : 20
button: Save
-----
letter: q
x : 21
y : 31
button: Quit
-----
letter: l
x : 32
y : 42
button: Load
-----
letter: x
x : 43
y : 53
button: Exit
output.txt 包含類似
22 serialization::archive 19 0 0 4 0 0 0 115 10 20 4 Save 113 21 31 4 Quit 108 32 42 4 Load 120 43 53 4 Exit
令我印象深刻的是, Command
類似於 Java 風格的准類 (PDF) 。 我可能會簡化: Live On Coliru
struct Command {
void serialize(auto& ar, unsigned) { ar& letter& x& y& button; }
char letter = '\0';
int x = 0, y = 0;
std::string button;
friend std::ostream& operator<<(std::ostream& os, Command const& cmd) {
return os << "['" << cmd.letter << "'," << cmd.x << "," << cmd.y << ","
<< std::quoted(cmd.button) << "]";
}
};
Output
['s',10,20,"Save"]
['q',21,31,"Quit"]
['l',32,42,"Load"]
['x',43,53,"Exit"]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.