Add dali-vector extension support for types that have destructor and/or copy construc... 14/39414/3
authorKimmo Hoikka <kimmo.hoikka@samsung.com>
Thu, 14 May 2015 12:55:58 +0000 (13:55 +0100)
committerKimmo Hoikka <kimmo.hoikka@samsung.com>
Thu, 14 May 2015 13:14:10 +0000 (14:14 +0100)
Change-Id: Ic4aa6336d6d3514d5c2d487d7550ae788be9eb70

13 files changed:
automated-tests/src/dali/dali-test-suite-utils/dali-test-suite-utils.cpp
automated-tests/src/dali/dali-test-suite-utils/dali-test-suite-utils.h
automated-tests/src/dali/utc-Dali-MeshData.cpp
automated-tests/src/dali/utc-Dali-Vector.cpp
dali/public-api/common/dali-vector.h
dali/public-api/common/type-traits.h [new file with mode: 0644]
dali/public-api/file.list
dali/public-api/math/matrix.h
dali/public-api/math/matrix3.h
dali/public-api/math/quaternion.h
dali/public-api/math/vector2.h
dali/public-api/math/vector3.h
dali/public-api/math/vector4.h

index 6503314..938d585 100644 (file)
@@ -226,47 +226,6 @@ void DALI_TEST_EQUALS( const char* str1, const std::string &str2, const char* lo
   DALI_TEST_EQUALS(str1, str2.c_str(), location);
 }
 
