2007-10-03 19:27:18 +02:00
|
|
|
// -*- C++ -*-
|
|
|
|
// Testing utilities for the rvalue reference.
|
|
|
|
//
|
2016-01-04 15:30:50 +01:00
|
|
|
// Copyright (C) 2005-2016 Free Software Foundation, Inc.
|
2007-10-03 19:27:18 +02:00
|
|
|
//
|
|
|
|
// This file is part of the GNU ISO C++ Library. This library is free
|
|
|
|
// software; you can redistribute it and/or modify it under the
|
|
|
|
// terms of the GNU General Public License as published by the
|
2009-04-09 17:00:19 +02:00
|
|
|
// Free Software Foundation; either version 3, or (at your option)
|
2007-10-03 19:27:18 +02:00
|
|
|
// any later version.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
2009-04-09 17:00:19 +02:00
|
|
|
// with this library; see the file COPYING3. If not see
|
|
|
|
// <http://www.gnu.org/licenses/>.
|
2007-10-03 19:27:18 +02:00
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef _GLIBCXX_TESTSUITE_RVALREF_H
|
|
|
|
#define _GLIBCXX_TESTSUITE_RVALREF_H 1
|
|
|
|
|
|
|
|
#include <testsuite_hooks.h>
|
2011-03-09 19:30:11 +01:00
|
|
|
#include <bits/functional_hash.h>
|
2007-10-03 19:27:18 +02:00
|
|
|
|
|
|
|
namespace __gnu_test
|
|
|
|
{
|
2010-10-28 18:01:05 +02:00
|
|
|
// This class is designed to test libstdc++'s template-based rvalue
|
|
|
|
// reference support. It should fail at compile-time if there is an
|
|
|
|
// attempt to copy it.
|
|
|
|
struct rvalstruct
|
2007-10-03 19:27:18 +02:00
|
|
|
{
|
|
|
|
int val;
|
|
|
|
bool valid;
|
|
|
|
|
2010-10-28 18:01:05 +02:00
|
|
|
rvalstruct() : val(0), valid(true)
|
2007-10-03 19:27:18 +02:00
|
|
|
{ }
|
|
|
|
|
|
|
|
rvalstruct(int inval) : val(inval), valid(true)
|
|
|
|
{ }
|
2010-10-28 18:01:05 +02:00
|
|
|
|
2007-10-03 19:27:18 +02:00
|
|
|
rvalstruct&
|
|
|
|
operator=(int newval)
|
2016-04-29 18:11:43 +02:00
|
|
|
{
|
2010-10-28 18:01:05 +02:00
|
|
|
val = newval;
|
2007-10-03 19:27:18 +02:00
|
|
|
valid = true;
|
2007-12-10 14:53:10 +01:00
|
|
|
return *this;
|
2007-10-03 19:27:18 +02:00
|
|
|
}
|
|
|
|
|
2010-10-28 18:01:05 +02:00
|
|
|
rvalstruct(const rvalstruct&) = delete;
|
|
|
|
|
2007-10-03 19:27:18 +02:00
|
|
|
rvalstruct(rvalstruct&& in)
|
2011-03-09 19:30:11 +01:00
|
|
|
{
|
|
|
|
VERIFY( in.valid == true );
|
2007-10-03 19:27:18 +02:00
|
|
|
val = in.val;
|
|
|
|
in.valid = false;
|
|
|
|
valid = true;
|
|
|
|
}
|
|
|
|
|
2010-10-28 18:01:05 +02:00
|
|
|
rvalstruct&
|
|
|
|
operator=(const rvalstruct&) = delete;
|
|
|
|
|
2007-10-03 19:27:18 +02:00
|
|
|
rvalstruct&
|
|
|
|
operator=(rvalstruct&& in)
|
2011-03-09 19:30:11 +01:00
|
|
|
{
|
2011-07-11 20:38:54 +02:00
|
|
|
VERIFY( this != &in );
|
2011-03-09 19:30:11 +01:00
|
|
|
VERIFY( in.valid == true );
|
2007-10-03 19:27:18 +02:00
|
|
|
val = in.val;
|
|
|
|
in.valid = false;
|
|
|
|
valid = true;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-04-29 18:11:43 +02:00
|
|
|
inline bool
|
2007-10-03 19:27:18 +02:00
|
|
|
operator==(const rvalstruct& lhs, const rvalstruct& rhs)
|
|
|
|
{ return lhs.val == rhs.val; }
|
|
|
|
|
2010-10-28 18:01:05 +02:00
|
|
|
inline bool
|
2007-10-03 19:27:18 +02:00
|
|
|
operator<(const rvalstruct& lhs, const rvalstruct& rhs)
|
|
|
|
{ return lhs.val < rhs.val; }
|
|
|
|
|
|
|
|
void
|
|
|
|
swap(rvalstruct& lhs, rvalstruct& rhs)
|
2011-03-09 19:30:11 +01:00
|
|
|
{
|
|
|
|
VERIFY( lhs.valid && rhs.valid );
|
2007-10-03 19:27:18 +02:00
|
|
|
int temp = lhs.val;
|
|
|
|
lhs.val = rhs.val;
|
|
|
|
rhs.val = temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is a moveable class which copies how many times it is copied.
|
|
|
|
// This is mainly of use in the containers, where the an element inserted
|
|
|
|
// into a container has to be copied once to get there, but we want to check
|
|
|
|
// nothing else is copied.
|
|
|
|
struct copycounter
|
|
|
|
{
|
|
|
|
static int copycount;
|
|
|
|
int val;
|
|
|
|
bool valid;
|
2016-04-29 18:11:43 +02:00
|
|
|
|
2007-10-03 19:27:18 +02:00
|
|
|
copycounter() : val(0), valid(true)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
copycounter(int inval) : val(inval), valid(true)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
copycounter(const copycounter& in) : val(in.val), valid(true)
|
2011-03-09 19:30:11 +01:00
|
|
|
{
|
|
|
|
VERIFY( in.valid == true );
|
2007-10-03 19:27:18 +02:00
|
|
|
++copycount;
|
|
|
|
}
|
|
|
|
|
2011-06-07 18:11:36 +02:00
|
|
|
copycounter(copycounter&& in) noexcept
|
2011-03-09 19:30:11 +01:00
|
|
|
{
|
|
|
|
VERIFY( in.valid == true );
|
2007-10-03 19:27:18 +02:00
|
|
|
val = in.val;
|
|
|
|
in.valid = false;
|
|
|
|
valid = true;
|
|
|
|
}
|
2016-04-29 18:11:43 +02:00
|
|
|
|
2007-10-03 19:27:18 +02:00
|
|
|
copycounter&
|
|
|
|
operator=(int newval)
|
2016-04-29 18:11:43 +02:00
|
|
|
{
|
2007-10-03 19:27:18 +02:00
|
|
|
val = newval;
|
|
|
|
valid = true;
|
2007-12-10 14:53:10 +01:00
|
|
|
return *this;
|
2007-10-03 19:27:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2016-04-29 18:11:43 +02:00
|
|
|
operator=(const copycounter& in)
|
2011-03-09 19:30:11 +01:00
|
|
|
{
|
|
|
|
VERIFY( in.valid == true );
|
2007-10-03 19:27:18 +02:00
|
|
|
++copycount;
|
|
|
|
val = in.val;
|
|
|
|
valid = true;
|
2007-12-10 14:53:10 +01:00
|
|
|
return true;
|
2007-10-03 19:27:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
copycounter&
|
|
|
|
operator=(copycounter&& in)
|
2011-03-09 19:30:11 +01:00
|
|
|
{
|
2007-10-03 19:27:18 +02:00
|
|
|
VERIFY(in.valid == true);
|
|
|
|
val = in.val;
|
|
|
|
in.valid = false;
|
|
|
|
valid = true;
|
|
|
|
return *this;
|
|
|
|
}
|
2016-04-29 18:11:43 +02:00
|
|
|
|
2011-06-07 18:11:36 +02:00
|
|
|
~copycounter() noexcept
|
2007-10-03 19:27:18 +02:00
|
|
|
{ valid = false; }
|
|
|
|
};
|
|
|
|
|
|
|
|
int copycounter::copycount = 0;
|
2016-04-29 18:11:43 +02:00
|
|
|
|
2010-10-28 18:01:05 +02:00
|
|
|
inline bool
|
2007-10-03 19:27:18 +02:00
|
|
|
operator==(const copycounter& lhs, const copycounter& rhs)
|
|
|
|
{ return lhs.val == rhs.val; }
|
|
|
|
|
2010-10-28 18:01:05 +02:00
|
|
|
inline bool
|
2007-10-03 19:27:18 +02:00
|
|
|
operator<(const copycounter& lhs, const copycounter& rhs)
|
|
|
|
{ return lhs.val < rhs.val; }
|
|
|
|
|
2010-10-28 18:01:05 +02:00
|
|
|
inline void
|
2007-10-03 19:27:18 +02:00
|
|
|
swap(copycounter& lhs, copycounter& rhs)
|
2011-03-09 19:30:11 +01:00
|
|
|
{
|
|
|
|
VERIFY( lhs.valid && rhs.valid );
|
2007-10-03 19:27:18 +02:00
|
|
|
int temp = lhs.val;
|
|
|
|
lhs.val = rhs.val;
|
|
|
|
rhs.val = temp;
|
|
|
|
}
|
|
|
|
|
2011-03-09 19:30:11 +01:00
|
|
|
// In the occasion of libstdc++/48038.
|
|
|
|
struct rvalstruct_compare_by_value
|
|
|
|
{
|
|
|
|
int val;
|
|
|
|
bool ok;
|
2011-01-20 21:04:25 +01:00
|
|
|
|
2011-03-09 19:30:11 +01:00
|
|
|
rvalstruct_compare_by_value(int v)
|
|
|
|
: val(v), ok(true) { }
|
|
|
|
|
|
|
|
rvalstruct_compare_by_value(const rvalstruct_compare_by_value& rh)
|
|
|
|
: val(rh.val), ok(rh.ok)
|
|
|
|
{
|
|
|
|
VERIFY(rh.ok);
|
|
|
|
}
|
|
|
|
|
|
|
|
rvalstruct_compare_by_value&
|
|
|
|
operator=(const rvalstruct_compare_by_value& rh)
|
|
|
|
{
|
|
|
|
VERIFY( rh.ok );
|
|
|
|
val = rh.val;
|
|
|
|
ok = rh.ok;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
rvalstruct_compare_by_value(rvalstruct_compare_by_value&& rh)
|
|
|
|
: val(rh.val), ok(rh.ok)
|
|
|
|
{
|
|
|
|
VERIFY( rh.ok );
|
|
|
|
rh.ok = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
rvalstruct_compare_by_value&
|
|
|
|
operator=(rvalstruct_compare_by_value&& rh)
|
|
|
|
{
|
|
|
|
VERIFY( rh.ok );
|
|
|
|
val = rh.val;
|
|
|
|
ok = rh.ok;
|
|
|
|
rh.ok = false;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
operator<(rvalstruct_compare_by_value lh,
|
|
|
|
rvalstruct_compare_by_value rh)
|
|
|
|
{
|
|
|
|
VERIFY( rh.ok );
|
|
|
|
VERIFY( lh.ok );
|
|
|
|
return lh.val < rh.val;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
order(rvalstruct_compare_by_value lh,
|
|
|
|
rvalstruct_compare_by_value rh)
|
|
|
|
{
|
|
|
|
VERIFY( rh.ok );
|
|
|
|
VERIFY( lh.ok );
|
|
|
|
return lh.val < rh.val;
|
|
|
|
}
|
|
|
|
|
2011-06-07 18:11:36 +02:00
|
|
|
struct throwing_move_constructor
|
|
|
|
{
|
|
|
|
throwing_move_constructor() = default;
|
|
|
|
|
|
|
|
throwing_move_constructor(throwing_move_constructor&&)
|
|
|
|
{ throw 1; }
|
|
|
|
|
|
|
|
throwing_move_constructor(const throwing_move_constructor&) = default;
|
2011-10-18 19:39:15 +02:00
|
|
|
|
|
|
|
throwing_move_constructor&
|
|
|
|
operator=(const throwing_move_constructor&) = default;
|
2011-06-07 18:11:36 +02:00
|
|
|
};
|
|
|
|
|
2011-03-09 19:30:11 +01:00
|
|
|
} // namespace __gnu_test
|
2011-01-20 21:04:25 +01:00
|
|
|
|
2010-10-28 18:01:05 +02:00
|
|
|
namespace std
|
|
|
|
{
|
2011-03-09 19:30:11 +01:00
|
|
|
/// std::hash specialization for __gnu_test::rvalstruct.
|
2010-10-28 18:01:05 +02:00
|
|
|
template<>
|
|
|
|
struct hash<__gnu_test::rvalstruct>
|
|
|
|
{
|
|
|
|
typedef size_t result_type;
|
|
|
|
typedef __gnu_test::rvalstruct argument_type;
|
|
|
|
|
|
|
|
size_t
|
|
|
|
operator()(const __gnu_test::rvalstruct& __rvs) const
|
|
|
|
{ return __rvs.val; }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2007-10-03 19:27:18 +02:00
|
|
|
#endif // _GLIBCXX_TESTSUITE_TR1_H
|