• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2010, Niels Dekker.
2 //
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)
6 //
7 // Test program for boost::initialized<T>.
8 //
9 // 2 May 2010 (Created) Niels Dekker
10 
11 #include <boost/utility/value_init.hpp>
12 #include <boost/core/lightweight_test.hpp>
13 
14 #include <string>
15 
16 namespace
17 {
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
22   {
23     std::string m_key;
24     boost::initialized<T> m_value;
25   public:
26 
27     // Value-initializes the object held by m_value.
key_value_pair()28     key_value_pair() { }
29 
30     // Value-initializes the object held by m_value.
key_value_pair(const std::string & key)31     explicit key_value_pair(const std::string& key)
32     :
33     m_key(key)
34     {
35     }
36 
37     // Direct-initializes the object held by m_value.
key_value_pair(const std::string & key,const T & value)38     key_value_pair(const std::string& key, const T& value)
39     :
40     m_key(key), m_value(value)
41     {
42     }
43 
get_value() const44     const T& get_value() const
45     {
46       return m_value;
47     }
48   };
49 
50 
51   // Tells whether the argument is value-initialized.
is_value_initialized(const int & arg)52   bool is_value_initialized(const int& arg)
53   {
54     return arg == 0;
55   }
56 
57 
58   // Tells whether the argument is value-initialized.
is_value_initialized(const std::string & arg)59   bool is_value_initialized(const std::string& arg)
60   {
61     return arg.empty();
62   }
63 
64   struct foo
65   {
66     int data;
67   };
68 
operator ==(const foo & lhs,const foo & rhs)69   bool operator==(const foo& lhs, const foo& rhs)
70   {
71     return lhs.data == rhs.data;
72   }
73 
74 
75   // Tells whether the argument is value-initialized.
is_value_initialized(const foo & arg)76   bool is_value_initialized(const foo& arg)
77   {
78     return arg.data == 0;
79   }
80 
81 
82   template <class T>
test_key_value_pair(const T & magic_value)83   void test_key_value_pair(const T& magic_value)
84   {
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() ) );
88 
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()) );
92 
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 );
96   }
97 }
98 
99 
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.
main()103 int main()
104 {
105 
106   const int magic_number = 42;
107   test_key_value_pair(magic_number);
108 
109   const std::string magic_string = "magic value";
110   test_key_value_pair(magic_string);
111 
112   const foo magic_foo = { 42 };
113   test_key_value_pair(magic_foo);
114 
115   return boost::report_errors();
116 }
117