Opaque pointer

Opaque pointer

In computer programming, 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 type.

Opaque pointers are present in several programming languages including Ada, C, C++ and Modula-2.

If the language is strongly typed, programs and procedures that have no other information about an opaque pointer type T can still declare variables, 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 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 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.[1] 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",[2] the "Pimpl idiom" (for "pointer to implementation idiom"),[3] "Compiler firewall idiom"[4] or "Cheshire Cat", especially among the C++ community.[2]

Contents

Examples

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.[5]

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 */
#include "cat.h"
#include <stdlib.h>
 
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 (encapsulation) aspect of Object-Oriented Programming 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 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:
#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, this method allows class declarations to omit private data members, except for the d-pointer itself.[6] 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 and KDE libraries.

C#

See Private class data pattern

See also

References

  1. ^ Chris McKillop. "Programming Tools — Opaque Pointers". QNX Software Systems. http://www.qnx.com/developers/articles/article_302_2.html. Retrieved 2005-08-29. 
  2. ^ a b Bruce Eckel (2000). "Chapter 5: Hiding the Implementation". Thinking in C++, Volume 1: Introduction to Standard C++ (2nd Edition ed.). Prentice Hall. ISBN 0-13-979809-9. http://www.codeguru.com/cpp/tic/tic0067.shtml. 
  3. ^ Vladimir Batov (2008-01-25). "Making Pimpl Easy". Dr. Dobb's Journal. http://ddj.com/cpp/205918714. Retrieved 2008-05-07. 
  4. ^ Herb Sutter. The Joy of Pimpls (or, More About the Compiler-Firewall Idiom)
  5. ^ Robert A. Duff (2002-07-29). "Re: What's its name again?". comp.lang.ada. (Web link). Retrieved 2007-10-11. 
  6. ^ Using a d-Pointer — Why and how KDE implements opaque pointers

External links


Wikimedia Foundation. 2010.

Игры ⚽ Нужно решить контрольную?

Look at other dictionaries:

  • 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… …   Wikipedia

  • Pointer (computing) — This article is about the programming data type. For the input interface (for example a computer mouse), see Pointing device. Pointer a pointing to the memory address associated with variable b. Note that in this particular diagram, the computing …   Wikipedia

  • Smart pointer — In computer science, a smart pointer is an abstract data type that simulates a pointer while providing additional features, such as automatic garbage collection or bounds checking. These additional features are intended to reduce bugs caused by… …   Wikipedia

  • Canadian Pointer — Infobox Dogbreed altname = Adirondak Pointing Dog country = United States image caption = Canadian Pointers may vary in color name = Canadian Pointer notrecognized = yes note= This breed is accepted by the American Rare Breed Association A… …   Wikipedia

  • Laser pointer — A laser pointer is a portable, pen sized laser designed to be held in the hand, and most commonly used to project a point of light to highlight items of interest during a presentation. Most laser pointers have low enough power that the projected… …   Wikipedia

  • GObject — The GLib Object System, or GObject, is a free software library (covered by the LGPL) that provides a portable object system and transparent cross language interoperability. GObject is designed for use both directly in C programs and through… …   Wikipedia

  • Handle — may be:* Handle (grip), a grip attached to an object for using or moving the object * Handle (mathematics), a topological ball * Handle (computing), a particular kind of smart pointer Handle may also be:* Handle System, a system for uniquely… …   Wikipedia

  • Application binary interface — In computer software, an application binary interface (ABI) describes the low level interface between an application program and the operating system, or the interface between an application and its libraries, or that between component parts of… …   Wikipedia

  • Private class data pattern — Private class data is a design pattern in computer programming used to encapsulate class attributes and their manipulation.tandard documentationThe following documentation categories for the private class data design pattern follows the design… …   Wikipedia

  • Cheshire Cat (disambiguation) — The Cheshire Cat is a fictional character from Lewis Carroll s Alice s Adventures in Wonderland and derived works. Cheshire Cat may also refer to: Cheshire Cat (Blink 182 album), 1994 Cheshire Cat (Ronnie Foster album), 1975 Cheshire Cat… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”