Hourglass Interface - C/C++ APIs for C++ Libraries
Many years ago when I started writing C++ programs that linked against commercial libraries, I wondered why most of these libraries had C APIs, even if everything pointed towards to these libraries having been written in C++ originally. Naturally, I was aware of the lack of a standardized Application Binary Interface (ABI) in C++, but it was in this context that I truly understood its implications.
As a disclaimer I should mention that I hold a PhD in computer vision and I still consider myself to be a computer vision scientist, even though I have been in managerial roles for over a decade now. In my PhD thesis, among other things, I developed complex solvers for partial differential equations (PDEs) in C and assembler, utilizing SSE intrinsics etc., but that is a story for another time. I consider myself fortunate that I have had the opportunity of implementing some of the algorithms I initially developed in Matlab and Python, in high-quality C++. This has given me insights regarding how software- and hardware interact and what kinds of problems one typically faces when implementing computer vision code for real-time applications. So, when I transitioned from writing code in C to C++, expressiveness of C++ felt like a breath of fresh air. This is the reason why it felt odd interfacing C's procedural approach with C++'s object-oriented programming.
领英推荐
Everyone writing software has heard of the Design Patterns: Elements of Reusable Object-Oriented Software, whose authors are often referred to as the Gang of Four (GoF). These patterns offer solutions, in object-oriented software, to typical problems, or implementation choices, faced by the developers. With this in mind, I recently came across an interesting technique called "Hourglass Interfaces for C++ APIs" that addresses the lack of ABI in C++. The approach is generic enough so that it might actually be considered a C/C++ pattern. It's called hourglass because it narrows down C++ to C and then widens it back to C++. In essence, we have a C API (that has a well defined ABI if we stick to primitive types) for a library written in C++ and then we implement a header only C++ API based on the C API. Among other things, the hourglass pattern proposes a nice solution to returning an array of elements from STL containers and then mapping these back to containers. Credit where credit is due, the innovative concept of the hourglass pattern was introduced by Stefanus DuToit at CppCon 2014. His insightful presentation on this topic is available on YouTube, offering a comprehensive overview of the technique and its applications in C++ programming.
Embarking on a journey to truly understand and test the hourglass approach, I implemented a C++ library with a C and C++ APIs. If you're interested, take a look at the code available at https://github.com/JarnoRalli/hourglass-c-api. Due to the complexities of the C++ language, I'm sure that there exists other very useful approaches, patterns and even anti-patterns.
Business Development Representative at dotcode | IT Sales Specialist | Blockchain & Web3 Enthusiast | Delivering high-quality software solutions | Your partner in digital growth
9 个月I always follow your posts. Thank you ??
Software Developer at AILiveSim ? CEO, Europa Software
9 个月Very interesting. I wonder if Vulkan-Hpp would qualify as an "hourglass interface".