delphi 泛型

System.Generics.Collections.pas

TList<T> 
http://docwiki.embarcadero.com/Libraries/Berlin/en/System.Generics.Collections.TList
http://docwiki.embarcadero.com/CodeExamples/Berlin/en/Generics_Collections_TList_(Delphi)
http://docwiki.embarcadero.com/CodeExamples/Berlin/en/Generics_Collections_TObjectList_(Delphi)
http://docwiki.embarcadero.com/Libraries/Berlin/en/System.Generics.Collections.TObjectList

listTime: TList<string>;
listVal: TList<Double>;

listVal: TList<integer>;

objlist:TObjectList<TEdit>;

alist:TList;//这个不是泛型

alist.add(是指针无类型)

List.LastIndexOf

list.add

List.AddRange([11,22,33]);

list.indexof

List.Reverse;

list.count;

list.item[i];

objlist.item[i];

list.Exchange

  使用TObjectList<T>会将对象自动销毁,使用TList<T>不会将对象自动销毁。

TArray.Sort<string>(arr);

TArray.BinarySearch

http://docwiki.embarcadero.com/RADStudio/XE6/en/How_to_Handle_Delphi_Generics_in_C%2B%2B

这个泛型是为delphi编写的,c++builder不能直接使用,非要用的话按下面的例子,先建立个delphiunit,集成新类,然后再c++builder引用此单元头文件,就可以了。真麻烦。

c++builder还是用map、vector方便。

How to Handle Delphi Generics in C++

Go Up to Handling Delphi Features in C++Builder Index

This topic describes some programming issues that you might encounter when dealing with generics, a feature supported by Delphi.

Delphi generics are exposed to C++ as templates. However, it is important to realize that the instantiations occur on the Delphi side, not in C++. Therefore, you can only use these template for types that were explicitly instantiated in Delphi code. For example, let's declare a simple generic, TList<T>, in Delphi:

 
unit DelphiUnit;
 
interface
uses System.Generics.Collections;
 
type
MyTList<T> = class(TList<T>)
public
// Anchors constructor/destructor
constructor Create;
destructor Destroy; override;
 
class procedure Cleanup(var L: MyTList<T>); static;
end;
 
// DoubleList: instantiates MyTList<double>
DoubleList = class(MyTList<double>)
end;
 
// StringList: instantiates MyTList<string>
StringList = class(MyTList<string>)
end;
 
implementation
 
class procedure MyTList<T>.Cleanup(var L: MyTList<T>);
begin
L.Free;
end;
 
constructor MyTList<T>.Create;
begin
inherited;
end;
 
destructor MyTList<T>.Destroy;
begin
inherited;
end;
 
end.

The interface above is exposed to C++ as the following:

 
// CodeGear C++Builder
// Copyright (c) 1995, 2012 by Embarcadero Technologies, Inc.
// All rights reserved
 
// (DO NOT EDIT: machine generated header) 'DelphiUnit.pas' rev: 24.00 (Windows)
 
#ifndef DelphiunitHPP
#define DelphiunitHPP
 
#pragma delphiheader begin
#pragma option push
#pragma option -w // Display all warnings
#pragma option -w-inl // Functions %s are not expanded inline
#pragma option -w-8111 // Accessing deprecated entity
#pragma option -Vx // Zero-length empty class member
#pragma pack(push,8)
#include <System.hpp> // Pascal unit
#include <SysInit.hpp> // Pascal unit
#include <System.Generics.Collections.hpp> // Pascal unit
#include <System.Generics.Defaults.hpp> // Pascal unit
#include <System.Types.hpp> // Pascal unit
 
//-- user supplied -----------------------------------------------------------
 
namespace Delphiunit
{
//-- type declarations -------------------------------------------------------
template<typename T> class DELPHICLASS MyTList__1;
// Template declaration generated by Delphi parameterized types is
// used only for accessing Delphi variables and fields.
// Don't instantiate with new type parameters in user code.
template<typename T> class PASCALIMPLEMENTATION MyTList__1 : public System::Generics::Collections::TList__1<T>
{
typedef System::Generics::Collections::TList__1<T> inherited;
 
public:
__fastcall MyTList__1(void);
__fastcall virtual ~MyTList__1(void);
static void __fastcall Cleanup(MyTList__1<T>* &L);
};
 
 
class DELPHICLASS DoubleList;
class PASCALIMPLEMENTATION DoubleList : public MyTList__1<double>
{
typedef MyTList__1<double> inherited;
 
public:
/* {DelphiUnit}MyTList<System_Double>.Create */ inline __fastcall DoubleList(void) : MyTList__1<double>() { }
/* {DelphiUnit}MyTList<System_Double>.Destroy */ inline __fastcall virtual ~DoubleList(void) { }
 
};
 
 
class DELPHICLASS StringList;
class PASCALIMPLEMENTATION StringList : public MyTList__1<System::UnicodeString>
{
typedef MyTList__1<System::UnicodeString> inherited;
 
public:
/* {DelphiUnit}MyTList<System_string>.Create */ inline __fastcall StringList(void) : MyTList__1<System::UnicodeString>() { }
/* {DelphiUnit}MyTList<System_string>.Destroy */ inline __fastcall virtual ~StringList(void) { }
 
};
 
 
//-- var, const, procedure ---------------------------------------------------
} /* namespace Delphiunit */
#if !defined(DELPHIHEADER_NO_IMPLICIT_NAMESPACE_USE) && !defined(NO_USING_NAMESPACE_DELPHIUNIT)
using namespace Delphiunit;
#endif
#pragma pack(pop)
#pragma option pop
 
