Jump to content

Opaque pointer: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
m remove broken attempt at self-link
Line 24: Line 24:
| url = http://ddj.com/cpp/205918714
| url = http://ddj.com/cpp/205918714
| accessdate = 2008-05-07
| accessdate = 2008-05-07
}}</ref>, "'''Compiler firewall idiom'''"<ref>Herb Sutter. ''[http://www.gotw.ca/publications/mill05.htm The Joy of Pimpls (or, More About the Compiler-Firewall Idiom)]''</ref> or "'''[[Cheshire Cat]]'''", especially among the C++ community.<ref name="eckel20000"/>
}}</ref>, "'''Compiler firewall idiom'''"<ref>Herb Sutter. ''[http://www.gotw.ca/publications/mill05.htm The Joy of Pimpls (or, More About the Compiler-Firewall Idiom)]''</ref> or "'''Cheshire Cat'''", especially among the C++ community.<ref name="eckel20000"/>
<!-- The latter term is attributed to [[John Carolan]], one of the early pioneers in C++. The implementation disappears (the cat), except a pointer to it (the smile) -->
<!-- The latter term is attributed to [[John Carolan]], one of the early pioneers in C++. The implementation disappears (the cat), except a pointer to it (the smile) -->



Revision as of 18:43, 17 June 2008

In computer programming, an opaque pointer is a datatype that hides its internal implementation using a pointer. This allows the implementation of the whole interface to change without the need to recompile the modules using it.[1] This is important for providing binary compatibility through different versions of a shared library, for example.

This technique is sometimes referred 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]

Examples

Opaque pointers are present in several programming languages, for example Ada, C/C++ or Modula-2.

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++

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

public:
    Handle();            // Constructor
    ~Handle();           // Destructor
    // Other operations...
};
#include "handle.h"

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

Handle::Handle() {
    smile = new CheshireCat;
}

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 defined in the class's implementation file. Developed 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's 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

/* 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)
{
  int result = -1;
  
  if (cat)
  {
    result = cat->smile;
    
    cat->smile = newsmile;
  }
    
  return result;
}

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.

See also

References

  1. ^ Chris McKillop. "Programming Tools — Opaque Pointers". QNX Software Systems. 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. {{cite book}}: |edition= has extra text (help); External link in |chapterurl= (help); Unknown parameter |chapterurl= ignored (|chapter-url= suggested) (help)
  3. ^ Template:Cite online journal
  4. ^ Herb Sutter. The Joy of Pimpls (or, More About the Compiler-Firewall Idiom)
  5. ^ Robert A. Duff (2002-07-29). "Re: What's it's name again?". Newsgroupcomp.lang.ada. Retrieved 2007-10-11.
  6. ^ Using a d-Pointer — Why and how KDE implements opaque pointers

External links