os/ossrv/stdcpp/tsrc/Boost_test/variant/src/test2.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
//-----------------------------------------------------------------------------
sl@0
     2
// boost-libs variant/test/test2.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/config.hpp"
sl@0
    17
#include "boost/test/minimal.hpp"
sl@0
    18
#include "boost/variant.hpp"
sl@0
    19
sl@0
    20
#include "jobs.h"
sl@0
    21
sl@0
    22
#include <cassert>
sl@0
    23
#include <iostream>
sl@0
    24
#include <algorithm>
sl@0
    25
#include <cstring>
sl@0
    26
#ifdef __SYMBIAN32__
sl@0
    27
#include "std_log_result.h"
sl@0
    28
#define LOG_FILENAME_LINE __FILE__, __LINE__
sl@0
    29
#endif
sl@0
    30
using boost::apply_visitor;
sl@0
    31
sl@0
    32
struct short_string
sl@0
    33
{
sl@0
    34
   BOOST_STATIC_CONSTANT(size_t, e_limit = 101);
sl@0
    35
sl@0
    36
   short_string() : len_(0) 
sl@0
    37
   { 
sl@0
    38
      buffer_[0] = '\0';
sl@0
    39
   }
sl@0
    40
sl@0
    41
   short_string(const char* src) 
sl@0
    42
   {
sl@0
    43
#ifndef BOOST_NO_STDC_NAMESPACE
sl@0
    44
      using std::strlen;
sl@0
    45
#endif // BOOST_NO_STDC_NAMESPACE
sl@0
    46
sl@0
    47
      size_t e_limit = this->e_limit; // avoid warnings on some compilers
sl@0
    48
      size_t src_len = strlen(src);
sl@0
    49
      
sl@0
    50
      len_ = (std::min)(src_len, e_limit-1);
sl@0
    51
      std::copy(src, src + len_, buffer_);
sl@0
    52
      buffer_[len_] = '\0';
sl@0
    53
   }
sl@0
    54
sl@0
    55
   short_string(const short_string& other) : len_(other.len_)
sl@0
    56
   {
sl@0
    57
      std::copy(other.buffer_, other.buffer_ + e_limit, buffer_);
sl@0
    58
   }
sl@0
    59
sl@0
    60
   void swap(short_string& other)
sl@0
    61
   {
sl@0
    62
      char temp[e_limit];
sl@0
    63
sl@0
    64
      std::copy(buffer_, buffer_ + e_limit, temp);
sl@0
    65
      std::copy(other.buffer_, other.buffer_ + e_limit, buffer_);
sl@0
    66
      std::copy(temp, temp + e_limit, other.buffer_);
sl@0
    67
sl@0
    68
      std::swap(len_, other.len_);
sl@0
    69
   }
sl@0
    70
sl@0
    71
   short_string& operator=(const short_string& rhs)
sl@0
    72
   {
sl@0
    73
      short_string temp(rhs);
sl@0
    74
      swap(temp);
sl@0
    75
sl@0
    76
      return *this;
sl@0
    77
   }
sl@0
    78
sl@0
    79
   operator const char*() const
sl@0
    80
   {
sl@0
    81
      return buffer_;
sl@0
    82
   }
sl@0
    83
sl@0
    84
sl@0
    85
private:
sl@0
    86
   char buffer_[e_limit];
sl@0
    87
   size_t len_;
sl@0
    88
}; //short_string
sl@0
    89
sl@0
    90
sl@0
    91
std::ostream& operator<<(std::ostream& out, const short_string& s)
sl@0
    92
{
sl@0
    93
   out << static_cast<const char*>(s);
sl@0
    94
   return out;
sl@0
    95
}
sl@0
    96
sl@0
    97
sl@0
    98
sl@0
    99
void run()
sl@0
   100
{   
sl@0
   101
   using boost::variant;
sl@0
   102
sl@0
   103
   variant<short, short_string> v0;
sl@0
   104
   variant<char, const char*> v1;
sl@0
   105
   variant<short_string, char > v2;
sl@0
   106
sl@0
   107
   //
sl@0
   108
   // Default construction
sl@0
   109
   //
sl@0
   110
   verify(v0, spec<short>());
sl@0
   111
   verify(v1, spec<char>());
sl@0
   112
   verify(v2, spec<short_string>());
sl@0
   113
sl@0
   114
   //
sl@0
   115
   // Implicit conversion to bounded type
sl@0
   116
   //
sl@0
   117
   v1 = "I am v1";
sl@0
   118
   verify(v1, spec<const char*>(), "[V] I am v1");
sl@0
   119
sl@0
   120
   v2 = "I am v2";
sl@0
   121
   verify(v2, spec<short_string>(), "[V] I am v2");
sl@0
   122
sl@0
   123
   //
sl@0
   124
   // Variant-to-variant assignment
sl@0
   125
   //
sl@0
   126
sl@0
   127
   v0 = v1;
sl@0
   128
   verify(v0, spec<short_string>(), "[V] I am v1");
sl@0
   129
sl@0
   130
   v1 = v0;
sl@0
   131
   verify(v1, spec<const char*>(), "[V] I am v1");
sl@0
   132
sl@0
   133
   const int n0 = 88;
sl@0
   134
   v1 = n0;
sl@0
   135
   v0 = v1;
sl@0
   136
sl@0
   137
   //
sl@0
   138
   // Implicit conversion to bounded type
sl@0
   139
   //
sl@0
   140
   verify(v0, spec<short>(), "[V] 88");
sl@0
   141
   verify(v1, spec<char>(), "[V] X");
sl@0
   142
}
sl@0
   143
sl@0
   144
sl@0
   145
int test_main(int , char* [])
sl@0
   146
{
sl@0
   147
std_log(LOG_FILENAME_LINE,"[Test Case for test2]");
sl@0
   148
   run();
sl@0
   149
 #ifdef __SYMBIAN32__
sl@0
   150
   	testResultXml("test2");
sl@0
   151
	close_log_file();
sl@0
   152
#endif
sl@0
   153
   return 0;
sl@0
   154
}
sl@0
   155