[Bf-blender-cvs] [375a600b8bc] functions-experimental-refactor: initial ArrayOrSingleRef implementation

Jacques Lucke noreply at git.blender.org
Tue Oct 15 15:56:36 CEST 2019


Commit: 375a600b8bcff7551ddd70d0916cb58b9208dd9e
Author: Jacques Lucke
Date:   Tue Oct 8 12:37:36 2019 +0200
Branches: functions-experimental-refactor
https://developer.blender.org/rB375a600b8bcff7551ddd70d0916cb58b9208dd9e

initial ArrayOrSingleRef implementation

===================================================================

A	source/blender/blenlib/BLI_array_or_single_ref.h
A	tests/gtests/blenlib/BLI_array_or_single_ref.cc
M	tests/gtests/blenlib/CMakeLists.txt

===================================================================

diff --git a/source/blender/blenlib/BLI_array_or_single_ref.h b/source/blender/blenlib/BLI_array_or_single_ref.h
new file mode 100644
index 00000000000..461b121a23a
--- /dev/null
+++ b/source/blender/blenlib/BLI_array_or_single_ref.h
@@ -0,0 +1,77 @@
+/*
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#ifndef __BLI_ARRAY_OR_SINGLE_REF_H__
+#define __BLI_ARRAY_OR_SINGLE_REF_H__
+
+/** \file
+ * \ingroup bli
+ */
+
+#include "BLI_array_ref.h"
+
+namespace BLI {
+
+template<typename T> class ArrayOrSingleRef {
+ private:
+  const T *m_buffer;
+  uint m_array_size;
+  bool m_is_single;
+
+  ArrayOrSingleRef(const T *buffer, uint array_size, bool is_single)
+      : m_buffer(buffer), m_array_size(array_size), m_is_single(is_single)
+  {
+  }
+
+ public:
+  ArrayOrSingleRef() : ArrayOrSingleRef(nullptr, 0, false)
+  {
+  }
+
+  ArrayOrSingleRef(ArrayRef<T> array) : ArrayOrSingleRef(array.begin(), array.size(), false)
+  {
+  }
+
+  static ArrayOrSingleRef FromSingle(const T *value, uint array_size)
+  {
+    return ArrayOrSingleRef(value, array_size, true);
+  }
+
+  static ArrayOrSingleRef FromArray(const T *value, uint size)
+  {
+    return ArrayOrSingleRef(value, size, false);
+  }
+
+  const T &operator[](uint index) const
+  {
+    BLI_assert(index <= m_array_size);
+    if (m_is_single) {
+      return *m_buffer;
+    }
+    else {
+      return m_buffer[index];
+    }
+  }
+
+  uint size() const
+  {
+    return m_array_size;
+  }
+};
+
+}  // namespace BLI
+
+#endif /* __BLI_ARRAY_OR_SINGLE_REF_H__ */
diff --git a/tests/gtests/blenlib/BLI_array_or_single_ref.cc b/tests/gtests/blenlib/BLI_array_or_single_ref.cc
new file mode 100644
index 00000000000..1a30fec2867
--- /dev/null
+++ b/tests/gtests/blenlib/BLI_array_or_single_ref.cc
@@ -0,0 +1,10 @@
+#include "testing/testing.h"
+#include "BLI_array_or_single_ref.h"
+
+using namespace BLI;
+
+TEST(array_ref, DefaultConstruct)
+{
+  ArrayOrSingleRef<int> ref;
+  EXPECT_EQ(ref.size(), 0);
+}
diff --git a/tests/gtests/blenlib/CMakeLists.txt b/tests/gtests/blenlib/CMakeLists.txt
index 8ae1c7450e2..02d37bb0b14 100644
--- a/tests/gtests/blenlib/CMakeLists.txt
+++ b/tests/gtests/blenlib/CMakeLists.txt
@@ -39,9 +39,10 @@ else()
 endif()
 
 BLENDER_TEST(BLI_array "bf_blenlib")
+BLENDER_TEST(BLI_array_or_single "bf_blenlib")
+BLENDER_TEST(BLI_array_ref "bf_blenlib")
 BLENDER_TEST(BLI_array_store "bf_blenlib")
 BLENDER_TEST(BLI_array_utils "bf_blenlib")
-BLENDER_TEST(BLI_array_ref "bf_blenlib")
 BLENDER_TEST(BLI_bit_array_ref "bf_blenlib")
 BLENDER_TEST(BLI_chained_strings "bf_blenlib")
 BLENDER_TEST(BLI_chunked_range "bf_blenlib")



More information about the Bf-blender-cvs mailing list