-
-/**
- * Test whether one unsigned integer value is greater than another.
- * Test succeeds if value1 > value2
- * @param[in] value1 The first value
- * @param[in] value2 The second value
- * @param[in] location The TEST_LOCATION macro should be used here
- */
-void DALI_TEST_GREATER(unsigned int value1, unsigned int value2, const char* location)
-{
-  if (!(value1 > value2))
-  {
-    fprintf(stderr, "%s, checking %d > %d\n", location, value1, value2);
-    tet_result(TET_FAIL);
-  }
-  else
-  {
-    tet_result(TET_PASS);
-  }
-}
-
-/**
- * Test whether one float value is greater than another.
- * Test succeeds if value1 > value2
- * @param[in] value1 The first value
- * @param[in] value2 The second value
- * @param[in] location The TEST_LOCATION macro should be used here
- */
-void DALI_TEST_GREATER( float value1, float value2, const char* location)
-{
-  if (!(value1 > value2))
-  {
-    fprintf(stderr, "%s, checking %f > %f\n", location, value1, value2);
-    tet_result(TET_FAIL);
-  }
-  else
-  {
-    tet_result(TET_PASS);
-  }
-}
-
 void DALI_TEST_ASSERT( DaliException& e, std::string conditionSubString, const char* location )
 {
   if( NULL == strstr( e.condition, conditionSubString.c_str() ) )
index ace09d4..07c8d1f 100644 (file)
@@ -20,7 +20,7 @@
 
 // EXTERNAL INCLUDES
 #include <cstdarg>
-#include <iosfwd>
+#include <iostream>
 
 // INTERNAL INCLUDES
 #include <dali/public-api/dali-core.h>
@@ -308,16 +308,19 @@ void DALI_TEST_EQUALS( const char* str1, const std::string &str2, const char* lo
  * @param[in] value2 The second value
  * @param[in] location The TEST_LOCATION macro should be used here
  */
-void DALI_TEST_GREATER(unsigned int value1, unsigned int value2, const char* location);
-
-/**
- * Test whether one float value is greater than another.
- * Test succeeds if value1 > value2
- * @param[in] value1 The first value
- * @param[in] value2 The second value
- * @param[in] location The TEST_LOCATION macro should be used here
- */
-void DALI_TEST_GREATER( float value1, float value2, const char* location);
+template< typename T >
+void DALI_TEST_GREATER( T value1, T value2, const char* location)
+{
+  if (!(value1 > value2))
+  {
+    std::cerr << location << ", checking " << value1 <<" > " << value2 << "\n";
+    tet_result(TET_FAIL);
+  }
+  else
+  {
+    tet_result(TET_PASS);
+  }
+}
 
 /**
  * Test whether the assertion condition that failed and thus triggered the
index 1aceab7..273bd27 100644 (file)
@@ -51,8 +51,8 @@ int UtcDaliMeshDataSetData(void)
   Material customMaterial = ConstructMaterial();
   meshData.SetData(vertices, faces, bones, customMaterial);
 
-  DALI_TEST_GREATER(meshData.GetVertexCount(), 0u, TEST_LOCATION);
-  DALI_TEST_GREATER(meshData.GetFaceCount(), 0u, TEST_LOCATION);
+  DALI_TEST_GREATER(meshData.GetVertexCount(), size_t(0), TEST_LOCATION);
+  DALI_TEST_GREATER(meshData.GetFaceCount(), size_t(0), TEST_LOCATION);
 
   const MeshData::FaceIndices& faces2 = meshData.GetFaces();
   const MeshData::VertexContainer& verts2 = meshData.GetVertices();
index 5220315..0cf200d 100644 (file)
@@ -1255,6 +1255,82 @@ int UtcDaliVectorIntEraseRangeAssert(void)
   END_TEST;
 }
 
+int UtcDaliVectorVector2P(void)
+{
+  tet_infoline("Testing Dali::Vector< Vector2 >");
+
+  Vector< Vector2 > classvector;
+  DALI_TEST_EQUALS( ZERO, classvector.Count(), TEST_LOCATION );
+  DALI_TEST_EQUALS( ZERO, classvector.Capacity(), TEST_LOCATION );
+
+  classvector.PushBack( Vector2() );
+
+  DALI_TEST_EQUALS( 1u, classvector.Count(), TEST_LOCATION );
+  DALI_TEST_GREATER( classvector.Capacity(), ZERO, TEST_LOCATION );
+
+  classvector.PushBack( Vector2( 0.1f, 0.2f ) );
+
+  DALI_TEST_EQUALS( 2u, classvector.Count(), TEST_LOCATION );
+
+  DALI_TEST_EQUALS( Vector2(), classvector[ 0 ], TEST_LOCATION );
+  DALI_TEST_EQUALS( Vector2( 0.1f, 0.2f ), classvector[ 1 ], TEST_LOCATION );
+
+  tet_result(TET_PASS); // for now
+  END_TEST;
+}
+
+int UtcDaliVectorVector3P(void)
+{
+  tet_infoline("Testing Dali::Vector< Vector3 >");
+
+  Vector< Vector3 > classvector;
+  DALI_TEST_EQUALS( ZERO, classvector.Count(), TEST_LOCATION );
+  DALI_TEST_EQUALS( ZERO, classvector.Capacity(), TEST_LOCATION );
+
+  classvector.PushBack( Vector3() );
+
+  DALI_TEST_EQUALS( 1u, classvector.Count(), TEST_LOCATION );
+  DALI_TEST_GREATER( classvector.Capacity(), ZERO, TEST_LOCATION );
+
+  classvector.PushBack( Vector3( 0.1f, 0.2f, 0.3f ) );
+
+  DALI_TEST_EQUALS( 2u, classvector.Count(), TEST_LOCATION );
+
+  DALI_TEST_EQUALS( Vector3(), classvector[ 0 ], TEST_LOCATION );
+  DALI_TEST_EQUALS( Vector3( 0.1f, 0.2f, 0.3f ), classvector[ 1 ], TEST_LOCATION );
+
+  tet_result(TET_PASS); // for now
+  END_TEST;
+}
+
+int UtcDaliVectorMatrixP(void)
+{
+  tet_infoline("Testing Dali::Vector< Matrix >");
+
+  Vector< Matrix > classvector;
+  DALI_TEST_EQUALS( ZERO, classvector.Count(), TEST_LOCATION );
+  DALI_TEST_EQUALS( ZERO, classvector.Capacity(), TEST_LOCATION );
+
+  classvector.PushBack( Matrix() );
+
+  DALI_TEST_EQUALS( 1u, classvector.Count(), TEST_LOCATION );
+  DALI_TEST_GREATER( classvector.Capacity(), ZERO, TEST_LOCATION );
+
+  classvector.PushBack( Matrix::IDENTITY );
+
+  DALI_TEST_EQUALS( 2u, classvector.Count(), TEST_LOCATION );
+
+  DALI_TEST_EQUALS( Matrix(), classvector[ 0 ], TEST_LOCATION );
+  DALI_TEST_EQUALS( Matrix::IDENTITY, classvector[ 1 ], TEST_LOCATION );
+
+  tet_result(TET_PASS); // for now
+  END_TEST;
+}
+
+/*
+ * this does not compile at the moment
+ * Vector< Actor > classvector; this does not compile yet either
+ *
 namespace
 {
 
@@ -1275,13 +1351,11 @@ struct ComplexType
 
 } // anonymous namespace
 
-
-int UtcDaliVectorComplex(void)
+int UtcDaliVectorComplex( void)
 {
-  tet_infoline("Testing Dali::Vector< int* > exception handling");
+  tet_infoline("Testing Dali::Vector< ComplexType > ");
 
-  // this does not compile at the moment
-/*  Vector< ComplexType > classvector;
+  Vector< ComplexType > classvector;
   DALI_TEST_EQUALS( ZERO, classvector.Count(), TEST_LOCATION );
   DALI_TEST_EQUALS( ZERO, classvector.Capacity(), TEST_LOCATION );
 
@@ -1291,8 +1365,7 @@ int UtcDaliVectorComplex(void)
   DALI_TEST_EQUALS( true, gConstructorCalled, TEST_LOCATION );
   classvector.Clear();
   DALI_TEST_EQUALS( true, gDestructorCalled, TEST_LOCATION );
-*/
-//  Vector< Actor > classvector; this does not compile yet either
   tet_result(TET_PASS); // for now
   END_TEST;
 }
+*/
index 82bb898..359e6dc 100644 (file)
@@ -24,6 +24,7 @@
 
 // INTERNAL INCLUDES
 #include <dali/public-api/common/dali-common.h>
+#include <dali/public-api/common/type-traits.h>
 #include <dali/public-api/math/math-utils.h>
 
 /**
@@ -357,7 +358,7 @@ private:
  *
  * @param type of the data that the vector holds.
  */
-template< class T, bool IsTrivialType = __has_trivial_destructor(T) && __has_trivial_copy(T) >
+template< class T, bool IsTrivialType = TypeTraits<T>::IS_TRIVIAL_TYPE == true >
 class Vector : public VectorAlgorithms< IsTrivialType >
 {
 public: // API
diff --git a/dali/public-api/common/type-traits.h b/dali/public-api/common/type-traits.h
new file mode 100644 (file)
index 0000000..0fd5162
--- /dev/null
@@ -0,0 +1,56 @@
+#ifndef __DALI_TYPE_TRAITS_H__
+#define __DALI_TYPE_TRAITS_H__
+
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+namespace Dali
+{
+
+/**
+ * @brief Basic type traits that every type has by default
+ * This allows specialisations to not have to repeat all flags
+ */
+template <typename Type>
+struct BasicTypes
+{
+  /**
+   * This flag tells Dali if a class can be considered POD. If it declares copy constructor and/or destructor, its not considered trivial
+   * and cannot be copied by using memcpy etc.
+   */
+  enum { IS_TRIVIAL_TYPE = __has_trivial_destructor(Type) && __has_trivial_copy(Type) };
+};
+
+/**
+ * @brief Type traits support
+ * An example of overriding a traits flag for a custom type can be done by:
+ * <code>
+ * namespace Dali
+ * {
+ *   /// Tell DALi that Matrix is POD, even though it has a copy constructor
+ *   template <> struct TypeTraits< Matrix > : public BasicTypes< Matrix > { enum { IS_TRIVIAL_TYPE = true }; };
+ * }
+ * </code>
+ */
+template <typename Type>
+struct TypeTraits : public BasicTypes< Type >
+{
+};
+
+} // namespace Dali
+
+#endif /* __DALI_TYPE_TRAITS_H__ */
index c4b4359..16658f5 100644 (file)
@@ -138,8 +138,8 @@ public_api_core_common_header_files = \
   $(public_api_src_dir)/common/compile-time-assert.h \
   $(public_api_src_dir)/common/constants.h \
   $(public_api_src_dir)/common/dali-common.h \
-  $(public_api_src_dir)/common/hash.h \
   $(public_api_src_dir)/common/dali-vector.h \
+  $(public_api_src_dir)/common/hash.h \
   $(public_api_src_dir)/common/intrusive-ptr.h \
   $(public_api_src_dir)/common/loading-state.h \
   $(public_api_src_dir)/common/mutex.h \
@@ -148,6 +148,7 @@ public_api_core_common_header_files = \
   $(public_api_src_dir)/common/set-wrapper.h \
   $(public_api_src_dir)/common/scoped-pointer.h \
   $(public_api_src_dir)/common/stage.h \
+  $(public_api_src_dir)/common/type-traits.h \
   $(public_api_src_dir)/common/vector-wrapper.h \
   $(public_api_src_dir)/common/view-mode.h
 
index d504f28..ab96e04 100644 (file)
@@ -23,6 +23,7 @@
 
 // INTERNAL INCLUDES
 #include <dali/public-api/common/dali-common.h>
+#include <dali/public-api/common/type-traits.h>
 #include <dali/public-api/math/vector4.h>
 
 namespace Dali
@@ -359,6 +360,9 @@ private:
  */
 DALI_IMPORT_API std::ostream& operator<< (std::ostream& o, const Matrix& matrix);
 
+// Allow Matrix to be treated as a POD type
+template <> struct TypeTraits< Matrix > : public BasicTypes< Matrix > { enum { IS_TRIVIAL_TYPE = true }; };
+
 } // namespace Dali
 
 #endif // __DALI_MATRIX_H__
index 42d83a6..05524aa 100644 (file)
@@ -21,6 +21,7 @@
 // INTERNAL INCLUDES
 #include <dali/public-api/math/vector3.h>
 #include <dali/public-api/math/matrix.h>
+#include <dali/public-api/common/type-traits.h>
 
 namespace Dali
 {
@@ -210,6 +211,9 @@ private:
  */
 DALI_IMPORT_API std::ostream& operator<< (std::ostream& o, const Matrix3& matrix);
 
+// Allow Matrix3 to be treated as a POD type
+template <> struct TypeTraits< Matrix3 > : public BasicTypes< Matrix3 > { enum { IS_TRIVIAL_TYPE = true }; };
+
 } // namespace Dali
 
 #endif //__DALI_MATRIX3_H__
index c873578..7f8b845 100644 (file)
@@ -22,8 +22,9 @@
 #include <iosfwd>
 
 // INTERNAL INCLUDES
-#include <dali/public-api/common/dali-common.h>
 #include <dali/public-api/common/constants.h>
+#include <dali/public-api/common/dali-common.h>
+#include <dali/public-api/common/type-traits.h>
 #include <dali/public-api/math/radian.h>
 #include <dali/public-api/math/vector4.h>
 
@@ -427,6 +428,9 @@ public:
  */
 DALI_IMPORT_API std::ostream& operator<< (std::ostream& o, const Quaternion& quaternion);
 
+// Allow Quaternion to be treated as a POD type
+template <> struct TypeTraits< Quaternion > : public BasicTypes< Quaternion > { enum { IS_TRIVIAL_TYPE = true }; };
+
 } // namespace Dali
 
 #endif // __DALI_QUATERNION_H__
