initialized_test.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. #include <boost/utility/value_init.hpp>
  11. #include <boost/core/lightweight_test.hpp>
  12. #include <string>
  13. namespace
  14. {
  15. // Typical use case for boost::initialized<T>: A generic class that
  16. // holds a value of type T, which must be initialized by either
  17. // value-initialization or direct-initialization.
  18. template <class T> class key_value_pair
  19. {
  20. std::string m_key;
  21. boost::initialized<T> m_value;
  22. public:
  23. // Value-initializes the object held by m_value.
  24. key_value_pair() { }
  25. // Value-initializes the object held by m_value.
  26. explicit key_value_pair(const std::string& key)
  27. :
  28. m_key(key)
  29. {
  30. }
  31. // Direct-initializes the object held by m_value.
  32. key_value_pair(const std::string& key, const T& value)
  33. :
  34. m_key(key), m_value(value)
  35. {
  36. }
  37. const T& get_value() const
  38. {
  39. return m_value;
  40. }
  41. };
  42. // Tells whether the argument is value-initialized.
  43. bool is_value_initialized(const int& arg)
  44. {
  45. return arg == 0;
  46. }
  47. // Tells whether the argument is value-initialized.
  48. bool is_value_initialized(const std::string& arg)
  49. {
  50. return arg.empty();
  51. }
  52. struct foo
  53. {
  54. int data;
  55. };
  56. bool operator==(const foo& lhs, const foo& rhs)
  57. {
  58. return lhs.data == rhs.data;
  59. }
  60. // Tells whether the argument is value-initialized.
  61. bool is_value_initialized(const foo& arg)
  62. {
  63. return arg.data == 0;
  64. }
  65. template <class T>
  66. void test_key_value_pair(const T& magic_value)
  67. {
  68. // The value component of a default key_value_pair must be value-initialized.
  69. key_value_pair<T> default_key_value_pair;
  70. BOOST_TEST( is_value_initialized(default_key_value_pair.get_value() ) );
  71. // The value component of a key_value_pair that only has its key explicitly specified
  72. // must also be value-initialized.
  73. BOOST_TEST( is_value_initialized(key_value_pair<T>("key").get_value()) );
  74. // However, the value component of the following key_value_pair must be
  75. // "magic_value", as it must be direct-initialized.
  76. BOOST_TEST( key_value_pair<T>("key", magic_value).get_value() == magic_value );
  77. }
  78. }
  79. // Tests boost::initialize for a fundamental type, a type with a
  80. // user-defined constructor, and a user-defined type without
  81. // a user-defined constructor.
  82. int main()
  83. {
  84. const int magic_number = 42;
  85. test_key_value_pair(magic_number);
  86. const std::string magic_string = "magic value";
  87. test_key_value_pair(magic_string);
  88. const foo magic_foo = { 42 };
  89. test_key_value_pair(magic_foo);
  90. return boost::report_errors();
  91. }