2008-10-26 22:35:01 +01:00
|
|
|
|
/*
|
|
|
|
|
|
VectorTransfer(Test) - intercept object copying when a STL vector grows
|
2010-12-10 02:55:40 +01:00
|
|
|
|
|
Copyright: clarify and simplify the file headers
* Lumiera source code always was copyrighted by individual contributors
* there is no entity "Lumiera.org" which holds any copyrights
* Lumiera source code is provided under the GPL Version 2+
== Explanations ==
Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above.
For this to become legally effective, the ''File COPYING in the root directory is sufficient.''
The licensing header in each file is not strictly necessary, yet considered good practice;
attaching a licence notice increases the likeliness that this information is retained
in case someone extracts individual code files. However, it is not by the presence of some
text, that legally binding licensing terms become effective; rather the fact matters that a
given piece of code was provably copyrighted and published under a license. Even reformatting
the code, renaming some variables or deleting parts of the code will not alter this legal
situation, but rather creates a derivative work, which is likewise covered by the GPL!
The most relevant information in the file header is the notice regarding the
time of the first individual copyright claim. By virtue of this initial copyright,
the first author is entitled to choose the terms of licensing. All further
modifications are permitted and covered by the License. The specific wording
or format of the copyright header is not legally relevant, as long as the
intention to publish under the GPL remains clear. The extended wording was
based on a recommendation by the FSF. It can be shortened, because the full terms
of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00
|
|
|
|
Copyright (C)
|
|
|
|
|
|
2008, Hermann Vosseler <Ichthyostega@web.de>
|
2010-12-10 02:55:40 +01:00
|
|
|
|
|
Copyright: clarify and simplify the file headers
* Lumiera source code always was copyrighted by individual contributors
* there is no entity "Lumiera.org" which holds any copyrights
* Lumiera source code is provided under the GPL Version 2+
== Explanations ==
Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above.
For this to become legally effective, the ''File COPYING in the root directory is sufficient.''
The licensing header in each file is not strictly necessary, yet considered good practice;
attaching a licence notice increases the likeliness that this information is retained
in case someone extracts individual code files. However, it is not by the presence of some
text, that legally binding licensing terms become effective; rather the fact matters that a
given piece of code was provably copyrighted and published under a license. Even reformatting
the code, renaming some variables or deleting parts of the code will not alter this legal
situation, but rather creates a derivative work, which is likewise covered by the GPL!
The most relevant information in the file header is the notice regarding the
time of the first individual copyright claim. By virtue of this initial copyright,
the first author is entitled to choose the terms of licensing. All further
modifications are permitted and covered by the License. The specific wording
or format of the copyright header is not legally relevant, as long as the
intention to publish under the GPL remains clear. The extended wording was
based on a recommendation by the FSF. It can be shortened, because the full terms
of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00
|
|
|
|
**Lumiera** is free software; you can redistribute it and/or modify it
|
|
|
|
|
|
under the terms of the GNU General Public License as published by the
|
|
|
|
|
|
Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
|
|
option) any later version. See the file COPYING for further details.
|
2010-12-10 02:55:40 +01:00
|
|
|
|
|
Copyright: clarify and simplify the file headers
* Lumiera source code always was copyrighted by individual contributors
* there is no entity "Lumiera.org" which holds any copyrights
* Lumiera source code is provided under the GPL Version 2+
== Explanations ==
Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above.
For this to become legally effective, the ''File COPYING in the root directory is sufficient.''
The licensing header in each file is not strictly necessary, yet considered good practice;
attaching a licence notice increases the likeliness that this information is retained
in case someone extracts individual code files. However, it is not by the presence of some
text, that legally binding licensing terms become effective; rather the fact matters that a
given piece of code was provably copyrighted and published under a license. Even reformatting
the code, renaming some variables or deleting parts of the code will not alter this legal
situation, but rather creates a derivative work, which is likewise covered by the GPL!
The most relevant information in the file header is the notice regarding the
time of the first individual copyright claim. By virtue of this initial copyright,
the first author is entitled to choose the terms of licensing. All further
modifications are permitted and covered by the License. The specific wording
or format of the copyright header is not legally relevant, as long as the
intention to publish under the GPL remains clear. The extended wording was
based on a recommendation by the FSF. It can be shortened, because the full terms
of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00
|
|
|
|
* *****************************************************************/
|
2008-10-26 22:35:01 +01:00
|
|
|
|
|
2017-02-22 01:54:20 +01:00
|
|
|
|
/** @file vector-transfer-test.cpp
|
2017-02-22 03:17:18 +01:00
|
|
|
|
** unit test \ref VectorTransfer_test
|
2016-11-03 18:20:10 +01:00
|
|
|
|
*/
|
|
|
|
|
|
|
2008-10-26 22:35:01 +01:00
|
|
|
|
|
|
|
|
|
|
|
2008-12-18 04:47:41 +01:00
|
|
|
|
#include "lib/test/run.hpp"
|
2008-10-26 22:35:01 +01:00
|
|
|
|
|
2010-12-03 03:13:57 +01:00
|
|
|
|
#include "lib/scoped-holder-transfer.hpp"
|
2024-06-15 18:14:00 +02:00
|
|
|
|
#include "lib/test/tracking-dummy.hpp"
|
2024-11-13 02:23:23 +01:00
|
|
|
|
#include "lib/format-cout.hpp"
|
2008-10-26 22:35:01 +01:00
|
|
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace lib {
|
2010-12-18 00:58:19 +01:00
|
|
|
|
namespace test {
|
|
|
|
|
|
|
|
|
|
|
|
using ::Test;
|
|
|
|
|
|
using std::vector;
|
|
|
|
|
|
|
|
|
|
|
|
namespace { // extending the Dummy for our special purpose....
|
|
|
|
|
|
|
|
|
|
|
|
class TransDummy
|
|
|
|
|
|
: public Dummy
|
2008-10-26 22:35:01 +01:00
|
|
|
|
{
|
2010-12-18 00:58:19 +01:00
|
|
|
|
public:
|
|
|
|
|
|
TransDummy()
|
2008-10-26 22:35:01 +01:00
|
|
|
|
{
|
2010-12-18 00:58:19 +01:00
|
|
|
|
TRACE (test, "CTOR TransDummy() --> this=%p", this);
|
|
|
|
|
|
setVal(0); // we use val_==0 to mark the "empty" state
|
2008-10-26 22:35:01 +01:00
|
|
|
|
}
|
2010-12-18 00:58:19 +01:00
|
|
|
|
|
|
|
|
|
|
~TransDummy()
|
|
|
|
|
|
{
|
|
|
|
|
|
TRACE (test, "DTOR ~TransDummy() this=%p", this);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* to make Dummy usable within vector, we need to provide
|
|
|
|
|
|
* \em special copy operations, an operator bool() and
|
|
|
|
|
|
* a transfer_control friend function to be used by
|
|
|
|
|
|
* our special allocator.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
TransDummy (const TransDummy& o)
|
|
|
|
|
|
: Dummy()
|
|
|
|
|
|
{
|
|
|
|
|
|
TRACE (test, "COPY-ctor TransDummy( ref=%p ) --> this=%p", &o,this);
|
|
|
|
|
|
CHECK (!o, "protocol violation: real copy operations inhibited");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TransDummy&
|
|
|
|
|
|
operator= (TransDummy const& ref)
|
|
|
|
|
|
{
|
|
|
|
|
|
TRACE (test, "COPY target=%p <-- source=%p", this,&ref);
|
|
|
|
|
|
CHECK (!(*this));
|
|
|
|
|
|
CHECK (!ref, "protocol violation: real copy operations inhibited");
|
|
|
|
|
|
return *this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
|
setup (int x=0)
|
|
|
|
|
|
{
|
2024-11-13 02:23:23 +01:00
|
|
|
|
setVal (x? x : rani (10000));
|
2010-12-18 00:58:19 +01:00
|
|
|
|
TRACE (test, "CREATE val=%d ---> this=%p", getVal(),this);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// define implicit conversion to "bool" the naive way...
|
|
|
|
|
|
operator bool() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return 0!=getVal();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
friend void transfer_control (TransDummy& from, TransDummy& to);
|
|
|
|
|
|
|
2008-10-26 22:35:01 +01:00
|
|
|
|
};
|
2010-12-18 00:58:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
|
transfer_control (TransDummy& from, TransDummy& to)
|
|
|
|
|
|
{
|
|
|
|
|
|
TRACE (test, "TRANSFER target=%p <-- source=%p", &to,&from);
|
|
|
|
|
|
CHECK (!to, "protocol violation: target already manages another object");
|
|
|
|
|
|
to.setVal (from.getVal());
|
|
|
|
|
|
from.setVal(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
typedef Allocator_TransferNoncopyable<TransDummy> Allo;
|
|
|
|
|
|
typedef vector<TransDummy, Allo> TransDummyVector;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-24 23:06:36 +02:00
|
|
|
|
/******************************************************************************//**
|
2010-12-18 00:58:19 +01:00
|
|
|
|
* @test growing (re-allocating) a vector with noncopyable objects, with the
|
|
|
|
|
|
* help of a special Allocator and a custom \c transfer_control operation
|
|
|
|
|
|
* provided by the contained objects. The idea is to allow some special
|
|
|
|
|
|
* copy-operations for the purpose of re-allocations within the vector,
|
|
|
|
|
|
* without requiring the object to be really copyable.
|
|
|
|
|
|
*/
|
|
|
|
|
|
class VectorTransfer_test : public Test
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
|
|
run (Arg)
|
|
|
|
|
|
{
|
2024-11-13 02:23:23 +01:00
|
|
|
|
seedRand();
|
|
|
|
|
|
|
2010-12-18 00:58:19 +01:00
|
|
|
|
cout << "\n..setup table space for 2 elements\n";
|
|
|
|
|
|
TransDummyVector table;
|
|
|
|
|
|
table.reserve(2);
|
2011-11-25 22:05:12 +01:00
|
|
|
|
CHECK (0 == Dummy::checksum());
|
2010-12-18 00:58:19 +01:00
|
|
|
|
|
|
|
|
|
|
cout << "\n..install one element at index[0]\n";
|
|
|
|
|
|
table.push_back(TransDummy());
|
2011-11-25 22:05:12 +01:00
|
|
|
|
CHECK (0 == Dummy::checksum());
|
2010-12-18 00:58:19 +01:00
|
|
|
|
|
|
|
|
|
|
table[0].setup(); // switches into "managed" state
|
2011-11-25 22:05:12 +01:00
|
|
|
|
CHECK (0 < Dummy::checksum());
|
|
|
|
|
|
int theSum = Dummy::checksum();
|
2010-12-18 00:58:19 +01:00
|
|
|
|
|
|
|
|
|
|
cout << "\n..*** resize table to 5 elements\n";
|
|
|
|
|
|
table.resize(5);
|
2011-11-25 22:05:12 +01:00
|
|
|
|
CHECK (theSum==Dummy::checksum());
|
2010-12-18 00:58:19 +01:00
|
|
|
|
|
|
|
|
|
|
cout << "\n..install another element\n";
|
|
|
|
|
|
table[3].setup(375);
|
2011-11-25 22:05:12 +01:00
|
|
|
|
CHECK (theSum+375==Dummy::checksum());
|
2010-12-18 00:58:19 +01:00
|
|
|
|
|
|
|
|
|
|
cout << "\n..kill all elements....\n";
|
|
|
|
|
|
table.clear();
|
2011-11-25 22:05:12 +01:00
|
|
|
|
CHECK (0 == Dummy::checksum());
|
2010-12-18 00:58:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
LAUNCHER (VectorTransfer_test, "unit common");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}} // namespace lib::test
|