2008-10-26 22:35:01 +01:00
|
|
|
/*
|
|
|
|
|
ScopedHolderTransfer(Test) - managing noncopyable objects within a growing vector
|
2010-12-10 02:55:40 +01:00
|
|
|
|
2008-10-26 22:35:01 +01:00
|
|
|
Copyright (C) Lumiera.org
|
|
|
|
|
2008, Hermann Vosseler <Ichthyostega@web.de>
|
2010-12-10 02:55:40 +01:00
|
|
|
|
2008-10-26 22:35:01 +01:00
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU General Public License as
|
2010-12-17 23:28:49 +01:00
|
|
|
published by the Free Software Foundation; either version 2 of
|
|
|
|
|
the License, or (at your option) any later version.
|
2010-12-10 02:55:40 +01:00
|
|
|
|
2008-10-26 22:35:01 +01:00
|
|
|
This program 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.
|
2010-12-10 02:55:40 +01:00
|
|
|
|
2008-10-26 22:35:01 +01:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
2010-12-10 02:55:40 +01:00
|
|
|
|
2008-10-26 22:35:01 +01:00
|
|
|
* *****************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-12-18 04:47:41 +01:00
|
|
|
#include "lib/test/run.hpp"
|
|
|
|
|
#include "lib/util.hpp"
|
2008-10-26 22:35:01 +01:00
|
|
|
|
2009-07-04 00:24:55 +02:00
|
|
|
#include "lib/scoped-holder.hpp"
|
2010-12-03 03:13:57 +01:00
|
|
|
#include "lib/scoped-holder-transfer.hpp"
|
2008-10-26 22:35:01 +01:00
|
|
|
#include "testdummy.hpp"
|
|
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace lib {
|
2010-12-18 00:58:19 +01:00
|
|
|
namespace test {
|
|
|
|
|
|
|
|
|
|
using ::Test;
|
|
|
|
|
using util::isnil;
|
|
|
|
|
|
|
|
|
|
using std::vector;
|
|
|
|
|
using std::cout;
|
|
|
|
|
|
|
|
|
|
namespace { // extending the Dummy for our special purpose....
|
|
|
|
|
|
|
|
|
|
bool throw_in_transfer = false;
|
|
|
|
|
|
|
|
|
|
class FixedDummy
|
|
|
|
|
: public Dummy
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
FixedDummy()
|
2008-10-28 05:01:20 +01:00
|
|
|
{
|
2010-12-18 00:58:19 +01:00
|
|
|
TRACE (test, "CTOR FixedDummy() --> this=%p val=%d", this, getVal());
|
2008-10-28 05:01:20 +01:00
|
|
|
}
|
2010-12-18 00:58:19 +01:00
|
|
|
|
|
|
|
|
~FixedDummy()
|
2008-10-26 22:35:01 +01:00
|
|
|
{
|
2010-12-18 00:58:19 +01:00
|
|
|
TRACE (test, "DTOR ~FixedDummy() this=%p val=%d", this, getVal());
|
2008-10-26 22:35:01 +01:00
|
|
|
}
|
2010-12-18 00:58:19 +01:00
|
|
|
|
|
|
|
|
friend void
|
|
|
|
|
transfer_control (FixedDummy& from, FixedDummy& to)
|
|
|
|
|
{
|
|
|
|
|
TRACE (test, "TRANSFER target=%p <-- source=%p (%d,%d)", &to,&from, to.getVal(),from.getVal());
|
|
|
|
|
|
|
|
|
|
if (throw_in_transfer)
|
|
|
|
|
throw to.getVal();
|
|
|
|
|
|
|
|
|
|
swap (from,to);
|
|
|
|
|
from.setVal(0); // remove the old Dummy from accounting (checksum)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
typedef ScopedHolder<FixedDummy> HolderD;
|
|
|
|
|
typedef ScopedPtrHolder<FixedDummy> PtrHolderD;
|
|
|
|
|
|
|
|
|
|
template<class HOL>
|
|
|
|
|
struct Table
|
|
|
|
|
{
|
|
|
|
|
typedef Allocator_TransferNoncopyable<HOL> Allo;
|
|
|
|
|
typedef typename std::vector<HOL,Allo> Type;
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}//(End) test helpers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************************************
|
|
|
|
|
* @test growing a vector containing noncopyable objects wrapped into ScopedHolder
|
|
|
|
|
* instances. This requires the use of a custom allocator, invoking a
|
|
|
|
|
* \c transfer_control() function to be provided for the concrete
|
|
|
|
|
* noncopyable class type, being invoked when the vector
|
|
|
|
|
* needs to reallocate.
|
|
|
|
|
*/
|
|
|
|
|
class ScopedHolderTransfer_test : public Test
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
|
run (Arg)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
cout << "checking ScopedHolder<Dummy>...\n";
|
|
|
|
|
buildVector<HolderD>();
|
|
|
|
|
growVector<HolderD>();
|
|
|
|
|
checkErrorHandling<HolderD>();
|
|
|
|
|
|
|
|
|
|
cout << "checking ScopedPtrHolder<Dummy>...\n";
|
|
|
|
|
buildVector<PtrHolderD>();
|
|
|
|
|
growVector<PtrHolderD>();
|
|
|
|
|
checkErrorHandling<PtrHolderD>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void create_contained_object (HolderD& holder) { holder.create(); }
|
|
|
|
|
void create_contained_object (PtrHolderD& holder) { holder.reset(new FixedDummy()); }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<class HO>
|
|
|
|
|
void
|
|
|
|
|
buildVector()
|
|
|
|
|
{
|
|
|
|
|
CHECK (0==checksum);
|
2008-10-26 22:35:01 +01:00
|
|
|
{
|
2010-12-18 00:58:19 +01:00
|
|
|
typedef typename Table<HO>::Type Vect;
|
|
|
|
|
|
|
|
|
|
Vect table(50);
|
2010-12-10 02:55:40 +01:00
|
|
|
CHECK (0==checksum);
|
2010-12-18 00:58:19 +01:00
|
|
|
|
|
|
|
|
for (uint i=0; i<10; ++i)
|
|
|
|
|
create_contained_object (table[i]);
|
|
|
|
|
|
|
|
|
|
CHECK (0 < checksum);
|
|
|
|
|
CHECK ( table[9]);
|
|
|
|
|
CHECK (!table[10]);
|
|
|
|
|
|
|
|
|
|
Dummy *rawP = table[5].get();
|
|
|
|
|
CHECK (rawP);
|
|
|
|
|
CHECK (table[5]);
|
|
|
|
|
CHECK (rawP == &(*table[5]));
|
|
|
|
|
CHECK (rawP->add(-555) == table[5]->add(-555));
|
2008-10-26 22:35:01 +01:00
|
|
|
}
|
2010-12-18 00:58:19 +01:00
|
|
|
CHECK (0==checksum);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<class HO>
|
|
|
|
|
void
|
|
|
|
|
growVector()
|
|
|
|
|
{
|
|
|
|
|
CHECK (0==checksum);
|
2008-10-26 22:35:01 +01:00
|
|
|
{
|
2010-12-18 00:58:19 +01:00
|
|
|
typedef typename Table<HO>::Type Vect;
|
|
|
|
|
|
|
|
|
|
Vect table;
|
|
|
|
|
table.reserve(2);
|
2010-12-10 02:55:40 +01:00
|
|
|
CHECK (0==checksum);
|
2010-12-18 00:58:19 +01:00
|
|
|
|
|
|
|
|
cout << ".\n..install one element at index[0]\n";
|
|
|
|
|
table.push_back(HO());
|
2010-12-10 02:55:40 +01:00
|
|
|
CHECK (0==checksum);
|
2010-12-18 00:58:19 +01:00
|
|
|
|
|
|
|
|
create_contained_object (table[0]); // switches into "managed" state
|
|
|
|
|
CHECK (0 < checksum);
|
|
|
|
|
int theSum = checksum;
|
|
|
|
|
|
|
|
|
|
cout << ".\n..*** resize table to 16 elements\n";
|
|
|
|
|
for (uint i=0; i<15; ++i)
|
|
|
|
|
table.push_back(HO());
|
|
|
|
|
|
|
|
|
|
CHECK (theSum==checksum);
|
2008-10-26 22:35:01 +01:00
|
|
|
}
|
2010-12-18 00:58:19 +01:00
|
|
|
CHECK (0==checksum);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<class HO>
|
|
|
|
|
void
|
|
|
|
|
checkErrorHandling()
|
|
|
|
|
{
|
|
|
|
|
CHECK (0==checksum);
|
2008-10-26 22:35:01 +01:00
|
|
|
{
|
2010-12-18 00:58:19 +01:00
|
|
|
typedef typename Table<HO>::Type Vect;
|
|
|
|
|
|
|
|
|
|
Vect table(5);
|
|
|
|
|
table.reserve(5);
|
2010-12-10 02:55:40 +01:00
|
|
|
CHECK (0==checksum);
|
2010-12-18 00:58:19 +01:00
|
|
|
|
|
|
|
|
create_contained_object (table[2]);
|
|
|
|
|
create_contained_object (table[4]);
|
|
|
|
|
CHECK (0 < checksum);
|
|
|
|
|
int theSum = checksum;
|
|
|
|
|
|
|
|
|
|
cout << ".\n.throw some exceptions...\n";
|
|
|
|
|
throw_in_ctor = true;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
create_contained_object (table[3]);
|
|
|
|
|
NOTREACHED ();
|
|
|
|
|
}
|
|
|
|
|
catch (int val)
|
|
|
|
|
{
|
|
|
|
|
CHECK (theSum < checksum);
|
|
|
|
|
checksum -= val;
|
|
|
|
|
CHECK (theSum==checksum);
|
|
|
|
|
}
|
|
|
|
|
CHECK ( table[2]);
|
|
|
|
|
CHECK (!table[3]); // not created because of exception
|
|
|
|
|
CHECK ( table[4]);
|
|
|
|
|
|
|
|
|
|
throw_in_ctor = false;
|
|
|
|
|
throw_in_transfer=true; // can do this only when using ScopedHolder
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
table.resize(10);
|
|
|
|
|
}
|
|
|
|
|
catch (int val)
|
|
|
|
|
{
|
|
|
|
|
CHECK ( table.size() < 10);
|
|
|
|
|
}
|
|
|
|
|
CHECK (theSum == checksum);
|
|
|
|
|
throw_in_transfer=false;
|
2008-10-26 22:35:01 +01:00
|
|
|
}
|
2010-12-18 00:58:19 +01:00
|
|
|
CHECK (0==checksum);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
LAUNCHER (ScopedHolderTransfer_test, "unit common");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}} // namespace lib::test
|