Type Tunnel pattern

In computer programming, a Type Tunnel pattern is where a group of physically unrelated types may be tunneled through an extensible adaptation layer and presented in unified form to an underlying layer for manipulation as a whole. It consists of the following:

  1. a generic, extensible interface layer, used in client code, which can interact with heterogeneous types, and
  2. a tunnel mechanism, which translates between the heterogeneous types expressed in the client code into the type understood by
  3. a concrete API layer, which manipulates a single concrete type.

Tunnel mechanism include Shims and conversion constructors.

Examples

C++

Example that uses Shims as tunnel mechanism.

// 1. Interface layer
template <typename S> 
void foo(S s) 
{    
    bar(to_cstr_ptr(s)); 
}

// 2. Tunnel mechanism: Shim
char const* to_cstr_ptr(int) { ... }
char const* to_cstr_ptr(char const*) { ... }
char const* to_cstr_ptr(std::string) { ... }

// 3. Concrete API layer
void bar(char const*) { ... }

// Usage
int main() 
{
    foo(123); 
    foo("a C string"); 
    foo(std::string("a std::string")); 
}

See also

References

  • Wilson, Matthew (August 2003), "Generalized String Manipulation: Access Shims and Type Tunneling", C/C++ Users Journal, 21 (8)
  • Wilson, Matthew. "Breaking Up The Monolith: Advanced C++ Design without Compromise". Retrieved 11 March 2010.


This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.