Vanetza
Loading...
Searching...
No Matches
byte_buffer.hpp
1#ifndef BYTE_BUFFER_HPP_7NOEQO4F
2#define BYTE_BUFFER_HPP_7NOEQO4F
3
4#include <cstdint>
5#include <type_traits>
6#include <vector>
7
8namespace vanetza
9{
10
11typedef std::vector<uint8_t> ByteBuffer;
12
13/**
14 * Cast byte buffer to a POD object
15 * \param buffer byte buffer containing requested object
16 * \return pointer to object or nullptr if cast is impossible
17 */
18template<typename MASK>
19MASK* buffer_cast(ByteBuffer& buffer)
20{
21 static_assert(std::is_standard_layout<MASK>::value, "MASK has to be standard layout type");
22 static_assert(std::is_trivially_copyable<MASK>::value, "MASK has to be trivially copyable");
23 static_assert(std::is_object<MASK>::value, "MASK has to be an object");
24
25 MASK* mask = nullptr;
26 if (sizeof(MASK) <= buffer.size()) {
27 mask = reinterpret_cast<MASK*>(&buffer[0]);
28 }
29 return mask;
30}
31
32template<typename MASK>
33const MASK* buffer_cast(const ByteBuffer& buffer)
34{
35 // const_cast is safe, const qualifier is added to return type
36 return buffer_cast<MASK>(const_cast<ByteBuffer&>(buffer));
37}
38
39/**
40 * Create byte buffer with copy of POD object
41 * \param obj POD object
42 * \return ByteBuffer object with copy
43 */
44template<class T>
45ByteBuffer buffer_copy(const T& object)
46{
47 static_assert(std::is_standard_layout<T>::value, "T has to be standard layout type");
48 auto ptr = reinterpret_cast<const uint8_t*>(&object);
49 return ByteBuffer(ptr, ptr + sizeof(T));
50}
51
52} // namespace vanetza
53
54#endif /* BYTE_BUFFER_HPP_7NOEQO4F */
bool decode(const ByteBuffer &buffer)
bool decode(const ByteBuffer &buffer)
void swap(asn1c_wrapper_common &other) noexcept
int compare(const asn1c_wrapper_common &other) const
bool validate(std::string &error) const
bool decode(const ByteBuffer &buffer)