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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
|
//-----------------------------------------------------------------------------
// boost-libs variant/test/test7.cpp header file
// See http://www.boost.org for updates, documentation, and revision history.
//-----------------------------------------------------------------------------
//
// Copyright (c) 2003
// Eric Friedman, Itay Maman
//
// 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)
#include "boost/test/minimal.hpp"
#include "boost/variant.hpp"
#include "jobs.h"
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include "boost/detail/workaround.hpp"
#if BOOST_WORKAROUND(BOOST_MSVC, <= 1200)
# include "boost/mpl/bool.hpp"
# include "boost/type_traits/is_same.hpp"
#endif
using namespace boost;
using namespace std;
struct jas
{
jas(int n = 364);
jas(const jas& other);
~jas();
jas& operator=(const jas& other);
void swap(jas& other);
int n_;
int sn_;
static int s_inst_id_;
};
struct Tracker
{
typedef map<const jas*,int> table_type;
typedef table_type::iterator iterator_type;
static table_type s_this_to_sn_;
static void insert(const jas& j)
{
s_this_to_sn_[&j] = j.sn_;
cout << "jas( " << j.sn_ << ") Registered" << endl;
}
static void remove(const jas& j)
{
iterator_type iter = s_this_to_sn_.find(&j);
BOOST_CHECK(iter != s_this_to_sn_.end());
BOOST_CHECK( ((*iter).second) == j.sn_);
int sn = (*iter).second;
if(sn != j.sn_)
{
cout << "Mismatch: this = " << (*iter).first << ", sn_ = " << sn
<< ", other: this = " << &j << ", j.sn_ = " << j.sn_ << endl;
}
BOOST_CHECK(sn == j.sn_);
s_this_to_sn_.erase(&j);
cout << "jas( " << j.sn_ << ") Removed" << endl;
}
static void check()
{
BOOST_CHECK(s_this_to_sn_.empty());
}
};
Tracker::table_type Tracker::s_this_to_sn_;
jas::jas(int n) : n_(n)
{
sn_ = s_inst_id_;
s_inst_id_ += 1;
Tracker::insert(*this);
}
jas::jas(const jas& other) : n_(other.n_)
{
sn_ = s_inst_id_;
s_inst_id_ += 1;
Tracker::insert(*this);
}
jas::~jas()
{
Tracker::remove(*this);
}
jas& jas::operator=(const jas& other)
{
jas temp(other);
swap(temp);
return *this;
}
void jas::swap(jas& other)
{
Tracker::remove(*this);
Tracker::remove(other);
std::swap(n_, other.n_);
std::swap(sn_, other.sn_);
Tracker::insert(*this);
Tracker::insert(other);
}
int jas::s_inst_id_ = 0;
bool operator==(const jas& a, const jas& b)
{
return a.n_ == b.n_;
}
ostream& operator<<(ostream& out, const jas& a)
{
cout << "jas::n_ = " << a.n_;
return out;
}
template<typename ValueType>
struct compare_helper : boost::static_visitor<bool>
{
compare_helper(ValueType& expected) : expected_(expected) { }
#if !BOOST_WORKAROUND(BOOST_MSVC, <= 1200)
bool operator()(const ValueType& value)
{
return value == expected_;
}
template <typename T>
bool operator()(const T& )
{
return false;
}
#else // MSVC6
private:
bool compare_impl(const ValueType& value, boost::mpl::true_)
{
return value == expected_;
}
template <typename T>
bool compare_impl(const T&, boost::mpl::false_)
{
return false;
}
public:
template <typename T>
bool operator()(const T& value)
{
typedef typename boost::is_same<T, ValueType>::type
T_is_ValueType;
return compare_impl(value, T_is_ValueType());
}
#endif // MSVC6 workaround
ValueType& expected_;
};
template<typename VariantType, typename ExpectedType>
void var_compare(const VariantType& v, ExpectedType expected)
{
compare_helper<ExpectedType> ch(expected);
bool checks = boost::apply_visitor(ch, v);
BOOST_CHECK(checks);
}
void run()
{
variant<string, short> v0;
var_compare(v0, string(""));
v0 = 8;
var_compare(v0, static_cast<short>(8));
v0 = "penny lane";
var_compare(v0, string("penny lane"));
variant<jas, string, int> v1, v2 = jas(195);
var_compare(v1, jas(364));
v1 = jas(500);
v1.swap(v2);
var_compare(v1, jas(195));
var_compare(v2, jas(500));
variant<string, int> v3;
var_compare(v3, string(""));
}
int test_main(int , char* [])
{
run();
Tracker::check();
return 0;
}
|