Coverage Report

Created: 2025-12-05 06:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/msgpack-c/include/msgpack/v1/adaptor/float.hpp
Line
Count
Source
1
//
2
// MessagePack for C++ static resolution routine
3
//
4
// Copyright (C) 2008-2009 FURUHASHI Sadayuki
5
//
6
//    Distributed under the Boost Software License, Version 1.0.
7
//    (See accompanying file LICENSE_1_0.txt or copy at
8
//    http://www.boost.org/LICENSE_1_0.txt)
9
//
10
#ifndef MSGPACK_V1_TYPE_FLOAT_HPP
11
#define MSGPACK_V1_TYPE_FLOAT_HPP
12
13
#include "msgpack/versioning.hpp"
14
#include "msgpack/object_fwd.hpp"
15
#include <vector>
16
17
namespace msgpack {
18
19
/// @cond
20
MSGPACK_API_VERSION_NAMESPACE(v1) {
21
/// @endcond
22
23
// FIXME check overflow, underflow
24
25
namespace adaptor {
26
27
template <>
28
struct convert<float> {
29
0
    msgpack::object const& operator()(msgpack::object const& o, float& v) const {
30
0
        if(o.type == msgpack::type::FLOAT32 || o.type == msgpack::type::FLOAT64) {
31
0
            v = static_cast<float>(o.via.f64);
32
0
        }
33
0
        else if (o.type == msgpack::type::POSITIVE_INTEGER) {
34
0
            v = static_cast<float>(o.via.u64);
35
0
        }
36
0
        else if (o.type == msgpack::type::NEGATIVE_INTEGER) {
37
0
            v = static_cast<float>(o.via.i64);
38
0
        }
39
0
        else {
40
0
            throw msgpack::type_error();
41
0
        }
42
0
        return o;
43
0
    }
44
};
45
46
template <>
47
struct pack<float> {
48
    template <typename Stream>
49
    msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const float& v) const {
50
        o.pack_float(v);
51
        return o;
52
    }
53
};
54
55
56
template <>
57
struct convert<double> {
58
0
    msgpack::object const& operator()(msgpack::object const& o, double& v) const {
59
0
        if(o.type == msgpack::type::FLOAT32 || o.type == msgpack::type::FLOAT64) {
60
0
            v = o.via.f64;
61
0
        }
62
0
        else if (o.type == msgpack::type::POSITIVE_INTEGER) {
63
0
            v = static_cast<double>(o.via.u64);
64
0
        }
65
0
        else if (o.type == msgpack::type::NEGATIVE_INTEGER) {
66
0
            v = static_cast<double>(o.via.i64);
67
0
        }
68
0
        else {
69
0
            throw msgpack::type_error();
70
0
        }
71
0
        return o;
72
0
    }
73
};
74
75
template <>
76
struct pack<double> {
77
    template <typename Stream>
78
    msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const double& v) const {
79
        o.pack_double(v);
80
        return o;
81
    }
82
};
83
84
85
template <>
86
struct object<float> {
87
0
    void operator()(msgpack::object& o, float v) const {
88
0
        o.type = msgpack::type::FLOAT32;
89
0
        o.via.f64 = static_cast<double>(v);
90
0
    }
91
};
92
93
template <>
94
struct object<double> {
95
0
    void operator()(msgpack::object& o, double v) const {
96
0
        o.type = msgpack::type::FLOAT64;
97
0
        o.via.f64 = v;
98
0
    }
99
};
100
101
template <>
102
struct object_with_zone<float> {
103
0
    void operator()(msgpack::object::with_zone& o, float v) const {
104
0
        static_cast<msgpack::object&>(o) << v;
105
0
    }
106
};
107
108
template <>
109
struct object_with_zone<double> {
110
0
    void operator()(msgpack::object::with_zone& o, double v) const {
111
0
        static_cast<msgpack::object&>(o) << v;
112
0
    }
113
};
114
115
} // namespace adaptor
116
117
/// @cond
118
}  // MSGPACK_API_VERSION_NAMESPACE(v1)
119
/// @endcond
120
121
}  // namespace msgpack
122
123
#endif // MSGPACK_V1_TYPE_FLOAT_HPP