2014-06-03 17:36:18 +02:00
|
|
|
// Copyright (C) 2014 Andrzej Krzemienski.
|
|
|
|
//
|
|
|
|
// Use, modification, and distribution is subject to 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)
|
|
|
|
//
|
|
|
|
// See http://www.boost.org/lib/optional for documentation.
|
|
|
|
//
|
|
|
|
// You are welcome to contact the author at:
|
|
|
|
// akrzemi1@gmail.com
|
|
|
|
|
|
|
|
#include "boost/optional/optional.hpp"
|
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#pragma hdrstop
|
|
|
|
#endif
|
|
|
|
|
2015-01-13 23:17:23 +01:00
|
|
|
#include "boost/core/lightweight_test.hpp"
|
2014-06-03 17:36:18 +02:00
|
|
|
#include "boost/none.hpp"
|
|
|
|
|
|
|
|
//#ifndef BOOST_OPTIONAL_NO_CONVERTING_ASSIGNMENT
|
|
|
|
//#ifndef BOOST_OPTIONAL_NO_CONVERTING_COPY_CTOR
|
|
|
|
|
2015-01-13 23:17:23 +01:00
|
|
|
using boost::optional;
|
|
|
|
using boost::none;
|
|
|
|
|
2014-06-03 17:36:18 +02:00
|
|
|
#if (!defined BOOST_NO_CXX11_RVALUE_REFERENCES) && (!defined BOOST_NO_CXX11_VARIADIC_TEMPLATES)
|
|
|
|
|
|
|
|
class Guard
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
int which_ctor;
|
|
|
|
Guard () : which_ctor(0) { }
|
|
|
|
Guard (int&, double&&) : which_ctor(1) { }
|
|
|
|
Guard (int&&, double&) : which_ctor(2) { }
|
|
|
|
Guard (int&&, double&&) : which_ctor(3) { }
|
|
|
|
Guard (int&, double&) : which_ctor(4) { }
|
|
|
|
Guard (std::string const&) : which_ctor(5) { }
|
|
|
|
Guard (std::string &) : which_ctor(6) { }
|
|
|
|
Guard (std::string &&) : which_ctor(7) { }
|
|
|
|
private:
|
|
|
|
Guard(Guard&&);
|
|
|
|
Guard(Guard const&);
|
|
|
|
void operator=(Guard &&);
|
|
|
|
void operator=(Guard const&);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void test_emplace()
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
double d = 0.0;
|
|
|
|
const std::string cs;
|
|
|
|
std::string ms;
|
|
|
|
optional<Guard> o;
|
|
|
|
|
|
|
|
o.emplace();
|
2015-01-13 23:17:23 +01:00
|
|
|
BOOST_TEST(o);
|
|
|
|
BOOST_TEST(0 == o->which_ctor);
|
2014-06-03 17:36:18 +02:00
|
|
|
|
|
|
|
o.emplace(i, 2.0);
|
2015-01-13 23:17:23 +01:00
|
|
|
BOOST_TEST(o);
|
|
|
|
BOOST_TEST(1 == o->which_ctor);
|
2014-06-03 17:36:18 +02:00
|
|
|
|
|
|
|
o.emplace(1, d);
|
2015-01-13 23:17:23 +01:00
|
|
|
BOOST_TEST(o);
|
|
|
|
BOOST_TEST(2 == o->which_ctor);
|
2014-06-03 17:36:18 +02:00
|
|
|
|
|
|
|
o.emplace(1, 2.0);
|
2015-01-13 23:17:23 +01:00
|
|
|
BOOST_TEST(o);
|
|
|
|
BOOST_TEST(3 == o->which_ctor);
|
2014-06-03 17:36:18 +02:00
|
|
|
|
|
|
|
o.emplace(i, d);
|
2015-01-13 23:17:23 +01:00
|
|
|
BOOST_TEST(o);
|
|
|
|
BOOST_TEST(4 == o->which_ctor);
|
2014-06-03 17:36:18 +02:00
|
|
|
|
|
|
|
o.emplace(cs);
|
2015-01-13 23:17:23 +01:00
|
|
|
BOOST_TEST(o);
|
|
|
|
BOOST_TEST(5 == o->which_ctor);
|
2014-06-03 17:36:18 +02:00
|
|
|
|
|
|
|
o.emplace(ms);
|
2015-01-13 23:17:23 +01:00
|
|
|
BOOST_TEST(o);
|
|
|
|
BOOST_TEST(6 == o->which_ctor);
|
2014-06-03 17:36:18 +02:00
|
|
|
|
|
|
|
o.emplace(std::string());
|
2015-01-13 23:17:23 +01:00
|
|
|
BOOST_TEST(o);
|
|
|
|
BOOST_TEST(7 == o->which_ctor);
|
2014-06-03 17:36:18 +02:00
|
|
|
}
|
|
|
|
|
2014-06-06 23:40:45 +02:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (!defined BOOST_NO_CXX11_RVALUE_REFERENCES)
|
|
|
|
|
|
|
|
|
|
|
|
struct ThrowOnMove
|
|
|
|
{
|
|
|
|
ThrowOnMove(ThrowOnMove&&) { throw int(); }
|
|
|
|
ThrowOnMove(ThrowOnMove const&) { throw int(); }
|
|
|
|
ThrowOnMove(int){}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void test_no_moves_on_emplacement()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
optional<ThrowOnMove> o;
|
|
|
|
o.emplace(1);
|
2015-01-13 23:17:23 +01:00
|
|
|
BOOST_TEST(o);
|
2014-06-06 23:40:45 +02:00
|
|
|
}
|
|
|
|
catch (...) {
|
2015-01-13 23:17:23 +01:00
|
|
|
BOOST_TEST(false);
|
2014-06-06 23:40:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct Thrower
|
|
|
|
{
|
|
|
|
Thrower(bool throw_) { if (throw_) throw int(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
Thrower(Thrower const&);
|
|
|
|
#if (!defined BOOST_NO_CXX11_RVALUE_REFERENCES)
|
|
|
|
Thrower(Thrower&&);
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2014-06-03 17:36:18 +02:00
|
|
|
void test_clear_on_throw()
|
|
|
|
{
|
|
|
|
optional<Thrower> ot;
|
|
|
|
try {
|
|
|
|
ot.emplace(false);
|
2015-01-13 23:17:23 +01:00
|
|
|
BOOST_TEST(ot);
|
2014-06-03 17:36:18 +02:00
|
|
|
} catch(...) {
|
2015-01-13 23:17:23 +01:00
|
|
|
BOOST_TEST(false);
|
2014-06-03 17:36:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
ot.emplace(true);
|
2015-01-13 23:17:23 +01:00
|
|
|
BOOST_TEST(false);
|
2014-06-03 17:36:18 +02:00
|
|
|
} catch(...) {
|
2015-01-13 23:17:23 +01:00
|
|
|
BOOST_TEST(!ot);
|
2014-06-03 17:36:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-06 23:40:45 +02:00
|
|
|
void test_no_assignment_on_emplacement()
|
2014-06-03 17:36:18 +02:00
|
|
|
{
|
2015-05-15 16:27:40 +02:00
|
|
|
optional<const std::string> os, ot;
|
2015-01-13 23:17:23 +01:00
|
|
|
BOOST_TEST(!os);
|
2014-06-06 23:40:45 +02:00
|
|
|
os.emplace("wow");
|
2015-01-13 23:17:23 +01:00
|
|
|
BOOST_TEST(os);
|
|
|
|
BOOST_TEST_EQ(*os, "wow");
|
2015-05-15 16:27:40 +02:00
|
|
|
|
|
|
|
BOOST_TEST(!ot);
|
|
|
|
ot.emplace();
|
|
|
|
BOOST_TEST(ot);
|
|
|
|
BOOST_TEST_EQ(*ot, "");
|
2014-06-03 17:36:18 +02:00
|
|
|
}
|
|
|
|
|
2015-01-13 23:17:23 +01:00
|
|
|
int main()
|
2014-06-03 17:36:18 +02:00
|
|
|
{
|
|
|
|
#if (!defined BOOST_NO_CXX11_RVALUE_REFERENCES) && (!defined BOOST_NO_CXX11_VARIADIC_TEMPLATES)
|
2015-01-13 23:17:23 +01:00
|
|
|
test_emplace();
|
2014-06-06 23:40:45 +02:00
|
|
|
#endif
|
|
|
|
#if (!defined BOOST_NO_CXX11_RVALUE_REFERENCES)
|
2015-01-13 23:17:23 +01:00
|
|
|
test_no_moves_on_emplacement();
|
2014-06-03 17:36:18 +02:00
|
|
|
#endif
|
2015-01-13 23:17:23 +01:00
|
|
|
test_clear_on_throw();
|
|
|
|
test_no_assignment_on_emplacement();
|
|
|
|
|
|
|
|
return boost::report_errors();
|
2014-06-03 17:36:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|