MessagePack for C++
Loading...
Searching...
No Matches
complex.hpp
Go to the documentation of this file.
1//
2// MessagePack for C++ static resolution routine
3//
4// Copyright (C) 2020 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_TYPE_COMPLEX_HPP
12#define MSGPACK_V1_TYPE_COMPLEX_HPP
13
14#include <complex>
17#include "msgpack/meta.hpp"
18
19namespace msgpack {
20
24
25namespace adaptor {
26
27#if !defined(MSGPACK_USE_CPP03)
28
29template <typename T>
30struct as<std::complex<T>, typename std::enable_if<msgpack::has_as<T>::value>::type> {
31 std::complex<T> operator()(msgpack::object const& o) const {
32 if (o.type != msgpack::type::ARRAY)
33 throw msgpack::type_error();
34 if (o.via.array.size != 2)
35 throw msgpack::type_error();
36 return std::complex<T>(o.via.array.ptr[0].as<T>(), o.via.array.ptr[1].as<T>());
37 }
38};
39
40#endif // !defined(MSGPACK_USE_CPP03)
41
42template <typename T>
43struct convert<std::complex<T> > {
44 msgpack::object const& operator()(msgpack::object const& o, std::complex<T>& v) const {
45 if(o.type != msgpack::type::ARRAY)
46 throw msgpack::type_error();
47 if(o.via.array.size != 2)
48 throw msgpack::type_error();
49 T real;
50 T imag;
51 o.via.array.ptr[0].convert(real);
52 o.via.array.ptr[1].convert(imag);
53 v.real(real);
54 v.imag(imag);
55 return o;
56 }
57};
58
59template <typename T>
60struct pack<std::complex<T> > {
61 template <typename Stream>
63 o.pack_array(2);
64 o.pack(v.real());
65 o.pack(v.imag());
66 return o;
67 }
68};
69
70template <typename T>
71struct object_with_zone<std::complex<T> > {
72 void operator()(msgpack::object::with_zone& o, std::complex<T> const& v) const {
73 o.type = msgpack::type::ARRAY;
74 msgpack::object* p = static_cast<msgpack::object*>(o.zone.allocate_align(sizeof(msgpack::object)*2, MSGPACK_ZONE_ALIGNOF(msgpack::object)));
75 o.via.array.ptr = p;
76 o.via.array.size = 2;
77 p[0] = msgpack::object(v.real(), o.zone);
78 p[1] = msgpack::object(v.imag(), o.zone);
79 }
80};
81
82} // namespace adaptor
83
85} // MSGPACK_API_VERSION_NAMESPACE(v1)
87
88} // namespace msgpack
89
90
91#endif // MSGPACK_V1_TYPE_COMPLEX_HPP
The class template that supports continuous packing.
Definition pack.hpp:33
Definition object_fwd.hpp:231
Definition adaptor_base.hpp:15
std::complex< T > operator()(msgpack::object const &o) const
Definition complex.hpp:31
Definition object_fwd_decl.hpp:61
msgpack::object const & operator()(msgpack::object const &o, std::complex< T > &v) const
Definition complex.hpp:44
Definition adaptor_base.hpp:27
void operator()(msgpack::object::with_zone &o, std::complex< T > const &v) const
Definition complex.hpp:72
Definition adaptor_base.hpp:43
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, std::complex< T > const &v) const
Definition complex.hpp:62
Definition adaptor_base.hpp:32
Definition object.hpp:35
Object class that corresponding to MessagePack format object.
Definition object_fwd.hpp:75
#define MSGPACK_ZONE_ALIGNOF(type)
Definition cpp03_zone_decl.hpp:30
#define MSGPACK_API_VERSION_NAMESPACE(ns)
Definition versioning.hpp:66