forked from cryfs/cryfs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerializationHelper.h
More file actions
78 lines (62 loc) · 2.56 KB
/
Copy pathSerializationHelper.h
File metadata and controls
78 lines (62 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#pragma once
#ifndef MESSMER_CPPUTILS_DATA_SERIALIZATIONHELPER_H
#define MESSMER_CPPUTILS_DATA_SERIALIZATIONHELPER_H
#include <type_traits>
#include <cstring>
#include <cstdint>
namespace cpputils {
namespace details {
constexpr bool greater_than(size_t lhs, size_t rhs) {
return lhs > rhs;
}
template<class DataType, class Enable = void> struct serialize;
// Specialize for 1-byte types for faster performance with direct pointer access (no memcpy).
template<class DataType>
struct serialize<DataType, typename std::enable_if<sizeof(DataType) == 1>::type> final {
static_assert(std::is_pod<DataType>::value, "Can only serialize PODs");
static void call(void *dst, const DataType &obj) {
*static_cast<DataType *>(dst) = obj;
}
};
// Specialize for larger types with memcpy because unaligned data accesses through pointers are undefined behavior.
template<class DataType>
struct serialize<DataType, typename std::enable_if<greater_than(sizeof(DataType), 1)>::type> final {
static_assert(std::is_pod<DataType>::value, "Can only serialize PODs");
static void call(void *dst, const DataType &obj) {
std::memcpy(dst, &obj, sizeof(DataType));
}
};
template<class DataType, class Enable = void> struct deserialize;
// Specialize for 1-byte types for faster performance with direct pointer access (no memcpy).
template<class DataType>
struct deserialize<DataType, typename std::enable_if<sizeof(DataType) == 1>::type> final {
static_assert(std::is_pod<DataType>::value, "Can only serialize PODs");
static DataType call(const void *src) {
return *static_cast<const DataType *>(src);
}
};
// Specialize for larger types with memcpy because unaligned data accesses through pointers are undefined behavior.
template<class DataType>
struct deserialize<DataType, typename std::enable_if<greater_than(sizeof(DataType), 1)>::type> final {
static_assert(std::is_pod<DataType>::value, "Can only deserialize PODs");
static DataType call(const void *src) {
typename std::remove_const<DataType>::type result{};
std::memcpy(&result, src, sizeof(DataType));
return result;
}
};
}
template<class DataType>
inline void serialize(void *dst, const DataType& obj) {
return details::serialize<DataType>::call(dst, obj);
}
template<class DataType>
inline DataType deserialize(const void *src) {
return details::deserialize<DataType>::call(src);
}
template<class DataType>
inline DataType deserializeWithOffset(const void* src, size_t offset) {
return deserialize<DataType>(static_cast<const uint8_t*>(src) + offset);
}
}
#endif