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