wrapper library
≈ 2 min read
Names in other languages
- Françaispaquetage de bibliothèques
- Українськаобгортка бібліотеки
- Русскийобёртка библиотеки
- Englishwrapper library
- 日本語ラッパーライブラリ
- Polskiwrapper
- 中文包装器
- 繁體中文包裝器
Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.
Wrapper libraries (or library 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: * To refine a poorly designed or complicated interface * Allow code to work together which otherwise cannot (e.g. incompatible data formats) * Enable cross language and/or runtime interoperability Wrapper libraries can be implemented using the adapter, façade, and to a lesser extent, proxy design patterns.
Wrapper libraries (or library 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: To refine a poorly designed or complicated interface Allow code to work together which otherwise cannot (e.g. incompatible data formats) Enable cross language and/or runtime interoperability Wrapper libraries can be implemented using the adapter, façade, and to a lesser extent, proxy design patterns. include // previous declarations... int pthread_mutex_init(pthread_mutex_t* mutex , const 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); // more functions... export module org.posix.PosixThread; import ; export namespace org::posix { // previous wrappers... class ThreadMutex { private: ::pthread_mutex_t mutex; friend class ThreadLock; void lock() noexcept { ::pthread_mutex_lock(&mutex); } void unlock() noexcept { ::pthread_mutex_unlock(&mutex); } public: ThreadMutex() { ::pthread_mutex_init(&mutex, 0); } ~ThreadMutex() { ::pthread_mutex_destroy(&mutex); } }; class ThreadLock { private: ThreadMutex& mutex; public: explicit ThreadLock(ThreadMutex& mutex): mutex{mutex} { mutex.lock(); } ~ThreadLock() { mutex.unlock(); } }; // more wrappers... } 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 and classes to ensure s are eventually unlocked and objects are automatically released. The above code closely mimics the implementation of and classes from Boost which are part of the Boost.Thread library. ==See also== Wrapper function Wrapper pattern Glue code
Text derived from https://en.wikipedia.org/w/index.php?oldid=1317928613 under CC BY-SA 4.0; modifications have been made.