2016-06-09 37 views
1

我一直在試圖約2周使用其writecomponent方法TMemoryStream對象寫信TComponent派生的任意類的一個對象,並再次使用readcomponent方法檢索該對象讀取組件。雖然我認爲這是一件容易的事,但我無法讓它正常工作。實際上沒有編譯錯誤,但對象的屬性沒有正確加載。請幫我看看我做錯了什麼。這是我的代碼片段。如何編寫和使用TMemoryStrream

#include <vcl.h> 
#pragma hdrstop 

#include <tchar.h> 
#include <memory> 
#include <iostream> 
#include <conio.h> 

#pragma argsused 

using namespace std; 

class Woman : public TComponent 
{ 
    private: 
    int Age; 
    public: 
    UnicodeString Name; 
    Woman(TComponent* _Owner, int InAge, UnicodeString InName) 
     : TComponent(_Owner) 
    { 
     Age = InAge; 
     Name = InName; 
    } 
    int GetAge() 
    { 
     return Age; 
    } 
}; 

void RegisterClassesWithStreamingSystem(void) 
{ 

    #pragma startup RegisterClassesWithStreamingSystem 

    Classes::RegisterClass(__classid(Woman)); 
} 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    Woman* FirstWoman = new Woman(NULL, 25, "Anjelina"); 
    UnicodeString as; 
    auto_ptr<TMemoryStream> MStr(new TMemoryStream); 
    auto_ptr<TStringStream> SStr(new TStringStream(as)); 

    MStr->WriteComponent(FirstWoman); 
    MStr->Seek(0, soFromBeginning); 
    ObjectBinaryToText(MStr.get(), SStr.get()); 
    SStr->Seek(0, soFromBeginning); 
    as = SStr->DataString; 

    auto_ptr<TMemoryStream> pms(new TMemoryStream); 
    auto_ptr<TStringStream> pss(new TStringStream(as)); 
    TComponent *pc; 

    ObjectTextToBinary(pss.get(), pms.get()); 
    pms->Seek(0, soFromBeginning); 

    pc = pms->ReadComponent(NULL); 


    Woman* AWoman; 
    AWoman = dynamic_cast<Woman*>(pc); 

    cout << AWoman->GetAge() << endl; 
    cout << AWoman->Name.c_str() << endl; 

    FirstWoman->Free(); 
    pc->Free(); 

    getch(); 
    return 0; 
} 

回答

0

它不工作,因爲

  1. Woman類沒有任何__published讀/寫性能。 WriteComponent()ReadComponent()方法是DFM流式傳輸系統的一部分,DFM依靠發佈屬性的RTTI來完成他們的工作。

  2. 由於DFM不兼容構造函數,所以DFM無法構建類的實例,因此您正在使用DFM無法調用的自定義構造函數。

您需要相應地更新你的類,如:

class Woman : public TComponent 
{ 
private: 
    int fAge; 
    UnicodeString fName; 

public: 
    __fastcall Woman(TComponent* Owner) 
     : TComponent(Owner) 
    { 
    } 

    __fastcall Woman(TComponent* Owner, int InAge, const UnicodeString &InName) 
     : TComponent(Owner) 
    { 
     fAge = InAge; 
     fName = InName; 
    } 

__published: 
    __property int Age = {read=fAge, write=fAge}; 
    __property UnicodeString Name = {read=fName, write=fName}; 
}; 

然後你可以這樣做:

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    auto_ptr<Woman> FirstWoman(new Woman(NULL, 25, L"Anjelina")); 

    auto_ptr<TMemoryStream> MStr(new TMemoryStream); 
    auto_ptr<TStringStream> SStr(new TStringStream(L"")); 

    MStr->WriteComponent(FirstWoman.get()); 
    MStr->Position = 0; 
    ObjectBinaryToText(MStr.get(), SStr.get()); 
    SStr->Position = 0; 
    UnicodeString as = SStr->DataString; 

    auto_ptr<TStringStream> pss(new TStringStream(as)); 
    auto_ptr<TMemoryStream> pms(new TMemoryStream); 

    ObjectTextToBinary(pss.get(), pms.get()); 
    pms->Position = 0; 

    auto_ptr<TComponent> pc(pms->ReadComponent(NULL)); 
    Woman* AWoman = static_cast<Woman*>(pc.get()); 

    cout << AWoman->Age << endl; 
    cout << AWoman->Name.c_str() << endl; 

    FirstWoman.reset(); 
    pc.reset(); 

    getch(); 
    return 0; 
} 

或者這樣:

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    auto_ptr<Woman> FirstWoman(new Woman(NULL, 25, L"Anjelina")); 

    auto_ptr<TMemoryStream> MStr(new TMemoryStream); 
    auto_ptr<TStringStream> SStr(new TStringStream(L"")); 

    MStr->WriteComponent(FirstWoman.get()); 
    MStr->Position = 0; 
    ObjectBinaryToText(MStr.get(), SStr.get()); 
    SStr->Position = 0; 
    UnicodeString as = SStr->DataString; 

    auto_ptr<TStringStream> pss(new TStringStream(as)); 
    auto_ptr<TMemoryStream> pms(new TMemoryStream); 

    ObjectTextToBinary(pss.get(), pms.get()); 
    pms->Position = 0; 

    auto_ptr<Woman> SecondWoman(new Woman(NULL)); 
    pms->ReadComponent(SecondWoman.get()); 

    cout << SecondWoman->Age << endl; 
    cout << SecondWoman->Name.c_str() << endl; 

    FirstWoman.reset(); 
    SecondWoman.reset(); 

    getch(); 
    return 0; 
} 
+0

感謝親愛的雷米。我喜歡你。有效。你是個天才 –