schema/Blob.hpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // <Blob.hpp> -*- C++ -*- | ||
| 2 | |||
| 3 | #pragma once | ||
| 4 | |||
| 5 | #include <array> | ||
| 6 | #include <cstddef> | ||
| 7 | #include <cstdint> | ||
| 8 | #include <iostream> | ||
| 9 | #include <vector> | ||
| 10 | |||
| 11 | namespace simdb { | ||
| 12 | |||
| 13 | /*! | ||
| 14 | * \struct SqlBlob | ||
| 15 | * | ||
| 16 | * \brief Blob descriptor used for writing and reading raw bytes to/from the | ||
| 17 | * database. Holds a pointer and byte count; used with SQL_VALUES() and | ||
| 18 | * blob columns (e.g. getPropertyBlob / setPropertyBlob on SqlRecord). | ||
| 19 | */ | ||
| 20 | struct SqlBlob | ||
| 21 | { | ||
| 22 | /// Pointer to the raw bytes to store or that were read. | ||
| 23 | const void* data_ptr = nullptr; | ||
| 24 | /// Number of bytes at \p data_ptr. | ||
| 25 | size_t num_bytes = 0; | ||
| 26 | |||
| 27 | /// Construct from a contiguous container (e.g. std::vector<T>). | ||
| 28 | /// \tparam T Element type; sizeof(T) * vals.size() gives num_bytes. | ||
| 29 | /// \param vals Container whose data() and size() define the blob. | ||
| 30 | template <typename T> | ||
| 31 | 8 | SqlBlob(const std::vector<T>& vals) : | |
| 32 | 8 | data_ptr(vals.data()), | |
| 33 | 8 | num_bytes(vals.size() * sizeof(T)) | |
| 34 | { | ||
| 35 | 8 | } | |
| 36 | |||
| 37 | /// Construct from a contiguous array. | ||
| 38 | template <typename T, size_t N> | ||
| 39 | 7258 | SqlBlob(const std::array<T, N>& vals) : | |
| 40 | 7258 | data_ptr(vals.data()), | |
| 41 | 7258 | num_bytes(N * sizeof(T)) | |
| 42 | { | ||
| 43 | 7258 | } | |
| 44 | |||
| 45 | 1 | SqlBlob() = default; | |
| 46 | SqlBlob(const SqlBlob&) = default; | ||
| 47 | }; | ||
| 48 | |||
| 49 | /// Write a short debug representation of the blob to \p os (e.g. blob(addr, n=size)). | ||
| 50 | ✗ | inline std::ostream& operator<<(std::ostream& os, const SqlBlob& blob) | |
| 51 | { | ||
| 52 | ✗ | if (!blob.data_ptr || blob.num_bytes == 0) | |
| 53 | { | ||
| 54 | ✗ | os << "blob(empty)"; | |
| 55 | ✗ | return os; | |
| 56 | } | ||
| 57 | |||
| 58 | ✗ | os << "blob(" << std::hex << reinterpret_cast<uintptr_t>(blob.data_ptr) << ", n=" << std::dec << blob.num_bytes | |
| 59 | ✗ | << ")"; | |
| 60 | |||
| 61 | ✗ | return os; | |
| 62 | } | ||
| 63 | |||
| 64 | } // namespace simdb | ||
| 65 |