]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 |
92f5a8d4 | 2 | // test_singleton.cpp |
7c673cae | 3 | |
92f5a8d4 | 4 | // (C) Copyright 2018 Robert Ramey - http://www.rrsd.com . |
7c673cae FG |
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) | |
7c673cae | 8 | |
92f5a8d4 TL |
9 | // should pass compilation and execution |
10 | ||
11 | #include <iostream> | |
7c673cae FG |
12 | #include <boost/serialization/singleton.hpp> |
13 | ||
92f5a8d4 TL |
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 | } | |
7c673cae FG |
25 | }; |
26 | ||
92f5a8d4 TL |
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 | }; | |
7c673cae | 35 | |
92f5a8d4 TL |
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 | }; | |
7c673cae | 44 | |
92f5a8d4 TL |
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); | |
7c673cae | 52 | |
92f5a8d4 TL |
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'; | |
7c673cae | 60 | |
92f5a8d4 TL |
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; | |
7c673cae | 71 | } |
92f5a8d4 TL |
72 | |
73 | // note: not a singleton | |
74 | D d; |