WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
DataLogReaderThread.hpp
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <atomic>
8#include <functional>
9#include <map>
10#include <string>
11#include <string_view>
12#include <thread>
13#include <utility>
14#include <vector>
15
16#include <upb/mem/arena.h>
17#include <upb/reflection/def.h>
18
20#include "wpi/util/DenseMap.hpp"
21#include "wpi/util/Signal.h"
22#include "wpi/util/mutex.hpp"
24
25namespace wpi::log {
26
39
41 public:
42 std::vector<DataLogReaderRange> ranges; // ranges where this entry is valid
43};
44
46 public:
48 : m_reader{std::move(reader)}, m_thread{[this] { ReadMain(); }} {}
50
51 bool IsDone() const { return m_done; }
52 std::string_view GetBufferIdentifier() const {
53 return m_reader.GetBufferIdentifier();
54 }
55 unsigned int GetNumRecords() const { return m_numRecords; }
56 unsigned int GetNumEntries() const {
57 std::scoped_lock lock{m_mutex};
58 return m_entriesByName.size();
59 }
60
61 // Passes Entry& to func
62 template <typename T>
63 void ForEachEntryName(T&& func) {
64 std::scoped_lock lock{m_mutex};
65 for (auto&& kv : m_entriesByName) {
66 func(kv.second);
67 }
68 }
69
70 const DataLogReaderEntry* GetEntry(std::string_view name) const {
71 std::scoped_lock lock{m_mutex};
72 auto it = m_entriesByName.find(name);
73 if (it == m_entriesByName.end()) {
74 return nullptr;
75 }
76 return &it->second;
77 }
78
79 const DataLogReaderEntry* GetEntry(int entry) const {
80 std::scoped_lock lock{m_mutex};
81 auto it = m_entriesById.find(entry);
82 if (it == m_entriesById.end()) {
83 return nullptr;
84 }
85 return it->second;
86 }
87
91 upb_DefPool* GetProtobufDatabase() { return m_protoPool; }
92 upb_Arena* GetProtobufArena() { return m_arena; }
93
94 const wpi::log::DataLogReader& GetReader() const { return m_reader; }
95
96 // note: these are called on separate thread
99
100 private:
101 void ReadMain();
102
104 mutable wpi::util::mutex m_mutex;
105 std::atomic_bool m_active{true};
106 std::atomic_bool m_done{false};
107 std::atomic<unsigned int> m_numRecords{0};
108 std::map<std::string, DataLogReaderEntry, std::less<>> m_entriesByName;
111 upb_DefPool* m_protoPool = upb_DefPool_New();
112 upb_Arena* m_arena = upb_Arena_New();
113 std::thread m_thread;
114};
115
116} // namespace wpi::log
This file defines the DenseMap class.
UPB_API_INLINE upb_Arena * upb_Arena_New(void)
Definition arena.h:80
@ name
Definition base.h:690
Definition DataLogReaderThread.hpp:40
std::vector< DataLogReaderRange > ranges
Definition DataLogReaderThread.hpp:42
Data log reader (reads logs written by the DataLog class).
Definition DataLogReader.hpp:299
DataLogIterator iterator
Definition DataLogReader.hpp:303
wpi::log::DataLogReader::iterator m_end
Definition DataLogReaderThread.hpp:37
DataLogReaderRange(wpi::log::DataLogReader::iterator begin, wpi::log::DataLogReader::iterator end)
Definition DataLogReaderThread.hpp:29
wpi::log::DataLogReader::iterator m_begin
Definition DataLogReaderThread.hpp:36
wpi::log::DataLogReader::iterator end() const
Definition DataLogReaderThread.hpp:34
wpi::log::DataLogReader::iterator begin() const
Definition DataLogReaderThread.hpp:33
wpi::util::sig::Signal_mt sigDone
Definition DataLogReaderThread.hpp:98
unsigned int GetNumRecords() const
Definition DataLogReaderThread.hpp:55
wpi::util::sig::Signal_mt< const DataLogReaderEntry & > sigEntryAdded
Definition DataLogReaderThread.hpp:97
upb_Arena * GetProtobufArena()
Definition DataLogReaderThread.hpp:92
DataLogReaderThread(wpi::log::DataLogReader reader)
Definition DataLogReaderThread.hpp:47
const DataLogReaderEntry * GetEntry(std::string_view name) const
Definition DataLogReaderThread.hpp:70
void ForEachEntryName(T &&func)
Definition DataLogReaderThread.hpp:63
const wpi::log::DataLogReader & GetReader() const
Definition DataLogReaderThread.hpp:94
unsigned int GetNumEntries() const
Definition DataLogReaderThread.hpp:56
const DataLogReaderEntry * GetEntry(int entry) const
Definition DataLogReaderThread.hpp:79
std::string_view GetBufferIdentifier() const
Definition DataLogReaderThread.hpp:52
wpi::util::StructDescriptorDatabase & GetStructDatabase()
Definition DataLogReaderThread.hpp:88
upb_DefPool * GetProtobufDatabase()
Definition DataLogReaderThread.hpp:91
bool IsDone() const
Definition DataLogReaderThread.hpp:51
Definition DenseMap.hpp:728
Database of raw struct dynamic descriptors.
Definition DynamicStruct.hpp:335
UPB_API upb_DefPool * upb_DefPool_New(void)
Definition StringMap.hpp:773
Definition DataLogReader.hpp:17
SignalBase< mutex, T... > Signal_mt
Specialization of SignalBase to be used in multi-threaded contexts.
Definition Signal.h:821
::std::mutex mutex
Definition mutex.hpp:17
struct upb_DefPool upb_DefPool
Definition common.h:24
Definition arena.h:29
Data contained in a start control record as created by DataLog::Start() when writing the log.
Definition DataLogReader.hpp:23