index d452c95..6fb54f7 100644 (file)
@@ -23,6 +23,7 @@
 
 // INTERNAL INCLUDES
 #include <dali/public-api/common/dali-common.h>
+#include <dali/public-api/common/type-traits.h>
 
 namespace Dali
 {
@@ -505,6 +506,9 @@ DALI_IMPORT_API Size FitScaleToFill( const Size& target, const Size& source );
  */
 DALI_IMPORT_API Size ShrinkInside( const Size& target, const Size& source );
 
+// Allow Vector2 to be treated as a POD type
+template <> struct TypeTraits< Vector2 > : public BasicTypes< Vector2 > { enum { IS_TRIVIAL_TYPE = true }; };
+
 } // namespace Dali
 
 #endif // __DALI_VECTOR_2_H__
index ba6e38f..5f744ea 100644 (file)
@@ -23,6 +23,7 @@
 
 // INTERNAL INCLUDES
 #include <dali/public-api/common/dali-common.h>
+#include <dali/public-api/common/type-traits.h>
 
 namespace Dali
 {
@@ -602,6 +603,8 @@ DALI_IMPORT_API Vector3 FillXYKeepAspectRatio( const Vector3& target, const Vect
  */
 DALI_IMPORT_API Vector3 ShrinkInsideKeepAspectRatio( const Vector3& target, const Vector3& source );
 
+// Allow Vector3 to be treated as a POD type
+template <> struct TypeTraits< Vector3 > : public BasicTypes< Vector3 > { enum { IS_TRIVIAL_TYPE = true }; };
 
 } // namespace Dali
 
index 685182e..5a59508 100644 (file)
@@ -23,6 +23,7 @@
 
 // INTERNAL INCLUDES
 #include <dali/public-api/common/dali-common.h>
+#include <dali/public-api/common/type-traits.h>
 
 namespace Dali
 {
@@ -560,6 +561,9 @@ inline Vector4 Max( const Vector4& a, const Vector4& b )
  */
 DALI_IMPORT_API Vector4 Clamp( const Vector4& v, const float& min, const float& max );
 
+// Allow Vector4 to be treated as a POD type
+template <> struct TypeTraits< Vector4 > : public BasicTypes< Vector4 > { enum { IS_TRIVIAL_TYPE = true }; };
+
 } // namespace Dali
 
 #endif // __DALI_VECTOR_4_H__