Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/variant/test/test7.cpp @ 29

Last change on this file since 29 was 29, checked in by landauf, 17 years ago

updated boost from 1_33_1 to 1_34_1

File size: 4.4 KB
Line 
1//-----------------------------------------------------------------------------
2// boost-libs variant/test/test7.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
13#include "boost/test/minimal.hpp"
14#include "boost/variant.hpp"
15
16#include "jobs.h"
17
18#include <iostream>
19#include <algorithm>
20#include <string>
21#include <map>
22
23#include "boost/detail/workaround.hpp"
24#if BOOST_WORKAROUND(BOOST_MSVC, <= 1200)
25#   include "boost/mpl/bool.hpp"
26#   include "boost/type_traits/is_same.hpp"
27#endif
28
29
30using namespace boost;
31using namespace std;
32
33
34struct jas
35{
36   jas(int n = 364);
37   jas(const jas& other);
38
39   ~jas();
40   jas& operator=(const jas& other);
41
42   void swap(jas& other);
43
44   int n_;
45
46   int sn_;
47   static int s_inst_id_;
48}; 
49
50struct Tracker
51{
52   typedef map<const jas*,int> table_type;
53   typedef table_type::iterator iterator_type;
54
55   static table_type s_this_to_sn_;
56
57   static void insert(const jas& j)
58   {
59      s_this_to_sn_[&j] = j.sn_;     
60      cout << "jas( " << j.sn_ << ") Registered" << endl;
61   }
62   
63   static void remove(const jas& j)
64   {
65      iterator_type iter = s_this_to_sn_.find(&j);
66      BOOST_CHECK(iter != s_this_to_sn_.end());
67      BOOST_CHECK( ((*iter).second) == j.sn_);
68
69      int sn = (*iter).second;
70      if(sn != j.sn_)
71      {
72         cout << "Mismatch: this = " << (*iter).first << ", sn_ = " << sn
73            << ", other: this = " << &j << ", j.sn_ = " << j.sn_ << endl;
74      }
75
76      BOOST_CHECK(sn == j.sn_);
77
78   
79
80     
81
82      s_this_to_sn_.erase(&j);
83      cout << "jas( " << j.sn_ << ") Removed" << endl;
84   }
85
86   static void check()
87   {
88      BOOST_CHECK(s_this_to_sn_.empty());     
89   }
90};
91
92Tracker::table_type Tracker::s_this_to_sn_;
93
94
95
96jas::jas(int n) : n_(n) 
97{ 
98   sn_ = s_inst_id_;
99   s_inst_id_ += 1;
100     
101   Tracker::insert(*this);
102}
103
104jas::jas(const jas& other) : n_(other.n_)
105{
106   sn_ = s_inst_id_;
107   s_inst_id_ += 1;     
108
109   Tracker::insert(*this);
110}
111
112jas::~jas()
113{
114   Tracker::remove(*this);
115}
116
117jas& jas::operator=(const jas& other)
118{
119   jas temp(other);
120   swap(temp);
121
122   return *this;
123}
124
125void jas::swap(jas& other)
126{   
127   Tracker::remove(*this);
128   Tracker::remove(other);
129
130   std::swap(n_, other.n_);
131   std::swap(sn_, other.sn_);
132
133   Tracker::insert(*this);
134   Tracker::insert(other);
135}
136
137int jas::s_inst_id_ = 0;
138
139
140bool operator==(const jas& a, const jas& b)
141{
142   return a.n_ == b.n_;
143}
144
145ostream& operator<<(ostream& out, const jas& a)
146{
147   cout << "jas::n_ = " << a.n_;
148   return out;
149}
150
151
152template<typename ValueType>
153struct compare_helper : boost::static_visitor<bool>
154{
155   compare_helper(ValueType& expected) : expected_(expected) { }
156
157#if !BOOST_WORKAROUND(BOOST_MSVC, <= 1200)
158
159   bool operator()(const ValueType& value)
160   {
161      return value == expected_;
162   }
163
164   template <typename T>
165   bool operator()(const T& )
166   {
167      return false;         
168   }
169
170#else // MSVC6
171
172private:
173
174   bool compare_impl(const ValueType& value, boost::mpl::true_)
175   {
176      return value == expected_;
177   }
178
179   template <typename T>
180   bool compare_impl(const T&, boost::mpl::false_)
181   {
182      return false;
183   }
184
185public:
186
187   template <typename T>
188   bool operator()(const T& value)
189   {
190      typedef typename boost::is_same<T, ValueType>::type
191          T_is_ValueType;
192
193      return compare_impl(value, T_is_ValueType());
194   }
195
196#endif // MSVC6 workaround
197
198   ValueType& expected_;
199
200};
201
202template<typename VariantType, typename ExpectedType>
203void var_compare(const VariantType& v, ExpectedType expected)
204{
205   compare_helper<ExpectedType> ch(expected);
206
207   bool checks = boost::apply_visitor(ch, v);
208   BOOST_CHECK(checks);
209}
210
211
212void run()
213{   
214   variant<string, short> v0;
215
216   var_compare(v0, string(""));
217
218   v0 = 8;
219   var_compare(v0, static_cast<short>(8));
220
221   v0 = "penny lane";
222   var_compare(v0, string("penny lane"));
223
224   variant<jas, string, int> v1, v2 = jas(195);
225   var_compare(v1, jas(364));
226
227   v1 = jas(500);
228   v1.swap(v2);
229
230   var_compare(v1, jas(195));
231   var_compare(v2, jas(500));
232
233
234   variant<string, int> v3;
235   var_compare(v3, string(""));
236}
237
238
239int test_main(int , char* [])
240{
241   run();
242   Tracker::check();
243
244   return 0;
245}
246
Note: See TracBrowser for help on using the repository browser.