diff options
| author | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2009-02-12 13:44:34 +0000 |
|---|---|---|
| committer | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2009-02-12 13:44:34 +0000 |
| commit | b8b6b5c56890122c182c035406457ae7a5791f6a (patch) | |
| tree | d14084200913324948bdf5485c176e71d6717abd | |
| parent | a73ef15904a8428d7e979aef419111f30fc12800 (diff) | |
2009-02-12 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Added array_wrapper template.
* src/array_fun.h
* test/array_funTest.cc
| -rw-r--r-- | ChangeLog | 6 | ||||
| -rw-r--r-- | src/array_fun.h | 33 | ||||
| -rw-r--r-- | test/array_funTest.cc | 23 |
3 files changed, 62 insertions, 0 deletions
@@ -1,5 +1,11 @@ 2009-02-12 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net> + Added array_wrapper template. + * src/array_fun.h + * test/array_funTest.cc + +2009-02-12 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net> + Use operator T* of array_ptr. * src/OptionParser.cc diff --git a/src/array_fun.h b/src/array_fun.h index 157456fb..6aac4d6c 100644 --- a/src/array_fun.h +++ b/src/array_fun.h @@ -262,6 +262,39 @@ public: } }; +template<typename T, size_t N> +class array_wrapper { +private: + T _array[N]; +public: + array_wrapper() {} + + T& operator[](size_t index) + { + return _array[index]; + } + + const T& operator[](size_t index) const + { + return _array[index]; + } + + operator T*() + { + return _array; + } + + operator const T*() const + { + return _array; + } + + size_t size() const + { + return N; + } +}; + } // namespace aria2 #endif // _D_ARRAY_FUN_H_ diff --git a/test/array_funTest.cc b/test/array_funTest.cc index 6e474fcc..be9b0162 100644 --- a/test/array_funTest.cc +++ b/test/array_funTest.cc @@ -12,6 +12,7 @@ class array_funTest:public CppUnit::TestFixture { CPPUNIT_TEST(testArray_and); CPPUNIT_TEST(testArrayLength); CPPUNIT_TEST(testArrayPtr); + CPPUNIT_TEST(testArrayWrapper); CPPUNIT_TEST_SUITE_END(); public: @@ -21,6 +22,7 @@ public: void testArray_and(); void testArrayLength(); void testArrayPtr(); + void testArrayWrapper(); struct X{ int m; @@ -109,4 +111,25 @@ void array_funTest::testArrayPtr() arrayPtrConstCast(ax); } +static void arrayWrapperConst(const array_wrapper<int, 10>& array) +{ + CPPUNIT_ASSERT_EQUAL(9, array[9]); +} + +void array_funTest::testArrayWrapper() +{ + array_wrapper<int, 10> a1; + CPPUNIT_ASSERT_EQUAL((size_t)10, a1.size()); + for(size_t i = 0; i < a1.size(); ++i) { + a1[i] = i; + } + CPPUNIT_ASSERT_EQUAL(9, a1[9]); + array_wrapper<int, 10> a2 = a1; + CPPUNIT_ASSERT_EQUAL(9, a2[9]); + + array_wrapper<struct X, 10> x1; + arrayPtrCast(x1); + arrayPtrConstCast(x1); +} + } // namespace aria2 |
