| 60 | /// serialized and a reference entry was emitted, false otherwise. |
| 61 | template <typename Writer, typename T> |
| 62 | bool try_write_shared_ref(Writer &writer, const std::shared_ptr<T> &ptr) { |
| 63 | if (!ptr) { |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | auto address = reinterpret_cast<uintptr_t>(ptr.get()); |
| 68 | auto *entry = ptr_to_id_.find(address); |
| 69 | if (entry != nullptr) { |
| 70 | writer.write_int8(static_cast<int8_t>(RefFlag::Ref)); |
| 71 | writer.write_var_uint32(entry->second); |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | uint32_t new_id = next_id_++; |
| 76 | ptr_to_id_.emplace(address, new_id); |
| 77 | writer.write_int8(static_cast<int8_t>(RefFlag::RefValue)); |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | /// reserve a ref_id slot without storing a pointer. |
| 82 | /// Used for types (like structs) that Java tracks but C++ doesn't reference. |
no test coverage detected