Browse Source

2009-02-12 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>

	Added array_wrapper template.
	* src/array_fun.h
	* test/array_funTest.cc
Tatsuhiro Tsujikawa 16 years ago
parent
commit
b8b6b5c568
3 changed files with 62 additions and 0 deletions
  1. 6 0
      ChangeLog
  2. 33 0
      src/array_fun.h
  3. 23 0
      test/array_funTest.cc

+ 6 - 0
ChangeLog

@@ -1,3 +1,9 @@
+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.

+ 33 - 0
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_

+ 23 - 0
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