test2.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //-----------------------------------------------------------------------------
  2. // boost-libs variant/test/test2.cpp header file
  3. // See http://www.boost.org for updates, documentation, and revision history.
  4. //-----------------------------------------------------------------------------
  5. //
  6. // Copyright (c) 2003
  7. // Eric Friedman, Itay Maman
  8. //
  9. // Distributed under the Boost Software License, Version 1.0. (See
  10. // accompanying file LICENSE_1_0.txt or copy at
  11. // http://www.boost.org/LICENSE_1_0.txt)
  12. #include "boost/config.hpp"
  13. #ifdef BOOST_MSVC
  14. #pragma warning(disable:4244) // conversion from 'const int' to 'const short'
  15. #endif
  16. #include "boost/core/lightweight_test.hpp"
  17. #include "boost/variant.hpp"
  18. #include "jobs.h"
  19. #include <cassert>
  20. #include <iostream>
  21. #include <algorithm>
  22. #include <cstring>
  23. using boost::apply_visitor;
  24. struct short_string
  25. {
  26. BOOST_STATIC_CONSTANT(size_t, e_limit = 101);
  27. short_string() : len_(0)
  28. {
  29. buffer_[0] = '\0';
  30. }
  31. short_string(const char* src)
  32. {
  33. #ifndef BOOST_NO_STDC_NAMESPACE
  34. using std::strlen;
  35. #endif // BOOST_NO_STDC_NAMESPACE
  36. size_t limit = this->e_limit; // avoid warnings on some compilers
  37. size_t src_len = strlen(src);
  38. len_ = (std::min)(src_len, limit-1);
  39. std::copy(src, src + len_, buffer_);
  40. buffer_[len_] = '\0';
  41. }
  42. short_string(const short_string& other) : len_(other.len_)
  43. {
  44. std::copy(other.buffer_, other.buffer_ + e_limit, buffer_);
  45. }
  46. void swap(short_string& other)
  47. {
  48. char temp[e_limit];
  49. std::copy(buffer_, buffer_ + e_limit, temp);
  50. std::copy(other.buffer_, other.buffer_ + e_limit, buffer_);
  51. std::copy(temp, temp + e_limit, other.buffer_);
  52. std::swap(len_, other.len_);
  53. }
  54. short_string& operator=(const short_string& rhs)
  55. {
  56. short_string temp(rhs);
  57. swap(temp);
  58. return *this;
  59. }
  60. operator const char*() const
  61. {
  62. return buffer_;
  63. }
  64. private:
  65. char buffer_[e_limit];
  66. size_t len_;
  67. }; //short_string
  68. std::ostream& operator<<(std::ostream& out, const short_string& s)
  69. {
  70. out << static_cast<const char*>(s);
  71. return out;
  72. }
  73. void run()
  74. {
  75. using boost::variant;
  76. variant<short, short_string> v0;
  77. variant<char, const char*> v1;
  78. variant<short_string, char > v2;
  79. //
  80. // Default construction
  81. //
  82. verify(v0, spec<short>());
  83. verify(v1, spec<char>());
  84. verify(v2, spec<short_string>());
  85. //
  86. // Implicit conversion to bounded type
  87. //
  88. v1 = "I am v1";
  89. verify(v1, spec<const char*>(), "[V] I am v1");
  90. v2 = "I am v2";
  91. verify(v2, spec<short_string>(), "[V] I am v2");
  92. //
  93. // Variant-to-variant assignment
  94. //
  95. v0 = v1;
  96. verify(v0, spec<short_string>(), "[V] I am v1");
  97. v1 = v0;
  98. verify(v1, spec<const char*>(), "[V] I am v1");
  99. const int n0 = 88;
  100. v1 = n0;
  101. v0 = v1;
  102. //
  103. // Implicit conversion to bounded type
  104. //
  105. verify(v0, spec<short>(), "[V] 88");
  106. verify(v1, spec<char>(), "[V] X");
  107. }
  108. int main()
  109. {
  110. run();
  111. return boost::report_errors();
  112. }