diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 18:24:20 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 18:24:20 +0000 |
commit | 483eb2f56657e8e7f419ab1a4fab8dce9ade8609 (patch) | |
tree | e5d88d25d870d5dedacb6bbdbe2a966086a0a5cf /src/boost/libs/variant/test/test2.cpp | |
parent | Initial commit. (diff) | |
download | ceph-upstream.tar.xz ceph-upstream.zip |
Adding upstream version 14.2.21.upstream/14.2.21upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/boost/libs/variant/test/test2.cpp')
-rw-r--r-- | src/boost/libs/variant/test/test2.cpp | 149 |
1 files changed, 149 insertions, 0 deletions
diff --git a/src/boost/libs/variant/test/test2.cpp b/src/boost/libs/variant/test/test2.cpp new file mode 100644 index 00000000..12e31836 --- /dev/null +++ b/src/boost/libs/variant/test/test2.cpp @@ -0,0 +1,149 @@ +//----------------------------------------------------------------------------- +// 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" + +#ifdef BOOST_MSVC +#pragma warning(disable:4244) // conversion from 'const int' to 'const short' +#endif + +#include "boost/core/lightweight_test.hpp" +#include "boost/variant.hpp" + +#include "jobs.h" + +#include <cassert> +#include <iostream> +#include <algorithm> +#include <cstring> + +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) + { +#ifndef BOOST_NO_STDC_NAMESPACE + using std::strlen; +#endif // BOOST_NO_STDC_NAMESPACE + + size_t limit = this->e_limit; // avoid warnings on some compilers + size_t src_len = strlen(src); + + len_ = (std::min)(src_len, 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 main() +{ + run(); + return boost::report_errors(); +} + |