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