|
12 | 12 | #if defined(MSGPACK_USE_BOOST) |
13 | 13 |
|
14 | 14 | #include <boost/fusion/adapted/struct/define_struct.hpp> |
| 15 | +#include <boost/fusion/adapted/struct/adapt_struct.hpp> |
15 | 16 |
|
16 | 17 | const double kEPS = 1e-10; |
17 | 18 |
|
@@ -48,4 +49,109 @@ TEST(MSGPACK_BOOST, object_with_zone_convert) |
48 | 49 | EXPECT_TRUE(fabs(val2.f2 - val1.f2) <= kEPS); |
49 | 50 | } |
50 | 51 |
|
| 52 | +#if !defined(MSGPACK_USE_CPP03) |
| 53 | + |
| 54 | +struct no_def_con1 { |
| 55 | + no_def_con1() = delete; |
| 56 | + no_def_con1(int i):i(i) {} |
| 57 | + int i; |
| 58 | + MSGPACK_DEFINE(i); |
| 59 | +}; |
| 60 | + |
| 61 | +inline bool operator==(no_def_con1 const& lhs, no_def_con1 const& rhs) { |
| 62 | + return lhs.i == rhs.i; |
| 63 | +} |
| 64 | + |
| 65 | +inline bool operator!=(no_def_con1 const& lhs, no_def_con1 const& rhs) { |
| 66 | + return !(lhs == rhs); |
| 67 | +} |
| 68 | + |
| 69 | +struct no_def_con2 { |
| 70 | + no_def_con2() = delete; |
| 71 | + no_def_con2(int i):i(i) {} |
| 72 | + int i; |
| 73 | + MSGPACK_DEFINE(i); |
| 74 | +}; |
| 75 | + |
| 76 | +inline bool operator==(no_def_con2 const& lhs, no_def_con2 const& rhs) { |
| 77 | + return lhs.i == rhs.i; |
| 78 | +} |
| 79 | + |
| 80 | +inline bool operator!=(no_def_con2 const& lhs, no_def_con2 const& rhs) { |
| 81 | + return !(lhs == rhs); |
| 82 | +} |
| 83 | + |
| 84 | + |
| 85 | +namespace msgpack { |
| 86 | +MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) { |
| 87 | +namespace adaptor { |
| 88 | + |
| 89 | +template <> |
| 90 | +struct as<no_def_con1> { |
| 91 | + no_def_con1 operator()(msgpack::object const& o) const { |
| 92 | + if (o.type != msgpack::type::ARRAY) throw msgpack::type_error(); |
| 93 | + if (o.via.array.size != 1) throw msgpack::type_error(); |
| 94 | + return no_def_con1(o.via.array.ptr[0].as<int>()); |
| 95 | + } |
| 96 | +}; |
| 97 | + |
| 98 | +template <> |
| 99 | +struct as<no_def_con2> { |
| 100 | + no_def_con2 operator()(msgpack::object const& o) const { |
| 101 | + if (o.type != msgpack::type::ARRAY) throw msgpack::type_error(); |
| 102 | + if (o.via.array.size != 1) throw msgpack::type_error(); |
| 103 | + return no_def_con2(o.via.array.ptr[0].as<int>()); |
| 104 | + } |
| 105 | +}; |
| 106 | + |
| 107 | +} // adaptor |
| 108 | +} // MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) |
| 109 | +} // msgpack |
| 110 | + |
| 111 | +struct mystruct_no_def_con { |
| 112 | + mystruct_no_def_con() = delete; |
| 113 | + // Constructor that have parameters corresponding to BOOST_FUSION_ADAPT_STRUCT is mandatory. |
| 114 | + // See *1, *2, and *3 |
| 115 | + mystruct_no_def_con( |
| 116 | + no_def_con1 i, |
| 117 | + no_def_con2 j, |
| 118 | + no_def_con1 k): |
| 119 | + f1(std::move(i)), |
| 120 | + f2(std::move(j)), |
| 121 | + f3(std::move(k)) {} |
| 122 | + |
| 123 | + no_def_con1 f1; |
| 124 | + no_def_con2 f2; |
| 125 | + no_def_con1 f3; |
| 126 | +}; |
| 127 | + |
| 128 | +inline bool operator==(mystruct_no_def_con const& lhs, mystruct_no_def_con const& rhs) { |
| 129 | + return lhs.f1 == rhs.f1 && lhs.f2 == rhs.f2 && lhs.f3 == rhs.f3; |
| 130 | +} |
| 131 | + |
| 132 | +inline bool operator!=(mystruct_no_def_con const& lhs, mystruct_no_def_con const& rhs) { |
| 133 | + return !(lhs == rhs); |
| 134 | +} |
| 135 | + |
| 136 | +BOOST_FUSION_ADAPT_STRUCT( |
| 137 | + mystruct_no_def_con, |
| 138 | + f1, // *1 |
| 139 | + f2, // *2 |
| 140 | + f3 // *3 |
| 141 | + ) |
| 142 | + |
| 143 | +TEST(MSGPACK_BOOST, pack_convert_no_def_con) |
| 144 | +{ |
| 145 | + std::stringstream ss; |
| 146 | + mystruct_no_def_con val1(no_def_con1(1), no_def_con2(2), no_def_con1(3)); |
| 147 | + msgpack::pack(ss, val1); |
| 148 | + msgpack::unpacked ret; |
| 149 | + msgpack::unpack(ret, ss.str().data(), ss.str().size()); |
| 150 | + mystruct_no_def_con val2 = ret.get().as<mystruct_no_def_con>(); |
| 151 | + EXPECT_TRUE(val1 == val2); |
| 152 | +} |
| 153 | + |
| 154 | + |
| 155 | +#endif // !defined(MSGPACK_USE_CPP03 |
| 156 | + |
51 | 157 | #endif // defined(MSGPACK_USE_BOOST) |
0 commit comments