我应该如何调整我的代码以实现TBytes和TIdBytes之间的兼容性? [英] How should I adapt my code for compatibility between TBytes and TIdBytes?

查看:77
本文介绍了我应该如何调整我的代码以实现TBytes和TIdBytes之间的兼容性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的问题与" Delphi中提到的相同TBytes和TidBytes之间的XE4 Indy兼容性问题,即使用Delphi XE4进行编译时,TBytes(Delphi RTL)和TIdBytes(Indy)数据类型之间的兼容性问题.我的问题的根源是代码不完全符合Indy的界面,并且某些函数在调用本地Indy IO过程时使用TBytes而不是TIdBytes.

I am having the same problem as mentioned in "Delphi XE4 Indy compatibility issue between TBytes and TidBytes ", i.e. compatibility issues between TBytes(Delphi RTL) and TIdBytes(Indy) datatypes when compiling with the Delphi XE4. The source of my problem is that the code is not exactly according to Indy's interface and some of functions use TBytes, instead of TIdBytes, when calling native Indy IO procedures.

所以我想知道最佳解决方案是什么?

So I was wondering what will the best fix be?

如我所见,有两种方法:

As I see it there are two approaches:

  1. 将项目中的所有功能重构为使用TIdBytes而不是TBytes.

  1. Refactor all functions in the project to use TIdBytes rather than TBytes.

执行TBytesToTidBytes转换过程(将TBytes转换为TIdBytes),并在进行上述本地Indy调用之前调用该过程.

Implement a TBytesToTidBytes conversion procedure (converts the TBytes to TIdBytes) and call that procedure prior to making the mentioned native Indy calls.

哪种方法更好/最好?您对我该如何做还有其他想法吗?

Which of the approaches is better/best? Do you have any other ideas on how I can do that?

仅供参考:我正在尝试使用XE4配置的项目可在sourceforge上在线获得:http://sourceforge.net/projects/indy10clieservr/?source=directory

FYI: The project I am trying to configure with the XE4 is available online on sourceforge : http://sourceforge.net/projects/indy10clieservr/?source=directory

建议的转换过程应类似于:

The suggested conversion procedure should be something like:

procedure TBytesToTIdBytes(const Input:TBytes, var Output: TIdBytes)
var 
    i,L : Integer;
    allocate : Boolean;
begin
    L := Length(Input);
    if(Length(Output) <> L) then 
    begin 
        SetLength(Output,L);
    end;
    if(L > 0) then 
        move(Pointer(Input)^,Pointer(Output)^,L);
end;

推荐答案

TBytes TIdBytes 都实现为动态数组,只是声明方式不同.政治上正确"的解决方案是复制字节.但这会浪费大型阵列的内存.一个简单的解决方案是使用类型转换,以便您可以利用数组的内部引用计数,例如:

TBytes and TIdBytes are both implemented as dynamic arrays, they are simply declared differently. The "politically correct" solution is to make a copy of the bytes. But that can waste memory for large arrays. A simpler solution is to use a typecast so you can utilize the array's internal reference count, eg:

type
  PIdBytes = ^TIdBytes;
var
  B1: TBytes;
  B2: TIdBytes;
begin
  B1 := ...;
  B2 := PIdBytes(@B1)^;
end;

或者简单地:

var
  B1: TBytes;
  B2: TIdBytes;
begin
  B1 := ...;
  B2 := TIdBytes(B1);
end;

这篇关于我应该如何调整我的代码以实现TBytes和TIdBytes之间的兼容性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