cpp-msgpack-light 0.3.0
A light library to serialize MessagePack.
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
28
30
34template <>
35struct serialization_traits<std::timespec> {
36public:
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);
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
Class of buffers to serialize data.
void serialize_fixext8_header(std::int8_t ext_type)
Serialize the size and type of an extension value in fixext 8 format.
void write_in_big_endian(T... values)
Write values in big endian.
void serialize_ext_header(std::int8_t ext_type, std::size_t data_size)
Serialize the size and type of an extension value.
void serialize_fixext4_header(std::int8_t ext_type)
Serialize the size and type of an extension value in fixext 4 format.
Forward declaration of classes to support serialization of data types.
Namespace of classes to support serialization of types.
Definition array.h:29
STL namespace.
Definition of serialization_buffer class.
static void serialize(serialization_buffer &buffer, const std::timespec &value)
Serialize a value.
Definition timespec.h:46
Class to define functions to serialize data of various types.
Definition fwd.h:43