summaryrefslogtreecommitdiff
path: root/src/third_party/boost-1.56.0/libs/utility/initialized_test.cpp
blob: 9380957a3a8320b7fee7b3d2bc3e3ac204a98a60 (plain)
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
// Copyright 2010, Niels Dekker.
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// Test program for boost::initialized<T>.
//
// 2 May 2010 (Created) Niels Dekker

#include <boost/utility/value_init.hpp>
#include <boost/detail/lightweight_test.hpp>

#include <string>

namespace
{
  // Typical use case for boost::initialized<T>: A generic class that 
  // holds a value of type T, which must be initialized by either 
  // value-initialization or direct-initialization.
  template <class T> class key_value_pair
  {
    std::string m_key;
    boost::initialized<T> m_value;
  public:
 
    // Value-initializes the object held by m_value.
    key_value_pair() { }

    // Value-initializes the object held by m_value.
    explicit key_value_pair(const std::string& key)
    :
    m_key(key)
    {
    }

    // Direct-initializes the object held by m_value.
    key_value_pair(const std::string& key, const T& value)
    :
    m_key(key), m_value(value)
    {
    }

    const T& get_value() const
    {
      return m_value;
    }
  };


  // Tells whether the argument is value-initialized.
  bool is_value_initialized(const int& arg)
  {
    return arg == 0;
  }


  // Tells whether the argument is value-initialized.
  bool is_value_initialized(const std::string& arg)
  {
    return arg.empty();
  }

  struct foo
  {
    int data;
  };

  bool operator==(const foo& lhs, const foo& rhs)
  {
    return lhs.data == rhs.data;
  }


  // Tells whether the argument is value-initialized.
  bool is_value_initialized(const foo& arg)
  {
    return arg.data == 0;
  }


  template <class T>
  void test_key_value_pair(const T& magic_value)
  {
    // The value component of a default key_value_pair must be value-initialized.
    key_value_pair<T> default_key_value_pair;
    BOOST_TEST( is_value_initialized(default_key_value_pair.get_value() ) );

    // The value component of a key_value_pair that only has its key explicitly specified
    // must also be value-initialized.
    BOOST_TEST( is_value_initialized(key_value_pair<T>("key").get_value()) );

    // However, the value component of the following key_value_pair must be 
    // "magic_value", as it must be direct-initialized.
    BOOST_TEST( key_value_pair<T>("key", magic_value).get_value() == magic_value );
  }
}


// Tests boost::initialize for a fundamental type, a type with a
// user-defined constructor, and a user-defined type without 
// a user-defined constructor.
int main()
{

  const int magic_number = 42;
  test_key_value_pair(magic_number);

  const std::string magic_string = "magic value";
  test_key_value_pair(magic_string);

  const foo magic_foo = { 42 };
  test_key_value_pair(magic_foo);

  return boost::report_errors();
}