cpp-msgpack-light
0.3.0
A light library to serialize MessagePack.
Toggle main menu visibility
Loading...
Searching...
No Matches
timespec.h
Go to the documentation of this file.
1
/*
2
* Copyright 2024 MusicScience37 (Kenta Kabashima)
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
* http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*/
21
#pragma once
22
23
#include <cstdint>
24
#include <ctime>
25
26
#include "
msgpack_light/serialization_buffer.h
"
27
#include "
msgpack_light/type_support/fwd.h
"
28
29
namespace
msgpack_light::type_support
{
30
34
template
<>
35
struct
serialization_traits
<
std
::timespec> {
36
public
:
46
static
void
serialize
(
47
serialization_buffer
& buffer,
const
std::timespec& value) {
48
constexpr
auto
ext_type =
static_cast<
std::int8_t
>
(-1);
49
50
constexpr
auto
max_timestamp32_seconds =
51
static_cast<
std::time_t
>
(0xFFFFFFFF);
52
if
(value.tv_nsec == 0 &&
53
static_cast<
std::time_t
>
(0U) <= value.tv_sec &&
54
value.tv_sec <= max_timestamp32_seconds) {
55
// timestamp 32 format
56
buffer.
serialize_fixext4_header
(ext_type);
57
buffer.
write_in_big_endian
(
58
static_cast<
std::uint32_t
>
(value.tv_sec));
59
return
;
60
}
61
62
constexpr
auto
max_timestamp64_seconds =
63
static_cast<
std::time_t
>
(0x3FFFFFFFF);
64
if
(
static_cast<
std::time_t
>
(0U) <= value.tv_sec &&
65
value.tv_sec <= max_timestamp64_seconds) {
66
// timestamp 64 format
67
auto
data =
static_cast<
std::uint64_t
>
(value.tv_nsec);
68
constexpr
unsigned
int
nsec_offset = 34;
69
data <<= nsec_offset;
70
data |=
static_cast<
std::uint64_t
>
(value.tv_sec);
71
72
buffer.
serialize_fixext8_header
(ext_type);
73
buffer.
write_in_big_endian
(data);
74
return
;
75
}
76
77
// timestamp 96 format
78
constexpr
std::size_t timestamp96_data_size = 12;
79
buffer.
serialize_ext_header
(ext_type, timestamp96_data_size);
80
buffer.
write_in_big_endian
(
static_cast<
std::uint32_t
>
(value.tv_nsec));
81
buffer.
write_in_big_endian
(
static_cast<
std::int64_t
>
(value.tv_sec));
82
}
83
};
84
85
}
// namespace msgpack_light::type_support
msgpack_light::serialization_buffer
Class of buffers to serialize data.
Definition
serialization_buffer.h:41
msgpack_light::serialization_buffer::serialize_fixext8_header
void serialize_fixext8_header(std::int8_t ext_type)
Serialize the size and type of an extension value in fixext 8 format.
Definition
serialization_buffer.h:624
msgpack_light::serialization_buffer::write_in_big_endian
void write_in_big_endian(T... values)
Write values in big endian.
Definition
serialization_buffer.h:796
msgpack_light::serialization_buffer::serialize_ext_header
void serialize_ext_header(std::int8_t ext_type, std::size_t data_size)
Serialize the size and type of an extension value.
Definition
serialization_buffer.h:691
msgpack_light::serialization_buffer::serialize_fixext4_header
void serialize_fixext4_header(std::int8_t ext_type)
Serialize the size and type of an extension value in fixext 4 format.
Definition
serialization_buffer.h:611
fwd.h
Forward declaration of classes to support serialization of data types.
msgpack_light::type_support
Namespace of classes to support serialization of types.
Definition
array.h:29
std
STL namespace.
serialization_buffer.h
Definition of serialization_buffer class.
msgpack_light::type_support::serialization_traits< std::timespec >::serialize
static void serialize(serialization_buffer &buffer, const std::timespec &value)
Serialize a value.
Definition
timespec.h:46
msgpack_light::type_support::serialization_traits
Class to define functions to serialize data of various types.
Definition
fwd.h:43
include
msgpack_light
type_support
timespec.h
Generated on
for cpp-msgpack-light by
1.18.0