BLI: add StringRefNull.c_str() method

This should be used whenever you rely on the fact, that the
returned pointer points to the beginning of a null-terminated array.
This commit is contained in:
2020-07-17 12:38:15 +02:00
parent 0fcd23a388
commit 0e3d34e48f
5 changed files with 15 additions and 5 deletions

View File

@@ -399,7 +399,7 @@ static void add_missing_particle_states(Simulation *simulation, Span<std::string
continue; continue;
} }
BKE_simulation_state_add(simulation, SIM_STATE_TYPE_PARTICLES, name.data()); BKE_simulation_state_add(simulation, SIM_STATE_TYPE_PARTICLES, name.c_str());
} }
} }

View File

@@ -187,7 +187,7 @@ class StringRefNull : public StringRefBase {
* Reference a std::string. Remember that when the std::string is destructed, the StringRefNull * Reference a std::string. Remember that when the std::string is destructed, the StringRefNull
* will point to uninitialized memory. * will point to uninitialized memory.
*/ */
StringRefNull(const std::string &str) : StringRefNull(str.data()) StringRefNull(const std::string &str) : StringRefNull(str.c_str())
{ {
} }
@@ -200,6 +200,16 @@ class StringRefNull : public StringRefBase {
BLI_assert(index <= size_); BLI_assert(index <= size_);
return data_[index]; return data_[index];
} }
/**
* Returns the beginning of a null-terminated char array.
*
* This is like ->data(), but can only be called on a StringRefNull.
*/
const char *c_str() const
{
return data_;
}
}; };
/** /**

View File

@@ -477,7 +477,7 @@ inline Span<const DNode *> DerivedNodeTree::nodes() const
inline Span<const DNode *> DerivedNodeTree::nodes_by_type(StringRefNull idname) const inline Span<const DNode *> DerivedNodeTree::nodes_by_type(StringRefNull idname) const
{ {
const bNodeType *nodetype = nodeTypeFind(idname.data()); const bNodeType *nodetype = nodeTypeFind(idname.c_str());
return this->nodes_by_type(nodetype); return this->nodes_by_type(nodetype);
} }

View File

@@ -240,7 +240,7 @@ class MFNetworkBuilderBase {
BLI_STATIC_ASSERT((std::is_base_of_v<fn::MultiFunction, T>), ""); BLI_STATIC_ASSERT((std::is_base_of_v<fn::MultiFunction, T>), "");
void *buffer = common_.resources.linear_allocator().allocate(sizeof(T), alignof(T)); void *buffer = common_.resources.linear_allocator().allocate(sizeof(T), alignof(T));
T *fn = new (buffer) T(std::forward<Args>(args)...); T *fn = new (buffer) T(std::forward<Args>(args)...);
common_.resources.add(destruct_ptr<T>(fn), fn->name().data()); common_.resources.add(destruct_ptr<T>(fn), fn->name().c_str());
return *fn; return *fn;
} }
}; };

View File

@@ -405,7 +405,7 @@ inline Span<const NodeRef *> NodeTreeRef::nodes() const
inline Span<const NodeRef *> NodeTreeRef::nodes_by_type(StringRefNull idname) const inline Span<const NodeRef *> NodeTreeRef::nodes_by_type(StringRefNull idname) const
{ {
const bNodeType *nodetype = nodeTypeFind(idname.data()); const bNodeType *nodetype = nodeTypeFind(idname.c_str());
return this->nodes_by_type(nodetype); return this->nodes_by_type(nodetype);
} }