Opaque pointer
Encyclopedia
In computer programming
Computer programming
Computer programming is the process of designing, writing, testing, debugging, and maintaining the source code of computer programs. This source code is written in one or more programming languages. The purpose of programming is to create a program that performs specific operations or exhibits a...

, an opaque pointer is a special case of an opaque data type
Opaque data type
In computer science, an opaque data type is a user defined data type used like built-in data type. It is incompletely defined in an interface, so that ordinary client programs can only manipulate data of that type by calling procedures that have access to the missing information.-Overview:Opaque...

, a datatype that is declared to be a pointer to a record
Record (computer science)
In computer science, a record is an instance of a product of primitive data types called a tuple. In C it is the compound data in a struct. Records are among the simplest data structures. A record is a value that contains other values, typically in fixed number and sequence and typically indexed...

 or data structure
Data structure
In computer science, a data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.Different kinds of data structures are suited to different kinds of applications, and some are highly specialized to specific tasks...

 of some unspecified type.

Opaque pointers are present in several programming language
Programming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....

s including Ada
Ada (programming language)
Ada is a structured, statically typed, imperative, wide-spectrum, and object-oriented high-level computer programming language, extended from Pascal and other languages...

, C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

, C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...

 and Modula-2
Modula-2
Modula-2 is a computer programming language designed and developed between 1977 and 1980 by Niklaus Wirth at ETH Zurich as a revision of Pascal to serve as the sole programming language for the operating system and application software for the personal workstation Lilith...

.

If the language is strongly typed, program
Computer program
A computer program is a sequence of instructions written to perform a specified task with a computer. A computer requires programs to function, typically executing the program's instructions in a central processor. The program has an executable form that the computer can use directly to execute...

s and procedures
Subroutine
In computer science, a subroutine is a portion of code within a larger program that performs a specific task and is relatively independent of the remaining code....

 that have no other information about an opaque pointer type T can still declare variable
Variable (programming)
In computer programming, a variable is a symbolic name given to some known or unknown quantity or information, for the purpose of allowing the name to be used independently of the information it represents...

s, arrays, and record fields of type T, assign values of that type, and compare those values for equality. However, they will not be able to de-reference
Reference (computer science)
In computer science, a reference is a value that enables a program to indirectly access a particular data item, such as a variable or a record, in the computer's memory or in some other storage device. The reference is said to refer to the data item, and accessing those data is called...

 such a pointer, and can only change the object's content by calling some procedure that has the missing information.

Opaque pointers are a way to hide the implementation details of an interface
Interface (computer science)
In the field of computer science, an interface is a tool and concept that refers to a point of interaction between components, and is applicable at the level of both hardware and software...

 from ordinary clients, so that the implementation may be changed without the need to recompile the modules using it. This benefits the programmer as well since a simple interface can be created, and most details can be hidden in another file. This is important for providing binary code compatibility through different versions of a shared library, for example.

This technique is sometimes referred to as "handle classes", the "Pimpl idiom" (for "pointer to implementation idiom"), "Compiler firewall idiom" or "Cheshire Cat", especially among the C++ community.

Ada


package Library_Interface is

type Handle is limited private;

-- Operations...

private
type Hidden_Implementation; -- Defined in the package body
type Handle is access Hidden_Implementation;
end Library_Interface;

The type Handle is an opaque pointer to the real implementation, that is not defined in the specification. Note that the type is not only private (to forbid the clients from accessing the type directly, and only through the operations), but also limited (to avoid the copy of the data structure, and thus preventing dangling references).

package body Library_Interface is

type Hidden_Implementation is record
... -- The actual implementation can be anything
end record;

-- Definition of the operations...

end Library_Interface;

These types are sometimes called "Taft types"—named after Tucker Taft, the main designer of Ada 95—because they were introduced in the so-called Taft Amendment to Ada 83.

C


/* cat.h */
typedef struct cat_t *cat_handle;

/*
* Even though the compiler doesn't know anything about the struct
* at this point, it can use a pointer to that struct.
*/

/*
* This function creates a cat object with the given smile value.
* A result of NULL indicates an error.
*/
cat_handle cat_Create(int smile);

