持久對象(persistent objects)廣泛應用於遊戲、分散式資料庫系統、多媒體以及圖形應用程式中。目前C++並不直接支援持久性(persistence)(但有一些在C++未來版本中添加持久性和反射(reflection)的建議)。持久對象可以在建立它的程式的範圍之外保持自身狀態。把對象寫入一個檔案並在以後重建之,或者把對象傳送到一台遠程機器,就是這樣的例子。對持久性的支援並不象第一眼看上去那樣簡單,同一對象的大小和記憶體布局在不同的平台上可能並不相同,而不同的位元組次序(byte ordering),或稱為endian-ness,使事情更加複雜化。在下文中我將討論如何?持久性,而無須求助於DCOM和 CORBA之類的第三方架構。對於小型和可移植的應用程式而言,這是一種有效並令人滿意的方案。
序列化(serialization)基礎
為了使一個對象持久存在,必須把它的狀態儲存在非易失的存放裝置中。考慮一個錄製和播放MP3檔案的應用程式,每首單曲都表示為一個包含標題、唱片、歌手、時間、速率、錄製日期以及相應的 MP3檔案的對象,該應用程式在跟蹤列表中顯示最近播放的曲目。你的目標是通過序列化,也就是把對象寫入一個檔案,使MP3對象成為持久對象,同時通過還原序列化(deserialization)在下一個 session中重建這些對象。
序列化內建資料類型
每個對象最終都由內建資料成員組成,如int, bool, char[]等等。你的第一個任務是把這樣的類型寫入一個輸出檔案流(ofstream)中。應用程式必須這些值儲存為相應的二進位形式,基於這個目的,應使用write() 和read() 成員函數。write() 以某個變數的地址和大小為參數,把該變數的位元模式寫入一個檔案流中。read() 的兩個參數為char*和long類型,分別指示記憶體緩衝區的地址和位元組大小。下面的例子示範如何在ofstream中儲存兩個整數:
#include
using namespace std;
int main()
{
int x,y; // mouse coordinates
// ..assign values to x and y
ofstream archive(”coord.dat”, ios::binary);
archive.write(reinterpret_cast(&x), sizeof (x));
archive.write(reinterpret_cast(&x), sizeof (x));
archive.close();
}
使用reinterpret_cast<>是必要的,因為write()的第一個參數類型為const char*,但&x和&y是int*類型。
以下代碼讀取剛才儲存的值:
#include
using namespace std;
int main()
{
int x,y;
ifstream archive(”coord.dat”);
archive.read((reinterpret_cast(&x), sizeof(x));
archive.read((reinterpret_cast(&y), sizeof(y));
}
序列化對象
要序列化一個完整的對象,應把每個資料成員寫入檔案中:
class MP3_clip
{
private:
std::time_t date;
std::string name;
int bitrate;
bool stereo;
public:
void serialize();
void deserialize();
//..
};
void MP3_clip::serialize()
{
{
int size=name.size();// store name’s length
//empty file if it already exists before writing new data
ofstream arc(”mp3.dat”, ios::binary|ios::trunc);
arc.write(reinterpret_cast(&date),sizeof(date));
arc.write(reinterpret_cast(&size),sizeof(size));
arc.write(name.c_str(), size+1); // write final ” too
arc.write(reinterpret_cast(&bitrate),
sizeof(bitrate));
arc.write(reinterpret_cast(&stereo),
sizeof(stereo));
}
實現deserialize() 需要一些技巧,因為你需要為字串分配一個臨時緩衝區。做法如下:
void MP3_clip::deserialize()
{
ifstream arce(”mp3.dat”);
int len=0;
char *p=0;
arc.read(reinterpret_cast(&date), sizeof(date));
arc.read(reinterpret_cast(&len), sizeof(len));
p=new char [len+1]; // allocate temp buffer for name
arc.read(p, len+1); // copy name to temp, including ”
name=p; // copy temp to data member
delete[] p;
arc.read(reinterpret_cast(&bitrate),
sizeof(bitrate));
arc.read(reinterpret_cast(&stereo),
sizeof(stereo));
}
效能最佳化
你可能會感到迷惑,為什麼不把整個對象一次性轉儲到檔案中,而必須對每個資料成員進行序列化呢?換句話說,難道不能用下面的方式實現serialize() 嗎?
void MP3_clip::serialize()
{
ofstream arc(”mp3.dat”, ios::binary|ios::trunc);
arc.write(reinterpret_cast(this),sizeof(*this));
}
不行,不能這樣做。這種方式至少存在兩個問題。通常,當被序列化的對象還包含其它一些對象時,你不能簡單地把該對象轉儲到一個檔案中並指望以後從中重建一個有效對象。在我們的例子中,外層對象包含一個std::string成員,一個淺拷貝(shallow copy)操作會把std::string成員歸檔,但其值是時變的,意思是說每次運行程式時都可能改變。更糟的是,由於std::string事實上並不包含一個字元數組,而是一個指標,使用淺拷貝試圖重建原始字串是不可能的。為克服這個問題,程式沒有序列化string對象,而是歸檔其含有的字元和長度。一般來說,指標,數組和控制代碼應以相同的方式進行處理。
另一個問題設計到多態對象。每個多態對象都含有一個vtpr,即一個指向虛擬函數地址分配表的隱藏指標。vtpr的值是時變的,如果你把整個多態對象轉儲到一個檔案中,然後強行把歸檔後的資料添加到一個新的對象上,則其vptr可能無效並導致未定義的行為。再次提醒,解決方案是只對非時變的資料成員進行序列化和還原序列化。另一種方法是計算vptr的確切位移量,在從檔案重建對象時不要動它。記住,vptr的位置是與實現相關的,因此這樣的代碼是不可移植的。
小結
雖然C++不直接支援對象持久性,但手工實現它並不難,只要你遵從一些基本的準則:首先把每個綜合物件分解為未經處理資料類型,然後對這些未經處理資料類型進行序列化。當序列化資料時,記住要跳過時變的值。在還原序列化過程中,讀取剛才儲存的值。處理string對象、數組和控制代碼需要一些技巧:總是要對它們解引用,儲存它們所指向的值。記住在一個單獨的欄位中儲存string或數組的大小。
///////////////////code///////////////////////
#include <fstream>
#include <cassert>
#include <iostream>
#include <string>
using namespace std;
class Mp3
{
public:
Mp3(time_t t, string& name, int bit, bool stero):date_(t), name_(name), bitrate_(bit), stero_(stero){}
Mp3():stero_(0) {}
void Print()
{
cout<<date_<<endl;
cout<<name_<<endl;
cout<<bitrate_<<endl;
cout<<stero_<<endl;
}
void serialize()
{
ofstream ofs;
ofs.open("mp3.dat", ios::binary);
assert(ofs.is_open());
ofs.write(reinterpret_cast<char*>(&date_), sizeof(date_));
int size = name_.length() + 1;
ofs.write(reinterpret_cast<char*>(&size), sizeof(size));
ofs.write((name_.c_str()), size);
ofs.write(reinterpret_cast<char*>(&bitrate_), sizeof(bitrate_));
ofs.write(reinterpret_cast<char*>(&stero_), sizeof(stero_));
ofs.close();
}
void deserialize()
{
ifstream ifs;
ifs.open("mp3.dat", ios::binary);
assert(ifs.is_open());
ifs.read(reinterpret_cast<char*>(&date_), sizeof(date_));
int len = 0;
ifs.read(reinterpret_cast<char*>(&len),sizeof(len));
char* str = new char[len];
ifs.read(str, len);
name_ = str;
delete []str;
ifs.read(reinterpret_cast<char*>(&bitrate_), sizeof(bitrate_));
ifs.read(reinterpret_cast<char*>(&stero_), sizeof(stero_));
ifs.close();
}
private:
time_t date_;
string name_;
int bitrate_;
bool stero_;
};
int main()
{
int x = 10;
int y = 100;
ofstream ofs;
ofs.open("hkx.dat", ios::binary);
assert(ofs.is_open());
ofs.write(reinterpret_cast<char*>(&x), sizeof(x));
ofs.write(reinterpret_cast<char*>(&y), sizeof(y));
ofs.close();
ifstream ifs;
ifs.open("hkx.dat", ios::binary);
assert(ifs.is_open());
int a, b;
ifs.read(reinterpret_cast<char*>(&a), sizeof(a));
ifs.read(reinterpret_cast<char*>(&b), sizeof(b));
cout<<a<<endl;
cout<<b<<endl;
ifs.close();
Mp3 song1(2011, string("i love you"), 19650, 1);
song1.serialize();
Mp3 song2;
song2.deserialize();
song2.Print();
return 0;
}