D2009 TStringlist ansistring [英] D2009 TStringlist ansistring

查看:124
本文介绍了D2009 TStringlist ansistring的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

夏季的商业平静已经开始,所以我拿起了D2009的迁移。我大致确定了程序的每个子系统,如果它们应该保持ascii,或者可以是unicode,并开始移植。



它很好,所有组件都在D2009版本(有些,像VSTView,略有不兼容,但是我现在遇到了一个问题,在某些方面必须保持安全,我广泛使用TStringList,大部分是基本的地图。



/ p>

我可以将它替换成一些简单的东西,还是应该简单地包含一个基于旧的Delphi或FPC源的剪切式的tstringlist?不能想象我是第一个遇到这个问题的人吗?



这些更改必须相对本地化,以便代码在BDS2006中可以编译,弹道。这里有几个ifdefs没有问题。
当然string-> ansistring和char - > ansichar等不会被视为我的源代码中的修改,因为我必须这样做,而且它完全是向后兼容。



编辑:我已经能够解决读者/作家类中的一些问题。这使得Mason的解决方案比我原来想象的容易一些。我将把Gabr的建议记在头脑里,作为一个后援。



泛型几乎是我买D2009的原因。可惜,他们使FPC不兼容,尽管

解决方案

如果通过map表示哈希表,您可以将其替换为通用的TDictionary。尝试声明如下:

 使用
Genericics.Collections;

type
TStringMap< T:class> = TDictionary< ansiString,T>;

然后只需将您的StringLists替换为正确对象类型的TStringMaps。 (更好的类型安全性被免费抛出。)另外,如果您希望字典拥有对象,并在完成后释放它们,请将其更改为TObjectDictionary,当调用构造函数时,将[doOwnsValues]传递给适当的参数。



(如果您要使用TDictionary,请确保您下载了D2009 Update 3)。原始版本在TDictionary中发生了一些严重的错误几乎不可用。)



编辑:如果仍然要在D2006下编译,那么你必须稍微调整一下。尝试这样的东西:

 键入
TStringMap =
{$ IFDEF UNICODE}
类TDictionary< ansiString,TObject>
(在这里添加一些基本的包装函数)
end;
{$ ELSE}
TStringList;
{$ ENDIF}

如果您使用的话,包装器不应该花太多的工作它作为地图首先。您会失去额外的类型安全性,以换取向后兼容性,但您将获得一个真正的哈希表,在O(1)时间内进行查找。


The businesswise calm of the summer has started so I picked up the migration to D2009. I roughly determined for every subsystem of the program if they should remain ascii, or can be unicode, and started porting.

It went pretty ok, all components were there in D2009 versions (some, like VSTView, slightly incompatible though) but I now have run into a problem, in some part that must remain ansistring, I extensively use TStringList, mostly as a basic map.

Is there already something easy to replace it with, or should I simply include a cut down ansistring tstringlist, based on old Delphi or FPC source?

I can't imagine I'm the first to run into this?

The changes must be relatively localised, so that the code remains compilable with BDS2006 while I go through the validation-trajectory. A few ifdefs here and there are no problem. Of course string->ansistring and char ->ansichar etc don't count as modifications in my source, since I have to do that anyway, and it is fully backwards compat.

Edit: I've been able to work away some of the stuff in reader/writer classes. This makes going for Mason's solution easier than I originally thought. I'll holds Gabr's suggestion in mind as a fallback.

Generics is pretty much the reason I bought D2009. Pity that they made it FPC incompatible though

解决方案

If by "map" you mean "hash table", you can replace it with the generic TDictionary. Try declaring something like this:

uses
  Generics.Collections;

type
  TStringMap<T: class> = TDictionary<ansiString, T>;

Then just replace your StringLists with TStringMaps of the right object type. (Better type-safety gets thrown in free.) Also, if you'd like the dictionary to own the objects and free them when you're done, change it to a TObjectDictionary and when you call the constructor, pass [doOwnsValues] to the appropriate parameter.

(BTW if you're going to use TDictionary, make sure you download D2009 Update 3. The original release had some severe bugs in TDictionary that made it almost unusable.)

EDIT: If it still has to compile under D2006, then you'll have to tweak things a little. Try something like this:

type
  TStringMap =
{$IFDEF UNICODE}
    class TDictionary<ansiString, TObject>
    (Add some basic wrapper functions here.)
    end;
{$ELSE}
    TStringList;
{$ENDIF}

The wrapper shouldn't take too much work if you were using it as a map in the first place. You lose the extra type safety in exchange for backwards compatibility, but you gain a real hash table that does its lookups in O(1) time.

这篇关于D2009 TStringlist ansistring的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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