|
| 1 | +//// |
| 2 | +Copyright (c) 2024 The C++ Alliance, Inc. (https://cppalliance.org) |
| 3 | + |
| 4 | +Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | +file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | + |
| 7 | +Official repository: https://github.com/boostorg/website-v2-docs |
| 8 | +//// |
| 9 | += Implementation Variation Techniques |
| 10 | +:navtitle: Implementation Variations |
| 11 | + |
| 12 | +This topic discusses the separation of interface and implementation. |
| 13 | + |
| 14 | +The interface specifications for boost.org library components (as well as for quality software in general) are conceptually separate from implementations of those interfaces. This may not be obvious, particularly when a component is implemented entirely within a header, but this separation of interface and implementation is always assumed. From the perspective of those concerned with software design, portability, and standardization, the interface is what is important, while the implementation is just a detail. |
| 15 | + |
| 16 | +Dietmar Kühl, one of the original Boost contributors, comments _"The main contribution is the interface, which is augmented with an implementation, proving that it is possible to implement the corresponding class and providing a free implementation."_ |
| 17 | + |
| 18 | +== Implementation Variations |
| 19 | + |
| 20 | +There may be a need for multiple implementations of an interface, to accommodate either platform dependencies or performance tradeoffs. Examples of platform dependencies include compiler shortcomings, file systems, thread mechanisms, and graphical user interfaces. The classic example of a performance tradeoff is a fast implementation that uses a lot of memory versus a slower implementation which uses less memory. |
| 21 | + |
| 22 | +Boost libraries generally use a configuration header, `boost/config.hpp`, to capture compiler and platform dependencies. Although the use of `boost/config.hpp` is not required, it is the preferred approach for simple configuration problems. |
| 23 | + |
| 24 | +== Boost Policy |
| 25 | + |
| 26 | +The Boost policy is to avoid platform dependent variations in interface specifications, but supply implementations which are usable over a wide range of platforms and applications. That means Boost libraries will use the techniques below described as appropriate for dealing with platform dependencies. |
| 27 | + |
| 28 | +The Boost policy toward implementation variations designed to enhance performance is to avoid them unless the benefits greatly exceed the full costs. The term _full costs_ is intended to include both tangible costs like extra maintenance, and intangible cost like increased difficulty in user understanding. |
| 29 | + |
| 30 | +== Techniques for Providing Implementation Variations |
| 31 | + |
| 32 | +Several techniques may be used to provide implementation variations. Each is appropriate in some situations, and not appropriate in others. |
| 33 | + |
| 34 | +=== Single General Purpose Implementation |
| 35 | + |
| 36 | +The first technique is to simply not provide implementation variation at all. Instead, provide a single general-purpose implementation, and forgo the increased complexity implied by all other techniques. |
| 37 | + |
| 38 | +[cols="1,4",stripes=none,frame=none] |
| 39 | +|=== |
| 40 | +| *Appropriate* | When it is possible to write a single portable implementation which has reasonable performance across a wide range of platforms. Particularly appropriate when alternative implementations differ only in esoteric ways. |
| 41 | +| *Not appropriate* | When implementation requires platform specific features, or when there are multiple implementations possible with widely differing performance characteristics. |
| 42 | +|=== |
| 43 | + |
| 44 | +In design discussions, some implementation is often alleged to be much faster than another, yet a timing test discovers no significant difference. The lesson is that while algorithmic differences may affect speed dramatically, coding differences such as changing a class from virtual to non-virtual members or removing a level of indirection are unlikely to make any measurable difference unless deep in an inner loop. And even in an inner loop, modern CPUs often execute such competing code sequences in the same number of clock cycles! A single general purpose implementation is often just fine. |
| 45 | + |
| 46 | +Or as Donald Knuth said in _Computing Surveys, vol 6, #4, p 268_, _"Premature optimization is the root of all evil."_. |
| 47 | + |
| 48 | +=== Macros |
| 49 | + |
| 50 | +While the evils of macros are well known, there remain a few cases where macros are the preferred solution: |
| 51 | + |
| 52 | +- Preventing multiple inclusion of headers via `#include` guards. |
| 53 | +- Passing minor configuration information from a configuration header to other files. |
| 54 | + |
| 55 | +[cols="1,4",stripes=none,frame=none] |
| 56 | +|=== |
| 57 | +| *Appropriate* | For small compile-time variations that would otherwise be costly or confusing to install, use, or maintain. More appropriate to communicate within and between library components than to communicate with library users. |
| 58 | +| *Not appropriate* | If other techniques will do. |
| 59 | +|=== |
| 60 | + |
| 61 | +To minimize the negative aspects of macros: |
| 62 | + |
| 63 | +- Only use macros when they are clearly superior to other techniques. Otherwise they should be viewed as a last resort. |
| 64 | +- Names should be all uppercase and begin with the namespace name. This will minimize the chance of name collisions. For example, the `#include` guard for a boost header called `foobar.h` might be named `BOOST_FOOBAR_H`. |
| 65 | + |
| 66 | +=== Separate Files |
| 67 | + |
| 68 | +A library component can have multiple variations, each contained in its own separate file or files. The files for the most appropriate variation are copied to the appropriate include or implementation directories at installation time. |
| 69 | + |
| 70 | +The way to provide this approach in Boost libraries is to include specialized implementations as separate files in separate sub-directories in the .ZIP distribution file. For example, the structure within the .ZIP distribution file for a library named `foobar` which has both default and specialized variations might look something like: |
| 71 | + |
| 72 | +```cpp |
| 73 | +foobar.h // The default header file |
| 74 | +foobar.cpp // The default implementation file |
| 75 | +readme.txt // Readme explains when to use which files |
| 76 | +self_contained/foobar.h // A variation with everything in the header |
| 77 | +linux/foobar.cpp // Implementation file to replace the default |
| 78 | +win32/foobar.h // Header file to replace the default |
| 79 | +win32/foobar.cpp // Implementation file to replace the default |
| 80 | +``` |
| 81 | + |
| 82 | +[cols="1,4",stripes=none,frame=none] |
| 83 | +|=== |
| 84 | +| *Appropriate* | When different platforms require different implementations, or when there are major performance differences between possible implementations. |
| 85 | +| *Not appropriate* | When it makes sense to use more that one of the variations in the same installation. |
| 86 | +|=== |
| 87 | + |
| 88 | +=== Separate Components |
| 89 | + |
| 90 | +Rather than have several implementation variations of a single component, supply several separate components. For example, the Boost library currently supplies `scoped_ptr` and `shared_ptr` classes rather than a single `smart_ptr` class parameterized to distinguish between the two cases. There are several ways to make the component choice: |
| 91 | + |
| 92 | +- Hardwired by the programmer during coding. |
| 93 | +- Chosen by programmer written runtime logic (trading off some extra space, time, and program complexity for the ability to select the implementation at run-time.) |
| 94 | + |
| 95 | +[cols="1,4",stripes=none,frame=none] |
| 96 | +|=== |
| 97 | +| *Appropriate* | When the interfaces for the variations diverge, and when it is reasonable to use more than one of the variations. When run-time selection of implementation is called for. |
| 98 | +| *Not appropriate* | When the variations are data type, traits, or specialization variations which can be better handled by making the component a template. Also not appropriate when choice of variation is best done by some setup or installation mechanism outside of the program itself. Thus usually not appropriate to cope with platform differences. |
| 99 | +|=== |
| 100 | + |
| 101 | +Note:: There is a related technique where the interface is specified as an abstract (pure virtual) base class (or an interface definition language), and the implementation choice is passed off to some third-party, such as a dynamic-link library or object-request broker. While that is a powerful technique, it is way beyond the scope of this discussion. |
| 102 | + |
| 103 | +=== Template-Based Approaches |
| 104 | + |
| 105 | +Turning a class or function into a template is often an elegant way to cope with variations. Template-based approaches provide optimal space and time efficiency in return for constraining the implementation selection to compile time. |
| 106 | + |
| 107 | +Important template techniques include: |
| 108 | + |
| 109 | +- _Data type parameterization_: this allows a single component to operate on a variety of data types and is why templates were originally invented. |
| 110 | +- _Traits parameterization_; if parameterization is complex, bundling up aspects into a single traits helper class can allow great variation while hiding messy details. The C++ Standard Library provides several examples of this idiom, such as `iterator_traits<>` (24.3.1 lib.iterator.traits) and `char_traits<>` (21.2 lib.char.traits). |
| 111 | +- _Specialization_: a template parameter can be used purely for the purpose of selecting a specialization. For example: |
| 112 | + |
| 113 | +```cpp |
| 114 | +SomeClass<fast> my_fast_object; // fast and small are empty classes |
| 115 | +SomeClass<small> my_small_object; // used just to select specialization |
| 116 | +``` |
| 117 | + |
| 118 | +[cols="1,4",stripes=none,frame=none] |
| 119 | +|=== |
| 120 | +| *Appropriate* | When the need for variation is due to data type or traits or is performance-related like selecting among several algorithms, and when a program might reasonably use more than one of the variations. |
| 121 | +| *Not appropriate* | When the interfaces for variations are different, or when choice of variation is best done by some mechanism outside of the program itself. Thus usually not appropriate to cope with platform differences. |
| 122 | +|=== |
| 123 | + |
| 124 | +== Acknowledgements |
| 125 | + |
| 126 | +This topic was originally written by xref:in-memoriam-beman-dawes.adoc[Beman Dawes], in 2001. |
| 127 | + |
| 128 | +== See Also |
| 129 | + |
| 130 | +The following libraries demonstrate the importance of separating interfaces from implementations: |
| 131 | + |
| 132 | +* boost:any[] : the implementation internally uses type erasure techniques to hide the actual type of the stored value, decoupling the interface from the concrete implementation details. |
| 133 | + |
| 134 | +* boost:function[] : the implementation internally uses type erasure techniques to store and invoke callable objects polymorphically, decoupling the interface from the specific callable object type. |
| 135 | + |
| 136 | +* boost:optional[] : the implementation internally uses _tag dispatching_ techniques to handle the presence or absence of the optional value, decoupling the interface from the concrete implementation details. |
| 137 | + |
| 138 | +Note:: Tag dispatching is a technique used to select different implementations of a function, or algorithm, based on the types or properties of its arguments. |
| 139 | + |
| 140 | +* boost:iterator[] : the implementation internally uses iterator traits and type traits to provide a generic interface for working with iterators, decoupling the interface from the specific iterator type and implementation. |
| 141 | + |
| 142 | +* boost:mp11[] : the implementation internally uses template metaprogramming techniques to perform type-level computations, decoupling the interface from the specific type and template instantiation. |
0 commit comments