1 | /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 |
---|---|
2 | // test_singleton.cpp |
3 | |
4 | // (C) Copyright 2018 Robert Ramey - http://www.rrsd.com . |
5 | // Use, modification and distribution is subject to the Boost Software |
6 | // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
7 | // http://www.boost.org/LICENSE_1_0.txt) |
8 | |
9 | // should pass compilation and execution |
10 | |
11 | #include <iostream> |
12 | #include <boost/serialization/singleton.hpp> |
13 | |
14 | #include "test_tools.hpp" |
15 | |
16 | static int i = 0; |
17 | |
18 | struct A { |
19 | int m_id; |
20 | A() : m_id(++i) {} |
21 | ~A(){ |
22 | // verify that objects are destroyed in sequence reverse of construction |
23 | if(i-- != m_id) std::terminate(); |
24 | } |
25 | }; |
26 | |
27 | struct B { |
28 | int m_id; |
29 | B() : m_id(++i) {} |
30 | ~B(){ |
31 | // verify that objects are destroyed in sequence reverse of construction |
32 | if(i-- != m_id) std::terminate(); |
33 | } |
34 | }; |
35 | |
36 | struct C { |
37 | int m_id; |
38 | C() : m_id(++i) {} |
39 | ~C(){ |
40 | // verify that objects are destroyed in sequence reverse of construction |
41 | if(i-- != m_id) std::terminate(); |
42 | } |
43 | }; |
44 | |
45 | struct D { |
46 | int m_id; |
47 | D(){ |
48 | // verify that only one object is indeed created |
49 | const C & c = boost::serialization::singleton<C>::get_const_instance(); |
50 | const C & c1 = boost::serialization::singleton<C>::get_const_instance(); |
51 | BOOST_CHECK_EQUAL(&c, &c1); |
52 | |
53 | // verify that objects are created in sequence of definition |
54 | BOOST_CHECK_EQUAL(c.m_id, 1); |
55 | const B & b = boost::serialization::singleton<B>::get_const_instance(); |
56 | BOOST_CHECK_EQUAL(b.m_id, 2); |
57 | const A & a = boost::serialization::singleton<A>::get_const_instance(); |
58 | BOOST_CHECK_EQUAL(a.m_id, 3); |
59 | std::cout << a.m_id << b.m_id << c.m_id << '\n'; |
60 | |
61 | m_id = ++i; |
62 | } |
63 | ~D(){ |
64 | // verify that objects are destroyed in sequence reverse of construction |
65 | if(i-- != m_id) std::terminate(); |
66 | } |
67 | }; |
68 | |
69 | int test_main(int, char *[]){ |
70 | return 0; |
71 | } |
72 | |
73 | // note: not a singleton |
74 | D d; |
75 |