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// sen
10#include "sen/core/base/span.h"
13#include "v1.stl.h"
14
15// spdlog
16#include <spdlog/logger.h>
17
18// std
19#include <cstdint>
20#include <filesystem>
21
22namespace sen::db
23{
24
25std::shared_ptr<spdlog::logger> getLogger();
26
27[[nodiscard]] inline uint32_t getFileHeaderSize();
28
29template <typename T, typename B>
30void inline writeToBuffer(const T& data, B& buffer);
31
32template <typename T>
33inline void write(T&& data, FILE* file);
34
35template <typename T>
36[[nodiscard]] inline std::shared_ptr<ResizableHeapBlock> writeSizeAndDataToBuffer(const T& data);
37
38void writeToCompressedBuffer(const std::vector<uint8_t>& memBuf, std::vector<uint8_t>& lz4Buf);
39
40void uncompressBuffer(const std::vector<uint8_t>& lz4Buf, std::vector<uint8_t>& memBuf, std::size_t decompressedSize);
41
42[[nodiscard]] FILE* openFile(const std::filesystem::path& path);
43
45[[nodiscard]] const ::sen::ClassType* searchOwner(const ::sen::ClassType* classType,
46 const ::sen::Property* property) noexcept;
47
49[[nodiscard]] const ::sen::ClassType* searchOwner(const ::sen::ClassType* classType,
50 const ::sen::Event* event) noexcept;
51
53[[nodiscard]] MemberHash computePlatformDependentPropertyId(const ::sen::ClassType* classType,
54 const ::sen::Property* property);
55
57[[nodiscard]] MemberHash computePlatformDependentEventId(const ::sen::ClassType* classType, const ::sen::Event* event);
58
59//----------------------------------------------------------------------------------------------------------------------
60// Inline implementation
61//----------------------------------------------------------------------------------------------------------------------
62
63void doWrite(const Span<const uint8_t>& span, FILE* file);
64
69
70template <typename T>
71inline void write(T&& data, FILE* file)
72{
73 auto block = std::make_shared<ResizableHeapBlock>();
74 ResizableBufferWriter writer(*block);
75 OutputStream out(writer);
77 doWrite(block->getConstSpan(), file);
78}
79
80template <typename T, typename B>
81inline void writeToBuffer(const T& data, B& buffer)
82{
83 buffer.reserve(SerializationTraits<T>::serializedSize(data));
84 ResizableBufferWriter writer(buffer);
85 OutputStream out(writer);
87}
88
89template <typename T>
90inline std::shared_ptr<ResizableHeapBlock> writeSizeAndDataToBuffer(const T& data)
91{
92 uint32_t dataSize = SerializationTraits<T>::serializedSize(data);
93 auto buffer = std::make_shared<ResizableHeapBlock>();
94
95 // reserve space, include some for the size at the start
96 buffer->reserve(SerializationTraits<uint32_t>::serializedSize(dataSize) + dataSize);
97 ResizableBufferWriter writer(*buffer);
98 OutputStream out(writer);
99
100 out.writeUInt32(dataSize);
102 return buffer;
103}
104
105} // namespace sen::db
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:58
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 annotation.h:20
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:65
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:81
void write(T &&data, FILE *file)
Definition db/src/util.h:71
std::shared_ptr< ResizableHeapBlock > writeSizeAndDataToBuffer(const T &data)
Definition db/src/util.h:90
std::shared_ptr< spdlog::logger > getLogger()
void writeToCompressedBuffer(const std::vector< uint8_t > &memBuf, std::vector< uint8_t > &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