#pragma delphiheader end.
//-- end unit ----------------------------------------------------------------
#endif // DelphiunitHPP

C++ code linking with the .obj created from the above Delphi unit can use instances of MyTList__1<double> or MyTList__1<System::String>.

 
void UseDLists()
{
// C++ code can use the Generics defined in Delphi directly
// as long as the C++ code limits itself to types for which
// the generic was instantiated on the Delphi side. For example,
// since the Delphi Unit instantiates MyTList<String>
// and MyTList<double> we can use these here.
// However, if we try to use MyTList__1<char> we'll get
// errors since the Delphi side did not instantiate
// MyTList<AnsiChar>.
MyTList__1<double>* dblList = new MyTList__1<double>();
dblList->Add(1.0);
dblList->Add(1.5);
double d = dblList->Items[1];
#ifdef _WIN64
delete dblList
#else
MyTList__1<double>::Cleanup(dblList);
#endif
 
MyTList__1<System::String> *strList = new MyTList__1<System::String>();
strList->Add("First");
strList->Add("Second");
strList->Add("Third");
assert(strList->Count == 3);
 
System::String str = strList->Items[0];
assert(str == "First");
assert(strList->Items[1] == "Second");
assert(strList->Items[2] == "Third");
 
strList->Insert(0, "Inserted");
assert(strList->Count == 4);
assert(strList->Items[0] == "Inserted");
assert(strList->Items[1] == "First");
 
strList->Reverse();
assert(strList->Items[0] == "Third");
assert(strList->Items[1] == "Second");
assert(strList->Items[2] == "First");
assert(strList->Items[3] == "Inserted");
 
assert(strList->Contains("Inserted"));
assert(!strList->Contains("Not Inserted"));
 
strList->Sort();
strList->Remove("Inserted");
 
assert(strList->Items[0] == "First");
assert(strList->Items[1] == "Second");
assert(strList->Items[2] == "Third");
 
#ifdef _WIN64
delete strList;
#else
MyTList__1<System::String>::Cleanup(strList);
#endif
}

If C++ code attempts to use a Delphi generic for types that were not instantiated in Delphi, you'll get errors at link time. For example, the following code attempts to use MyTList__1<char> when the Delphi code did not explicitly instantiate MyTList<AnsiChar>:

 
void UseListOfChar()
{
MyTList__1<char>* charList = new MyTList__1<char>();
charList->Add('a');
// ...
}

While the code above compiles, the following errors are generated at link time:

 
[ilink32 Error] Error: Unresolved external 'Delphiunit::MyTList__1<char>::' referenced from CPPUNIT.OBJ
[ilink32 Error] Error: Unresolved external '__fastcall Delphiunit::MyTList__1<char>::MyTList__1<char>()' referenced from CPPUNIT.OBJ
[ilink32 Error] Error: Unresolved external '__fastcall System::Generics::Collections::TList__1<char>::Add(const const char)' referenced from CPPUNIT.OBJ
[ilink32 Error] Error: Unable to perform link

To eliminate the error, you have to make sure that the Delphi code uses the type MyTList<AnsiChar>.

TDictionary

http://docwiki.embarcadero.com/CodeExamples/Berlin/en/Generics_Collections_TDictionary_(Delphi)

字典

队列

TQueue
http://docwiki.embarcadero.com/Libraries/Berlin/en/System.Generics.Collections.TQueue
http://docwiki.embarcadero.com/CodeExamples/Berlin/en/Generics_Collections_TQueue_(Delphi) http://docwiki.embarcadero.com/Libraries/Berlin/en/System.Generics.Collections TArray    
TDictionary    
TEnumerable    
TEnumerator    
TList    
TObjectDictionary    
TObjectList    
TObjectQueue    
TObjectStack    
TQueue    
TStack    
TThreadedQueue    
TThreadList

