sl@0: //----------------------------------------------------------------------------- sl@0: // boost-libs variant/test/test7.cpp header file sl@0: // See http://www.boost.org for updates, documentation, and revision history. sl@0: //----------------------------------------------------------------------------- sl@0: // sl@0: // Copyright (c) 2003 sl@0: // Eric Friedman, Itay Maman sl@0: // sl@0: // Distributed under the Boost Software License, Version 1.0. (See sl@0: // accompanying file LICENSE_1_0.txt or copy at sl@0: // http://www.boost.org/LICENSE_1_0.txt) sl@0: /* sl@0: * © Portions copyright (c) 2006-2007 Nokia Corporation. All rights reserved. sl@0: */ sl@0: sl@0: #include "boost/test/minimal.hpp" sl@0: #include "boost/variant.hpp" sl@0: sl@0: #include "jobs.h" sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #include "boost/detail/workaround.hpp" sl@0: #if BOOST_WORKAROUND(BOOST_MSVC, <= 1200) sl@0: # include "boost/mpl/bool.hpp" sl@0: # include "boost/type_traits/is_same.hpp" sl@0: #endif sl@0: sl@0: sl@0: using namespace boost; sl@0: using namespace std; sl@0: sl@0: sl@0: struct jas sl@0: { sl@0: jas(int n = 364); sl@0: jas(const jas& other); sl@0: sl@0: ~jas(); sl@0: jas& operator=(const jas& other); sl@0: sl@0: void swap(jas& other); sl@0: sl@0: int n_; sl@0: sl@0: int sn_; sl@0: static int s_inst_id_; sl@0: }; sl@0: sl@0: struct Tracker sl@0: { sl@0: typedef map table_type; sl@0: typedef table_type::iterator iterator_type; sl@0: sl@0: static table_type s_this_to_sn_; sl@0: sl@0: static void insert(const jas& j) sl@0: { sl@0: s_this_to_sn_[&j] = j.sn_; sl@0: cout << "jas( " << j.sn_ << ") Registered" << endl; sl@0: } sl@0: sl@0: static void remove(const jas& j) sl@0: { sl@0: iterator_type iter = s_this_to_sn_.find(&j); sl@0: BOOST_CHECK(iter != s_this_to_sn_.end()); sl@0: BOOST_CHECK( ((*iter).second) == j.sn_); sl@0: sl@0: int sn = (*iter).second; sl@0: if(sn != j.sn_) sl@0: { sl@0: cout << "Mismatch: this = " << (*iter).first << ", sn_ = " << sn sl@0: << ", other: this = " << &j << ", j.sn_ = " << j.sn_ << endl; sl@0: } sl@0: sl@0: BOOST_CHECK(sn == j.sn_); sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: s_this_to_sn_.erase(&j); sl@0: cout << "jas( " << j.sn_ << ") Removed" << endl; sl@0: } sl@0: sl@0: static void check() sl@0: { sl@0: BOOST_CHECK(s_this_to_sn_.empty()); sl@0: } sl@0: }; sl@0: sl@0: Tracker::table_type Tracker::s_this_to_sn_; sl@0: sl@0: sl@0: sl@0: jas::jas(int n) : n_(n) sl@0: { sl@0: sn_ = s_inst_id_; sl@0: s_inst_id_ += 1; sl@0: sl@0: Tracker::insert(*this); sl@0: } sl@0: sl@0: jas::jas(const jas& other) : n_(other.n_) sl@0: { sl@0: sn_ = s_inst_id_; sl@0: s_inst_id_ += 1; sl@0: sl@0: Tracker::insert(*this); sl@0: } sl@0: sl@0: jas::~jas() sl@0: { sl@0: Tracker::remove(*this); sl@0: } sl@0: sl@0: jas& jas::operator=(const jas& other) sl@0: { sl@0: jas temp(other); sl@0: swap(temp); sl@0: sl@0: return *this; sl@0: } sl@0: sl@0: void jas::swap(jas& other) sl@0: { sl@0: Tracker::remove(*this); sl@0: Tracker::remove(other); sl@0: sl@0: std::swap(n_, other.n_); sl@0: std::swap(sn_, other.sn_); sl@0: sl@0: Tracker::insert(*this); sl@0: Tracker::insert(other); sl@0: } sl@0: sl@0: int jas::s_inst_id_ = 0; sl@0: sl@0: sl@0: bool operator==(const jas& a, const jas& b) sl@0: { sl@0: return a.n_ == b.n_; sl@0: } sl@0: sl@0: ostream& operator<<(ostream& out, const jas& a) sl@0: { sl@0: cout << "jas::n_ = " << a.n_; sl@0: return out; sl@0: } sl@0: sl@0: sl@0: template sl@0: struct compare_helper : boost::static_visitor sl@0: { sl@0: compare_helper(ValueType& expected) : expected_(expected) { } sl@0: sl@0: #if !BOOST_WORKAROUND(BOOST_MSVC, <= 1200) sl@0: sl@0: bool operator()(const ValueType& value) sl@0: { sl@0: return value == expected_; sl@0: } sl@0: sl@0: template sl@0: bool operator()(const T& ) sl@0: { sl@0: return false; sl@0: } sl@0: sl@0: #else // MSVC6 sl@0: sl@0: private: sl@0: sl@0: bool compare_impl(const ValueType& value, boost::mpl::true_) sl@0: { sl@0: return value == expected_; sl@0: } sl@0: sl@0: template sl@0: bool compare_impl(const T&, boost::mpl::false_) sl@0: { sl@0: return false; sl@0: } sl@0: sl@0: public: sl@0: sl@0: template sl@0: bool operator()(const T& value) sl@0: { sl@0: typedef typename boost::is_same::type sl@0: T_is_ValueType; sl@0: sl@0: return compare_impl(value, T_is_ValueType()); sl@0: } sl@0: sl@0: #endif // MSVC6 workaround sl@0: sl@0: ValueType& expected_; sl@0: sl@0: }; sl@0: sl@0: template sl@0: void var_compare(const VariantType& v, ExpectedType expected) sl@0: { sl@0: compare_helper ch(expected); sl@0: sl@0: bool checks = boost::apply_visitor(ch, v); sl@0: BOOST_CHECK(checks); sl@0: } sl@0: sl@0: sl@0: void run() sl@0: { sl@0: variant v0; sl@0: sl@0: var_compare(v0, string("")); sl@0: sl@0: v0 = 8; sl@0: var_compare(v0, static_cast(8)); sl@0: sl@0: v0 = "penny lane"; sl@0: var_compare(v0, string("penny lane")); sl@0: sl@0: variant v1, v2 = jas(195); sl@0: var_compare(v1, jas(364)); sl@0: sl@0: v1 = jas(500); sl@0: v1.swap(v2); sl@0: sl@0: var_compare(v1, jas(195)); sl@0: var_compare(v2, jas(500)); sl@0: sl@0: sl@0: variant v3; sl@0: var_compare(v3, string("")); sl@0: } sl@0: sl@0: sl@0: int test_main(int , char* []) sl@0: { sl@0: std_log(LOG_FILENAME_LINE,"[Test Case for test7]"); sl@0: run(); sl@0: Tracker::check(); sl@0: sl@0: #ifdef __SYMBIAN32__ sl@0: testResultXml("test7"); sl@0: close_log_file(); sl@0: #endif sl@0: return 0; sl@0: } sl@0: