如何使用 TMemoryStrream 编写和读取组件
how to write and read components using TMemoryStrream
我已经尝试了大约 2 周,使用其 writecomponent
方法在TMemoryStream
对象上编写从 TComponent
派生的任意类的对象,并使用readcomponent
方法再次检索此对象。虽然我认为这是一件容易的事,但我无法使其正常工作。实际上没有编译错误,但对象的属性未正确加载。请帮我找到我做错了什么。这是我的代码片段。
#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;
}
它不起作用,因为
-
Woman
类没有任何__published
读/写属性。WriteComponent()
和ReadComponent()
方法是 DFM 流系统的一部分,DFM 依赖于已发布属性的 RTTI 来完成其工作。 -
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;
}
相关文章:
- 理解boost::asio-async_read在无需读取内容时的行为
- 使用新行和不使用新行读取文件
- 读取文件并输入到矢量中
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 无法找到/读取配置文件.conf-FileIOException
- 如何使用Luacneneneba API正确读取字符串和表参数
- 如何将 txt 文件中的行分隔为组件C++
- C++将文本文件中的数据读取到结构数组中
- 正在将csv文件读取为双精度矢量
- 为什么 sscanf 无法从一个字符串中读取uint64_t和字符?
- 为什么在读取文件大小时文件IO速度会发生变化
- 正在读取二进制文件(is_open)
- 如何在c++中从文本文件中逐行读取整数
- 如何逐行读取文件并分隔行组件?
- 如何使用 TMemoryStrream 编写和读取组件
- 从文本文件中读取组件矢量数据到结构数组
- 如何读取存储在HDF5中的ArrayType的一个组件
- 当我实现 DBMS 的磁盘管理组件时,是否可以在 C++ 中使用 fstream 来读取或写入文件