PTypes 介绍

Wolf0403 2005-02-19 03:22:35
PTypes (C++ Portable Types Library) is a simple alternative to the STL that includes multithreading and networking. It defines dynamic strings, variants, character sets, lists and other basic data types along with portable thread and synchronization objects, IP sockets and named pipes. Its main `target audience' is developers of complex network daemons, robots or non-visual client/server applications of any kind.

PTypes (C++ Portable Types Library)是一个 STL 的简单替代品,包括了多线程和网络通信。它定义了动态字符串、通用类型、字符集合、列表和其它基本数据类型以及可遗址的线程和同步对象,IP 套接字以及命名管道。它的主要实现目标是用于实现各种复杂的网络服务程序、机器人或者非可视化交互的客户机/服务器程序。

PTypes defines simple and intuitive interfaces and differs from the STL in fairly moderate use of templates. The library is portable across many modern operating systems (currently FreeBSD, Linux, SunOS, Mac OS X and Windows). All platform-dependent issues are hidden inside. A simple web server called wshare is included in the package to demonstrate the full power of the library.

PTypes 定义了简单而直观的接口;与 STL 不同的是它对使用模板采取的保守态度。PTypes 库可以移植到许多现代操作系统上(目前已经有 FreeBSD、Linux、SunOS、Mac OS X 和 Windows 平台的移植)。所有平台相关细节都被隐藏在内部。一个简单的叫做 wshare 的网页服务器包含在其中,用于展示库的使用。

主页:http://www.melikyan.com/ptypes/
下载:http://prdownloads.sourceforge.net/ptypes/ptypes-2.0.2.tar.gz
http://www.melikyan.com/ptypes/ptypes-2.0.2.tar.gz
...全文
727 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
Wolf0403 2005-02-21
  • 打赏
  • 举报
回复
呵呵,谢谢鸡丁和血精灵两位斑竹置顶。

上面这些都是翻译的 PTypes 文档中非细节相关的内容,旨在对 PTypes 做一个概括的介绍。PTypes 是我用过的时间最长、分析的相对而言最透彻的一个库(Win32 部分代码完整分析过),感觉对于简单的情况还是非常好用的,因此在这里向更多的人做一个介绍。
yjh1982 2005-02-21
  • 打赏
  • 举报
回复
废人啊废人,早就请你写,才开始.....置顶一个月!
nodummy 2005-02-21
  • 打赏
  • 举报
回复
本来还想抢1来着……
Wolf0403 2005-02-21
  • 打赏
  • 举报
回复
Portability and performance issues
可移植性与性能问题

64-bit integers: most compilers provide long long type modifier for declaring 64-bit integers, but at least two of them, Microsoft C/C++ and Borland C/C++, use quite unaesthetic keyword __int64. PTypes offers its own wrapper type large (in <pport.h>), which is a portable signed 64-bit integer. Unsigned large or ularge are also available. For portable 64-bit string conversions use itostring() and stringto*() declared in <ptypes.h>.
64位整数:多数编译器提供了 long long 类型修饰符用以声明 64 位整数,但是其中至少有两种,Microsoft C/C++ 和 Borland C/C++,使用缺乏美感的 __int64 关键字。PTypes 提供了自己的类型包装:large(在 <pport.h> 中声明),用以声明可移植的有符号 64 位整数。同时提供了对应的无符号类型 unsigned large 或 ularge。

Another incompatibility exists for printf-style formatted output routines: Windows uses "I64" type modifier for 64-bit integers, whereas standard-compliant libraries recognize "ll" (ell-ell). PTypes solves this problem in its outstm::putf() method: you can safely use "ll" modifier on all platforms.
另外一个不兼容问题出现在 printf 类的格式化输出函数中:Windows 使用“I64”类型限定符表示 64 位整数,而与标准兼容的库函数使用“ll”(两个 l )。PTypes 在它的 outstm::putf() 函数中解决了这个问题:你可以安全的在任意平台使用“ll”修饰符。

End-of-line sequences: PTypes input stream routines automatically recognize three types of EOL sequences: LF (UNIX), CR (Mac) and CR-LF (DOS/Win) and set instm::eol property accordingly.
行结束序列:PTypes 输入流函数自动识别三种行结束序列:LF(UNIX),CR(MAC)和 CR-LF(DOS/Windows),同时对应的设定 instm::eol 的值。

As for output streams, unlike many Windows-based C run-time libraries, PTypes does NOT make any translations and always sends your data as is. In order to generate an EOL sequence appropriate to the target platform (i.e. the system your application is running on), use outstm::puteol() and outstm::putline(string). Some protocols and data formats, however, require some predetermined EOL sequence regardless of the target platform, e.g. HTTP always uses CR-LF, PostScript uses CR, etc. In such cases you can use standard C escape sequences to send appropriate codes to the output stream, e.g. "\r\n" or "\r".
对于输出流,不同于基于 Windows 平台的 C 运行时库,PTypes 不对数据做任何处理而是直接发送。要生成平台对应的行结束(EOL)序列(例如,将要运行你的程序的平台),使用 outstm::puteol() 和 outstm::putline(string)。但是。一些协议与数据格始终要求特定的 EOL 序列而不论什么平台,例如 HTTP 始终要求 CR-LF,PostScript 使用 CR,等等。在这种情况下你可以使用标准 C 替换序列来发送特定代码到输出流,例如,"\r\n" 或 "\r"。

File names: you can always use UNIX-style path names (with slash '/') when working with files through PTypes streaming classes. Since drive specifiers in path names are specific only to Windows, try to use relative paths in your program whenever possible. For additional notes on pipe naming, please see introduction for the namedpipe class.
文件名:使用 PTypes 流类型的时候,你可以始终使用 UNIX 式样的路径名(用斜线 '/')。因为路径中的驱动器识别符是特定于 Windows 平台的,应尽量避免在你的程序中使用。关于命名管道的更多信息请参考 namedpipe 类的介绍文档。

Arithmetic operations and multithreading: a simple increment and decrement operation (i++ and i--) in a multithreaded application may NOT be thread-safe on most RISC CPU's and on i386-based multiprocessor systems. Even if you are not interested in the resulting value of an increment/decrement operation, still use PTypes pincrement() and pdecrement() on variables shared between threads.
算术操作与多线程:在 RISC 处理器环境或基于 i386 平台的多处理器环境中,简单的递增、递减操作(i++ 和 i--)在多线程环境中可能是不安全的。即使你并不关心递增、递减操作的结果,也应该使用 PTypes 的 pincrement() 和 pdecrement() 操作修改线程共享数据。

Performance of dynamic strings: most of the string operations rely on dynamic memory reallocation. PTypes tries to avoid frequent use of physical reallocation by aligning the dynamic string buffers on 16, 32, 64 ... 4096-byte boundaries, depending on the logical size of a string. Physical reallocation highly depends on the performance of the standard realloc() function, which may vary from OS to OS. Linux, for example, performs reallocations up to 30 times faster than Windows on a similar computer. PTypes dynamic strings also depend on the performance of the atomic increment and decrement functions (see below).
动态字符串的性能:多数字符串操作依赖动态内存重分配操作。PTypes 为试图尽量避免物理重分配内存而尽量将动态字符串内存按照 16、32、64……4096 字节进行边界对齐,具体的值取决于字符串的逻辑长度。物理重分配内存的效率取决于标准 realloc() 函数的效率,而这在各个平台会有所区别。例如,在一台相似的电脑上,Linux 系统可能比 Windows 系统快 30 倍。PTypes 动态字符串同时还依赖原子递增、递减函数的效率(见下)。

Performance of atomic increment/decrement/exchange operations: on PowerPC and i386-based platforms PTypes implements atomic operations through short assembly language code. On other platforms, however, these operations use slower mutex locking (see src/patomic.cxx). PTypes is in desperate need of developers who can write SMP-safe atomic increment, decrement and exchange for Sparc, Alpha and other processors in assembly language. This would speed up dynamic strings and other parts of the library on these platforms.
原子递增递减操作的效率:PowerPC 和基于 i386 的平台上 PTypes 以简短的汇编码实现原子操作。不过在其它平台上,这些操作通过 mutex 锁实现(见 src/patomic.cxx)。PTypes 急需可以开发 Sparc、Alpha 和其它平台 SMP 安全的递增、递减和交换操作汇编码的程序员。这些代码可以提高字符串和库的其它部分在这些平台上的效率。

Reentrancy of synchronization objects: the POSIX standard does not specify whether synchronization objects should be reentrant or not. For instance, if you use recursive locks on a mutex object the behaviour of your application may vary on different systems. Most POSIX systems, such like Linux, provide a faster but non-reentrant implementation of synchronization primitives.
同步对象的可重入性:POSIX 标准没有规定同步对象是否可重入。例如,如果你对一个 mutex 对象使用了递归锁,在不同平台上程序的行为可能有所不同。多数 POSIX 系统,例如 Linux,提供了更快但是不可重入的同步原语。

Read/write lock portability issues: the rwlock class is built on top of the POSIX rwlock interface, wherever available, or uses the internal implementation (see src/rwlock.cxx). Various system-level implementations use different prioritization schemes: they may be 'fair' with respect to both reader and writer threads, or a higher priority may be given to one of them. Unfortunately the behaviour of rwlock is not standardized yet. PTypes' internal implementation uses the 'fair' scheme which we believe is the best and the only DoS-proof (denial-of-service) among other algorithms. On the other hand, replacing the system-level rwlock with our own implementation on all systems would lead to a preformance penalty compared to using 'native' objects. We set this issue aside for some time and we are open to discuss it with interested/experienced developers.
读写所移植性问题:rwlock 类是建立在 POSIX rwlock 接口之上的;当 POSIX rwlock 不可用则使用内部实现(见 src/rwlock.cxx)。不同的系统层实现使用了不同的优先级策略:读写线程可能是“平等”的,或者某一个具有更高优先级。不幸的是,rwlock 的行为不存在一个标准。PTypes 的内部实现使用了“平等”策略,因为我们相信这是最好的,并且也是唯一一个可以应对 Dos (拒绝服务)攻击的算法。但同时,使用 PTyps 内部实现取代系统级 rwlock 可能导致性能下降。我们将这个问题暂时搁置,并且欢迎有兴趣、有经验的开发者与我们共同探讨。


Wolf0403 2005-02-20
  • 打赏
  • 举报
回复
Why use PTypes?
为什么使用 PTypes

PTypes is yet another generic class library among tens and hundreds of others. To find its own niche in this variety of generic programming tools we decided to emphasis on simplicity of the interfaces and readability of the resulting code (i.e. the code that uses the library). The library focuses only on most widely used data types and functional interfaces. We think that like programming languages themselves, low-level libraries, too, should use the minimal set of notions for covering most programming patterns.
PTypes 是数以百计的各种通用库中的一个。为在如此众多的选择中求得生存,我们选择突出接口的简化和代码(如使用这个库的代码)的可读性。这个库集中关注最广泛使用的数据类型和功能接口。我们认为与编程语言本身一样,低层次的库同样应该使用尽可能少的概念覆盖尽可能广泛的编程模式。

Some data types in the library (strings, character sets and variants) use algebraic notation rather than object-oriented, i.e. you write length(s) for a string object instead of s.length(). We believe that traditional notation better fits to a class that represents some fundamental notion and is not supposed to produce subclasses with overridden methods. In other words, if there is no inheritance and polymorphism, there is no need for method calls as well.
库中包含的一些数据类型(字符串、字符集合和 variant)使用的接口是代数式样而非面向对象的,譬如用 length(s) 求字符串长度,而非 s.length()。我们相信传统的表示方式更适合于一些反映基础概念的、不应被派生并重写方法的类。换言之,如果不需要继承和多态,也就没有必要使用成员函数调用。

PTypes uses old-fashioned all-lowercase naming style, since it can peacefully co-exist with other styles in one program. Whether you are a `unixoid' or you are using modern writing styles (Hungarian, Borland, etc.) you can combine your favorite style with PTypes with no or very little harm to your source code.
PTypes 使用旧式的、全部小写子目的命名方式,因为这样的代码可以很轻易与其它样式的代码共存。不论你是一个“unixoid”还是使用新式规范(匈牙利式、宝兰式等等),你可以很轻易的把它们与 PTypes 结合而几乎不需要做任何改动。

