Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/variant/test/test2.cpp @ 29

Last change on this file since 29 was 29, checked in by landauf, 16 years ago

updated boost from 1_33_1 to 1_34_1

File size: 2.9 KB
Line 
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#include "boost/config.hpp"
14#include "boost/test/minimal.hpp"
15#include "boost/variant.hpp"
16
17#include "jobs.h"
18
19#include <cassert>
20#include <iostream>
21#include <algorithm>
22#include <cstring>
23
24using boost::apply_visitor;
25
26struct short_string
27{
28   BOOST_STATIC_CONSTANT(size_t, e_limit = 101);
29
30   short_string() : len_(0) 
31   { 
32      buffer_[0] = '\0';
33   }
34
35   short_string(const char* src) 
36   {
37#ifndef BOOST_NO_STDC_NAMESPACE
38      using std::strlen;
39#endif // BOOST_NO_STDC_NAMESPACE
40
41      size_t e_limit = this->e_limit; // avoid warnings on some compilers
42      size_t src_len = strlen(src);
43     
44      len_ = (std::min)(src_len, e_limit-1);
45      std::copy(src, src + len_, buffer_);
46      buffer_[len_] = '\0';
47   }
48
49   short_string(const short_string& other) : len_(other.len_)
50   {
51      std::copy(other.buffer_, other.buffer_ + e_limit, buffer_);
52   }
53
54   void swap(short_string& other)
55   {
56      char temp[e_limit];
57
58      std::copy(buffer_, buffer_ + e_limit, temp);
59      std::copy(other.buffer_, other.buffer_ + e_limit, buffer_);
60      std::copy(temp, temp + e_limit, other.buffer_);
61
62      std::swap(len_, other.len_);
63   }
64
65   short_string& operator=(const short_string& rhs)
66   {
67      short_string temp(rhs);
68      swap(temp);
69
70      return *this;
71   }
72
73   operator const char*() const
74   {
75      return buffer_;
76   }
77
78
79private:
80   char buffer_[e_limit];
81   size_t len_;
82}; //short_string
83
84
85std::ostream& operator<<(std::ostream& out, const short_string& s)
86{
87   out << static_cast<const char*>(s);
88   return out;
89}
90
91
92
93void run()
94{   
95   using boost::variant;
96
97   variant<short, short_string> v0;
98   variant<char, const char*> v1;
99   variant<short_string, char > v2;
100
101   //
102   // Default construction
103   //
104   verify(v0, spec<short>());
105   verify(v1, spec<char>());
106   verify(v2, spec<short_string>());
107
108   //
109   // Implicit conversion to bounded type
110   //
111   v1 = "I am v1";
112   verify(v1, spec<const char*>(), "[V] I am v1");
113
114   v2 = "I am v2";
115   verify(v2, spec<short_string>(), "[V] I am v2");
116
117   //
118   // Variant-to-variant assignment
119   //
120
121   v0 = v1;
122   verify(v0, spec<short_string>(), "[V] I am v1");
123
124   v1 = v0;
125   verify(v1, spec<const char*>(), "[V] I am v1");
126
127   const int n0 = 88;
128   v1 = n0;
129   v0 = v1;
130
131   //
132   // Implicit conversion to bounded type
133   //
134   verify(v0, spec<short>(), "[V] 88");
135   verify(v1, spec<char>(), "[V] X");
136}
137
138
139int test_main(int , char* [])
140{
141   run();
142   return 0;
143}
144
Note: See TracBrowser for help on using the repository browser.