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