Coverage Report

Created: 2023-06-07 06:44

/src/msgpack-c/include/msgpack/v1/object_fwd.hpp
Line
Count
Source (jump to first uncovered line)
1
//
2
// MessagePack for C++ static resolution routine
3
//
4
// Copyright (C) 2008-2014 FURUHASHI Sadayuki and KONDO Takatoshi
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
11
#ifndef MSGPACK_V1_OBJECT_FWD_HPP
12
#define MSGPACK_V1_OBJECT_FWD_HPP
13
14
#include "msgpack/object_fwd_decl.hpp"
15
16
namespace msgpack {
17
18
/// @cond
19
MSGPACK_API_VERSION_NAMESPACE(v1) {
20
/// @endcond
21
22
struct object_array {
23
    uint32_t size;
24
    msgpack::object* ptr;
25
};
26
27
struct object_map {
28
    uint32_t size;
29
    msgpack::object_kv* ptr;
30
};
31
32
struct object_str {
33
    uint32_t size;
34
    const char* ptr;
35
};
36
37
struct object_bin {
38
    uint32_t size;
39
    const char* ptr;
40
};
41
42
struct object_ext {
43
0
    int8_t type() const { return static_cast<int8_t>(ptr[0]); }
44
0
    const char* data() const { return &ptr[1]; }
45
    uint32_t size;
46
    const char* ptr;
47
};
48
49
50
#if !defined(MSGPACK_USE_CPP03)
51
52
template <typename T>
53
struct has_as {
54
private:
55
    template <typename U>
56
    static auto check(U*) ->
57
        // Check v1 specialization
58
        typename std::is_same<
59
            decltype(adaptor::as<U>()(std::declval<msgpack::object>())),
60
            T
61
        >::type;
62
    template <typename...>
63
    static std::false_type check(...);
64
public:
65
    using type = decltype(check<T>(MSGPACK_NULLPTR));
66
    static constexpr bool value = type::value;
67
};
68
69
#endif // !defined(MSGPACK_USE_CPP03)
70
71
/// Object class that corresponding to MessagePack format object
72
/**
73
 * See https://github.com/msgpack/msgpack-c/wiki/v1_1_cpp_object
74
 */
75
struct object {
76
    union union_type {
77
        bool boolean;
78
        uint64_t u64;
79
        int64_t  i64;
80
#if defined(MSGPACK_USE_LEGACY_NAME_AS_FLOAT)
81
        MSGPACK_DEPRECATED("please use f64 instead")
82
        double   dec; // obsolete
83
#endif // MSGPACK_USE_LEGACY_NAME_AS_FLOAT
84
        double   f64;
85
        msgpack::object_array array;
86
        msgpack::object_map map;
87
        msgpack::object_str str;
88
        msgpack::object_bin bin;
89
        msgpack::object_ext ext;
90
    };
91
92
    msgpack::type::object_type type;
93
    union_type via;
94
95
    /// Cheking nil
96
    /**
97
     * @return If the object is nil, then return true, else return false.
98
     */
99
0
    bool is_nil() const { return type == msgpack::type::NIL; }
100
101
#if defined(MSGPACK_USE_CPP03)
102
103
    /// Get value as T
104
    /**
105
     * If the object can't be converted to T, msgpack::type_error would be thrown.
106
     * @tparam T The type you want to get.
107
     * @return The converted object.
108
     */
109
    template <typename T>
110
    T as() const;
111
112
#else  // defined(MSGPACK_USE_CPP03)
113
114
    /// Get value as T
115
    /**
116
     * If the object can't be converted to T, msgpack::type_error would be thrown.
117
     * @tparam T The type you want to get.
118
     * @return The converted object.
119
     */
120
    template <typename T>
121
    typename std::enable_if<msgpack::has_as<T>::value, T>::type as() const;
122
123
    /// Get value as T
124
    /**
125
     * If the object can't be converted to T, msgpack::type_error would be thrown.
126
     * @tparam T The type you want to get.
127
     * @return The converted object.
128
     */
129
    template <typename T>
130
    typename std::enable_if<!msgpack::has_as<T>::value, T>::type as() const;
131
132
#endif // defined(MSGPACK_USE_CPP03)
133
134
    /// Convert the object
135
    /**
136
     * If the object can't be converted to T, msgpack::type_error would be thrown.
137
     * @tparam T The type of v.
138
     * @param v The value you want to get. `v` is output parameter. `v` is overwritten by converted value from the object.
139
     * @return The reference of `v`.
140
     */
141
    template <typename T>
142
    typename msgpack::enable_if<
143
        !msgpack::is_array<T>::value && !msgpack::is_pointer<T>::value,
144
        T&
145
    >::type
146
    convert(T& v) const;
147
148
    template <typename T, std::size_t N>
149
    T (&convert(T(&v)[N]) const)[N];
150
151
152
#if !defined(MSGPACK_DISABLE_LEGACY_CONVERT)
153
    /// Convert the object (obsolete)
154
    /**
155
     * If the object can't be converted to T, msgpack::type_error would be thrown.
156
     * @tparam T The type of v.
157
     * @param v The pointer of the value you want to get. `v` is output parameter. `*v` is overwritten by converted value from the object.
158
     * @return The pointer of `v`.
159
     */
160
    template <typename T>
161
    MSGPACK_DEPRECATED("please use reference version instead")
162
    typename msgpack::enable_if<
163
        msgpack::is_pointer<T>::value,
164
        T
165
    >::type
166
    convert(T v) const;
167
#endif // !defined(MSGPACK_DISABLE_LEGACY_CONVERT)
168
169
    /// Convert the object if not nil
170
    /**
171
     * If the object is not nil and can't be converted to T, msgpack::type_error would be thrown.
172
     * @tparam T The type of v.
173
     * @param v The value you want to get. `v` is output parameter. `v` is overwritten by converted value from the object if the object is not nil.
174
     * @return If the object is nil, then return false, else return true.
175
     */
176
    template <typename T>
177
    bool convert_if_not_nil(T& v) const;
178
179
    /// Default constructor. The object is set to nil.
180
    object();
181
182
    /// Construct object from T
183
    /**
184
     * If `v` is the type that is corresponding to MessegePack format str, bin, ext, array, or map,
185
     * you need to call `object(const T& v, msgpack::zone& z)` instead of this constructor.
186
     *
187
     * @tparam T The type of `v`.
188
     * @param v The value you want to convert.
189
     */
190
    template <typename T>
191
    explicit object(const T& v);
192
193
    /// Construct object from T
194
    /**
195
     * The object is constructed on the zone `z`.
196
     * See https://github.com/msgpack/msgpack-c/wiki/v1_1_cpp_object
197
     *
198
     * @tparam T The type of `v`.
199
     * @param v The value you want to convert.
200
     * @param z The zone that is used by the object.
201
     */
202
    template <typename T>
203
    object(const T& v, msgpack::zone& z);
204
205
    /// Construct object from T (obsolete)
206
    /**
207
     * The object is constructed on the zone `z`.
208
     * Use `object(const T& v, msgpack::zone& z)` instead of this constructor.
209
     * See https://github.com/msgpack/msgpack-c/wiki/v1_1_cpp_object
210
     *
211
     * @tparam T The type of `v`.
212
     * @param v The value you want to convert.
213
     * @param z The pointer to the zone that is used by the object.
214
     */
215
    template <typename T>
216
    MSGPACK_DEPRECATED("please use zone reference version instead of the pointer version")
217
    object(const T& v, msgpack::zone* z);
218
219
    template <typename T>
220
    object& operator=(const T& v);
221
222
    struct with_zone;
223
224
protected:
225
    struct implicit_type;
226
227
public:
228
    implicit_type convert() const;
229
};
230
231
class type_error : public std::bad_cast { };
232
233
struct object::implicit_type {
234
0
    implicit_type(object const& o) : obj(o) { }
235
0
    ~implicit_type() { }
236
237
    template <typename T>
238
    operator T();
239
240
private:
241
    object const& obj;
242
};
243
244
/// @cond
245
} // MSGPACK_API_VERSION_NAMESPACE(v1)
246
/// @endcond
247
248
} // namespace msgpack
249
250
#endif // MSGPACK_V1_OBJECT_FWD_HPP