And finally, in order to achieve better readability of the resulting code we use clean class naming, i.e. the string type is called string, thread is thread, etc.
最后,我们使用了清晰的类命名方式以提高代码可读性。例如,字符串类称为 string,线程类称作 thread,等等。

We designed this library in the hope that PTypes with its conciseness and intuitiveness could find its own 'target audience'.
我们设计这个库并且希望它的简明和直观的特色能吸引需要、适合它的人。
FireEmissary 2005-02-20
  • 打赏
  • 举报
回复
废人开始了?
支持支持
Wolf0403 2005-02-20
  • 打赏
  • 举报
回复
Basic features
基本特征

Threads and synchronization primitives solve the vital problem of diversity of the threading API's on different platforms. The library also offers message queues and job queues as additional methods of thread synchronization and maintenance.
线程和同步原语解决了线程 API 在不同平台上的区别导致的问题。这个库同时提供了消息队列和工作队列作为附加的线程通信和维护工具。

IP socket classes and utilities provide complete IP-based framework for both client-side and server-side programming. Combined with PTypes multithreading, these classes can be used for designing complex non-visual applications, such like network daemons or web robots.
IP 套接字类和工具类提供了完整的基于 IP 的客户端和服务器端编程框架。与 PTypes 多线程搭配,这些类可以用于设计复杂的非可视化程序,如网络服务器或者网页机器人等。