/*
* This function destroys a cat object.
* If called with NULL, no action is taken.
*/
void cat_Destroy(cat_handle cat);

/*
* This function sets the smile value of a cat object to the
* new value, and returns the old value.
* A result of -1 indicates an error.
*/
int cat_Smile(cat_handle cat, int newsmile);


/* cat.c */
  1. include "cat.h"
  2. include


struct cat_t {
int smile;
};

/* Create a cat object */
cat_handle cat_Create(int smile) {
cat_handle result = malloc(sizeof(struct cat_t));

if (result) {
result->smile = smile;
}

return result;
}

/* Destroy a cat object */
void cat_Destroy(cat_handle cat) {
free(cat);
}

/* Set the smile value of a cat */
int cat_Smile(cat_handle cat, int newsmile) {
if (cat) {
int t = cat->smile;
cat->smile = newsmile;
return t;
}

return -1;
}

This example demonstrates a way to achieve the information hiding
Information hiding
In computer science, information hiding is the principle of segregation of the design decisions in a computer program that are most likely to change, thus protecting other parts of the program from extensive modification if the design decision is changed...

 (encapsulation) aspect of Object-Oriented Programming
Object-oriented programming
Object-oriented programming is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction,...

 using the C language. If someone wanted to change the declaration of struct cat_t, it would be unnecessary to recompile any other modules in the program that use the cat.h header file unless the API was also changed.

C++

In the example below, the copy assignment operator
Assignment operator in C++
In the C++ programming language, the assignment operator, '=', is the operator used for assignment. Like most other operators in C++, it can be overloaded....

 takes its argument by value, eliminating the need to explicitly create a copy of the other object.


//header file:
class Handle {
private:
struct CheshireCat; // Not defined here
CheshireCat* smile; // Handle

public:
Handle; // Constructor
Handle(const Handle&); // Copy constructor
const Handle& operator=(Handle); // Copy assignment operator
~Handle; // Destructor
// Other operations...
};


//CPP file:
  1. include "handle.h"


struct Handle::CheshireCat {
// The actual implementation can be anything
// ...
};

Handle::Handle
: smile(new CheshireCat) {
// do nothing
}

Handle::Handle(const Handle& other)
: smile(new CheshireCat(*(other.smile))) {
// do nothing
}

const Handle& Handle::operator=(Handle other) {
std::swap(this->smile, other.smile);
return* this;
}

Handle::~Handle {
delete smile;
}

One type of opaque pointer commonly used in C++ class declarations is the d-pointer. The d-pointer is the only private data member of the class and points to an instance of a struct. Named by Arnt Gulbrandsen of Trolltech
Trolltech
Qt Development Frameworks is an Oslo, Norway-based software company best known for its Qt toolkit and application framework. Qt Development Frameworks is a wholly owned subsidiary of Nokia Corporation...

, this method allows class declarations to omit private data members, except for the d-pointer itself. The result is that more of the class' implementation is hidden from view, that adding new data members to the private struct does not affect binary compatibility, and that the header file containing the class declaration only has to #include those other files that are needed for the class interface, rather than for its implementation. As a side benefit, compiles are faster because the header file changes less often. The d-pointer is heavily used in the Qt
Qt (toolkit)
Qt is a cross-platform application framework that is widely used for developing application software with a graphical user interface , and also used for developing non-GUI programs such as command-line tools and consoles for servers...

 and KDE
KDE
KDE is an international free software community producing an integrated set of cross-platform applications designed to run on Linux, FreeBSD, Microsoft Windows, Solaris and Mac OS X systems...

 libraries.

See also

  • Application binary interface
    Application binary interface
    In computer software, an application binary interface describes the low-level interface between an application program and the operating system or another application.- Description :...

  • Handle (computer science)
  • Programming idiom
    Programming idiom
    A programming idiom is a means of expressing a recurring construct in one or more programming languages. Generally speaking, a programming idiom is an expression of a simple task or algorithm that is not a built-in feature in the programming language being used, or, conversely, the use of an...


External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK