|
| 1 | +[cols="^1,1,1,1,1,1,1", options="header"] |
| 2 | +|=== |
| 3 | +| Branch | GH Actions | Coverity Scan | codecov.io | Deps | Docs | Tests |
| 4 | + |
| 5 | +| https://github.com/boostorg/openmethod/tree/master[`master`] |
| 6 | +| image:https://github.com/boostorg/openmethod/actions/workflows/ci.yml/badge.svg?branch=master[CI,link=https://github.com/boostorg/openmethod/actions/workflows/ci.yml] |
| 7 | +| image:https://scan.coverity.com/projects/32486/badge.svg[Coverity Scan Build Status,link=https://scan.coverity.com/projects/boostorg-openmethod] |
| 8 | +| image:https://codecov.io/gh/boostorg/openmethod/branch/master/graph/badge.svg[codecov,link=https://codecov.io/gh/boostorg/openmethod/branch/master] |
| 9 | +| image:https://img.shields.io/badge/deps-master-brightgreen.svg[Deps,link=https://pdimov.github.io/boostdep-report/master/openmethod.html] |
| 10 | +| image:https://img.shields.io/badge/docs-master-brightgreen.svg[Documentation,link=https://www.boost.org/doc/libs/master/libs/openmethod/doc/html/openmethod.html] |
| 11 | +| image:https://img.shields.io/badge/matrix-master-brightgreen.svg[Enter the Matrix,link=http://www.boost.org/development/tests/master/developer/openmethod.html] |
| 12 | + |
| 13 | +| https://github.com/boostorg/openmethod/tree/develop[`develop`] |
| 14 | +| image:https://github.com/boostorg/openmethod/actions/workflows/ci.yml/badge.svg?branch=develop[CI,link=https://github.com/boostorg/openmethod/actions/workflows/ci.yml] |
| 15 | +| image:https://scan.coverity.com/projects/32486/badge.svg[Coverity Scan Build Status,link=https://scan.coverity.com/projects/boostorg-openmethod] |
| 16 | +| image:https://codecov.io/gh/boostorg/openmethod/branch/develop/graph/badge.svg[codecov,link=https://codecov.io/gh/boostorg/openmethod/branch/develop] |
| 17 | +| image:https://img.shields.io/badge/deps-develop-brightgreen.svg[Deps,link=https://pdimov.github.io/boostdep-report/develop/openmethod.html] |
| 18 | +| image:https://img.shields.io/badge/docs-develop-brightgreen.svg[Documentation,link=https://www.boost.org/doc/libs/develop/libs/openmethod/doc/html/openmethod.html] |
| 19 | +| image:https://img.shields.io/badge/matrix-develop-brightgreen.svg[Enter the Matrix,link=http://www.boost.org/development/tests/develop/developer/openmethod.html] |
| 20 | +|=== |
| 21 | + |
| 22 | +== Boost.OpenMethod |
| 23 | + |
| 24 | +=== Overview |
| 25 | + |
| 26 | +Boost.OpenMethod implements open-(multi-)methods in C++17 and above. |
| 27 | +Open-methods are virtual functions that exist outside of classes, as |
| 28 | +free-standing functions. This allows defining new methods for existing |
| 29 | +classes without modifying their definition, and even for classes defined in |
| 30 | +third-party libraries. Open-methods support both single and multiple dispatch. |
| 31 | + |
| 32 | +=== Example |
| 33 | + |
| 34 | +[source,c++] |
| 35 | +---- |
| 36 | +// library |
| 37 | +
|
| 38 | +struct Matrix { |
| 39 | + virtual ~Matrix() = default; |
| 40 | +}; |
| 41 | +
|
| 42 | +struct SquareMatrix : Matrix {}; |
| 43 | +struct SymmetricMatrix : Matrix {}; |
| 44 | +struct DiagonalMatrix : SymmetricMatrix {}; |
| 45 | +
|
| 46 | +// application |
| 47 | +
|
| 48 | +#include <iostream> |
| 49 | +#include <memory> |
| 50 | +#include <vector> |
| 51 | +
|
| 52 | +#include <boost/openmethod.hpp> |
| 53 | +#include <boost/openmethod/initialize.hpp> |
| 54 | +
|
| 55 | +using boost::openmethod::virtual_ptr; |
| 56 | +
|
| 57 | +BOOST_OPENMETHOD_CLASSES(Matrix, SquareMatrix, SymmetricMatrix, DiagonalMatrix); |
| 58 | +
|
| 59 | +BOOST_OPENMETHOD(to_json, (virtual_ptr<const Matrix>, std::ostream& os), void); |
| 60 | +
|
| 61 | +BOOST_OPENMETHOD_OVERRIDE( |
| 62 | + to_json, (virtual_ptr<const SquareMatrix>, std::ostream& os), void) { |
| 63 | + os << "all the elements\n"; |
| 64 | +} |
| 65 | +
|
| 66 | +BOOST_OPENMETHOD_OVERRIDE( |
| 67 | + to_json, (virtual_ptr<const SymmetricMatrix>, std::ostream& os), void) { |
| 68 | + os << "elements above and including the diagonal\n"; |
| 69 | +} |
| 70 | +
|
| 71 | +BOOST_OPENMETHOD_OVERRIDE( |
| 72 | + to_json, (virtual_ptr<const DiagonalMatrix>, std::ostream& os), void) { |
| 73 | + os << "just the diagonal\n"; |
| 74 | +} |
| 75 | +
|
| 76 | +int main() { |
| 77 | + std::array<std::unique_ptr<Matrix>, 3> matrices = { |
| 78 | + std::make_unique<SquareMatrix>(), |
| 79 | + std::make_unique<SymmetricMatrix>(), |
| 80 | + std::make_unique<DiagonalMatrix>(), |
| 81 | + }; |
| 82 | +
|
| 83 | + boost::openmethod::initialize(); |
| 84 | +
|
| 85 | + for (const auto& m : matrices) { |
| 86 | + to_json(*m, std::cout); |
| 87 | + } |
| 88 | +} |
| 89 | +
|
| 90 | +// output: |
| 91 | +// all the elements |
| 92 | +// elements above and including the diagonal |
| 93 | +// just the diagonal |
| 94 | +---- |
| 95 | + |
| 96 | +=== Features |
| 97 | + |
| 98 | +* Single dispatch can be as fast as equivalent virtual function calls. |
| 99 | + |
| 100 | +* Multiple dispatch in constant time (for a given number of virtual parameters). |
| 101 | + |
| 102 | +* Redundancy-free multiple dispatch tables. |
| 103 | + |
| 104 | +* Inter-operation with standard smart pointers, extensible to other pointer-like |
| 105 | + types. |
| 106 | + |
| 107 | +* Exception agnostic by default. |
| 108 | + |
| 109 | +* Macro-based interface for convenience. |
| 110 | + |
| 111 | +* Macro-free interface for inter-operation with templates. |
| 112 | + |
| 113 | +* Customization points for alternative RTTI systems, error handling, vptr |
| 114 | + placement, etc. |
| 115 | + |
| 116 | +* Headers-only. |
| 117 | + |
| 118 | +=== Requirements |
| 119 | + |
| 120 | +The library requires an optimizing compiler supporting C++17 or above. |
| 121 | + |
| 122 | +=== Tested Compilers |
| 123 | + |
| 124 | +Boost.OpenMethod is tested with the following compilers: |
| 125 | + |
| 126 | +* clang: 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 |
| 127 | + |
| 128 | +* gcc: 8, 9, 10, 11, 12, 13, 14, 15 |
| 129 | + |
| 130 | +* msvc: 14.3 |
| 131 | + |
| 132 | +* icpx |
| 133 | + |
| 134 | +and on the following platforms: |
| 135 | + |
| 136 | +* Linux |
| 137 | + |
| 138 | +* macOS |
| 139 | + |
| 140 | +* Windows |
| 141 | + |
| 142 | +* s390x |
| 143 | + |
| 144 | +=== Quality Assurance |
| 145 | + |
| 146 | +The development infrastructure for the library includes these per-commit |
| 147 | +analyses: |
| 148 | + |
| 149 | +* Coverage reports |
| 150 | + |
| 151 | +* Clang sanitizers |
| 152 | + |
| 153 | +* Compilation and tests on Drone.io and GitHub Actions |
| 154 | + |
| 155 | +=== Acknowledgments |
| 156 | + |
| 157 | +I would like to thank the C++ Alliance for their support, in particular |
| 158 | +Joaquín M. López Muñoz for encouraging me to submit my library and being the |
| 159 | +first to endorse it; and Dmitryi Arkhipov for volunteering to be the review |
| 160 | +manager. |
| 161 | + |
| 162 | +Thanks to the members of the Boost community who posted a formal review: |
| 163 | + |
| 164 | +* Andrzej Krzemienski |
| 165 | + |
| 166 | +* Christian Mazakas |
| 167 | + |
| 168 | +* Joaquin M López Muñoz |
| 169 | + |
| 170 | +* Klemens Morgenstern |
| 171 | + |
| 172 | +* Ruben Perez |
| 173 | + |
| 174 | +* Yannick Le Goc |
| 175 | + |
| 176 | +Also thanks to Steven Watanabe for his cogent feedback and advice, and all |
| 177 | +the people who posted remarks and suggestions. |
| 178 | + |
| 179 | +This work was strongly influenced by the following papers: |
| 180 | + |
| 181 | +* https://www.stroustrup.com/multimethods.pdf[Open Multi-Methods for C++], |
| 182 | +Peter Pirkelbauer, Yuriy Solodkyy and Bjarne Stroustrup |
| 183 | + |
| 184 | +* https://core.ac.uk/download/pdf/18599789.pdf[Simplifying the Analysis of |
| 185 | +C++ Programs], Yuriy Solodkyy. |
| 186 | + |
| 187 | +* http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2216.pdf[Report |
| 188 | +on language support for Multi-Methods and Open-Methods for C++], Peter |
| 189 | +Pirkelbauer, Yuriy Solodkyy and Bjarne Stroustrup. |
| 190 | + |
| 191 | +* https://dl.acm.org/doi/abs/10.1145/191081.191117[Optimizing multi-method |
| 192 | +dispatch using compressed dispatch tables], Eric Amiel, Olivier Gruber and |
| 193 | +Eric Simon. |
| 194 | + |
| 195 | +Finally, thanks to Prof. J.D. Garcia, of the Universidad Carlos III in Madrid, |
| 196 | +for organizing the "using std::cpp" conference, and introducing me to Joaquín. |
| 197 | + |
| 198 | +=== More information |
| 199 | + |
| 200 | +* http://stackoverflow.com/questions/ask?tags=c%2B%2B,boost,boost-openmethod[Ask questions] |
| 201 | +* https://github.com/boostorg/openmethod/issues[Report bugs]: Be sure to mention Boost version, platform and compiler you're using. A small compilable code sample to reproduce the problem is always good as well. |
| 202 | +* Submit your patches as pull requests against *develop* branch. Note that by submitting patches you agree to license your modifications under the http://www.boost.org/LICENSE_1_0.txt[Boost Software License, Version 1.0]. |
| 203 | +* Discussions about the library are held on the http://www.boost.org/community/groups.html#main[Boost developers mailing list]. Be sure to read the http://www.boost.org/community/policy.html[discussion policy] before posting and add the `[openmethod]` tag at the beginning of the subject line. |
| 204 | + |
| 205 | +=== License |
| 206 | + |
| 207 | +Distributed under the http://www.boost.org/LICENSE_1_0.txt[Boost Software License, Version 1.0]. |
0 commit comments