delphi 泛型 c++builder 泛型的更多相关文章

  1. Delphi 2010 中的泛型

    Delphi 2010 中的泛型 2010已发布很长时间了,口碑还不错,准备用它开发下一项目,但对泛型等新东西的认识还不够,就搜了一下,发现下面这篇文章,还不错,大家一起补补课吧! C++中的模板.C ...

  2. Delphi 和 C++Builder 2014年及以后技术路线图

    RAD Studio, Delphi 和 C++Builder 2014年及以后技术路线图 By: Embarcadero News 内容源自Embarcadero新闻组,本人水平有限,欢迎各位高人修 ...

  3. delphi 连接 c++ builder 生成obj文件

    delphi 连接 c++ builder 生成obj文件 delphi 可以连接c++ builder 生成OMF格式的obj文件,会报一个错.[DCC Error] E2065 Unsatisfi ...

  4. Java 泛型 Java使用泛型的意义

    Java 泛型 Java使用泛型的意义 @author ixenos 直接意义 在编译时保证类型安全 根本意义 a) 类型安全问题源自可复用性代码的设计,泛型保证了类型安全的复用模板 b) 使用复用性 ...

  5. C#非泛型集合和泛型集合的超级详解

    C# 泛型集合之非泛型集合类与泛型集合类的对应: ArrayList对应List HashTable对应Dictionary Queue对应Queue Stack对应Stack SortedList对 ...

  6. 重读《深入理解Java虚拟机》六、Java泛型 VS C#泛型 (伪泛型 VS 真泛型)

    一.泛型的本质 泛型是参数化类型的应用,操作的数据类型不限定于特定类型,可以根据实际需要设置不同的数据类型,以实现代码复用. 二.Java泛型 Java 泛型是Java1.5新增的特性,JVM并不支持 ...

  7. delphi 调用 c++builder

    delphi 调用 c++builder c++builder 调用delphi 混合调用,mix https://community.embarcadero.com/blogs/entry/mixi ...

  8. Java泛型总结——吃透泛型开发

    什么是泛型 泛型是jdk5引入的类型机制,就是将类型参数化,它是早在1999年就制定的jsr14的实现. 泛型机制将类型转换时的类型检查从运行时提前到了编译时,使用泛型编写的代码比杂乱的使用objec ...

  9. 自定义泛型_无多态_通配符无泛型数组_jdk7泛型使用

    通配符 T, K, V, E 等泛型字母为有类型, 类型参数赋予具体的值 ? 未知类型 类型参数赋予不确定值, 任意类型 只能用在 声明类型上,方法参数上, 不能用在定义泛型类上 上限 extends ...

随机推荐

  1. svn分支开发注意事项

    1.切换的时候最好查看本文件的是主干上的还是分支上的, 单击右键,点击属性,可以看到以下图片,其中"URL"就可以 看到是主干还是分支 2.切换到分支 点击切换后就选择要切换到的路 ...

  2. php 的交互命令行

    php 的交互命令行 使用过 python 都知道 python 可以使用交互命令. 如下图: 但是 执行 php 显示这个是什么鬼? 按回车和加分号都没用,这是什么原因? 其实是因为使用 php 交 ...

  3. 先进驾驶员辅助系统ADSA

    ADSA(Advanced Driver-Assistance Systems)字面翻译过来是“先进驾驶员辅助系统”,实际上它是一种“辅助驾驶员更便捷更安全使用汽车”的系统. ADAS的研发历史可以追 ...

  4. 转换sql文件的create table语句为drop table语句

    package com.csii.pweb.query.action; import java.io.BufferedReader; import java.io.FileNotFoundExcept ...

  5. oracle 无法启动图形界面,no protocol specified

    linux 终端启动图形化程序界面时报错:No protocol specified这是因为Xserver默认情况下不允许别的用户的图形程序的图形显示在当前屏幕上. 如果需要别的用户的图形显示在当前屏 ...

  6. 获取post发送过来的xml包

     if (Request.HttpMethod.ToLower() == "post") { byte[] ar; ar = new byte[this.Request.Input ...

  7. js正则表达式30分钟入门教程

    2011-10-27 13:23:15 如何使用本教程 最重要的是——请给我30分钟,如果你没有使用正则表达式的经验,请不要试图在30秒内入门——除非你是超人 :) 别被下面那些复杂的表达式吓倒,只要 ...

  8. Python中的偏函数

    偏函数是从Python2.5引入的一个概念,通过functools模块被用户调用. 偏函数是将所要承载的函数作为partial()函数的第一个参数,原函数的各个参数依次作为partial()函数后续的 ...

  9. linux 信号处理 六(全)

    一.信号及信号来源 信号本质 信号是在软件层次上对中断机制的一种模拟,在原理上,一个进程收到一个信号与处理器收到一个中断请求可以说是一样的.信号是异步的,一个进程不必通过任何操作来等待信号的到达,事实 ...

  10. MyEclipse中复制web项目,部署之后访问报错