MessagePack for C++
float.hpp
Go to the documentation of this file.
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 
22 
23 // FIXME check overflow, underflow
24 
25 namespace adaptor {
26 
27 template <>
28 struct convert<float> {
29  msgpack::object const& operator()(msgpack::object const& o, float& v) const {
31  v = static_cast<float>(o.via.f64);
32  }
33  else if (o.type == msgpack::type::POSITIVE_INTEGER) {
34  v = static_cast<float>(o.via.u64);
35  }
36  else if (o.type == msgpack::type::NEGATIVE_INTEGER) {
37  v = static_cast<float>(o.via.i64);
38  }
39  else {
40  throw msgpack::type_error();
41  }
42  return o;
43  }
44 };
45 
46 template <>
47 struct pack<float> {
48  template <typename Stream>
50  o.pack_float(v);
51  return o;
52  }
53 };
54 
55 
56 template <>
57 struct convert<double> {
58  msgpack::object const& operator()(msgpack::object const& o, double& v) const {
60  v = o.via.f64;
61  }
62  else if (o.type == msgpack::type::POSITIVE_INTEGER) {
63  v = static_cast<double>(o.via.u64);
64  }
65  else if (o.type == msgpack::type::NEGATIVE_INTEGER) {
66  v = static_cast<double>(o.via.i64);
67  }
68  else {
69  throw msgpack::type_error();
70  }
71  return o;
72  }
73 };
74 
75 template <>
76 struct pack<double> {
77  template <typename Stream>
79  o.pack_double(v);
80  return o;
81  }
82 };
83 
84 
85 template <>
86 struct object<float> {
87  void operator()(msgpack::object& o, float v) const {
89  o.via.f64 = static_cast<double>(v);
90  }
91 };
92 
93 template <>
94 struct object<double> {
95  void operator()(msgpack::object& o, double v) const {
97  o.via.f64 = v;
98  }
99 };
100 
101 template <>
102 struct object_with_zone<float> {
103  void operator()(msgpack::object::with_zone& o, float v) const {
104  static_cast<msgpack::object&>(o) << v;
105  }
106 };
107 
108 template <>
109 struct object_with_zone<double> {
110  void operator()(msgpack::object::with_zone& o, double v) const {
111  static_cast<msgpack::object&>(o) << v;
112  }
113 };
114 
115 } // namespace adaptor
116 
118 } // MSGPACK_API_VERSION_NAMESPACE(v1)
120 
121 } // namespace msgpack
122 
123 #endif // MSGPACK_V1_TYPE_FLOAT_HPP
The class template that supports continuous packing.
Definition: pack.hpp:33
packer< Stream > & pack_double(double d)
Packing double.
Definition: pack.hpp:1150
packer< Stream > & pack_float(float d)
Packing float.
Definition: pack.hpp:1139
Definition: object_fwd.hpp:231
@ FLOAT64
Definition: object_fwd_decl.hpp:33
@ POSITIVE_INTEGER
Definition: object_fwd_decl.hpp:30
@ NEGATIVE_INTEGER
Definition: object_fwd_decl.hpp:31
@ FLOAT32
Definition: object_fwd_decl.hpp:32
Definition: adaptor_base.hpp:15
msgpack::object const & operator()(msgpack::object const &o, double &v) const
Definition: float.hpp:58
msgpack::object const & operator()(msgpack::object const &o, float &v) const
Definition: float.hpp:29
Definition: adaptor_base.hpp:27
void operator()(msgpack::object &o, double v) const
Definition: float.hpp:95
void operator()(msgpack::object &o, float v) const
Definition: float.hpp:87
void operator()(msgpack::object::with_zone &o, double v) const
Definition: float.hpp:110
void operator()(msgpack::object::with_zone &o, float v) const
Definition: float.hpp:103
Definition: adaptor_base.hpp:43
Definition: adaptor_base.hpp:38
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const double &v) const
Definition: float.hpp:78
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const float &v) const
Definition: float.hpp:49
Definition: adaptor_base.hpp:32
Definition: object.hpp:35
Object class that corresponding to MessagePack format object.
Definition: object_fwd.hpp:75
union_type via
Definition: object_fwd.hpp:93
msgpack::type::object_type type
Definition: object_fwd.hpp:92
uint64_t u64
Definition: object_fwd.hpp:78
int64_t i64
Definition: object_fwd.hpp:79
double f64
Definition: object_fwd.hpp:84
#define MSGPACK_API_VERSION_NAMESPACE(ns)
Definition: versioning.hpp:66