From 98510cc943e285dcab7710883b47dac7a6c871d8 Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Sat, 4 Jul 2009 00:24:55 +0200 Subject: [PATCH] rename and clean up some headers --- src/lib/allocationcluster.hpp | 2 +- .../{scopedholder.hpp => scoped-holder.hpp} | 8 +- src/lib/scoped-ptrvect.hpp | 2 +- src/lib/scopedholdertransfer.hpp | 2 +- tests/lib/Makefile.am | 2 +- tests/lib/allocationclustertest.cpp | 2 +- tests/lib/scoped-holder-test.cpp | 275 +++++++++++++++++ tests/lib/scopedholdertest.cpp | 277 ------------------ tests/lib/scopedholdertransfertest.cpp | 2 +- 9 files changed, 285 insertions(+), 287 deletions(-) rename src/lib/{scopedholder.hpp => scoped-holder.hpp} (98%) create mode 100644 tests/lib/scoped-holder-test.cpp delete mode 100644 tests/lib/scopedholdertest.cpp diff --git a/src/lib/allocationcluster.hpp b/src/lib/allocationcluster.hpp index a9593316e..be99f7329 100644 --- a/src/lib/allocationcluster.hpp +++ b/src/lib/allocationcluster.hpp @@ -53,7 +53,7 @@ #include "lib/error.hpp" #include "lib/sync-classlock.hpp" -#include "lib/scopedholder.hpp" +#include "lib/scoped-holder.hpp" #include "lib/scopedholdertransfer.hpp" diff --git a/src/lib/scopedholder.hpp b/src/lib/scoped-holder.hpp similarity index 98% rename from src/lib/scopedholder.hpp rename to src/lib/scoped-holder.hpp index 042c78844..bb535be2e 100644 --- a/src/lib/scopedholder.hpp +++ b/src/lib/scoped-holder.hpp @@ -1,5 +1,5 @@ /* - SCOPEDHOLDER.hpp - general purpose wrapper for dealing with ownership problems + SCOPED-HOLDER.hpp - general purpose wrapper for dealing with ownership problems Copyright (C) Lumiera.org 2008, Hermann Vosseler @@ -20,7 +20,7 @@ */ -/** @file scopedholder.hpp +/** @file scoped-holder.hpp ** Some wrappers for coping with ownership problems. ** Working with collections of objects, especially in conjunction with ** polymorphism, can be challenging when we are bound to care for lifecycle @@ -46,7 +46,7 @@ ** holder instances. This is the purpose of the \c transfer_control ** friend function. ** - ** @see scopedholdertest.cpp + ** @see scoped-holder-test.cpp ** @see scopedholdertransfer.hpp use in std::vector ** @see AllocationCluster usage example ** @see scoped-ptrvect.hpp simple pointer-holding collection @@ -246,7 +246,7 @@ namespace lib { } } }; - + diff --git a/src/lib/scoped-ptrvect.hpp b/src/lib/scoped-ptrvect.hpp index af48dac45..0dc509e8d 100644 --- a/src/lib/scoped-ptrvect.hpp +++ b/src/lib/scoped-ptrvect.hpp @@ -35,7 +35,7 @@ ** - TODO: retro-fit with refarray interface (--> builder branch) ** ** @see scoped-ptrvect-test.cpp - ** @see scopedholder.hpp + ** @see scoped-holder.hpp ** @see gui::DisplayService usage example */ diff --git a/src/lib/scopedholdertransfer.hpp b/src/lib/scopedholdertransfer.hpp index c4e4e66e9..3d600e61a 100644 --- a/src/lib/scopedholdertransfer.hpp +++ b/src/lib/scopedholdertransfer.hpp @@ -35,7 +35,7 @@ namespace lib { /** - * Addendum to scopedholder.hpp for transferring the lifecycle + * Addendum to scoped-holder.hpp for transferring the lifecycle * management to another instance. Using these wrappers within * STL vector and similar containers may result in the need to * do a re-allocation in response to a request to grow. diff --git a/tests/lib/Makefile.am b/tests/lib/Makefile.am index f895638ce..ad0844463 100644 --- a/tests/lib/Makefile.am +++ b/tests/lib/Makefile.am @@ -59,7 +59,7 @@ test_lib_SOURCES = \ $(testlib_srcdir)/query/queryutilstest.cpp \ $(testlib_srcdir)/removefromsettest.cpp \ $(testlib_srcdir)/sanitizedidentifiertest.cpp \ - $(testlib_srcdir)/scopedholdertest.cpp \ + $(testlib_srcdir)/scoped-holder-test.cpp \ $(testlib_srcdir)/scopedholdertransfertest.cpp \ $(testlib_srcdir)/scoped-ptrvect-test.cpp \ $(testlib_srcdir)/singletonsubclasstest.cpp \ diff --git a/tests/lib/allocationclustertest.cpp b/tests/lib/allocationclustertest.cpp index c818ad63e..1badb8ac9 100644 --- a/tests/lib/allocationclustertest.cpp +++ b/tests/lib/allocationclustertest.cpp @@ -26,7 +26,7 @@ #include "lib/util.hpp" #include "lib/allocationcluster.hpp" -#include "lib/scopedholder.hpp" +#include "lib/scoped-holder.hpp" #include #include diff --git a/tests/lib/scoped-holder-test.cpp b/tests/lib/scoped-holder-test.cpp new file mode 100644 index 000000000..62011d193 --- /dev/null +++ b/tests/lib/scoped-holder-test.cpp @@ -0,0 +1,275 @@ +/* + ScopedHolder(Test) - holding and owning noncopyable objects + + Copyright (C) Lumiera.org + 2008, Hermann Vosseler + + This program 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. + + 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. + + 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. + +* *****************************************************/ + + + +#include "lib/test/run.hpp" +#include "lib/util.hpp" + +#include "lib/scoped-holder.hpp" +#include "testdummy.hpp" + +#include +#include +#include + + +namespace lib { +namespace test{ + + using ::Test; + using util::isnil; + + using std::map; + using std::cout; + + typedef ScopedHolder HolderD; + typedef ScopedPtrHolder PtrHolderD; + + + /********************************************************************************** + * @test ScopedHolder and ScopedPtrHolder are initially empty and copyable. + * After taking ownership, they prohibit copy operations, manage the + * lifecycle of the contained object and provide smart-ptr like access. + * A series of identical tests is conducted both with the ScopedPtrHolder + * (the contained objects are heap allocated but managed by the holder) + * and with the ScopedHolder (objects placed inline) + */ + class ScopedHolder_test : public Test + { + + virtual void + run (Arg) + { + + cout << "checking ScopedHolder...\n"; + checkAllocation(); + checkErrorHandling(); + checkCopyProtocol(); + checkSTLContainer(); + + cout << "checking ScopedPtrHolder...\n"; + checkAllocation(); + checkErrorHandling(); + checkCopyProtocol(); + checkSTLContainer(); + } + + void create_contained_object (HolderD& holder) { holder.create(); } + void create_contained_object (PtrHolderD& holder) { holder.reset(new Dummy()); } + + + template + void + checkAllocation() + { + ASSERT (0==checksum); + { + HO holder; + ASSERT (!holder); + ASSERT (0==checksum); + + create_contained_object (holder); + ASSERT (holder); + ASSERT (false!=holder); + ASSERT (holder!=false); + + ASSERT (0!=checksum); + ASSERT ( &(*holder)); + ASSERT (holder->add(2) == checksum+2); + + Dummy *rawP = holder.get(); + ASSERT (rawP); + ASSERT (holder); + ASSERT (rawP == &(*holder)); + ASSERT (rawP->add(-5) == holder->add(-5)); + + TRACE (test, "holder at %x", &holder); + TRACE (test, "object at %x", holder.get() ); + TRACE (test, "size(object) = %d", sizeof(*holder)); + TRACE (test, "size(holder) = %d", sizeof(holder)); + } + ASSERT (0==checksum); + } + + + template + void + checkErrorHandling() + { + ASSERT (0==checksum); + { + HO holder; + + throw_in_ctor = true; + try + { + create_contained_object (holder); + NOTREACHED ; + } + catch (int val) + { + ASSERT (0!=checksum); + checksum -= val; + ASSERT (0==checksum); + } + ASSERT (!holder); /* because the exception happens in ctor + object doesn't count as "created" */ + throw_in_ctor = false; + } + ASSERT (0==checksum); + } + + + template + void + checkCopyProtocol() + { + ASSERT (0==checksum); + { + HO holder; + HO holder2 (holder); + holder2 = holder; + // copy and assignment of empty holders is tolerated + + // but after enclosing an object it will be copy protected... + ASSERT (!holder); + create_contained_object (holder); + ASSERT (holder); + long currSum = checksum; + void* adr = holder.get(); + try + { + holder2 = holder; + NOTREACHED ; + } + catch (lumiera::error::Logic&) + { + ASSERT (holder); + ASSERT (!holder2); + ASSERT (holder.get()==adr); + ASSERT (checksum==currSum); + } + + try + { + holder = holder2; + NOTREACHED ; + } + catch (lumiera::error::Logic&) + { + ASSERT (holder); + ASSERT (!holder2); + ASSERT (holder.get()==adr); + ASSERT (checksum==currSum); + } + + create_contained_object (holder2); + ASSERT (holder2); + ASSERT (checksum != currSum); + currSum = checksum; + try + { + holder = holder2; + NOTREACHED ; + } + catch (lumiera::error::Logic&) + { + ASSERT (holder); + ASSERT (holder2); + ASSERT (holder.get()==adr); + ASSERT (checksum==currSum); + } + + try + { + HO holder3 (holder2); + NOTREACHED ; + } + catch (lumiera::error::Logic&) + { + ASSERT (holder); + ASSERT (holder2); + ASSERT (checksum==currSum); + } + } + ASSERT (0==checksum); + } + + + /** @test collection of noncopyable objects + * maintained within a STL map + */ + template + void + checkSTLContainer() + { + typedef std::map MapHO; + + ASSERT (0==checksum); + { + MapHO maph; + ASSERT (isnil (maph)); + + for (uint i=0; i<100; ++i) + { + HO & contained = maph[i]; + ASSERT (!contained); + } // 100 holder objects created by sideeffect + + ASSERT (0==checksum); // ..... without creating any contained object! + ASSERT (!isnil (maph)); + ASSERT (100==maph.size()); + + for (uint i=0; i<100; ++i) + { + create_contained_object (maph[i]); + ASSERT (maph[i]); + ASSERT (0 < maph[i]->add(12)); + } + ASSERT (100==maph.size()); + ASSERT (0!=checksum); + + + long value55 = maph[55]->add(0); + long currSum = checksum; + + ASSERT (1 == maph.erase(55)); + ASSERT (checksum == currSum - value55); // proves object#55's dtor has been invoked + ASSERT (maph.size() == 99); + + maph[55]; // create new empty holder by sideeffect... + ASSERT (&maph[55]); + ASSERT (!maph[55]); + ASSERT (maph.size() == 100); + } + ASSERT (0==checksum); + } + + + }; + + LAUNCHER (ScopedHolder_test, "unit common"); + + +}} // namespace lib::test + diff --git a/tests/lib/scopedholdertest.cpp b/tests/lib/scopedholdertest.cpp deleted file mode 100644 index 778cc2e4f..000000000 --- a/tests/lib/scopedholdertest.cpp +++ /dev/null @@ -1,277 +0,0 @@ -/* - ScopedHolder(Test) - holding and owning noncopyable objects - - Copyright (C) Lumiera.org - 2008, Hermann Vosseler - - This program 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. - - 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. - - 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. - -* *****************************************************/ - - - -#include "lib/test/run.hpp" -#include "lib/util.hpp" - -#include "lib/scopedholder.hpp" -#include "testdummy.hpp" - -#include -#include -#include - - -namespace lib { - namespace test { - - using ::Test; - using util::isnil; - - using std::map; - using std::cout; - - typedef ScopedHolder HolderD; - typedef ScopedPtrHolder PtrHolderD; - - - /********************************************************************************** - * @test ScopedHolder and ScopedPtrHolder are initially empty and copyable. - * After taking ownership, they prohibit copy operations, manage the - * lifecycle of the contained object and provide smart-ptr like access. - * A series of identical tests is conducted both with the ScopedPtrHolder - * (the contained objects are heap allocated but managed by the holder) - * and with the ScopedHolder (objects placed inline) - */ - class ScopedHolder_test : public Test - { - - virtual void - run (Arg) - { - - cout << "checking ScopedHolder...\n"; - checkAllocation(); - checkErrorHandling(); - checkCopyProtocol(); - checkSTLContainer(); - - cout << "checking ScopedPtrHolder...\n"; - checkAllocation(); - checkErrorHandling(); - checkCopyProtocol(); - checkSTLContainer(); - } - - void create_contained_object (HolderD& holder) { holder.create(); } - void create_contained_object (PtrHolderD& holder) { holder.reset(new Dummy()); } - - - template - void - checkAllocation() - { - ASSERT (0==checksum); - { - HO holder; - ASSERT (!holder); - ASSERT (0==checksum); - - create_contained_object (holder); - ASSERT (holder); - ASSERT (false!=holder); - ASSERT (holder!=false); - - ASSERT (0!=checksum); - ASSERT ( &(*holder)); - ASSERT (holder->add(2) == checksum+2); - - Dummy *rawP = holder.get(); - ASSERT (rawP); - ASSERT (holder); - ASSERT (rawP == &(*holder)); - ASSERT (rawP->add(-5) == holder->add(-5)); - - TRACE (test, "holder at %x", &holder); - TRACE (test, "object at %x", holder.get() ); - TRACE (test, "size(object) = %d", sizeof(*holder)); - TRACE (test, "size(holder) = %d", sizeof(holder)); - } - ASSERT (0==checksum); - } - - - template - void - checkErrorHandling() - { - ASSERT (0==checksum); - { - HO holder; - - throw_in_ctor = true; - try - { - create_contained_object (holder); - NOTREACHED ; - } - catch (int val) - { - ASSERT (0!=checksum); - checksum -= val; - ASSERT (0==checksum); - } - ASSERT (!holder); /* because the exception happens in ctor - object doesn't count as "created" */ - throw_in_ctor = false; - } - ASSERT (0==checksum); - } - - - template - void - checkCopyProtocol() - { - ASSERT (0==checksum); - { - HO holder; - HO holder2 (holder); - holder2 = holder; - // copy and assignment of empty holders is tolerated - - // but after enclosing an object it will be copy protected... - ASSERT (!holder); - create_contained_object (holder); - ASSERT (holder); - long currSum = checksum; - void* adr = holder.get(); - try - { - holder2 = holder; - NOTREACHED ; - } - catch (lumiera::error::Logic&) - { - ASSERT (holder); - ASSERT (!holder2); - ASSERT (holder.get()==adr); - ASSERT (checksum==currSum); - } - - try - { - holder = holder2; - NOTREACHED ; - } - catch (lumiera::error::Logic&) - { - ASSERT (holder); - ASSERT (!holder2); - ASSERT (holder.get()==adr); - ASSERT (checksum==currSum); - } - - create_contained_object (holder2); - ASSERT (holder2); - ASSERT (checksum != currSum); - currSum = checksum; - try - { - holder = holder2; - NOTREACHED ; - } - catch (lumiera::error::Logic&) - { - ASSERT (holder); - ASSERT (holder2); - ASSERT (holder.get()==adr); - ASSERT (checksum==currSum); - } - - try - { - HO holder3 (holder2); - NOTREACHED ; - } - catch (lumiera::error::Logic&) - { - ASSERT (holder); - ASSERT (holder2); - ASSERT (checksum==currSum); - } - } - ASSERT (0==checksum); - } - - - /** @test collection of noncopyable objects - * maintained within a STL map - */ - template - void - checkSTLContainer() - { - typedef std::map MapHO; - - ASSERT (0==checksum); - { - MapHO maph; - ASSERT (isnil (maph)); - - for (uint i=0; i<100; ++i) - { - HO & contained = maph[i]; - ASSERT (!contained); - } // 100 holder objects created by sideeffect - - ASSERT (0==checksum); // ..... without creating any contained object! - ASSERT (!isnil (maph)); - ASSERT (100==maph.size()); - - for (uint i=0; i<100; ++i) - { - create_contained_object (maph[i]); - ASSERT (maph[i]); - ASSERT (0 < maph[i]->add(12)); - } - ASSERT (100==maph.size()); - ASSERT (0!=checksum); - - - long value55 = maph[55]->add(0); - long currSum = checksum; - - ASSERT (1 == maph.erase(55)); - ASSERT (checksum == currSum - value55); // proves object#55's dtor has been invoked - ASSERT (maph.size() == 99); - - maph[55]; // create new empty holder by sideeffect... - ASSERT (&maph[55]); - ASSERT (!maph[55]); - ASSERT (maph.size() == 100); - } - ASSERT (0==checksum); - } - - - }; - - LAUNCHER (ScopedHolder_test, "unit common"); - - - }// namespace test - -} // namespace lib - diff --git a/tests/lib/scopedholdertransfertest.cpp b/tests/lib/scopedholdertransfertest.cpp index abbd3a959..274a4b31f 100644 --- a/tests/lib/scopedholdertransfertest.cpp +++ b/tests/lib/scopedholdertransfertest.cpp @@ -25,7 +25,7 @@ #include "lib/test/run.hpp" #include "lib/util.hpp" -#include "lib/scopedholder.hpp" +#include "lib/scoped-holder.hpp" #include "lib/scopedholdertransfer.hpp" #include "testdummy.hpp"