Jump to content
 







Main menu
   


Navigation  



Main page
Contents
Current events
Random article
About Wikipedia
Contact us
Donate
 




Contribute  



Help
Learn to edit
Community portal
Recent changes
Upload file
 








Search  

































Create account

Log in
 









Create account
 Log in
 




Pages for logged out editors learn more  



Contributions
Talk
 



















Contents

   



(Top)
 


1 Structure and implementation  



1.1  Example  



1.1.1  C interface  





1.1.2  C++ wrapper  







1.2  Driver wrappers  







2 Cross-language/runtime interoperability  





3 Existing wrapper libraries  





4 See also  














Wrapper library






Français

Magyar
Polski
Русский
Türkçe
Українська

 

Edit links
 









Article
Talk
 

















Read
Edit
View history
 








Tools
   


Actions  



Read
Edit
View history
 




General  



What links here
Related changes
Upload file
Special pages
Permanent link
Page information
Cite this page
Get shortened URL
Download QR code
Wikidata item
 




Print/export  



Download as PDF
Printable version
 
















Appearance
   

 






From Wikipedia, the free encyclopedia
 


Wrapper libraries (orlibrary wrappers) consist of a thin layer of code (a "shim") which translates a library's existing interface into a compatible interface. This is done for several reasons:

Wrapper libraries can be implemented using the adapter, façade, and to a lesser extent, proxy design patterns.

Structure and implementation[edit]

The specific way in which a wrapper library is implemented is highly specific to the environment it is being written in and the scenarios which it intends to address. This is especially true in the case when cross-language/runtime interoperability is a consideration.

Example[edit]

The following provides a general illustration of a common wrapper library implementation. In this example, a C++ interface acts as a "wrapper" around a C interface.

C interface[edit]

int pthread_mutex_init(pthread_mutex_t * mutex , pthread_mutexattr_t * attr);
int pthread_mutex_destroy (pthread_mutex_t * mutex);
int pthread_mutex_lock (pthread_mutex_t * mutex );
int pthread_mutex_unlock (pthread_mutex_t * mutex );

C++ wrapper[edit]

class Mutex
{
     pthread_mutex_t mutex;

public:
     Mutex() 
     {
          pthread_mutex_init(&mutex, 0);
     }

     ~Mutex()
     {
          pthread_mutex_destroy(&mutex);
     }

private:
     friend class Lock;

     void lock()
     {
          pthread_mutex_lock(&mutex);
     }

     void unlock()
     {
          pthread_mutex_unlock(&mutex);
     }
};

class Lock
{
private:
      Mutex &mutex;

public:
      Lock(Mutex &mutex): mutex{mutex}
      {
            mutex.lock();
      }

      ~Lock()
      {
            mutex.unlock();
      }
};

The original C interface can be regarded as error prone, particularly in the case where users of the library forget to unlock an already locked mutex. The new interface effectively utilizes resource acquisition is initialization (RAII) in the new Mutex and Lock classes to ensure Mutexs are eventually unlocked and pthread_mutex_t objects are automatically released.

The above code closely mimics the implementation of boost::scoped_lock and boost::mutex which are part of the boost::thread library.

Driver wrappers[edit]

Cross-language/runtime interoperability[edit]

Some wrapper libraries exist to act as a bridge between a client application and a library written using an incompatible technology. For instance, a Java application may need to execute a system call. However system calls are typically exposed as C library functions. To resolve this issue Java implements wrapper libraries which make these system calls callable from a Java application.

In order to achieve this, languages like Java provide a mechanism called foreign function interface that makes this possible. Some examples of these mechanisms include:

Existing wrapper libraries[edit]

Some examples of existing wrapper libraries:

See also[edit]


Retrieved from "https://en.wikipedia.org/w/index.php?title=Wrapper_library&oldid=1233824614"

Category: 
Computer libraries
Hidden categories: 
Articles with short description
Short description is different from Wikidata
 



This page was last edited on 11 July 2024, at 02:35 (UTC).

Text is available under the Creative Commons Attribution-ShareAlike License 4.0; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc., a non-profit organization.



Privacy policy

About Wikipedia

Disclaimers

Contact Wikipedia

Code of Conduct

Developers

Statistics

Cookie statement

Mobile view



Wikimedia Foundation
Powered by MediaWiki