/workspaces/astro/sol3-sdk/cpp/sol3/core/any_message_view.h Source File

Space-ng SDK: /workspaces/astro/sol3-sdk/cpp/sol3/core/any_message_view.h Source File
Space-ng SDK
any_message_view.h
Go to the documentation of this file.
1 // Copyright (c) Space-ng, inc. All rights reserved.
2 #pragma once
3 #include "sol3/core/fb_json.h"
6 #include "sol3/core/msg/message_header.h"
7 #include "sol3/core/seq_ring.h"
8 #include "sol3/log/except.h"
9 
10 #include <flatbuffers/flatbuffers.h>
11 #include <flatbuffers/idl.h>
12 #include <flatbuffers/reflection.h>
13 #include <spdlog/spdlog.h>
14 
15 #include <vector>
16 
17 namespace sol3::core {
18 
25  public:
27  AnyMessageView() = default;
28 
31  void update(
32  SeqRingSlotView const& slot_view,
34  std::chrono::steady_clock::duration time_out);
35 
38  bool valid() const;
40  bool hasValue() const;
41 
42  operator bool() const;
43 
44  bool cached() const;
45  void setCached(bool cached);
46 
48  void checkHasValue() const;
49 
51  void checkValid() const;
52 
53  msg::MessageHeader const& header() const;
54 
55  template <typename TTableType>
56  TTableType const& payload() const {
57  if (makeFileIdentifier<typename TTableType::NativeTableType>() !=
58  expected_file_identifier_) {
59  SOL3_PANIC(
60  "payload type mismatch: {} != {}",
62  makeFileIdentifier<typename TTableType::NativeTableType>()),
63  stringFromFileIdentifier(expected_file_identifier_));
64  }
65  checkHasValue();
66  return *flatbuffers::GetSizePrefixedRoot<TTableType>(payload_data_);
67  }
68 
69  uint64_t seq() const;
70 
72  flatbuffers::span<uint8_t const> headerSpan() const;
73 
75  flatbuffers::span<uint8_t const> payloadSpan() const;
76 
78  flatbuffers::span<uint8_t const> messageSpan() const;
79 
80  void reset();
81 
82  MessageSchema const& schema() const;
83 
84  void payloadToJson(JsonFormat format, std::string& out_json) const;
85  std::string payloadToJson(JsonFormat format) const;
86 
87  private:
88  uint32_t expected_file_identifier_ = 0;
89 
90  std::shared_ptr<std::vector<uint8_t>> buffer_;
91  std::shared_ptr<uint64_t> reader_seq_;
92  MessageSchema schema_;
93  uint64_t seq_ = 0;
94 
95  uint8_t const* header_data_ = nullptr;
96  size_t header_size_ = 0;
97 
98  uint8_t const* payload_data_ = nullptr;
99  size_t payload_size_ = 0;
100 
101  bool cached_ = false;
102 };
103 
104 void toJson(
105  AnyMessageView const& view,
106  std::string& out_json,
108 std::string toJson(
109  AnyMessageView const& view, JsonFormat format = JsonFormat::SINGLE_LINE);
110 
111 inline std::string format_as(AnyMessageView& view) { return toJson(view); }
112 } // namespace sol3::core
Definition: any_message_view.h:24
flatbuffers::span< uint8_t const > messageSpan() const
header and payload span
void checkHasValue() const
Throws if hasValue() is false.
void checkValid() const
Throws if valid() is false.
void update(SeqRingSlotView const &slot_view, MessageSchema schema, std::chrono::steady_clock::duration time_out)
TTableType const & payload() const
Definition: any_message_view.h:56
flatbuffers::span< uint8_t const > headerSpan() const
Size-prefixed header buffer.
MessageSchema const & schema() const
flatbuffers::span< uint8_t const > payloadSpan() const
Non-size-prefixed payload buffer.
AnyMessageView()=default
Default-constructed, empty view.
void setCached(bool cached)
bool hasValue() const
True if valid() and both header and payload buffers are present.
std::string payloadToJson(JsonFormat format) const
void payloadToJson(JsonFormat format, std::string &out_json) const
msg::MessageHeader const & header() const
Definition: message_schema.h:12
A read only view of a specific seq ring buffer slot.
Definition: seq_ring.h:73
#define SOL3_PANIC(...)
Definition: except.h:213
root_type MessageHeader
Definition: message_header.fbs:32
Definition: any_message_input.h:15
std::string format_as(AnyMessageView &view)
Definition: any_message_view.h:111
std::string stringFromFileIdentifier(uint32_t v)
Converts a file identifier to a printable 4-character string.
void toJson(AnyMessageView const &view, std::string &out_json, JsonFormat format=JsonFormat::SINGLE_LINE)
JsonFormat
Definition: fb_json.h:14