| 663 | } |
| 664 | |
| 665 | void emitHelpers() { |
| 666 | out_ << "namespace detail {\n" |
| 667 | "inline std::span<uint8> BufferBytes(CUtlBuffer* buffer) {\n" |
| 668 | " if (!buffer || !buffer->Base() || buffer->m_Put < 0) return {};\n" |
| 669 | " return {buffer->Base(), static_cast<size_t>(buffer->m_Put)};\n" |
| 670 | "}\n\n" |
| 671 | "template <class T>\n" |
| 672 | "T Read(std::span<const uint8> bytes, size_t offset) {\n" |
| 673 | " T value{};\n" |
| 674 | " if (offset <= bytes.size() && sizeof(T) <= bytes.size() - offset)\n" |
| 675 | " std::memcpy(&value, bytes.data() + offset, sizeof(T));\n" |
| 676 | " return value;\n" |
| 677 | "}\n\n" |
| 678 | "template <class T>\n" |
| 679 | "void Write(std::span<uint8> bytes, size_t offset, const T& value) {\n" |
| 680 | " if (offset <= bytes.size() && sizeof(T) <= bytes.size() - offset)\n" |
| 681 | " std::memcpy(bytes.data() + offset, &value, sizeof(T));\n" |
| 682 | "}\n\n" |
| 683 | "template <class T>\n" |
| 684 | "size_t ByteCount(T value) {\n" |
| 685 | " static_assert(std::is_integral_v<T>);\n" |
| 686 | " if constexpr (std::is_signed_v<T>) {\n" |
| 687 | " if (value < 0) return (std::numeric_limits<size_t>::max)();\n" |
| 688 | " }\n" |
| 689 | " return static_cast<size_t>(value);\n" |
| 690 | "}\n\n" |
| 691 | "inline size_t Sum(std::initializer_list<size_t> values) {\n" |
| 692 | " size_t result = 0;\n" |
| 693 | " for (size_t value : values) {\n" |
| 694 | " if (value > (std::numeric_limits<size_t>::max)() - result)\n" |
| 695 | " return (std::numeric_limits<size_t>::max)();\n" |
| 696 | " result += value;\n" |
| 697 | " }\n" |
| 698 | " return result;\n" |
| 699 | "}\n\n" |
| 700 | "inline bool Fits(std::span<const uint8> bytes, size_t size) {\n" |
| 701 | " return size != (std::numeric_limits<size_t>::max)() && bytes.size() >= size;\n" |
| 702 | "}\n\n" |
| 703 | "inline std::span<uint8> Slice(std::span<uint8> bytes, size_t offset, size_t size) {\n" |
| 704 | " if (offset > bytes.size() || size > bytes.size() - offset) return {};\n" |
| 705 | " return bytes.subspan(offset, size);\n" |
| 706 | "}\n\n" |
| 707 | "inline std::span<const uint8> Slice(std::span<const uint8> bytes, size_t offset, size_t size) {\n" |
| 708 | " if (offset > bytes.size() || size > bytes.size() - offset) return {};\n" |
| 709 | " return bytes.subspan(offset, size);\n" |
| 710 | "}\n\n" |
| 711 | "inline bool CopyBytes(std::span<uint8> bytes, size_t offset, size_t capacity,\n" |
| 712 | " std::span<const uint8> value) {\n" |
| 713 | " auto dst = Slice(bytes, offset, capacity);\n" |
| 714 | " if (dst.size() != capacity) return false;\n" |
| 715 | " const size_t count = (std::min)(dst.size(), value.size());\n" |
| 716 | " if (count) std::memcpy(dst.data(), value.data(), count);\n" |
| 717 | " if (count < dst.size()) std::memset(dst.data() + count, 0, dst.size() - count);\n" |
| 718 | " return true;\n" |
| 719 | "}\n\n" |
| 720 | "template <class T>\n" |
| 721 | "void AppendField(std::ostringstream& os, const char* name, const T& value) {\n" |
| 722 | " os << name << '=' << value;\n" |
nothing calls this directly
no outgoing calls
no test coverage detected