]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/utility/initialized_test.cpp
1 // Copyright 2010, Niels Dekker.
3 // Distributed under the Boost Software License, Version 1.0. (See
4 // accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
7 // Test program for boost::initialized<T>.
9 // 2 May 2010 (Created) Niels Dekker
11 #include <boost/utility/value_init.hpp>
12 #include <boost/detail/lightweight_test.hpp>
18 // Typical use case for boost::initialized<T>: A generic class that
19 // holds a value of type T, which must be initialized by either
20 // value-initialization or direct-initialization.
21 template <class T
> class key_value_pair
24 boost::initialized
<T
> m_value
;
27 // Value-initializes the object held by m_value.
30 // Value-initializes the object held by m_value.
31 explicit key_value_pair(const std::string
& key
)
37 // Direct-initializes the object held by m_value.
38 key_value_pair(const std::string
& key
, const T
& value
)
40 m_key(key
), m_value(value
)
44 const T
& get_value() const
51 // Tells whether the argument is value-initialized.
52 bool is_value_initialized(const int& arg
)
58 // Tells whether the argument is value-initialized.
59 bool is_value_initialized(const std::string
& arg
)
69 bool operator==(const foo
& lhs
, const foo
& rhs
)
71 return lhs
.data
== rhs
.data
;
75 // Tells whether the argument is value-initialized.
76 bool is_value_initialized(const foo
& arg
)
83 void test_key_value_pair(const T
& magic_value
)
85 // The value component of a default key_value_pair must be value-initialized.
86 key_value_pair
<T
> default_key_value_pair
;
87 BOOST_TEST( is_value_initialized(default_key_value_pair
.get_value() ) );
89 // The value component of a key_value_pair that only has its key explicitly specified
90 // must also be value-initialized.
91 BOOST_TEST( is_value_initialized(key_value_pair
<T
>("key").get_value()) );
93 // However, the value component of the following key_value_pair must be
94 // "magic_value", as it must be direct-initialized.
95 BOOST_TEST( key_value_pair
<T
>("key", magic_value
).get_value() == magic_value
);
100 // Tests boost::initialize for a fundamental type, a type with a
101 // user-defined constructor, and a user-defined type without
102 // a user-defined constructor.
106 const int magic_number
= 42;
107 test_key_value_pair(magic_number
);
109 const std::string magic_string
= "magic value";
110 test_key_value_pair(magic_string
);
112 const foo magic_foo
= { 42 };
113 test_key_value_pair(magic_foo
);
115 return boost::report_errors();