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