Commit 57b6c32
add virtual_any
virtual_any<Any, Registry> is to `any` what virtual_ptr is to a pointer:
it combines an `any` - held by value - with the v-table pointer for the
contained value, so methods dispatch on the contained type without
looking it up on every call. The v-table pointer is acquired at
construction: from the dynamic type of an existing `any` (a hash table
lookup via virtual_traits<const Any&>::vptr), or statically when the
contained type is known (the value constructor, emplace, and the
make_*_virtual factories use static_vptr, like make_unique_virtual).
Assignment and emplace re-derive it, and no mutable accessor to the
`any` is exposed, so the vptr always matches the payload.
Methods take virtual_any by const, mutable or rvalue reference;
overriders receive the contained type by a reference of a compatible
category - the casts delegate to the existing virtual_traits<Any cvref>
specializations - or the virtual_any itself, unchanged, for a catch-all
overrider. Passing virtual_any by value is rejected: it would copy the
payload on every call. The value constructor makes overrider parameters
convertible to the method's, so BOOST_OPENMETHOD_OVERRIDE locates
virtual_any methods; the mutable lvalue case still needs
method<...>::override<Fn>, as with virtual_<Any&>.
No changes to core.hpp: dispatch reads the stored vptr through the
boost_openmethod_vptr hook (a friend, so ADL only finds it when a
virtual_any is an argument), and the detail templates (is_virtual,
parameter_traits, validate_method_parameter, validate_overrider_parameter,
select_overrider_virtual_type_aux) are specialized on the concrete class.
The exact-pair validate_overrider_parameter specializations disambiguate
with the generic <T, T> one, which partial ordering ranks neither above
nor below <virtual_any cvref, T2>.
The class is generic: it only requires virtual_traits<Any cvref> with
vptr and cast, so it serves std::any, boost::any, and future any-likes.
std_any.hpp and boost_any.hpp provide the default-registry aliases
virtual_std_any and virtual_boost_any and the make_std_any_virtual and
make_boost_any_virtual factories. They also delete the final_virtual_ptr
overloads for their `any` type: the primary template would silently use
static_vptr<any> - the v-table of the `any` root class, not of the
contained value.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>1 parent 65cd1b5 commit 57b6c32
9 files changed
Lines changed: 1173 additions & 2 deletions
File tree
- doc/modules/ROOT/pages
- include/boost/openmethod/interop
- test
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
71 | 71 | | |
72 | 72 | | |
73 | 73 | | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
74 | 88 | | |
75 | 89 | | |
76 | 90 | | |
77 | 91 | | |
78 | 92 | | |
79 | 93 | | |
80 | | - | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
81 | 99 | | |
82 | 100 | | |
83 | 101 | | |
84 | 102 | | |
85 | 103 | | |
86 | 104 | | |
87 | 105 | | |
88 | | - | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
89 | 111 | | |
90 | 112 | | |
91 | 113 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
| |||
238 | 239 | | |
239 | 240 | | |
240 | 241 | | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
241 | 293 | | |
242 | 294 | | |
243 | 295 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
| |||
201 | 202 | | |
202 | 203 | | |
203 | 204 | | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
204 | 256 | | |
205 | 257 | | |
206 | 258 | | |
0 commit comments