File: shared_ptr_cpp11.cpp

package info (click to toggle)
msgpack-cxx 7.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 8,520 kB
  • sloc: cpp: 87,413; ansic: 3,571; sh: 56; makefile: 39
file content (127 lines) | stat: -rw-r--r-- 3,357 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <msgpack.hpp>
#include <sstream>
#include <iterator>

#define BOOST_TEST_MODULE SHARED_PTR
#include <boost/test/unit_test.hpp>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#if !defined(MSGPACK_USE_CPP03)

BOOST_AUTO_TEST_CASE(pack_convert_nil)
{
    std::stringstream ss;
    std::shared_ptr<int> val1;
    msgpack::pack(ss, val1);
    std::string const& str = ss.str();
    msgpack::object_handle oh =
        msgpack::unpack(str.data(), str.size());
    std::shared_ptr<int> val2 = oh.get().as<std::shared_ptr<int>>();
    BOOST_CHECK(val1 == val2);
}

BOOST_AUTO_TEST_CASE(pack_convert_int)
{
    std::stringstream ss;
    std::shared_ptr<int> val1(new int(1));
    msgpack::pack(ss, val1);
    std::string const& str = ss.str();
    msgpack::object_handle oh =
        msgpack::unpack(str.data(), str.size());
    std::shared_ptr<int> val2 = oh.get().as<std::shared_ptr<int>>();
    BOOST_CHECK(*val1 == *val2);
}

BOOST_AUTO_TEST_CASE(object_nil)
{
    std::shared_ptr<int> val1;
    msgpack::object obj(val1);
    std::shared_ptr<int> val2 = obj.as<std::shared_ptr<int>>();
    BOOST_CHECK(val1 == val2);
}

BOOST_AUTO_TEST_CASE(object_int)
{
    std::shared_ptr<int> val1(new int(1));
    msgpack::object obj(val1);
    std::shared_ptr<int> val2 = obj.as<std::shared_ptr<int>>();
    BOOST_CHECK(*val1 == *val2);
}

// Compile error as expected
// object::with_zone is required not object
/*
BOOST_AUTO_TEST_CASE(object_vector)
{
    typedef std::shared_ptr<std::vector<int>> ovi_t;
    ovi_t val1(new std::vector<int>());
    msgpack::object obj(val1);
    ovi_t  val2 = obj.as<ovi_t>();
    BOOST_CHECK(val1 == val2);
}
*/

BOOST_AUTO_TEST_CASE(object_with_zone_nil)
{
    msgpack::zone z;
    std::shared_ptr<int> val1;
    msgpack::object obj(val1, z);
    std::shared_ptr<int> val2 = obj.as<std::shared_ptr<int>>();
    BOOST_CHECK(val1 == val2);
}

BOOST_AUTO_TEST_CASE(object_with_zone_int)
{
    msgpack::zone z;
    std::shared_ptr<int> val1(new int(1));
    msgpack::object obj(val1, z);
    std::shared_ptr<int> val2 = obj.as<std::shared_ptr<int>>();
    BOOST_CHECK(*val1 == *val2);
}

struct no_def_con {
    no_def_con() = delete;
    no_def_con(int i):i(i) {}
    int i;
    MSGPACK_DEFINE(i);
};

inline bool operator==(no_def_con const& lhs, no_def_con const& rhs) {
    return lhs.i == rhs.i;
}

inline bool operator!=(no_def_con const& lhs, no_def_con const& rhs) {
    return !(lhs == rhs);
}

namespace msgpack {
MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) {
namespace adaptor {
template <>
struct as<no_def_con> {
    no_def_con operator()(msgpack::object const& o) const {
        if (o.type != msgpack::type::ARRAY) throw msgpack::type_error();
        if (o.via.array.size != 1) throw msgpack::type_error();
        return no_def_con(o.via.array.ptr[0].as<int>());
    }
};
} // adaptor
} // MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS)
} // msgpack

BOOST_AUTO_TEST_CASE(pack_convert_nil_no_def_con)
{
    std::stringstream ss;
    std::shared_ptr<no_def_con> val1(new no_def_con(1));
    msgpack::pack(ss, val1);
    std::string const& str = ss.str();
    msgpack::object_handle oh =
        msgpack::unpack(str.data(), str.size());
    std::shared_ptr<no_def_con> val2 = oh.get().as<std::shared_ptr<no_def_con>>();
    BOOST_CHECK(*val1 == *val2);
}


#endif // !defined(MSGPACK_USE_CPP03)