Dynamic strings, variants, character sets, date/time type and various kinds of dynamic and associative arrays: Delphi programmers will find them very similar to the ones in their favorite language. The collection of these basic data types may be useful, among other things, for building compilers and interpreters for higher-level languages.
动态字符串、variant、字符集合、日期/时间类型和其它一些动态和关联数组:Delphi 程序员会发现这些与他们最喜爱的语言中的对应元素相当类似。这些基本类型可能在设计编译器和高层语言解释器的时候相当有用。

Streaming interfaces provide buffered I/O with simple and powerful text parsing methods. A strictly defined syntax for a given text format or a formal language can be represented by calls to PTypes token extraction methods. The unified streaming interface is applicable to files, named pipes and network sockets.
流接口提供了带缓冲的 I/O 以及简单的文本解析方法。一种文件格式或者正式语言的严格语法定义可以通过一系列 PTypes 节点提取方法表示出来。可以通过单一接口操作文件、命名管道和网络套接字。

Special thread class with enhanced functionality called unit. Units have their own main() and input/output 'plugs'; they can be connected to each other within one application to form pipes, like processes in the UNIX shell.
一个功能有所加强的线程类被称为 unit。Unit 拥有自己的 main() 和输入/输出“插口”;在同一个程序中它们可以通过管道相互连接,类似 UNIX shell 中的进程。

Finally, everything above is portable: all platform-dependent details are hidden inside.
最后,以上所有都是可以移植的:所有平台相关的细节都被隐藏了。

BluntBlade 2005-02-19
  • 打赏
  • 举报
回复
死废人
xuelong_zl 2005-02-19
  • 打赏
  • 举报
回复
支持呀,可惜不是说的是什么呀,天书?
Jinhao 2005-02-19
  • 打赏
  • 举报
回复
继续,如果能把文档翻译出来,嘿嘿~~~

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