Sen API
Sen Libraries
Loading...
Searching...
No Matches
db/src/util.h
Go to the documentation of this file.
1// === util.h ==========================================================================================================
2// Sen Infrastructure
3// Released under the Apache License v2.0 (SPDX-License-Identifier Apache-2.0).
4// See the LICENSE.txt file for more information.
5// © Airbus SAS, Airbus Helicopters, and Airbus Defence and Space SAU/GmbH/SAS.
6// =====================================================================================================================
7
8#ifndef SEN_LIBS_DB_SRC_UTIL_H
9#define SEN_LIBS_DB_SRC_UTIL_H
10
11// sen
13#include "sen/core/base/span.h"
16#include "v1.stl.h"
17
18// spdlog
19#include <spdlog/logger.h>
20
21// std
22#include <cstdint>
23#include <filesystem>
24
25namespace sen::db
26{
27
28std::shared_ptr<spdlog::logger> getLogger();
29
30[[nodiscard]] inline uint32_t getFileHeaderSize();
31
32template <typename T, typename B>
33void inline writeToBuffer(const T& data, B& buffer);
34
35template <typename T>
36inline void write(T&& data, FILE* file);
37
38template <typename T>
39[[nodiscard]] inline std::shared_ptr<ResizableHeapBlock> writeSizeAndDataToBuffer(const T& data);
40
41void writeToCompressedBuffer(const std::vector<uint8_t>& memBuf, kernel::Buffer& lz4Buf);
42
43void uncompressBuffer(const std::vector<uint8_t>& lz4Buf, std::vector<uint8_t>& memBuf, std::size_t decompressedSize);
44
45[[nodiscard]] FILE* openFile(const std::filesystem::path& path);
46
48[[nodiscard]] const ::sen::ClassType* searchOwner(const ::sen::ClassType* classType,
49 const ::sen::Property* property) noexcept;
50
52[[nodiscard]] const ::sen::ClassType* searchOwner(const ::sen::ClassType* classType,
53 const ::sen::Event* event) noexcept;
54
56[[nodiscard]] MemberHash computePlatformDependentPropertyId(const ::sen::ClassType* classType,
57 const ::sen::Property* property);
58
60[[nodiscard]] MemberHash computePlatformDependentEventId(const ::sen::ClassType* classType, const ::sen::Event* event);
61
62//----------------------------------------------------------------------------------------------------------------------
63// Inline implementation
64//----------------------------------------------------------------------------------------------------------------------
65
66void doWrite(const Span<const uint8_t>& span, FILE* file);
67
72
73template <typename T>
74inline void write(T&& data, FILE* file)
75{
76 auto block = std::make_shared<ResizableHeapBlock>();
77 ResizableBufferWriter writer(*block);
78 OutputStream out(writer);
80 doWrite(block->getConstSpan(), file);
81}
82
83template <typename T, typename B>
84inline void writeToBuffer(const T& data, B& buffer)
85{
86 buffer.reserve(SerializationTraits<T>::serializedSize(data));
87 ResizableBufferWriter writer(buffer);
88 OutputStream out(writer);
90}
91
92template <typename T>
93inline std::shared_ptr<ResizableHeapBlock> writeSizeAndDataToBuffer(const T& data)
94{
95 uint32_t dataSize = SerializationTraits<T>::serializedSize(data);
96 auto buffer = std::make_shared<ResizableHeapBlock>();
97
98 // reserve space, include some for the size at the start
99 buffer->reserve(SerializationTraits<uint32_t>::serializedSize(dataSize) + dataSize);
100 ResizableBufferWriter writer(*buffer);
101 OutputStream out(writer);
102
103 out.writeUInt32(dataSize);
105 return buffer;
106}
107
108} // namespace sen::db
109
110#endif // SEN_LIBS_DB_SRC_UTIL_H
void writeUInt32(uint32_t val)
Definition output_stream.h:44
A writer that owns a buffer that gets resized on demand.
Definition buffer_writer.h:59
Contiguous view of elements of type T. Inspired by http://www.open-std.org/jtc1/sc22/wg21/docs/papers...
Definition span.h:34
Definition type_traits.h:47
Definition type_specs.h:19
void doWrite(const Span< const uint8_t > &span, FILE *file)
const ::sen::ClassType * searchOwner(const ::sen::ClassType *classType, const ::sen::Property *property) noexcept
Returns the class in the hierarchy of classes that owns the property. Returns nullptr if not found.
void uncompressBuffer(const std::vector< uint8_t > &lz4Buf, std::vector< uint8_t > &memBuf, std::size_t decompressedSize)
uint32_t getFileHeaderSize()
Definition db/src/util.h:68
FILE * openFile(const std::filesystem::path &path)
MemberHash computePlatformDependentEventId(const ::sen::ClassType *classType, const ::sen::Event *event)
Computes the old version (platform-dependent) of the event id. Used for retro-compatibility.
void writeToBuffer(const T &data, B &buffer)
Definition db/src/util.h:84
void write(T &&data, FILE *file)
Definition db/src/util.h:74
std::shared_ptr< ResizableHeapBlock > writeSizeAndDataToBuffer(const T &data)
Definition db/src/util.h:93
std::shared_ptr< spdlog::logger > getLogger()
void writeToCompressedBuffer(const std::vector< uint8_t > &memBuf, kernel::Buffer &lz4Buf)
MemberHash computePlatformDependentPropertyId(const ::sen::ClassType *classType, const ::sen::Property *property)
Computes the old version (platform-dependent) of the property id. Used for retro-compatibility.
OutputStreamTemplate< LittleEndian > OutputStream
Definition output_stream.h:64
The hash of a member.
Definition type.h:39