|
10 | 10 | #include "databento/constants.hpp" // kSymbolCstrLen |
11 | 11 | #include "databento/datetime.hpp" // UnixNanos |
12 | 12 | #include "databento/enums.hpp" |
| 13 | +#include "databento/exceptions.hpp" // InvalidArgumentError |
13 | 14 | #include "databento/flag_set.hpp" // FlagSet |
14 | 15 | #include "databento/publishers.hpp" // Publisher |
15 | 16 | #include "databento/with_ts_out.hpp" |
@@ -54,11 +55,33 @@ class Record { |
54 | 55 |
|
55 | 56 | template <typename T> |
56 | 57 | const T& Get() const { |
57 | | - return *reinterpret_cast<const T*>(record_); |
| 58 | + if (const auto* r = GetIf<T>()) { |
| 59 | + return *r; |
| 60 | + } |
| 61 | + throw InvalidArgumentError{ |
| 62 | + "Get", "T", std::string{"rtype mismatch, found "} + ToString(RType())}; |
58 | 63 | } |
59 | 64 | template <typename T> |
60 | 65 | T& Get() { |
61 | | - return *reinterpret_cast<T*>(record_); |
| 66 | + if (auto* r = GetIf<T>()) { |
| 67 | + return *r; |
| 68 | + } |
| 69 | + throw InvalidArgumentError{ |
| 70 | + "Get", "T", std::string{"rtype mismatch, found "} + ToString(RType())}; |
| 71 | + } |
| 72 | + template <typename T> |
| 73 | + const T* GetIf() const { |
| 74 | + if (!Holds<T>()) { |
| 75 | + return nullptr; |
| 76 | + } |
| 77 | + return reinterpret_cast<const T*>(record_); |
| 78 | + } |
| 79 | + template <typename T> |
| 80 | + T* GetIf() { |
| 81 | + if (!Holds<T>()) { |
| 82 | + return nullptr; |
| 83 | + } |
| 84 | + return reinterpret_cast<T*>(record_); |
62 | 85 | } |
63 | 86 |
|
64 | 87 | std::size_t Size() const; |
@@ -629,6 +652,8 @@ inline bool operator!=(const SymbolMappingMsg& lhs, |
629 | 652 |
|
630 | 653 | std::string ToString(const RecordHeader& header); |
631 | 654 | std::ostream& operator<<(std::ostream& stream, const RecordHeader& header); |
| 655 | +std::string ToString(const Record& header); |
| 656 | +std::ostream& operator<<(std::ostream& stream, const Record& header); |
632 | 657 | std::string ToString(const MboMsg& mbo_msg); |
633 | 658 | std::ostream& operator<<(std::ostream& stream, const MboMsg& mbo_msg); |
634 | 659 | std::string ToString(const BidAskPair& ba_pair); |
|
0 commit comments