File: test2.cpp

package info (click to toggle)
boost 1.32.0-6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 93,952 kB
  • ctags: 128,458
  • sloc: cpp: 492,477; xml: 52,125; python: 13,519; ansic: 13,013; sh: 1,773; yacc: 853; makefile: 526; perl: 418; lex: 110; csh: 6
file content (140 lines) | stat: -rw-r--r-- 2,835 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//-----------------------------------------------------------------------------
// boost-libs variant/test/test2.cpp header file
// See http://www.boost.org for updates, documentation, and revision history.
//-----------------------------------------------------------------------------
//
// Copyright (c) 2003
// Eric Friedman, Itay Maman
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#include "boost/config.hpp"
#include "boost/test/minimal.hpp"
#include "boost/variant.hpp"

#include "jobs.h"

#include <cassert>
#include <iostream>
#include <algorithm>


using boost::apply_visitor;

struct short_string
{
   BOOST_STATIC_CONSTANT(size_t, e_limit = 101);

   short_string() : len_(0) 
   { 
      buffer_[0] = '\0';
   }

   short_string(const char* src) 
   {
      size_t e_limit = this->e_limit; // avoid warnings on some compilers
      size_t src_len = strlen(src);
      
      len_ = (std::min)(src_len, e_limit-1);
      std::copy(src, src + len_, buffer_);
      buffer_[len_] = '\0';
   }

   short_string(const short_string& other) : len_(other.len_)
   {
      std::copy(other.buffer_, other.buffer_ + e_limit, buffer_);
   }

   void swap(short_string& other)
   {
      char temp[e_limit];

      std::copy(buffer_, buffer_ + e_limit, temp);
      std::copy(other.buffer_, other.buffer_ + e_limit, buffer_);
      std::copy(temp, temp + e_limit, other.buffer_);

      std::swap(len_, other.len_);
   }

   short_string& operator=(const short_string& rhs)
   {
      short_string temp(rhs);
      swap(temp);

      return *this;
   }

   operator const char*() const
   {
      return buffer_;
   }


private:
   char buffer_[e_limit];
   size_t len_;
}; //short_string


std::ostream& operator<<(std::ostream& out, const short_string& s)
{
   out << static_cast<const char*>(s);
   return out;
}



void run()
{   
   using boost::variant;

   variant<short, short_string> v0;
   variant<char, const char*> v1;
   variant<short_string, char > v2;

   //
   // Default construction
   //
   verify(v0, spec<short>());
   verify(v1, spec<char>());
   verify(v2, spec<short_string>());

   //
   // Implicit conversion to bounded type
   //
   v1 = "I am v1";
   verify(v1, spec<const char*>(), "[V] I am v1");

   v2 = "I am v2";
   verify(v2, spec<short_string>(), "[V] I am v2");

   //
   // Variant-to-variant assignment
   //

   v0 = v1;
   verify(v0, spec<short_string>(), "[V] I am v1");

   v1 = v0;
   verify(v1, spec<const char*>(), "[V] I am v1");

   const int n0 = 88;
   v1 = n0;
   v0 = v1;

   //
   // Implicit conversion to bounded type
   //
   verify(v0, spec<short>(), "[V] 88");
   verify(v1, spec<char>(), "[V] X");
}


int test_main(int , char* [])
{
   run();
   return 0;
}