[Bf-blender-cvs] [3b6bed43cd2] functions: use string ref for type names

Jacques Lucke noreply at git.blender.org
Wed May 15 11:43:10 CEST 2019


Commit: 3b6bed43cd272eb15dfe703e0caa17e8a70e7e32
Author: Jacques Lucke
Date:   Wed May 15 11:10:02 2019 +0200
Branches: functions
https://developer.blender.org/rB3b6bed43cd272eb15dfe703e0caa17e8a70e7e32

use string ref for type names

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

M	source/blender/blenlib/BLI_string_ref.hpp
M	source/blender/functions/core/type.hpp
M	source/blender/functions/types/types-c.cpp

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

diff --git a/source/blender/blenlib/BLI_string_ref.hpp b/source/blender/blenlib/BLI_string_ref.hpp
index f8f5f9efe4e..17d8577886e 100644
--- a/source/blender/blenlib/BLI_string_ref.hpp
+++ b/source/blender/blenlib/BLI_string_ref.hpp
@@ -48,6 +48,37 @@ class StringRefNull {
     BLI_assert(index <= m_size);
     return m_data[index];
   }
+
+  std::string to_std_string() const
+  {
+    return std::string(m_data, m_size);
+  }
+
+  friend std::ostream &operator<<(std::ostream &stream, StringRefNull ref)
+  {
+    stream << ref.to_std_string();
+    return stream;
+  }
+
+  friend std::string operator+(const char *a, const StringRefNull b)
+  {
+    return a + b.to_std_string();
+  }
+
+  friend std::string operator+(const StringRefNull a, const char *b)
+  {
+    return a.to_std_string() + b;
+  }
+
+  friend std::string operator+(const std::string &a, const StringRefNull b)
+  {
+    return a + b.data();
+  }
+
+  friend std::string operator+(const StringRefNull a, const std::string &b)
+  {
+    return a.data() + b;
+  }
 };
 
 class StringRef {
diff --git a/source/blender/functions/core/type.hpp b/source/blender/functions/core/type.hpp
index 8a66e44ba3c..50bba24ab1f 100644
--- a/source/blender/functions/core/type.hpp
+++ b/source/blender/functions/core/type.hpp
@@ -4,6 +4,7 @@
 #include <mutex>
 #include "BLI_composition.hpp"
 #include "BLI_shared.hpp"
+#include "BLI_string_ref.hpp"
 
 namespace FN {
 
@@ -39,7 +40,7 @@ class Type final : public RefCountedBase {
   {
   }
 
-  const std::string &name() const
+  const StringRefNull name() const
   {
     return m_name;
   }
diff --git a/source/blender/functions/types/types-c.cpp b/source/blender/functions/types/types-c.cpp
index 934fea2be4b..9a75af75c2f 100644
--- a/source/blender/functions/types/types-c.cpp
+++ b/source/blender/functions/types/types-c.cpp
@@ -4,7 +4,7 @@ using namespace FN;
 
 const char *FN_type_name(FnType type)
 {
-  return unwrap(type)->name().c_str();
+  return unwrap(type)->name().data();
 }
 
 void FN_type_free(FnType type)



More information about the Bf-blender-cvs mailing list