From 1c388287c9cc38a5a1af7318253e2401c0bc10bb Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Tue, 7 Oct 2008 05:16:02 +0200 Subject: [PATCH] simple vector wrapping implementation of the RefArray interface --- src/lib/refarrayimpl.hpp | 69 ++++++++++++++++++++++ tests/40components.tests | 5 ++ tests/common/refarraytest.cpp | 108 ++++++++++++++++++++++++++++++++++ 3 files changed, 182 insertions(+) create mode 100644 src/lib/refarrayimpl.hpp create mode 100644 tests/common/refarraytest.cpp diff --git a/src/lib/refarrayimpl.hpp b/src/lib/refarrayimpl.hpp new file mode 100644 index 000000000..c0ea481d8 --- /dev/null +++ b/src/lib/refarrayimpl.hpp @@ -0,0 +1,69 @@ +/* + REFARRAYIMPL.hpp - some implementations of the ref-array interface + + 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. + +*/ + + +#ifndef LIB_REFARRAYIMPL_H +#define LIB_REFARRAYIMPL_H + + +#include "lib/refarray.hpp" +#include "proc/nobugcfg.hpp" + +#include +using std::vector; + + + +namespace lib { + + /** + * Wrap a vector holding objects of a subtype + * and provide array-like access using the interface type. + */ + template + class RefArrayVectorWrapper + : public RefArray + { + typedef vector const& Tab; + Tab table_; + + public: + + RefArrayVectorWrapper (Tab toWrap) + : table_(toWrap) + { } + + virtual size_t size() const + { + return table_.size(); + } + + virtual E const& operator[] (uint i) const + { + REQUIRE (i < size()); + return table_[i]; + } + }; + + +} // namespace lib +#endif diff --git a/tests/40components.tests b/tests/40components.tests index b301b262d..ab40bd9e4 100644 --- a/tests/40components.tests +++ b/tests/40components.tests @@ -308,6 +308,11 @@ out: ::0::1::2::3::4::5::6::7::8::9::10::11::12::13::14::15::16::17::18::19 END +TEST "RefArray_test" RefArray_test < \[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, \] out: removed 0 ---> \[ 1, 2, 3, 4, 5, 6, 7, 8, 9, \] diff --git a/tests/common/refarraytest.cpp b/tests/common/refarraytest.cpp new file mode 100644 index 000000000..84ec954c2 --- /dev/null +++ b/tests/common/refarraytest.cpp @@ -0,0 +1,108 @@ +/* + RefArray(Test) - unittest for wrapping with array-of-refs access + + 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 "common/test/run.hpp" +#include "common/util.hpp" + +#include "lib/refarrayimpl.hpp" + +//#include +//#include +#include +#include + +//using boost::lexical_cast; +//using boost::format; +//using util::isnil; +//using std::string; +using test::Test; +using std::cout; +using std::vector; + + +namespace lib { + namespace test { + + namespace { // test types + + struct I + { + virtual int op(int i) const =0; + virtual ~I() {} + }; + + struct Sub1 : I + { + int op (int i) { return i+1; } + }; + + struct Sub2 : I + { + const char* letterz_; + Sub2() : letterz_("ABCDEFGHIJKLMNOPQRSTUVWXYZ") {} + + int op (int i) const { return (int)letterz_[i % 26]; } + }; + + } // (END) test types + + + + + + + + /******************************************************************* + * @test build a wrapper providing RefArray access to a given vector + * @see refarrayimpl.hpp + */ + class RefArray_test : public Test + { + + virtual void + run (Arg arg) + { + vector subz(10); + RefArrayVectorWrapper subWrapz (subz); + + RefArray & rArr (subWrapz); + + ASSERT (subWrapz.size()==subz.size()); + ASSERT (INSTANCEOF(I, &rArr[0])); + for (size_t i=0; i