Skip to content

Opaque data type

1、在学习"pointer to implementation idiom"的时候,发现了Opaque pointer,进而又发现了"Opaque data type"。

2、"opaque"的中文意思是"晦涩的、不透明的",在本文中,它的准确含义是"invisible",即"不可见的";显然它的反义词是"visible",即"可见的"。在 wikipedia Opaque data type 中,介绍的反义词是 "transparent",相比之下,我觉得"visible"是更好的。

3、显然,"opaque"就意味着information hiding: information是"opaque",则意味着hiding。

4、使用C++ programming language的术语来说明,"Opaque data type"是"incomplete type"。另外它还涉及forward declaration。

wikipedia Opaque data type

In computer science, an opaque data type is a data type whose concrete data structure is not defined in an interface. This enforces information hiding, since its values can only be manipulated by calling subroutines that have access to the missing information. The concrete representation of the type is hidden from its users, and the visible implementation is incomplete. A data type whose representation is visible is called transparent.[1] Opaque data types are frequently used to implement abstract data types.

NOTE:

1、有了前面的描述,再来看 'transparent' 就非常容易理解了

Typical examples of opaque data types include handles for resources provided by an operating system to application software. For example, the POSIX standard for threads defines an application programming interface based on a number of opaque types that represent threads or synchronization primitives like mutexes or condition variables.[2]

NOTE:

1、这一段的描述中的handle让我想到了file descriptor

2、这一段总结非常好,它说明清楚了很多C library的做法,现在想起来Linux system call大多数都是采用的这种做法

An opaque pointer is a special case of an opaque data type, a datatype that is declared to be a pointer to a record or data structure of some unspecified data type. For example, the standard library that forms part of the specification of the C programming language provides functions for file input and output that return or take values of type "pointer to FILE" that represent file streams (see C file input/output), but the concrete implementation of the type FILE is not specified.[3]

NOTE:

1、 opaque pointer 会进行专门的说明

Uses in various languages

Some languages, such as C, allow the declaration of opaque records (structs), whose size and fields are hidden from the client. The only thing that the client can do with an object of such a type is to take its memory address, to produce an opaque pointer.

The information which is missing in the interface may be declared in its implementation, or in another "friends-only" interface. This second option allows the hidden information to be shared by two or more modules.

NOTE:

1、interface and implementation